blob: b9435d682032af0a38fa3ef71afd8e1ca4ad40db [file] [log] [blame]
Yulia Portnovac15bfc42015-02-27 18:18:29 +02001import argparse
2import imp
3import os.path
4import shutil
5import sqlite3
6import sys
7
8from os import remove
Yulia Portnovac15bfc42015-02-27 18:18:29 +02009from web_app.app import db
gstepanove6ad1f02015-03-02 19:58:00 +020010from config import DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, basedir
Yulia Portnovac15bfc42015-02-27 18:18:29 +020011from migrate.versioning import api
12
Yulia Portnovac15bfc42015-02-27 18:18:29 +020013
14ACTIONS = {}
15
16
17def 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
27def 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")
36def 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")
49def 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")
60def 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")
79def 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")
86def 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
93def main(argv):
94 opts = parse_args(argv)
95 func = ACTIONS.get(opts.action)
96 func()
97
98
99if __name__ == '__main__':
gstepanove6ad1f02015-03-02 19:58:00 +0200100 exit(main(sys.argv[1:]))