Database migrations on django production

前端 未结 5 404
自闭症患者
自闭症患者 2021-01-31 10:20

From someone who has a django application in a non-trivial production environment, how do you handle database migrations? I know there is south, but it seems like t

5条回答
  •  遥遥无期
    2021-01-31 10:54

    I have sometimes taken an unconventional approach (reading the other answers perhaps it's not that unconventional) to this problem. I never tried it with django so I just did some experiments with it.

    In short, I let the code catch the exception resulting from the old schema and apply the appropriate schema upgrade. I don't expect this to be the accepted answer - it is only appropriate in some cases (and some might argue never). But I think it has an ugly-duckling elegance.

    Of course, I have a test environment which I can reset back to the production state at any point. Using that test environment, I update my schema and write code against it - as usual.

    I then revert the schema change and test the new code again. I catch the resulting errors, perform the schema upgrade and then re-try the erring query.

    The upgrade function must be written so it will "do no harm" so that if it's called multiple times (as may happen when put into production) it only acts once.

    Actual python code - I put this at the end of my settings.py to test the concept, but you would probably want to keep it in a separate module:

    from django.db.models.sql.compiler import SQLCompiler
    from MySQLdb import OperationalError
    
    orig_exec = SQLCompiler.execute_sql
    def new_exec(self, *args, **kw):
        try:
            return orig_exec(self, *args, **kw)
        except OperationalError, e:
            if e[0] != 1054: # unknown column
                raise
            upgradeSchema(self.connection)
            return orig_exec(self, *args, **kw)
    SQLCompiler.execute_sql = new_exec
    
    def upgradeSchema(conn):
        cursor = conn.cursor()
        try:
            cursor.execute("alter table users add phone varchar(255)")
        except OperationalError, e:
            if e[0] != 1060: # duplicate column name
                raise
    

    Once your production environment is up to date, you are free to remove this self-upgrade code from your codebase. But even if you don't, the code isn't doing any significant unnecessary work.

    You would need to tailor the exception class (MySQLdb.OperationalError in my case) and numbers (1054 "unknown column" / 1060 "duplicate column" in my case) to your database engine and schema change, but that should be easy.

    You might want to add some additional checks to ensure the sql being executed is actually erring because of the schema change in question rather than some other problem, but even if you don't, this should re-raise unrelated exception. The only penalty is that in that situation you'd be trying the upgrade and the bad query twice before raising the exception.

    One of my favorite things about python is one's ability to easily override system methods at run-time like this. It provides so much flexibility.

提交回复
热议问题