21 from datetime
import datetime
22 from django.core.management.base
import BaseCommand
25 from django.conf
import settings
26 from django.db
import connections
27 from django.db
import transaction
28 from optparse
import make_option
36 'name':
'clm_message',
45 'name':
'clm_usergroup',
55 MCURSOR.execute(query)
56 return MCURSOR.fetchall()
61 PCURSOR.execute(query)
66 PCURSOR.execute(query)
67 return PCURSOR.fetchall()
79 nrow.append(
"E'%s'" % str(i).replace(
"'",
"\\'").decode(
'utf-8'))
85 help =
'Run migration from version 1.7'
86 option_list = BaseCommand.option_list + (
91 help=
'MySQL database name'),
92 make_option(
'--password',
96 help=
'MySQL database password'),
101 help=
'MySQL database user'),
102 make_option(
'--host',
106 help=
'MySQL database host'),
107 make_option(
'--port',
111 help=
'MySQL database port'),
115 sys.stdout.write(
'Run migration at: %s\n' % datetime.now())
117 settings.DATABASES.update({
119 'ENGINE':
'django.db.backends.mysql',
120 'NAME': options[
'name'],
121 'USER': options[
'user'],
122 'PASSWORD': options[
'password'],
123 'HOST': options[
'host'],
124 'PORT': options[
'port'],
129 MCURSOR = connections[
'mysql'].cursor()
131 print 'Cannot connect to MySQL database: %s' % e
136 PCURSOR = connections[
'default'].cursor()
138 print 'Cannot connect to PostgresSQL database: %s' % e
141 with transaction.commit_manually():
144 for table, desc
in tables.iteritems():
145 print 'Migrate table %s' % table
146 new_table = desc.get(
'name')
or 'cm_%s' % table
147 old_cols = [i[0]
for i
in mselect(
"describe %s;" % table)]
148 for name
in desc.get(
'drop_column', []):
149 old_cols.remove(name)
150 new_cols = old_cols[:]
151 rename_columns = desc.get(
'rename_columns')
153 for i, c
in enumerate(old_cols):
154 if c
in rename_columns:
155 new_cols[i] = rename_columns[c]
157 values =
mselect(
'select %s from %s;' % (
','.join(old_cols), table))
161 pinsert(
"insert into %s(%s) values (%s);" % (new_table,
','.join(new_cols),
prepare(row)))
163 seq_id_table = new_table +
'_id_seq'
164 if pselect(
"select column_name from information_schema.columns where table_name='%s' and column_name='id';" % (new_table)):
165 pinsert(
"select setval('%s', (select max(id) from %s));" % (seq_id_table, new_table))
167 values =
mselect(
"select id,leader_id,name,group.desc from clm.group;")
169 pinsert(
'insert into clm_group (id,leader_id,name,"desc") values(%s);' %
prepare(row))
170 pinsert(
"select setval('%s', (select max(id) from %s));" % (
'clm_group_id_seq',
'clm_group'))
172 values =
mselect(
"select id,user_id,name,fingerprint,data,creation_date from clm.key;")
175 "insert into clm_key (id,user_id,name,fingerprint,data,creation_date) values(%s);" %
prepare(
177 pinsert(
"select setval('%s', (select max(id) from %s));" % (
'clm_key_id_seq',
'clm_key'))
179 print "Migration complete"
181 traceback.print_exc()
183 transaction.rollback()