Yulia Portnova | c15bfc4 | 2015-02-27 18:18:29 +0200 | [diff] [blame] | 1 | import argparse |
| 2 | import imp |
| 3 | import os.path |
| 4 | import shutil |
| 5 | import sqlite3 |
| 6 | import sys |
| 7 | |
| 8 | from os import remove |
Yulia Portnova | c15bfc4 | 2015-02-27 18:18:29 +0200 | [diff] [blame] | 9 | from web_app.app import db |
gstepanov | e6ad1f0 | 2015-03-02 19:58:00 +0200 | [diff] [blame] | 10 | from config import DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, basedir |
Yulia Portnova | c15bfc4 | 2015-02-27 18:18:29 +0200 | [diff] [blame] | 11 | from migrate.versioning import api |
| 12 | |
Yulia Portnova | c15bfc4 | 2015-02-27 18:18:29 +0200 | [diff] [blame] | 13 | |
| 14 | ACTIONS = {} |
| 15 | |
| 16 | |
| 17 | def action(act): |
| 18 | def wrap(f): |
| 19 | ACTIONS[act] = f |
| 20 | |
| 21 | def inner(*args, **kwargs): |
| 22 | return f(*args, **kwargs) |
| 23 | return inner |
| 24 | return wrap |
| 25 | |
| 26 | |
| 27 | def parse_args(argv): |
| 28 | parser = argparse.ArgumentParser( |
| 29 | description="Manage DB") |
| 30 | parser.add_argument("action", |
| 31 | choices=["dropdb", "createdb", "migrate", "downgrade"]) |
| 32 | return parser.parse_args(argv) |
| 33 | |
| 34 | |
| 35 | @action("createdb") |
| 36 | def createdb(): |
| 37 | sqlite3.connect(os.path.join(basedir, 'app.db')) |
| 38 | |
| 39 | db.create_all() |
| 40 | if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): |
| 41 | api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') |
| 42 | api.version_control(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) |
| 43 | else: |
| 44 | api.version_control(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, |
| 45 | api.version(SQLALCHEMY_MIGRATE_REPO)) |
| 46 | |
| 47 | |
| 48 | @action("dropdb") |
| 49 | def dropdb(): |
| 50 | db.create_all() |
| 51 | if os.path.exists(SQLALCHEMY_MIGRATE_REPO): |
| 52 | shutil.rmtree(SQLALCHEMY_MIGRATE_REPO) |
| 53 | |
| 54 | db.drop_all() |
| 55 | if os.path.exists(os.path.join(basedir, 'app.db')): |
| 56 | remove(os.path.join(basedir, 'app.db')) |
| 57 | |
| 58 | |
| 59 | @action("migrate") |
| 60 | def migrate(): |
| 61 | v = api.db_version(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) |
| 62 | migration = SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % |
| 63 | (v+1)) |
| 64 | tmp_module = imp.new_module('old_model') |
| 65 | old_model = api.create_model(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) |
| 66 | |
| 67 | exec old_model in tmp_module.__dict__ |
| 68 | script = api.make_update_script_for_model(DATABASE_URI, |
| 69 | SQLALCHEMY_MIGRATE_REPO, |
| 70 | tmp_module.meta, db.metadata) |
| 71 | open(migration, "wt").write(script) |
| 72 | api.upgrade(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) |
| 73 | v = api.db_version(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) |
| 74 | print('New migration saved as ' + migration) |
| 75 | print('Current database version: ' + str(v)) |
| 76 | |
| 77 | |
| 78 | @action("upgrade") |
| 79 | def upgrade(): |
| 80 | api.upgrade(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) |
| 81 | v = api.db_version(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) |
| 82 | print('Current database version: ' + str(v)) |
| 83 | |
| 84 | |
| 85 | @action("downgrade") |
| 86 | def downgrade(): |
| 87 | v = api.db_version(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) |
| 88 | api.downgrade(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, v - 1) |
| 89 | v = api.db_version(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) |
| 90 | print('Current database version: ' + str(v)) |
| 91 | |
| 92 | |
| 93 | def main(argv): |
| 94 | opts = parse_args(argv) |
| 95 | func = ACTIONS.get(opts.action) |
| 96 | func() |
| 97 | |
| 98 | |
| 99 | if __name__ == '__main__': |
gstepanov | e6ad1f0 | 2015-03-02 19:58:00 +0200 | [diff] [blame] | 100 | exit(main(sys.argv[1:])) |