-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
98 lines (81 loc) · 2.67 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
import os
import imp
import json
from datetime import datetime
from flask_script import Manager
from migrate.versioning import api
from app import create_app
from app import db
from app.models import User
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
@manager.command
def createDB(drop_first=False):
''' Create the database '''
if drop_first:
db.drop_all()
db.create_all()
repo = app.config['SQLALCHEMY_MIGRATE_REPO']
uri = app.config['SQLALCHEMY_DATABASE_URI']
if not os.path.exists(repo):
api.create(repo, 'database repository')
api.version_control(uri, repo)
else:
api.version_control(uri, repo, api.version(repo))
@manager.command
def migrateDB():
repo = app.config['SQLALCHEMY_MIGRATE_REPO']
uri = app.config['SQLALCHEMY_DATABASE_URI']
v = api.db_version(uri, repo)
migration = repo + ('/versions/%03d_migration.py' % (v+1))
tmp_module = imp.new_module('old_model')
old_model = api.create_model(uri, repo)
exec(old_model, tmp_module.__dict__)
script = api.make_update_script_for_model(uri, repo,
tmp_module.meta, db.metadata)
open(migration, "wt").write(script)
api.upgrade(uri, repo)
v = api.db_version(uri, repo)
print('New migration saved as ' + migration)
print('Current database version: ' + str(v))
@manager.command
def upgradeDB():
repo = app.config['SQLALCHEMY_MIGRATE_REPO']
uri = app.config['SQLALCHEMY_DATABASE_URI']
api.upgrade(uri, repo)
v = api.db_version(uri, repo)
print('Current database version: ' + str(v))
@manager.command
def adduser(email, user_id):
'''Register a new user.'''
from getpass import getpass
password = getpass()
password2 = getpass(prompt='confirm: ')
if password != password2:
import sys
sys.exit('Error: passwords do not match.')
db.create_all()
#user = User.query.filter_by(id=user_id).first()
user = User.query.get(int(user_id))
user.email = email
user.password = password
user.member_since = datetime.utcnow()
db.session.commit()
print('User {0} was registered successfully.'.format(user_id))
@manager.command
def createDBfromJSON():
''' Create DB from json file '''
db.drop_all()
db.create_all()
with open('ckc00.json') as fin:
users = json.load(fin)
for user in users:
if user['bbs_id']:
user['username'] = user.get('username', user['bbs_id'])
else:
user['username'] = user.get('username', user['id'])
db.session.add(User(**user))
db.session.commit()
if __name__ == '__main__':
manager.run()