Skip to content

Commit 9ebcc04

Browse files
committed
Merge pull request ps0uth#55 from rraub/54-add-comments
expanded the migration to use the comments from mysql
2 parents 3487f1d + 442f685 commit 9ebcc04

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

mysql2pgsql/lib/mysql_reader.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def __init__(self, reader, name):
8585
self._foreign_keys = []
8686
self._triggers = []
8787
self._columns = self._load_columns()
88+
self._comment = self._load_table_comment()
8889
self._load_indexes()
8990
self._load_triggers()
9091

@@ -120,7 +121,7 @@ def _convert_type(self, data_type):
120121

121122
def _load_columns(self):
122123
fields = []
123-
for row in self.reader.db.query('EXPLAIN `%s`' % self.name):
124+
for row in self.reader.db.query('SHOW FULL COLUMNS FROM `%s`' % self.name):
124125
res = ()
125126
for field in row:
126127
if type(field) == unicode:
@@ -132,17 +133,19 @@ def _load_columns(self):
132133
length = length_match.group(1) if length_match else \
133134
precision_match.group(1) if precision_match else None
134135
name = res[0]
136+
comment = res[8]
135137
field_type = self._convert_type(res[1])
136138
desc = {
137139
'name': name,
138140
'table_name': self.name,
139141
'type': field_type,
140142
'length': int(length) if length else None,
141143
'decimals': precision_match.group(2) if precision_match else None,
142-
'null': res[2] == 'YES' or field_type.startswith('enum') or field_type in ('date', 'datetime', 'timestamp'),
143-
'primary_key': res[3] == 'PRI',
144-
'auto_increment': res[5] == 'auto_increment',
145-
'default': res[4] if not res[4] == 'NULL' else None,
144+
'null': res[3] == 'YES' or field_type.startswith('enum') or field_type in ('date', 'datetime', 'timestamp'),
145+
'primary_key': res[4] == 'PRI',
146+
'auto_increment': res[6] == 'auto_increment',
147+
'default': res[5] if not res[5] == 'NULL' else None,
148+
'comment': comment,
146149
'select': '`%s`' % name if not field_type.startswith('enum') else
147150
'CASE `%(name)s` WHEN "" THEN NULL ELSE `%(name)s` END' % {'name': name},
148151
}
@@ -154,6 +157,12 @@ def _load_columns(self):
154157

155158
return fields
156159

160+
def _load_table_comment(self):
161+
table_status = self.reader.db.query('SHOW TABLE STATUS WHERE Name="%s"' % self.name, one=True)
162+
comment = table_status[17]
163+
return comment
164+
165+
157166
def _load_indexes(self):
158167
explain = self.reader.db.query('SHOW CREATE TABLE `%s`' % self.name, one=True)
159168
explain = explain[1]
@@ -207,6 +216,10 @@ def name(self):
207216
def columns(self):
208217
return self._columns
209218

219+
@property
220+
def comment(self):
221+
return self._comment
222+
210223
@property
211224
def indexes(self):
212225
return self._indexes

mysql2pgsql/lib/postgres_writer.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ def get_type(column):
131131

132132
return '%s%s%s' % (column_type, (default if not default == None else ''), null)
133133

134+
def table_comments(self, table):
135+
comments = StringIO()
136+
if table.comment:
137+
comments.write(self.table_comment(table.name, table.comment))
138+
for column in table.columns:
139+
comments.write(self.column_comment(table.name, column))
140+
return comments.getvalue()
141+
142+
def column_comment(self, tablename, column):
143+
if not column['comment']:
144+
return (' COMMENT ON COLUMN %s.%s is %s;' % ( tablename, column['name'], QuotedString(column['comment']).getquoted()))
145+
else:
146+
return ''
147+
148+
def table_comment(self, tablename, comment):
149+
return (' COMMENT ON TABLE %s is %s;' % ( tablename, QuotedString(comment).getquoted()))
150+
134151
def process_row(self, table, row):
135152
"""Examines row data from MySQL and alters
136153
the values when necessary to be compatible with
@@ -222,6 +239,7 @@ def write_table(self, table):
222239

223240
table_sql.append('DROP TABLE IF EXISTS "%s" CASCADE;' % table.name)
224241
table_sql.append('CREATE TABLE "%s" (\n%s\n)\nWITHOUT OIDS;' % (table.name.encode('utf8'), columns))
242+
table_sql.append( self.table_comments(table))
225243
return (table_sql, serial_key_sql)
226244

227245
def write_indexes(self, table):

0 commit comments

Comments
 (0)