-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
99 lines (77 loc) · 3.31 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os.path
import imp
from app import app, db, models
from app.settings import Config
from migrate.versioning import api
from flask.ext.script import Manager, Shell, Server, Command
manager = Manager(app)
manager.add_command('runserver', Server(host='localhost'))
manager.add_command('shell', Shell())
@manager.command
def createdb():
db.create_all()
if not os.path.exists(Config.SQLALCHEMY_MIGRATE_REPO):
api.create(Config.SQLALCHEMY_MIGRATE_REPO, 'database repository')
api.version_control(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO)
else:
api.version_control(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO,
api.version(Config.SQLALCHEMY_MIGRATE_REPO))
print 'DB created'
@manager.command
def migratedb():
v = api.db_version(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO)
migration = Config.SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (v+1))
tmp_module = imp.new_module('old_model')
old_model = api.create_model(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO)
exec(old_model, tmp_module.__dict__)
script = api.make_update_script_for_model(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO,
tmp_module.meta, db.metadata)
open(migration, "wt").write(script)
api.upgrade(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO)
v = api.db_version(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO)
print 'New migration saved as ' + migration
print 'Current database version: ' + str(v)
@manager.command
def upgraddb():
api.upgrade(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO)
v = api.db_version(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO)
print 'Current database version: ' + str(v)
@manager.command
def downgraddb():
v = api.db_version(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO)
api.downgrade(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO, v - 1)
v = api.db_version(Config.SQLALCHEMY_DATABASE_URI,
Config.SQLALCHEMY_MIGRATE_REPO)
print 'Current database version: ' + str(v)
@manager.command
def import_dataset():
from app import test_dataset
for user in test_dataset.datasets:
user_model = models.User(social_id=user['social_id'],
nickname=user['nickname'],
email=user['email'])
db.session.add(user_model)
db.session.commit()
for word in user['words']:
word_model = models.Word(name=word['name'],
explain=word['explain'],
example=word['example'],
created_at=word['created_at'],
updated_at=word['updated_at'],
user_id=user_model.id)
db.session.add(word_model)
db.session.commit()
print 'Import ends'
if __name__ == "__main__":
manager.run()