diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-09-16 16:55:05 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-09-16 16:55:05 -0400 |
| commit | ecda5429af1a3850304db3087cbc0b4f8a34ed77 (patch) | |
| tree | 833d85709024a8803ae1b63d1e9eb68ca18fe785 | |
| parent | cc3dba01db0367d4172cca1b902976ac7718e4cf (diff) | |
| parent | fd2faa9bc2c6d2d1b0b8e1738f0bce21e2527bb0 (diff) | |
| download | sqlalchemy-ecda5429af1a3850304db3087cbc0b4f8a34ed77.tar.gz | |
Merge remote-tracking branch 'origin/pr/128' into pr128
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 23 | ||||
| -rw-r--r-- | lib/sqlalchemy/engine/reflection.py | 3 | ||||
| -rw-r--r-- | setup.cfg | 7 | ||||
| -rw-r--r-- | test/dialect/postgresql/test_reflection.py | 102 | ||||
| -rw-r--r-- | test/requirements.py | 8 |
5 files changed, 139 insertions, 4 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 575d2a6dd..df9797658 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1679,6 +1679,23 @@ class PGInspector(reflection.Inspector): schema = schema or self.default_schema_name return self.dialect._load_enums(self.bind, schema) + def get_foreign_table_names(self, connection, schema=None, **kw): + if schema is not None: + current_schema = schema + else: + current_schema = self.default_schema_name + + result = connection.execute( + sql.text("SELECT relname FROM pg_class c " + "WHERE relkind = 'f' " + "AND '%s' = (select nspname from pg_namespace n " + "where n.oid = c.relnamespace) " % + current_schema, + typemap={'relname': sqltypes.Unicode} + ) + ) + return [row[0] for row in result] + class CreateEnumType(schema._CreateDropBase): __visit_name__ = "create_enum_type" @@ -2024,7 +2041,7 @@ class PGDialect(default.DefaultDialect): FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE (%s) - AND c.relname = :table_name AND c.relkind in ('r','v') + AND c.relname = :table_name AND c.relkind in ('r', 'v', 'm', 'f') """ % schema_where_clause # Since we're binding to unicode, table_name and schema_name must be # unicode. @@ -2086,7 +2103,7 @@ class PGDialect(default.DefaultDialect): s = """ SELECT relname FROM pg_class c - WHERE relkind = 'v' + WHERE relkind IN ('m', v') AND '%(schema)s' = (select nspname from pg_namespace n where n.oid = c.relnamespace) """ % dict(schema=current_schema) @@ -2448,7 +2465,7 @@ class PGDialect(default.DefaultDialect): pg_attribute a on t.oid=a.attrelid and %s WHERE - t.relkind = 'r' + t.relkind IN ('r', 'v', 'f', 'm') and t.oid = :table_oid and ix.indisprimary = 'f' ORDER BY diff --git a/lib/sqlalchemy/engine/reflection.py b/lib/sqlalchemy/engine/reflection.py index cf1f2d3dd..b72290588 100644 --- a/lib/sqlalchemy/engine/reflection.py +++ b/lib/sqlalchemy/engine/reflection.py @@ -227,6 +227,9 @@ class Inspector(object): :param schema: Optional, retrieve names from a non-default schema. For special quoting, use :class:`.quoted_name`. + .. versionchanged:: 1.0.0 now returns materialized views as well + as normal views. + """ return self.dialect.get_view_names(self.bind, schema, @@ -26,6 +26,13 @@ profile_file=test/profiles.txt # create database link test_link connect to scott identified by tiger using 'xe'; oracle_db_link = test_link +# host name of a postgres database that has the postgres_fdw extension. +# to create this run: +# CREATE EXTENSION postgres_fdw; +# GRANT USAGE ON FOREIGN DATA WRAPPER postgres_fdw TO public; +# this can be localhost to create a loopback foreign table +postgres_test_db_link = localhost + [db] default=sqlite:///:memory: diff --git a/test/dialect/postgresql/test_reflection.py b/test/dialect/postgresql/test_reflection.py index bab41b0f7..3bc4cd715 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -13,8 +13,108 @@ import sqlalchemy as sa from sqlalchemy.dialects.postgresql import base as postgresql -class DomainReflectionTest(fixtures.TestBase, AssertsExecutionResults): +class AltRelkindReflectionTest(fixtures.TestBase, AssertsExecutionResults): + """Test reflection on materialized views and foreign tables""" + + __requires__ = 'postgresql_test_dblink', + __only_on__ = 'postgresql >= 9.3' + __backend__ = True + + @classmethod + def setup_class(cls): + from sqlalchemy.testing import config + cls.dblink = config.file_config.get('sqla_testing', + 'postgres_test_db_link') + + metadata = MetaData(testing.db) + testtable = Table( + 'testtable', metadata, + Column( + 'id', Integer, primary_key=True), + Column( + 'data', String(30))) + metadata.create_all() + testtable.insert().execute({'id': 89, 'data': 'd1'}) + + con = testing.db.connect() + + for ddl in \ + "CREATE MATERIALIZED VIEW test_mview AS SELECT * FROM testtable;", \ + "CREATE SERVER test_server FOREIGN DATA WRAPPER postgres_fdw \ + OPTIONS (dbname 'test', host '%s');" % cls.dblink, \ + "CREATE USER MAPPING FOR public \ + SERVER test_server options (user 'scott', password 'tiger');", \ + "CREATE FOREIGN TABLE test_foreigntable ( \ + id INT, \ + data VARCHAR(30) \ + ) SERVER test_server OPTIONS (table_name 'testtable');": + try: + con.execute(ddl) + except exc.DBAPIError as e: + if 'already exists' not in str(e): + raise e + + @classmethod + def teardown_class(cls): + con = testing.db.connect() + con.execute('DROP FOREIGN TABLE test_foreigntable;') + con.execute('DROP USER MAPPING FOR public SERVER test_server;') + con.execute('DROP SERVER test_server;') + con.execute('DROP MATERIALIZED VIEW test_mview;') + con.execute('DROP TABLE testtable;') + + def test_mview_is_reflected(self): + metadata = MetaData(testing.db) + table = Table('test_mview', metadata, autoload=True) + eq_(set(table.columns.keys()), set(['id', 'data']), + "Columns of reflected mview didn't equal expected columns") + + def test_mview_select(self): + metadata = MetaData(testing.db) + table = Table('test_mview', metadata, autoload=True) + assert table.select().execute().fetchall() == [ + (89, 'd1',) + ] + + def test_foreign_table_is_reflected(self): + metadata = MetaData(testing.db) + table = Table('test_foreigntable', metadata, autoload=True) + eq_(set(table.columns.keys()), set(['id', 'data']), + "Columns of reflected foreign table didn't equal expected columns") + + def test_foreign_table_select(self): + metadata = MetaData(testing.db) + table = Table('test_foreigntable', metadata, autoload=True) + assert table.select().execute().fetchall() == [ + (89, 'd1',) + ] + + def test_foreign_table_roundtrip(self): + metadata = MetaData(testing.db) + table = Table('test_foreigntable', metadata, autoload=True) + connection = testing.db.connect() + trans = connection.begin() + try: + table.delete().execute() + table.insert().execute({'id': 89, 'data': 'd1'}) + trans.commit() + except: + trans.rollback() + raise + + assert table.select().execute().fetchall() == [ + (89, 'd1',) + ] + + def test_get_foreign_table_names(self): + inspector = inspect(testing.db) + connection = testing.db.connect() + ft_names = inspector.get_foreign_table_names(connection) + assert u'test_foreigntable' in ft_names + + +class DomainReflectionTest(fixtures.TestBase, AssertsExecutionResults): """Test PostgreSQL domains""" __only_on__ = 'postgresql > 8.3' diff --git a/test/requirements.py b/test/requirements.py index 7eeabef2b..14bb25691 100644 --- a/test/requirements.py +++ b/test/requirements.py @@ -706,6 +706,14 @@ class DefaultRequirements(SuiteRequirements): ) @property + def postgresql_test_dblink(self): + return skip_if( + lambda config: not config.file_config.has_option( + 'sqla_testing', 'postgres_test_db_link'), + "postgres_test_db_link option not specified in config" + ) + + @property def percent_schema_names(self): return skip_if( [ |
