/
manage.py
82 lines (61 loc) · 3.12 KB
/
manage.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
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, unicode_literals, print_function, division)
import os
from flask.ext.script import Manager, Server
from server import create_app
from flask import url_for
from config import Config, DevelopmentConfig, TestingConfig, ProductionConfig
app = create_app(config=DevelopmentConfig)
manager = Manager(app)
manager.add_command("runserver", Server(
use_debugger = app.config.get('DEBUG', True),
use_reloader = app.config.get('DEBUG', True),
host = app.config.get('HOST', '0.0.0.0'))
)
@manager.command
def init_db():
from migrate.versioning import api
from database.model import db
print("Initializing database (going to drop all tables)...")
#app.test_request_context().push()
db.drop_all()
db.create_all()
print("Versioning the database...")
if not os.path.exists(app.config.get('SQLALCHEMY_MIGRATE_REPO')):
api.create(app.config.get('SQLALCHEMY_MIGRATE_REPO'), 'database repository')
api.version_control(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'))
else:
api.version_control(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'), api.version(app.config.get('SQLALCHEMY_MIGRATE_REPO')))
db.session.commit()
print("All done.")
@manager.command
def migrate_db():
import imp
from migrate.versioning import api
from database.model import db
print("Migrating the database...")
v = api.db_version(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'))
migration = app.config.get('SQLALCHEMY_MIGRATE_REPO') + ('/versions/%03d_migration.py' % (v+1))
tmp_module = imp.new_module('old_model')
old_model = api.create_model(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'))
exec(old_model, tmp_module.__dict__)
script = api.make_update_script_for_model(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'), tmp_module.meta, db.metadata)
open(migration, "wt").write(script)
api.upgrade(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'))
v = api.db_version(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'))
print("New migration saved as " + migration)
print("Current database version: " + str(v))
@manager.command
def upgrade_db():
from migrate.versioning import api
api.upgrade(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'))
v = api.db_version(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'))
print('Current database version: ' + str(v))
@manager.command
def downgrade_db():
from migrate.versioning import api
v = api.db_version(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'))
api.downgrade(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'), v-1)
print('Current database version: ' + str(v))
if __name__ == '__main__':
manager.run()