migrations.rst 3.0 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 `Alembic <https://bitbucket.org/zzzeek/alembic>`_ to run
  24. migrations. We also make heavy use of the
  25. `branching model <http://alembic.readthedocs.org/en/latest/branches.html>`_
  26. for our plugins. Every plugin gets its own migration branc.
  27. - We used to use `sqlalchemy-migrate
  28. <http://code.google.com/p/sqlalchemy-migrate/>`_.
  29. See `their docs <https://sqlalchemy-migrate.readthedocs.org/>`_.
  30. sqlalchemy-migrate is now only kept around for legacy migrations;
  31. don't add any more! But some users are still using older databases,
  32. and we need to provide the intermediary "old" migrations for a while.
  33. - SQLAlchemy has two parts to it, the ORM and the "core" interface.
  34. We DO NOT use the ORM when running migrations. Think about it: the
  35. ORM is set up with an expectation that the models already reflect a
  36. certain pattern. But if a person is moving from their old patern
  37. and are running tools to *get to* the current pattern, of course
  38. their current database structure doesn't match the state of the ORM!
  39. Anyway, Alembic has its own conventions for migrations; follow those.
  40. - Alembic's documentation is pretty good; you don't need to worry about
  41. setting up the migration environment or the config file so you can
  42. skip those parts. You can start at the
  43. `Create a Migration Script <http://alembic.readthedocs.org/en/latest/tutorial.html#create-a-migration-script>`_
  44. section.
  45. - *Users* should only use `./bin/gmg dbupdate`. However, *developers*
  46. may wish to use the `./bin/gmg alembic` subcommand, which wraps
  47. alembic's own command line interface. Alembic has some tools for
  48. `autogenerating migrations <http://alembic.readthedocs.org/en/latest/autogenerate.html>`_,
  49. and they aren't perfect, but they are helpful. (You can pass in
  50. `./bin/gmg alembic --with-plugins revision --autogenerate` if you need
  51. to include plugins in the generated output; see the
  52. :ref:`plugin database chapter <plugin-database-chapter>` for more info.)
  53. That's it for now! Good luck!