comdab 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- comdab/__init__.py +25 -0
- comdab/build.py +238 -0
- comdab/compare.py +182 -0
- comdab/exceptions.py +18 -0
- comdab/main.py +99 -0
- comdab/models/__init__.py +34 -0
- comdab/models/base.py +5 -0
- comdab/models/column.py +31 -0
- comdab/models/constraint.py +101 -0
- comdab/models/function.py +22 -0
- comdab/models/index.py +24 -0
- comdab/models/schema.py +33 -0
- comdab/models/sequence.py +32 -0
- comdab/models/table.py +37 -0
- comdab/models/trigger.py +22 -0
- comdab/models/type.py +150 -0
- comdab/models/view.py +24 -0
- comdab/path.py +161 -0
- comdab/py.typed +0 -0
- comdab/report.py +37 -0
- comdab/source.py +11 -0
- comdab/specific/__init__.py +0 -0
- comdab/specific/postgresql/__init__.py +0 -0
- comdab/specific/postgresql/build.py +100 -0
- comdab/specific/postgresql/exclude_constraints.py +82 -0
- comdab/specific/postgresql/functions.py +39 -0
- comdab/specific/postgresql/sequences.py +53 -0
- comdab/specific/postgresql/triggers.py +45 -0
- comdab/specific/postgresql/views.py +40 -0
- comdab/utils.py +43 -0
- comdab-0.1.0.dist-info/METADATA +87 -0
- comdab-0.1.0.dist-info/RECORD +34 -0
- comdab-0.1.0.dist-info/WHEEL +4 -0
- comdab-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
from collections.abc import Iterator
|
|
2
|
+
from functools import cached_property
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from sqlalchemy import MetaData, Table, types
|
|
6
|
+
from sqlalchemy.dialects.postgresql import HSTORE, ranges
|
|
7
|
+
|
|
8
|
+
from comdab.build import ComdabBuilder
|
|
9
|
+
from comdab.exceptions import UnhandledTypeError
|
|
10
|
+
from comdab.models.constraint import ComdabConstraint, ComdabExcludeConstraint
|
|
11
|
+
from comdab.models.function import ComdabFunction
|
|
12
|
+
from comdab.models.sequence import ComdabSequence
|
|
13
|
+
from comdab.models.trigger import ComdabTrigger
|
|
14
|
+
from comdab.models.type import ComdabType, ComdabTypes
|
|
15
|
+
from comdab.models.view import ComdabView
|
|
16
|
+
from comdab.specific.postgresql.exclude_constraints import get_postgresql_exclude_constraints_by_table
|
|
17
|
+
from comdab.specific.postgresql.functions import get_postgresql_functions
|
|
18
|
+
from comdab.specific.postgresql.sequences import get_postgresql_sequences
|
|
19
|
+
from comdab.specific.postgresql.triggers import get_postgresql_triggers_by_table
|
|
20
|
+
from comdab.specific.postgresql.views import get_postgresql_views
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ComdabPostgreSQLBuilder(ComdabBuilder):
|
|
24
|
+
# Level 1
|
|
25
|
+
|
|
26
|
+
def generate_views(self, sa_metadata: MetaData) -> Iterator[ComdabView]:
|
|
27
|
+
yield from super().generate_views(sa_metadata)
|
|
28
|
+
yield from get_postgresql_views(self.source)
|
|
29
|
+
|
|
30
|
+
def generate_sequences(self, sa_metadata: MetaData) -> Iterator[ComdabSequence]:
|
|
31
|
+
yield from super().generate_sequences(sa_metadata)
|
|
32
|
+
yield from get_postgresql_sequences(self.source)
|
|
33
|
+
|
|
34
|
+
def generate_functions(self, sa_metadata: MetaData) -> Iterator[ComdabFunction]:
|
|
35
|
+
yield from super().generate_functions(sa_metadata)
|
|
36
|
+
yield from get_postgresql_functions(self.source)
|
|
37
|
+
|
|
38
|
+
# Level 2
|
|
39
|
+
|
|
40
|
+
@cached_property
|
|
41
|
+
def _exclude_constraints_by_table(self) -> dict[str, list[ComdabExcludeConstraint]]:
|
|
42
|
+
return get_postgresql_exclude_constraints_by_table(self.source)
|
|
43
|
+
|
|
44
|
+
def generate_constraints(self, sa_table: Table) -> Iterator[ComdabConstraint]:
|
|
45
|
+
yield from super().generate_constraints(sa_table)
|
|
46
|
+
yield from self._exclude_constraints_by_table.get(sa_table.name, ())
|
|
47
|
+
|
|
48
|
+
@cached_property
|
|
49
|
+
def _triggers_by_table(self) -> dict[str, list[ComdabTrigger]]:
|
|
50
|
+
return get_postgresql_triggers_by_table(self.source)
|
|
51
|
+
|
|
52
|
+
def generate_triggers(self, sa_table: Table) -> Iterator[ComdabTrigger]:
|
|
53
|
+
yield from super().generate_triggers(sa_table)
|
|
54
|
+
yield from self._triggers_by_table.get(sa_table.name, ())
|
|
55
|
+
|
|
56
|
+
# Level 3
|
|
57
|
+
|
|
58
|
+
def build_type(self, sa_type: types.TypeEngine[Any]) -> ComdabType:
|
|
59
|
+
implem_name = sa_type.__visit_name__
|
|
60
|
+
match sa_type:
|
|
61
|
+
case HSTORE():
|
|
62
|
+
return ComdabTypes.HSTORE(implem_name=implem_name)
|
|
63
|
+
case ranges.AbstractMultiRange():
|
|
64
|
+
return ComdabTypes.MultiRange(implem_name=implem_name, item_type=self._get_multirange_type(sa_type))
|
|
65
|
+
case ranges.AbstractRange():
|
|
66
|
+
return ComdabTypes.Range(implem_name=implem_name, item_type=self._get_range_type(sa_type))
|
|
67
|
+
case _:
|
|
68
|
+
return super().build_type(sa_type)
|
|
69
|
+
|
|
70
|
+
def _get_range_type(self, sa_type: ranges.AbstractRange[Any]) -> ComdabType:
|
|
71
|
+
sub_implem_name = sa_type.__visit_name__.removesuffix("RANGE")
|
|
72
|
+
match sa_type:
|
|
73
|
+
case ranges.INT4RANGE() | ranges.INT8RANGE():
|
|
74
|
+
return ComdabTypes.Integer(implem_name=sub_implem_name)
|
|
75
|
+
case ranges.NUMRANGE():
|
|
76
|
+
return ComdabTypes.Numeric(implem_name=sub_implem_name, precision=None, scale=None)
|
|
77
|
+
case ranges.TSRANGE():
|
|
78
|
+
return ComdabTypes.DateTime(implem_name=sub_implem_name, with_timezone=False)
|
|
79
|
+
case ranges.TSTZRANGE():
|
|
80
|
+
return ComdabTypes.DateTime(implem_name=sub_implem_name, with_timezone=True)
|
|
81
|
+
case ranges.DATERANGE():
|
|
82
|
+
return ComdabTypes.Date(implem_name=sub_implem_name)
|
|
83
|
+
case _:
|
|
84
|
+
raise UnhandledTypeError(f"Unhandled PostgreSQL RANGE type: {sa_type}")
|
|
85
|
+
|
|
86
|
+
def _get_multirange_type(self, sa_type: ranges.AbstractMultiRange[Any]) -> ComdabType:
|
|
87
|
+
sub_implem_name = sa_type.__visit_name__.removesuffix("MULTIRANGE")
|
|
88
|
+
match sa_type:
|
|
89
|
+
case ranges.INT4MULTIRANGE() | ranges.INT8MULTIRANGE():
|
|
90
|
+
return ComdabTypes.Integer(implem_name=sub_implem_name)
|
|
91
|
+
case ranges.NUMMULTIRANGE():
|
|
92
|
+
return ComdabTypes.Numeric(implem_name=sub_implem_name, precision=None, scale=None)
|
|
93
|
+
case ranges.TSMULTIRANGE():
|
|
94
|
+
return ComdabTypes.DateTime(implem_name=sub_implem_name, with_timezone=False)
|
|
95
|
+
case ranges.TSTZMULTIRANGE():
|
|
96
|
+
return ComdabTypes.DateTime(implem_name=sub_implem_name, with_timezone=True)
|
|
97
|
+
case ranges.DATEMULTIRANGE():
|
|
98
|
+
return ComdabTypes.Date(implem_name=sub_implem_name)
|
|
99
|
+
case _:
|
|
100
|
+
raise UnhandledTypeError(f"Unhandled PostgreSQL MULTIRANGE type: {sa_type}")
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
from itertools import groupby
|
|
2
|
+
from typing import NamedTuple
|
|
3
|
+
|
|
4
|
+
from comdab.models.constraint import ComdabExcludeConstraint
|
|
5
|
+
from comdab.source import ComdabSource
|
|
6
|
+
from comdab.utils import typed_sql
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class _ExcludeConstraintsQuery(NamedTuple):
|
|
10
|
+
relname: str
|
|
11
|
+
conname: str
|
|
12
|
+
condeferrable: bool
|
|
13
|
+
condeferred: bool
|
|
14
|
+
ix_typ: str
|
|
15
|
+
attributes: list[str]
|
|
16
|
+
operators: list[str]
|
|
17
|
+
where_expr: str | None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
_EXCLUDE_CONSTRAINTS_QUERY = typed_sql[_ExcludeConstraintsQuery](
|
|
21
|
+
"""--sql
|
|
22
|
+
SELECT
|
|
23
|
+
rel.relname,
|
|
24
|
+
con.conname,
|
|
25
|
+
con.condeferrable,
|
|
26
|
+
con.condeferred,
|
|
27
|
+
ix_info.ix_typ,
|
|
28
|
+
ix_info.attributes,
|
|
29
|
+
op_info.operators,
|
|
30
|
+
pg_get_expr(ix_info.indpred, rel.oid) AS where_expr
|
|
31
|
+
FROM pg_catalog.pg_constraint con -- Constraint
|
|
32
|
+
JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid -- Constraint table
|
|
33
|
+
JOIN pg_catalog.pg_namespace nsp ON nsp.oid = con.connamespace -- Constraint schema
|
|
34
|
+
JOIN (
|
|
35
|
+
SELECT con2.oid, am.amname AS ix_typ, ARRAY_AGG(att.attname ORDER BY att.attnum ASC) AS attributes, ind.indpred
|
|
36
|
+
FROM pg_catalog.pg_constraint con2
|
|
37
|
+
JOIN pg_catalog.pg_class ix ON ix.oid = con2.conindid -- Index backing the constraint
|
|
38
|
+
JOIN pg_catalog.pg_index ind on ind.indexrelid = ix.oid -- Same but other view
|
|
39
|
+
JOIN pg_catalog.pg_am am ON am.oid = ix.relam -- Index access method (eg. type)
|
|
40
|
+
JOIN pg_catalog.pg_attribute att ON att.attrelid = ix.oid -- Attribute referenced in the index
|
|
41
|
+
GROUP BY con2.oid, ind.indexrelid, am.oid
|
|
42
|
+
) ix_info ON ix_info.oid = con.oid
|
|
43
|
+
JOIN (
|
|
44
|
+
SELECT con3.oid, ARRAY_AGG(opr.oprname) AS operators
|
|
45
|
+
FROM pg_catalog.pg_constraint con3
|
|
46
|
+
JOIN (
|
|
47
|
+
SELECT con4.oid, unnest(con4.conexclop) as opid -- Unnest constraint operators
|
|
48
|
+
FROM pg_catalog.pg_constraint con4
|
|
49
|
+
) con_ops ON con_ops.oid = con3.oid
|
|
50
|
+
JOIN pg_operator opr ON opr.oid = con_ops.opid -- Operator info
|
|
51
|
+
GROUP BY con3.oid
|
|
52
|
+
) op_info ON op_info.oid = con.oid
|
|
53
|
+
WHERE nsp.nspname = :schema_name
|
|
54
|
+
AND con.contype = 'x'
|
|
55
|
+
ORDER BY rel.relname;
|
|
56
|
+
""",
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def get_postgresql_exclude_constraints_by_table(
|
|
61
|
+
source: ComdabSource,
|
|
62
|
+
) -> dict[str, list[ComdabExcludeConstraint]]:
|
|
63
|
+
result = source.connection.execute(
|
|
64
|
+
_EXCLUDE_CONSTRAINTS_QUERY,
|
|
65
|
+
{"schema_name": source.schema_name},
|
|
66
|
+
)
|
|
67
|
+
return {
|
|
68
|
+
table_name: [
|
|
69
|
+
ComdabExcludeConstraint(
|
|
70
|
+
name=row.conname,
|
|
71
|
+
attributes_and_operators=list(zip(row.attributes, row.operators, strict=True)),
|
|
72
|
+
deferrable=row.condeferrable,
|
|
73
|
+
initially="DEFERRED" if row.condeferred else None,
|
|
74
|
+
extra={
|
|
75
|
+
**({"postgresql_using": row.ix_typ}),
|
|
76
|
+
**({"postgresql_where": row.where_expr} if row.where_expr else {}),
|
|
77
|
+
},
|
|
78
|
+
)
|
|
79
|
+
for row in rows
|
|
80
|
+
]
|
|
81
|
+
for table_name, rows in groupby(result.t, lambda row: row.relname)
|
|
82
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from typing import NamedTuple
|
|
2
|
+
|
|
3
|
+
from comdab.models.function import ComdabFunction
|
|
4
|
+
from comdab.source import ComdabSource
|
|
5
|
+
from comdab.utils import typed_sql
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class _FunctionsQuery(NamedTuple):
|
|
9
|
+
proname: str
|
|
10
|
+
functiondef: str
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
_FUNCTIONS_QUERY = typed_sql[_FunctionsQuery](
|
|
14
|
+
"""--sql
|
|
15
|
+
SELECT
|
|
16
|
+
pro.proname,
|
|
17
|
+
pg_get_functiondef(pro.oid) AS functiondef
|
|
18
|
+
FROM pg_catalog.pg_proc pro -- Function
|
|
19
|
+
JOIN pg_catalog.pg_namespace nsp ON nsp.oid = pro.pronamespace -- Function schema
|
|
20
|
+
JOIN pg_catalog.pg_language lan ON lan.oid = pro.prolang -- Function implementation language
|
|
21
|
+
WHERE nsp.nspname = :schema_name
|
|
22
|
+
AND pro.prokind = 'f' -- TODO: handle procedure/aggregate/window functions?
|
|
23
|
+
AND lan.lanname != 'c';
|
|
24
|
+
""",
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def get_postgresql_functions(source: ComdabSource) -> list[ComdabFunction]:
|
|
29
|
+
result = source.connection.execute(
|
|
30
|
+
_FUNCTIONS_QUERY,
|
|
31
|
+
{"schema_name": source.schema_name},
|
|
32
|
+
)
|
|
33
|
+
return [
|
|
34
|
+
ComdabFunction(
|
|
35
|
+
name=row.proname,
|
|
36
|
+
definition=row.functiondef,
|
|
37
|
+
)
|
|
38
|
+
for row in result.t
|
|
39
|
+
]
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from typing import NamedTuple
|
|
2
|
+
|
|
3
|
+
from comdab.models.sequence import ComdabSequence
|
|
4
|
+
from comdab.source import ComdabSource
|
|
5
|
+
from comdab.utils import typed_sql
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class _SequencesQuery(NamedTuple):
|
|
9
|
+
relname: str
|
|
10
|
+
typname: str
|
|
11
|
+
seqstart: int
|
|
12
|
+
seqincrement: int
|
|
13
|
+
seqmin: int
|
|
14
|
+
seqmax: int
|
|
15
|
+
seqcycle: bool
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
_SEQUENCES_QUERY = typed_sql[_SequencesQuery](
|
|
19
|
+
"""--sql
|
|
20
|
+
SELECT
|
|
21
|
+
rel.relname,
|
|
22
|
+
typ.typname,
|
|
23
|
+
seq.seqstart,
|
|
24
|
+
seq.seqincrement,
|
|
25
|
+
seq.seqmin,
|
|
26
|
+
seq.seqmax,
|
|
27
|
+
seq.seqcycle
|
|
28
|
+
FROM pg_catalog.pg_sequence seq -- Sequence
|
|
29
|
+
JOIN pg_catalog.pg_class rel ON rel.oid = seq.seqrelid -- Sequence, as a class
|
|
30
|
+
JOIN pg_catalog.pg_namespace nsp ON nsp.oid = rel.relnamespace -- Sequence schema
|
|
31
|
+
JOIN pg_catalog.pg_type typ ON typ.oid = seq.seqtypid -- Sequence type
|
|
32
|
+
WHERE nsp.nspname = :schema_name;
|
|
33
|
+
""",
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def get_postgresql_sequences(source: ComdabSource) -> list[ComdabSequence]:
|
|
38
|
+
result = source.connection.execute(
|
|
39
|
+
_SEQUENCES_QUERY,
|
|
40
|
+
{"schema_name": source.schema_name},
|
|
41
|
+
)
|
|
42
|
+
return [
|
|
43
|
+
ComdabSequence(
|
|
44
|
+
name=row.relname,
|
|
45
|
+
type_name=row.typname,
|
|
46
|
+
start=row.seqstart,
|
|
47
|
+
increment=row.seqincrement,
|
|
48
|
+
min=row.seqmin,
|
|
49
|
+
max=row.seqmax,
|
|
50
|
+
cycle=row.seqcycle,
|
|
51
|
+
)
|
|
52
|
+
for row in result.t
|
|
53
|
+
]
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from itertools import groupby
|
|
2
|
+
from typing import NamedTuple
|
|
3
|
+
|
|
4
|
+
from comdab.models.trigger import ComdabTrigger
|
|
5
|
+
from comdab.source import ComdabSource
|
|
6
|
+
from comdab.utils import typed_sql
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class _TriggersQuery(NamedTuple):
|
|
10
|
+
relname: str
|
|
11
|
+
tgname: str
|
|
12
|
+
triggerdef: str
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
_TRIGGERS_QUERY = typed_sql[_TriggersQuery](
|
|
16
|
+
"""--sql
|
|
17
|
+
SELECT
|
|
18
|
+
rel.relname,
|
|
19
|
+
tg.tgname,
|
|
20
|
+
pg_get_triggerdef(tg.oid) AS triggerdef
|
|
21
|
+
FROM pg_catalog.pg_trigger tg -- Trigger
|
|
22
|
+
JOIN pg_catalog.pg_class rel ON rel.oid = tg.tgrelid -- Trigger table
|
|
23
|
+
JOIN pg_catalog.pg_namespace nsp ON nsp.oid = rel.relnamespace -- Trigger schema
|
|
24
|
+
WHERE nsp.nspname = :schema_name
|
|
25
|
+
AND NOT tg.tgisinternal
|
|
26
|
+
ORDER BY rel.relname;
|
|
27
|
+
""",
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def get_postgresql_triggers_by_table(source: ComdabSource) -> dict[str, list[ComdabTrigger]]:
|
|
32
|
+
result = source.connection.execute(
|
|
33
|
+
_TRIGGERS_QUERY,
|
|
34
|
+
{"schema_name": source.schema_name},
|
|
35
|
+
)
|
|
36
|
+
return {
|
|
37
|
+
table_name: [
|
|
38
|
+
ComdabTrigger(
|
|
39
|
+
name=row.tgname,
|
|
40
|
+
definition=row.triggerdef,
|
|
41
|
+
)
|
|
42
|
+
for row in rows
|
|
43
|
+
]
|
|
44
|
+
for table_name, rows in groupby(result.t, lambda row: row.relname)
|
|
45
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from typing import NamedTuple
|
|
2
|
+
|
|
3
|
+
from comdab.models.view import ComdabView
|
|
4
|
+
from comdab.source import ComdabSource
|
|
5
|
+
from comdab.utils import typed_sql
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class _ViewsQuery(NamedTuple):
|
|
9
|
+
relname: str
|
|
10
|
+
relkind: str
|
|
11
|
+
viewdef: str
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
_VIEWS_QUERY = typed_sql[_ViewsQuery](
|
|
15
|
+
"""--sql
|
|
16
|
+
SELECT
|
|
17
|
+
rel.relname,
|
|
18
|
+
rel.relkind,
|
|
19
|
+
pg_get_viewdef(rel.oid) AS viewdef
|
|
20
|
+
FROM pg_catalog.pg_class rel -- View
|
|
21
|
+
JOIN pg_catalog.pg_namespace nsp ON nsp.oid = rel.relnamespace -- View schema
|
|
22
|
+
WHERE nsp.nspname = :schema_name
|
|
23
|
+
AND rel.relkind IN ('v', 'm');
|
|
24
|
+
""",
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def get_postgresql_views(source: ComdabSource) -> list[ComdabView]:
|
|
29
|
+
result = source.connection.execute(
|
|
30
|
+
_VIEWS_QUERY,
|
|
31
|
+
{"schema_name": source.schema_name},
|
|
32
|
+
)
|
|
33
|
+
return [
|
|
34
|
+
ComdabView(
|
|
35
|
+
name=row.relname,
|
|
36
|
+
definition=row.viewdef.strip(),
|
|
37
|
+
materialized=row.relkind == "m",
|
|
38
|
+
)
|
|
39
|
+
for row in result.t
|
|
40
|
+
]
|
comdab/utils.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Internal helpers used by comdab."""
|
|
2
|
+
|
|
3
|
+
import textwrap
|
|
4
|
+
from collections.abc import Iterable
|
|
5
|
+
from typing import LiteralString, NamedTuple, Protocol, cast
|
|
6
|
+
|
|
7
|
+
from sqlalchemy import text
|
|
8
|
+
from sqlalchemy.sql.selectable import TypedReturnsRows
|
|
9
|
+
|
|
10
|
+
from comdab.exceptions import ComdabInternalError
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class _HasName(Protocol):
|
|
14
|
+
@property
|
|
15
|
+
def name(self) -> str: ...
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def dict_by_name[T: _HasName](iterable: Iterable[T]) -> dict[str, T]:
|
|
19
|
+
"""Transform an iterable into a mapping of items by their "name" property.
|
|
20
|
+
|
|
21
|
+
* Order items by name alphabetically
|
|
22
|
+
* Ensure names are unique
|
|
23
|
+
"""
|
|
24
|
+
items = sorted(((item.name, item) for item in iterable), key=lambda it: it[0])
|
|
25
|
+
dic = dict(items)
|
|
26
|
+
if len(dic) < len(items):
|
|
27
|
+
_seen = set[str]()
|
|
28
|
+
_duplicate_keys = {x for x, _ in items if x in _seen or _seen.add(x)}
|
|
29
|
+
raise ComdabInternalError(f"dict_by_name: duplicate keys: {_duplicate_keys}")
|
|
30
|
+
return dic
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class typed_sql[T: NamedTuple]: # Use a class rather than a function to allow subscription, waiting for PEP 718
|
|
34
|
+
"""Clean & type a SQL textual query multiline string.
|
|
35
|
+
|
|
36
|
+
* Make the string an executable :cls:`sqlalchemy.TextClause`
|
|
37
|
+
* Remove "--sql" comment needed by https://marketplace.visualstudio.com/items?itemName=ptweir.python-string-sql
|
|
38
|
+
* Remove indentation-caused leading whitespace
|
|
39
|
+
* Type is as a SQLAlchemy selectable producing rows typed as the NamedTuple passed as generic argument
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __new__(cls, sql: LiteralString) -> TypedReturnsRows[T]:
|
|
43
|
+
return cast(TypedReturnsRows[T], text(textwrap.dedent(sql.removeprefix("--sql\n"))))
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: comdab
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Compare Database Schemas
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: pydantic>=2.0
|
|
8
|
+
Requires-Dist: sqlalchemy>=2.0
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# comdab — Compare Database Schemas
|
|
12
|
+
|
|
13
|
+
_comdab_ allows you to compare in depth two database schemas to find
|
|
14
|
+
all differences: missing columns, different nullabilities or defaults,
|
|
15
|
+
slight changes in function or triggers definitions...
|
|
16
|
+
|
|
17
|
+
> [!WARNING]
|
|
18
|
+
>
|
|
19
|
+
> _comdab_ is still in development, only tested with PostgreSQL 14 to date.
|
|
20
|
+
>
|
|
21
|
+
> All feedback and contributions are welcome!
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Use [pip](https://pip.pypa.io) to install comdab:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install comdab
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Requirements
|
|
32
|
+
|
|
33
|
+
- Python `>= 3.12`
|
|
34
|
+
- [sqlalchemy](https://pypi.org/project/sqlalchemy) `>= 2.0`
|
|
35
|
+
- [pydantic](https://pypi.org/project/pydantic) `>= 2.0`
|
|
36
|
+
|
|
37
|
+
## Goals
|
|
38
|
+
|
|
39
|
+
_comdab_ is especially useful in combination with a migration tool like
|
|
40
|
+
[Alembic](https://alembic.sqlalchemy.org), to make sure that applying a
|
|
41
|
+
migration to an existing database
|
|
42
|
+
and creating a new database from scratch produce **the exact same schema**.
|
|
43
|
+
|
|
44
|
+
Indeed, while migration tools can auto-detect model changes and write
|
|
45
|
+
automatically the migrations to apply to pre-existing databases, it does not
|
|
46
|
+
cover the whole schema complexity, and does not prevent human errors (like
|
|
47
|
+
modifying the model without re-generating migrations, or manually editing
|
|
48
|
+
migrations in a slightly wrong way...)
|
|
49
|
+
|
|
50
|
+
This may cause dangerous and hard to spot bugs, especially if your unit tests
|
|
51
|
+
and CI run on a fresh database created from the Python-written model, and not
|
|
52
|
+
on pre-existing databases with the new migration applied.
|
|
53
|
+
|
|
54
|
+
By running _comdab_, you can ensure the two are the nearly-exact same.
|
|
55
|
+
|
|
56
|
+
_comdab_ stands on the shoulders of the [SQLAlchemy](https://sqlalchemy.org)
|
|
57
|
+
library, used to connect to the database and for most of schema introspection.
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
```py
|
|
62
|
+
from comdab import compare_databases
|
|
63
|
+
from sqlalchemy import create_engine
|
|
64
|
+
|
|
65
|
+
engine_1 = create_engine("postgresql://user:pass@host/foo")
|
|
66
|
+
engine_2 = create_engine("postgresql://user:pass@host/bar")
|
|
67
|
+
|
|
68
|
+
with engine_1.connect() as left_conn, engine_2.connect() as right_conn:
|
|
69
|
+
reports = compare_databases(left_conn, right_conn)
|
|
70
|
+
|
|
71
|
+
if reports:
|
|
72
|
+
print("❌ Database schemas are different:", reports)
|
|
73
|
+
else:
|
|
74
|
+
print("✅ Database schemas are the same!")
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
See documentation for configuration and other details.
|
|
78
|
+
|
|
79
|
+
## Contributing
|
|
80
|
+
|
|
81
|
+
Issues and pull requests are welcome!
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
This work is shared under [the MIT license](LICENSE);
|
|
86
|
+
|
|
87
|
+
@ 2025 Loïc Simon ([loic.simon@espci.org](mailto:loic.simon@espci.org))
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
comdab/__init__.py,sha256=mLKALYP0-gv7tSxKmrjxIuc3qgWubREOquZPFJpykXI,887
|
|
2
|
+
comdab/build.py,sha256=J0_1oDGcI-hhCKCuHcm9_nlmcKfxUxDcGwiqHv8sICw,10098
|
|
3
|
+
comdab/compare.py,sha256=qdc5zfbVtHV1JtyJjQdS7qXXd0zZli0_4_fRLHThZqE,6964
|
|
4
|
+
comdab/exceptions.py,sha256=y_XpqM6d42xu6jvnka1pNtLcicWzL-qyRakBJWjX3XE,513
|
|
5
|
+
comdab/main.py,sha256=UdSPk-6vrPzMzfxqxrqnGsJp7-W1agEOHRJxCarcpp4,4053
|
|
6
|
+
comdab/path.py,sha256=NuTK4c9CtAf3Oy-dxFBye5EL6D83RMOFrvprdlzFmBE,5771
|
|
7
|
+
comdab/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
comdab/report.py,sha256=1h6pGXQlgorgqdgfgHctSOXqZfbg3ni0Rc1OvQ-g75A,1305
|
|
9
|
+
comdab/source.py,sha256=MabLkot_RmZ4dC1_68G1K0_bJ4Ie2UrnY5YKUusQ9nA,248
|
|
10
|
+
comdab/utils.py,sha256=uiLgqqMWhM4voa03VyJ0H9y-T4Jb2LPjFszmEV6da6U,1614
|
|
11
|
+
comdab/models/__init__.py,sha256=rglTwHVc3sdYZnUNax6fOSyneh2f9XMIEHR8Ig3aG2g,973
|
|
12
|
+
comdab/models/base.py,sha256=qEey7c7_nZih1mWWGW1UUcpV79nAEyv9A6hpwHjPoSU,123
|
|
13
|
+
comdab/models/column.py,sha256=lEIAX2OtymiOovUh7hrqQnzgJgABIc4m0vr9KpOMJ4I,891
|
|
14
|
+
comdab/models/constraint.py,sha256=r9BOA57nlPXTrFGWgCK_Gdamdh8E7hzfBmFplrfUSh8,2639
|
|
15
|
+
comdab/models/function.py,sha256=V-vgzErNZhS4d2fpXU6cuDg6mqYeimUxrOefbHNXbJ4,534
|
|
16
|
+
comdab/models/index.py,sha256=m2_RlXZzsHM_Ut5OgJlTMobBqOLajAcEIFuYyerZqak,583
|
|
17
|
+
comdab/models/schema.py,sha256=WdjXSGJWaAIlDwf3FImF-Z4behLYUXlG3DSDo6_MbWU,1024
|
|
18
|
+
comdab/models/sequence.py,sha256=q8wCniXh-ozuQ3M2E7UydJTd62UuSnUZwmkfcnSo5wQ,751
|
|
19
|
+
comdab/models/table.py,sha256=9wqX8uR-Vi3C1FOITuU8T-8QsTdc47clZP0qnpzNRLQ,1358
|
|
20
|
+
comdab/models/trigger.py,sha256=iT_W07xtZP8YW-q9skLWVBqPkzl0d3Ts8nnzsH52Iuc,515
|
|
21
|
+
comdab/models/type.py,sha256=euFp80W1IHJbAowYOA-zAJtoFT3IZRAMPUFs08GoY5c,4173
|
|
22
|
+
comdab/models/view.py,sha256=z6N__xE9pzyJ-MFZ9SZ-BsEHZH1qeq7LEFhgxJsx_kc,585
|
|
23
|
+
comdab/specific/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
24
|
+
comdab/specific/postgresql/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
25
|
+
comdab/specific/postgresql/build.py,sha256=G3xTYJhGwkqgmppU4EtCJpRRl9wMhM3oKhjmoy-GJHQ,4945
|
|
26
|
+
comdab/specific/postgresql/exclude_constraints.py,sha256=HPfe3O4sMOZE8RYoZoKbqSeCwdYzFALXgDnbjbrA01M,3155
|
|
27
|
+
comdab/specific/postgresql/functions.py,sha256=QSumJvhTSOcGNmcNNjKoVRLTC8s76-wpoKbawB2zAAM,1161
|
|
28
|
+
comdab/specific/postgresql/sequences.py,sha256=8OKuwH7iT_aRJB8kO0MTDSJz0cgwSZehWiqUCcXKslo,1451
|
|
29
|
+
comdab/specific/postgresql/triggers.py,sha256=4-CTn0WF4LMJ7lVxiACwoMpoLWKIDBj_0yr2bj54XaU,1278
|
|
30
|
+
comdab/specific/postgresql/views.py,sha256=jYsowMcdY7G6Oy65Wfrwi5SahDtUT0NclE2AgeZCr5Q,1010
|
|
31
|
+
comdab-0.1.0.dist-info/METADATA,sha256=NkztV9RApYFhXGZOjh4gaWveq_bOVvuUdd8iEn53cGM,2627
|
|
32
|
+
comdab-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
33
|
+
comdab-0.1.0.dist-info/licenses/LICENSE,sha256=riYgac7C22wFTsJmMelv5ll5Dg61Y5_bZAru9FKPXqs,1068
|
|
34
|
+
comdab-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Loïc Simon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|