forked from Pogchamp-company/alembic-postgresql-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompare_dispatch.py
More file actions
87 lines (71 loc) · 2.87 KB
/
compare_dispatch.py
File metadata and controls
87 lines (71 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import logging
from typing import Iterable, Union
import alembic
from alembic.autogenerate.api import AutogenContext
from alembic.operations.ops import UpgradeOps, CreateTableOp
from alembic_postgresql_enum.add_create_type_false import add_create_type_false
from alembic_postgresql_enum.add_postgres_using_to_text import (
add_postgres_using_to_text,
)
from alembic_postgresql_enum.detection_of_changes import (
sync_changed_enums,
create_new_enums,
drop_unused_enums,
)
from alembic_postgresql_enum.get_enum_data import get_defined_enums, get_declared_enums
from alembic_postgresql_enum.configuration import get_configuration
log = logging.getLogger(f"alembic.{__name__}")
@alembic.autogenerate.comparators.dispatch_for("schema")
def compare_enums(
autogen_context: AutogenContext,
upgrade_ops: UpgradeOps,
schema_names: Iterable[Union[str, None]],
):
"""
Walk the declared SQLAlchemy schema for every referenced Enum, walk the PG
schema for every defined Enum, then generate SyncEnumValuesOp migrations
for each defined enum that has changed new entries when compared to its
declared version.
"""
assert (
autogen_context.dialect is not None
and autogen_context.dialect.default_schema_name is not None
and autogen_context.connection is not None
and autogen_context.metadata is not None
)
if autogen_context.dialect.name != "postgresql":
log.warning(
f"This library only supports postgresql, but you are using {autogen_context.dialect.name}, skipping"
)
return
add_create_type_false(upgrade_ops)
add_postgres_using_to_text(upgrade_ops)
configuration = get_configuration()
schema_names = list(schema_names)
# Issue #40
# Add schema if it is gonna be created inside the migration
for operations_group in upgrade_ops.ops:
if isinstance(operations_group, CreateTableOp) and operations_group.schema not in schema_names:
schema_names.append(operations_group.schema)
for schema in schema_names:
default_schema = autogen_context.dialect.default_schema_name
if schema is None:
schema = default_schema
definitions = get_defined_enums(autogen_context.connection, schema, configuration.include_name)
declarations = get_declared_enums(
autogen_context.metadata,
schema,
default_schema,
autogen_context.connection,
upgrade_ops,
configuration.include_name,
)
create_new_enums(definitions, declarations.enum_values, schema, upgrade_ops)
drop_unused_enums(definitions, declarations.enum_values, schema, upgrade_ops)
sync_changed_enums(
definitions,
declarations.enum_values,
declarations.enum_table_references,
schema,
upgrade_ops,
)