def _get_table_sql(self, connection, table_name, schema=None, **kw):
try:
s = ("SELECT sql FROM "
- " (SELECT * FROM sqlite_master UNION ALL "
- " SELECT * FROM sqlite_temp_master) "
- "WHERE name = '%s' "
- "AND type = 'table'") % table_name
+ " (SELECT * FROM %(schema)ssqlite_master UNION ALL "
+ " SELECT * FROM %(schema)ssqlite_temp_master) "
+ "WHERE name = '%(table)s' "
+ "AND type = 'table'" % {
+ "schema": ("%s." % schema) if schema else "",
+ "table": table_name})
rs = connection.execute(s)
except exc.DBAPIError:
- s = ("SELECT sql FROM sqlite_master WHERE name = '%s' "
- "AND type = 'table'") % table_name
+ s = ("SELECT sql FROM %(schema)ssqlite_master "
+ "WHERE name = '%(table)s' "
+ "AND type = 'table'" % {
+ "schema": ("%s." % schema) if schema else "",
+ "table": table_name})
rs = connection.execute(s)
return rs.scalar()
from sqlalchemy import event
from sqlalchemy.sql.elements import quoted_name
from sqlalchemy import ForeignKey
+import re
metadata, users = None, None
refl.pop('duplicates_index', None)
eq_(orig, refl)
+ @testing.requires.check_constraint_reflection
+ def test_get_check_constraints(self):
+ self._test_get_check_constraints()
+
+ @testing.requires.check_constraint_reflection
+ @testing.requires.schemas
+ def test_get_check_constraints_schema(self):
+ self._test_get_check_constraints(schema=testing.config.test_schema)
+
+ @testing.provide_metadata
+ def _test_get_check_constraints(self, schema=None):
+ orig_meta = self.metadata
+ Table(
+ 'sa_cc', orig_meta,
+ Column('a', Integer()),
+ sa.CheckConstraint('a > 1 AND a < 5', name='cc1'),
+ sa.CheckConstraint('a = 1 OR (a > 2 AND a < 5)', name='cc2'),
+ schema=schema
+ )
+
+ orig_meta.create_all()
+
+ inspector = inspect(orig_meta.bind)
+ reflected = sorted(
+ inspector.get_check_constraints('sa_cc', schema=schema),
+ key=operator.itemgetter('name')
+ )
+
+ reflected = [
+ {"name": item["name"],
+ # trying to minimize effect of quoting, parenthesis, etc.
+ # may need to add more to this as new dialects get CHECK
+ # constraint reflection support
+ "sqltext": re.sub(r"[`'\(\)]", '', item["sqltext"].lower())}
+ for item in reflected
+ ]
+ eq_(
+ reflected,
+ [
+ {'name': 'cc1', 'sqltext': 'a > 1 and a < 5'},
+ {'name': 'cc2', 'sqltext': 'a = 1 or a > 2 and a < 5'}
+ ]
+ )
+
@testing.provide_metadata
def _test_get_view_definition(self, schema=None):
meta = self.metadata