forked from pythononwheels/pow_devel
/
generate_migration.py
150 lines (122 loc) · 5.69 KB
/
generate_migration.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!C:\Python26\python.exe -u
# pow model generator.
#
# options are:
# see: python generate_migration.py --help
import os, datetime, time
from optparse import OptionParser
import sqlite3
import sys
import datetime
from sqlalchemy.orm import mapper
from sqlalchemy import *
sys.path.append( os.path.abspath(os.path.join( os.path.dirname(os.path.abspath(__file__)), "./lib" )))
sys.path.append( os.path.abspath(os.path.join( os.path.dirname(os.path.abspath(__file__)), "./models" )))
import powlib
import Appinfo
# setting the right defaults
MODE_CREATE = 1
MODE_REMOVE = 0
PARTS_DIR = powlib.PARTS_DIR
def main():
parser = OptionParser()
mode= MODE_CREATE
parser.add_option("-n", "--name", action="store", type="string", dest="name", help="creates migration with name = <name>. Only for jobs", default ="None")
parser.add_option("-m", "--model", action="store", type="string", dest="model", help="defines the model for this migration.", default ="None")
parser.add_option("-c", "--comment", action="store", type="string", dest="comment", help="defines a comment for this migration.", default ="No Comment")
parser.add_option("-j", "--job", action="store", type="string", dest="job", help="creates migration job, e.g for backups, restores etc.",default="None")
parser.add_option("-cd", "--column-definitions", action="store",
type="string", dest="col_defs",
help="Pre define the column definitions. Name, type, options (all SQLAlchemy style).",default="None")
(options, args) = parser.parse_args()
#print options
if options.model == "None" and options.job == "None":
if len(args) > 0:
# if no option flag (like -m) is given, it is assumed that the first argument is the model. (representing -m arg1)
options.model = args[0]
migration_name = options.model
migration_model = options.model
else:
parser.error("You must at least specify an appname by giving -n <name>.")
return
else:
if options.name == "None":
migration_name = options.model
else:
migration_name = options.name
#migration_name = options.name
migration_model = options.model
start = None
end = None
start = datetime.datetime.now()
if options.job != "None":
render_migration_job(options.job)
else:
render_migration(migration_name, migration_model,options.comment)
end = datetime.datetime.now()
duration = None
duration = end - start
print "generated_migration in("+ str(duration) +")"
return
def render_migration(name, model, comment):
#
#print "generate_migration: " + name + " for model: " + model
# add the auto generated warning to the outputfile
infile = open (os.path.normpath(PARTS_DIR + "/can_be_edited.txt"), "r")
ostr = infile.read()
infile.close()
# add a creation date
ostr = ostr + os.linesep
ostr = ostr + "# date created: \t" + str(datetime.date.today())
ostr = ostr + os.linesep
# Add the model_stub part1 content to the newly generated file.
infile = open (os.path.normpath( PARTS_DIR + "db_migration_stub2_part1.py"), "r")
ostr = ostr + infile.read()
infile.close()
pluralname = powlib.plural(model)
ostr += powlib.tab + "table_name=\"" + pluralname + "\""
ostr += powlib.linesep
#print "modelname was: " + model + " pluralized table_name is:" + pluralname
# Add the model_stub part2 content to the newly generated file.
infile = open (os.path.normpath( PARTS_DIR + "db_migration_stub2_part2.py"), "r")
ostr = ostr + infile.read()
infile.close()
#ostr += powlib.tab + powlib.tab + powlib.tab + "Column('id', Integer, Sequence('" + model +"_id_seq'), primary_key=True),"
#ostr += powlib.newline
app = powlib.load_class( "Appinfo", "Appinfo")
app_versions = powlib.load_class( "Version", "Version")
sess = app.pbo.getSession()
app = sess.query(Appinfo.Appinfo).first()
version = app.maxversion
oldmaxversion = version
version += 1
verstring = powlib.version_to_string(version)
print "generate_migration: " + name + " for model: " + model
#print "version: " + str(version)
#print "version2string: " + verstring
filename = os.path.normpath ( "./migrations/" + verstring +"_" + name +".py" )
#update the app table with the new version
#appTable.update().values(maxversion= str(version) ).execute()
app.maxversion = str(version)
app.update()
app_versions.filename = str(verstring +"_" + name )
app_versions.version = str(version)
app_versions.comment = str(comment)
app_versions.update()
print " -- maxversion (old,new): (" + str(oldmaxversion) + "," + str(app.maxversion) +")"
ofile = open( filename , "w+")
print " -- created file:" + str(filename)
ofile.write( ostr )
ofile.close()
return
def render_migration_job(filename):
"""create a 'job' or task that has to be done on the database.
typical examples are backup/restore scripts for dbs or tables or loading data into a table.
These migrations are not part of the migration versioning system.
They can be executed with python migrate.py -f <migrationname>
"""
print " -- creating migration job:"
powlib.check_copy_file(os.path.normpath( PARTS_DIR + "migration_job.py"), "./migrations/" + filename + "_migration.py")
return
if __name__ == '__main__':
main()