summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-09-23 12:08:40 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2017-09-23 12:08:40 -0400
commit8e219c56614b787592887aa9097d83f0f210e695 (patch)
treef6bb19d5981500bd3d6fd07ee74bd01981f30625
parent08430e689f1a0190f671636016b12ff2ca08531f (diff)
downloadsqlalchemy-8e219c56614b787592887aa9097d83f0f210e695.tar.gz
Add table / column comments into tometadata()
Fixed bug in new SQL comments feature where table and column comment would not be copied when using :meth:`.Table.tometadata`. Change-Id: Ib3112e5e02930245daacb36c8ed38c01fa3e7dbd Fixes: #4087
-rw-r--r--doc/build/changelog/unreleased_12/4087.rst6
-rw-r--r--lib/sqlalchemy/sql/schema.py2
-rw-r--r--test/sql/test_metadata.py27
3 files changed, 30 insertions, 5 deletions
diff --git a/doc/build/changelog/unreleased_12/4087.rst b/doc/build/changelog/unreleased_12/4087.rst
new file mode 100644
index 000000000..2ebc0576e
--- /dev/null
+++ b/doc/build/changelog/unreleased_12/4087.rst
@@ -0,0 +1,6 @@
+.. change::
+ :tags: bug, sql
+ :tickets: 4087
+
+ Fixed bug in new SQL comments feature where table and column comment
+ would not be copied when using :meth:`.Table.tometadata`.
diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py
index 7a78a715f..446d0118b 100644
--- a/lib/sqlalchemy/sql/schema.py
+++ b/lib/sqlalchemy/sql/schema.py
@@ -865,6 +865,7 @@ class Table(DialectKWArgs, SchemaItem, TableClause):
args.append(c.copy(schema=schema))
table = Table(
name, metadata, schema=schema,
+ comment=self.comment,
*args, **self.kwargs
)
for c in self.constraints:
@@ -1433,6 +1434,7 @@ class Column(SchemaItem, ColumnClause):
onupdate=self.onupdate,
server_onupdate=self.server_onupdate,
doc=self.doc,
+ comment=self.comment,
*args
)
return self._schema_item_copy(c)
diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py
index e204375f4..87403be63 100644
--- a/test/sql/test_metadata.py
+++ b/test/sql/test_metadata.py
@@ -18,7 +18,7 @@ from sqlalchemy.testing import ComparesTables, AssertsCompiledSQL
from sqlalchemy.testing import eq_, is_, mock, is_true
from contextlib import contextmanager
from sqlalchemy import util
-
+from sqlalchemy.testing import engines
class MetaDataTest(fixtures.TestBase, ComparesTables):
@@ -610,18 +610,27 @@ class ToMetaDataTest(fixtures.TestBase, ComparesTables):
),
test_needs_fk=True)
+ table3 = Table(
+ 'has_comments', meta,
+ Column('foo', Integer, comment='some column'),
+ comment='table comment'
+ )
+
def test_to_metadata():
meta2 = MetaData()
table_c = table.tometadata(meta2)
table2_c = table2.tometadata(meta2)
- return (table_c, table2_c)
+ table3_c = table3.tometadata(meta2)
+ return (table_c, table2_c, table3_c)
def test_pickle():
meta.bind = testing.db
meta2 = pickle.loads(pickle.dumps(meta))
assert meta2.bind is None
pickle.loads(pickle.dumps(meta2))
- return (meta2.tables['mytable'], meta2.tables['othertable'])
+ return (
+ meta2.tables['mytable'],
+ meta2.tables['othertable'], meta2.tables['has_comments'])
def test_pickle_via_reflect():
# this is the most common use case, pickling the results of a
@@ -629,11 +638,15 @@ class ToMetaDataTest(fixtures.TestBase, ComparesTables):
meta2 = MetaData(bind=testing.db)
t1 = Table('mytable', meta2, autoload=True)
Table('othertable', meta2, autoload=True)
+ Table('has_comments', meta2, autoload=True)
meta3 = pickle.loads(pickle.dumps(meta2))
assert meta3.bind is None
assert meta3.tables['mytable'] is not t1
- return (meta3.tables['mytable'], meta3.tables['othertable'])
+ return (
+ meta3.tables['mytable'], meta3.tables['othertable'],
+ meta3.tables['has_comments']
+ )
meta.create_all(testing.db)
try:
@@ -641,7 +654,7 @@ class ToMetaDataTest(fixtures.TestBase, ComparesTables):
(test_to_metadata, True, False), \
(test_pickle, True, False), \
(test_pickle_via_reflect, False, True):
- table_c, table2_c = test()
+ table_c, table2_c, table3_c = test()
self.assert_tables_equal(table, table_c)
self.assert_tables_equal(table2, table2_c)
assert table is not table_c
@@ -677,6 +690,10 @@ class ToMetaDataTest(fixtures.TestBase, ComparesTables):
assert c.columns.contains_column(table_c.c.name)
assert not c.columns.contains_column(table.c.name)
+ if testing.requires.comment_reflection.enabled:
+ eq_(table3_c.comment, "table comment")
+ eq_(table3_c.c.foo.comment, "some column")
+
finally:
meta.drop_all(testing.db)