Code Monkey home page Code Monkey logo

postgres-migration's Introduction

Django: From MySQL to Postgres

Why

./images/postgresql_versus_mysql.jpg

Why, really

This sucks:

SELECT X.* FROM no_chain_samplemodel as X
       JOIN (SELECT user_id, MAX(timestamp) AS timestamp
       FROM no_chain_samplemodel
       GROUP BY user_id) AS Y
       ON (X.user_id = Y.user_id and X.timestamp = Y.timestamp)i
       WHERE X.staff_id = %s

This is great:

SELECT DISTINCT ON (user_id) FROM no_chain_samplemodel 
      WHERE timestamp <= '2017-01-01' ORDER BY user_id ASC, timestamp DESC;

With the Django ORM:

SampleModel.objects.distinct('user_id').\
    filter(timestamp__gt=mydate).order_by('user_id', '-timestamp')

How

From this

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        ...
     }
}

To this

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        ...
     }
}

Done?

./images/done_yet.png

A few numbers

Massive fintech Django project:

  • 190k lines of Python code
  • > 100 Apps
  • 383 tables to migrate
  • 3000 tests running in ~3 minutes

What’s the plan

  • adapt code
  • migrate data
  • profit!

Data migration

Pgloader

Pgloader to the rescue:

./images/pgloader.png

No live replication == Downtime!!!

Very big tables

  • drop foreign keys (ForeignKey → IntegerField)
  • adapt queries
  • write a database router

Code changes

  • get Postgres on CI (stable tests)
  • search for untested raw queries
  • manual testing on real data

Regression testing

./images/notebook.png

Tips for switchers

  • use migrations for everything
  • test everything
  • NEVER rely on implicit ordering
  • make Django apps really independent
  • split that monolith ASAP

Conclusions

Hofstadter’s Database Migration Law:

Migrating from MySQL to Postgres always takes longer than you expect, even when you take into account Hofstadter’s Law.

@andreacrotti https://www.iwoca.co.uk/

postgres-migration's People

Contributors

andreacrotti avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.