-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmigrate.py
More file actions
64 lines (55 loc) · 2.01 KB
/
migrate.py
File metadata and controls
64 lines (55 loc) · 2.01 KB
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
from config import config
from library import app
from database import Database
from models import Meta
from migration import *
import migration
class DatabaseMigration(object):
def __init__(self):
self.db = Database(app)
def get_database_version(self):
"""
DatabaseMigration.get_database_version(): returns the int value of db version
Params:
- None
Returns:
- (int) : the version of the database
"""
version = Meta.query.filter_by(key="DB_VERSION").first()
return int(version.value)
def update_database_version(self, version=None):
"""
DatabaseMigration.update_database_version() - updates a db version
Params:
- version (int) [optional] : the version to set. if ommitted, increments version
Returns:
- None
"""
current_version = Meta.query.filter_by(key="DB_VERSION").first()
if version == None:
new_version = str(int(current_version.value) + 1)
else:
new_version = str(version)
current_version.value = new_version
self.db.session.add(current_version)
self.db.session.commit()
def update_database(self):
current_version = self.get_database_version()
print "current db version is " + str(current_version)
migration_contents = sorted(dir(migration))
for contents in migration_contents:
parts = contents.split("_")
if parts[0]: #does not start with _
script_version = int(parts[0])
if script_version > int(current_version):
print "running " + contents
module = getattr(migration, contents)
module.main()
self.update_database_version()
current_version = self.get_database_version()
print "current db version is " + str(current_version)
def main():
dm = DatabaseMigration()
dm.update_database()
if __name__=="__main__":
main()