migrations.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. .. MediaGoblin Documentation
  2. Written in 2011, 2012 by MediaGoblin contributors
  3. To the extent possible under law, the author(s) have dedicated all
  4. copyright and related and neighboring rights to this software to
  5. the public domain worldwide. This software is distributed without
  6. any warranty.
  7. You should have received a copy of the CC0 Public Domain
  8. Dedication along with this software. If not, see
  9. <http://creativecommons.org/publicdomain/zero/1.0/>.
  10. ==========
  11. Migrations
  12. ==========
  13. So, about migrations. Every time we change the way the database
  14. structure works, we need to add a migration so that people running
  15. older codebases can have their databases updated to the new structure
  16. when they run `./bin/gmg dbupdate`.
  17. The first time `./bin/gmg dbupdate` is run by a user, it creates the
  18. tables at the current state that they're defined in models.py and sets
  19. the migration number to the current migration... after all, migrations
  20. only exist to get things to the current state of the db. After that,
  21. every migration is run with dbupdate.
  22. There's a few things you need to know:
  23. - We use `sqlalchemy-migrate
  24. <http://code.google.com/p/sqlalchemy-migrate/>`_.
  25. See `their docs <https://sqlalchemy-migrate.readthedocs.org/>`_.
  26. - `Alembic <https://bitbucket.org/zzzeek/alembic>`_ might be a better
  27. choice than sqlalchemy-migrate now or in the future, but we
  28. originally decided not to use it because it didn't have sqlite
  29. support. It's not clear if that's changed.
  30. - SQLAlchemy has two parts to it, the ORM and the "core" interface.
  31. We DO NOT use the ORM when running migrations. Think about it: the
  32. ORM is set up with an expectation that the models already reflect a
  33. certain pattern. But if a person is moving from their old patern
  34. and are running tools to *get to* the current pattern, of course
  35. their current database structure doesn't match the state of the ORM!
  36. - How to write migrations? Maybe there will be a tutorial here in the
  37. future... in the meanwhile, look at existing migrations in
  38. `mediagoblin/db/migrations.py` and look in
  39. `mediagoblin/tests/test_sql_migrations.py` for examples.
  40. - Common pattern: use `inspect_table` to get the current state
  41. of the table before we run alterations on it.
  42. - Make sure you set the RegisterMigration to be the next migration in
  43. order.
  44. - What happens if you're adding a *totally new* table? In this case,
  45. you should copy the table in entirety as it exists into
  46. migrations.py then create the tables based off of that... see
  47. add_collection_tables. This is easier than reproducing the SQL by
  48. hand.
  49. - If you're writing a feature branch, you don't need to keep adding
  50. migrations every time you change things around if your database
  51. structure is in flux. Just alter your migrations so that they're
  52. correct for the merge into master.
  53. That's it for now! Good luck!