diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-01-30 20:29:29 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-01-30 20:29:29 -0500 |
commit | 4bd2739ec9fb3b789880bea096522d36fb4c4b01 (patch) | |
tree | 1bd49476c5a9a266840ef9b4b3b55ff627f4219a /alembic/autogenerate | |
parent | eb5c4ca10e6b47b07ecd2961443b0ca578f8f04b (diff) | |
download | alembic-4bd2739ec9fb3b789880bea096522d36fb4c4b01.tar.gz |
- Added support for autogenerate covering the use case where :class:`.Table`
objects specified in the metadata have an explicit ``schema`` attribute
whose name matches that of the connection's default schema
(e.g. "public" for Postgresql). Previously, it was assumed that "schema"
was ``None`` when it matched the "default" schema, now the comparison
adjusts for this. #170
Diffstat (limited to 'alembic/autogenerate')
-rw-r--r-- | alembic/autogenerate/api.py | 4 | ||||
-rw-r--r-- | alembic/autogenerate/compare.py | 32 |
2 files changed, 32 insertions, 4 deletions
diff --git a/alembic/autogenerate/api.py b/alembic/autogenerate/api.py index 910d6ec..148e352 100644 --- a/alembic/autogenerate/api.py +++ b/alembic/autogenerate/api.py @@ -188,13 +188,15 @@ def _produce_net_changes(connection, metadata, diffs, autogen_context, inspector = Inspector.from_engine(connection) # TODO: not hardcode alembic_version here ? conn_table_names = set() + + default_schema = connection.dialect.default_schema_name if include_schemas: schemas = set(inspector.get_schema_names()) # replace default schema name with None schemas.discard("information_schema") # replace the "default" schema with None schemas.add(None) - schemas.discard(connection.dialect.default_schema_name) + schemas.discard(default_schema) else: schemas = [None] diff --git a/alembic/autogenerate/compare.py b/alembic/autogenerate/compare.py index 49f04f1..d932736 100644 --- a/alembic/autogenerate/compare.py +++ b/alembic/autogenerate/compare.py @@ -19,11 +19,37 @@ def _compare_tables(conn_table_names, metadata_table_names, object_filters, inspector, metadata, diffs, autogen_context): + default_schema = inspector.bind.dialect.default_schema_name + + # tables coming from the connection will not have "schema" + # set if it matches default_schema_name; so we need a list + # of table names from local metadata that also have "None" if schema + # == default_schema_name. Most setups will be like this anyway but + # some are not (see #170) + metadata_table_names_no_dflt_schema = OrderedSet([ + (schema if schema != default_schema else None, tname) + for schema, tname in metadata_table_names + ]) + + # to adjust for the MetaData collection storing the tables either + # as "schemaname.tablename" or just "tablename", create a new lookup + # which will match the "non-default-schema" keys to the Table object. + tname_to_table = dict( + ( + no_dflt_schema, + metadata.tables[sa_schema._get_table_key(tname, schema)] + ) + for no_dflt_schema, (schema, tname) in zip( + metadata_table_names_no_dflt_schema, + metadata_table_names) + ) + metadata_table_names = metadata_table_names_no_dflt_schema + for s, tname in metadata_table_names.difference(conn_table_names): name = '%s.%s' % (s, tname) if s else tname - metadata_table = metadata.tables[sa_schema._get_table_key(tname, s)] + metadata_table = tname_to_table[(s, tname)] if _run_filters(metadata_table, tname, "table", False, None, object_filters): - diffs.append(("add_table", metadata.tables[name])) + diffs.append(("add_table", metadata_table)) log.info("Detected added table %r", name) _compare_indexes_and_uniques(s, tname, object_filters, None, @@ -55,7 +81,7 @@ def _compare_tables(conn_table_names, metadata_table_names, for s, tname in sorted(existing_tables): name = '%s.%s' % (s, tname) if s else tname - metadata_table = metadata.tables[name] + metadata_table = tname_to_table[(s, tname)] conn_table = existing_metadata.tables[name] if _run_filters(metadata_table, tname, "table", False, conn_table, object_filters): |