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,101 @@
|
|
|
1
|
+
from typing import Any, get_args
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
|
|
5
|
+
from comdab.models.base import ComdabModel
|
|
6
|
+
from comdab.path import ComdabPath, dict_of_paths, terminal_path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class _BaseComdabConstraint(ComdabModel, frozen=True):
|
|
10
|
+
"""Root class representing a database constraint.
|
|
11
|
+
|
|
12
|
+
Equivalent to a :class:`sqlalchemy.Constraint` object.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
type: str
|
|
16
|
+
name: str
|
|
17
|
+
deferrable: bool | None
|
|
18
|
+
initially: str | None
|
|
19
|
+
extra: dict[str, Any] = Field(default_factory=dict)
|
|
20
|
+
|
|
21
|
+
class Path(ComdabPath):
|
|
22
|
+
type = terminal_path()
|
|
23
|
+
name = terminal_path()
|
|
24
|
+
deferrable = terminal_path()
|
|
25
|
+
initially = terminal_path()
|
|
26
|
+
extra = dict_of_paths(ComdabPath)
|
|
27
|
+
|
|
28
|
+
# Union of fields of all constraint types
|
|
29
|
+
columns = terminal_path()
|
|
30
|
+
columns_mapping = dict_of_paths(ComdabPath)
|
|
31
|
+
on_update = terminal_path()
|
|
32
|
+
on_delete = terminal_path()
|
|
33
|
+
sql_text = terminal_path()
|
|
34
|
+
attributes_and_operators = terminal_path()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ComdabUniqueConstraint(_BaseComdabConstraint, frozen=True):
|
|
38
|
+
"""A database unique constraint.
|
|
39
|
+
|
|
40
|
+
Equivalent to a :class:`sqlalchemy.UniqueConstraint` object.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
type: str = "unique"
|
|
44
|
+
columns: set[str]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class ComdabPrimaryKeyConstraint(_BaseComdabConstraint, frozen=True):
|
|
48
|
+
"""A database primary key constraint.
|
|
49
|
+
|
|
50
|
+
Equivalent to a :class:`sqlalchemy.PrimaryKeyConstraint` object.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
type: str = "primary_key"
|
|
54
|
+
columns: set[str]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class ComdabForeignKeyConstraint(_BaseComdabConstraint, frozen=True):
|
|
58
|
+
"""A database foreign key constraint.
|
|
59
|
+
|
|
60
|
+
Equivalent to a :class:`sqlalchemy.ForeignKeyConstraint` object.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
type: str = "foreign_key"
|
|
64
|
+
columns_mapping: dict[str, str]
|
|
65
|
+
on_update: str | None
|
|
66
|
+
on_delete: str | None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class ComdabCheckConstraint(_BaseComdabConstraint, frozen=True):
|
|
70
|
+
"""A database check constraint.
|
|
71
|
+
|
|
72
|
+
Equivalent to a :class:`sqlalchemy.CheckConstraint` object.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
type: str = "check"
|
|
76
|
+
sql_text: str
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class ComdabExcludeConstraint(_BaseComdabConstraint, frozen=True):
|
|
80
|
+
"""A database constraint.
|
|
81
|
+
|
|
82
|
+
Not natively handled by SQLAlchemy.
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
type: str = "exclude"
|
|
86
|
+
attributes_and_operators: list[tuple[str, str]]
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
type ComdabConstraint = (
|
|
90
|
+
ComdabUniqueConstraint
|
|
91
|
+
| ComdabPrimaryKeyConstraint
|
|
92
|
+
| ComdabForeignKeyConstraint
|
|
93
|
+
| ComdabCheckConstraint
|
|
94
|
+
| ComdabExcludeConstraint
|
|
95
|
+
)
|
|
96
|
+
ComdabConstraint_Path = _BaseComdabConstraint.Path
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
assert set(get_args(ComdabConstraint.__value__)) == set(_BaseComdabConstraint.__subclasses__()), (
|
|
100
|
+
"Mismatch between ComdabConstraint members and _BaseComdabConstraint subclasses!"
|
|
101
|
+
)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
|
|
5
|
+
from comdab.models.base import ComdabModel
|
|
6
|
+
from comdab.path import ComdabPath, dict_of_paths, terminal_path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ComdabFunction(ComdabModel, frozen=True):
|
|
10
|
+
"""A database function.
|
|
11
|
+
|
|
12
|
+
Equivalent to a :class:`sqlalchemy.Function` object.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
name: str
|
|
16
|
+
definition: str
|
|
17
|
+
extra: dict[str, Any] = Field(default_factory=dict)
|
|
18
|
+
|
|
19
|
+
class Path(ComdabPath):
|
|
20
|
+
name = terminal_path()
|
|
21
|
+
definition = terminal_path()
|
|
22
|
+
extra = dict_of_paths(ComdabPath)
|
comdab/models/index.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
|
|
5
|
+
from comdab.models.base import ComdabModel
|
|
6
|
+
from comdab.path import ComdabPath, dict_of_paths, terminal_path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ComdabIndex(ComdabModel, frozen=True):
|
|
10
|
+
"""A database index.
|
|
11
|
+
|
|
12
|
+
Equivalent to a :class:`sqlalchemy.Index` object.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
name: str
|
|
16
|
+
expressions: list[str]
|
|
17
|
+
unique: bool
|
|
18
|
+
extra: dict[str, Any] = Field(default_factory=dict)
|
|
19
|
+
|
|
20
|
+
class Path(ComdabPath):
|
|
21
|
+
name = terminal_path()
|
|
22
|
+
expressions = terminal_path()
|
|
23
|
+
unique = terminal_path()
|
|
24
|
+
extra = dict_of_paths(ComdabPath)
|
comdab/models/schema.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
|
|
5
|
+
from comdab.models.base import ComdabModel
|
|
6
|
+
from comdab.models.function import ComdabFunction
|
|
7
|
+
from comdab.models.sequence import ComdabSequence
|
|
8
|
+
from comdab.models.table import ComdabTable
|
|
9
|
+
from comdab.models.view import ComdabView
|
|
10
|
+
from comdab.path import ComdabPath, dict_of_paths
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ComdabSchema(ComdabModel, frozen=True):
|
|
14
|
+
"""A database schema, the top-level comdab model.
|
|
15
|
+
|
|
16
|
+
Equivalent to a :class:`sqlalchemy.Metadata` object.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
tables: dict[str, ComdabTable]
|
|
20
|
+
views: dict[str, ComdabView]
|
|
21
|
+
sequences: dict[str, ComdabSequence]
|
|
22
|
+
functions: dict[str, ComdabFunction]
|
|
23
|
+
extra: dict[str, Any] = Field(default_factory=dict)
|
|
24
|
+
|
|
25
|
+
class Path(ComdabPath):
|
|
26
|
+
tables = dict_of_paths(ComdabTable.Path)
|
|
27
|
+
views = dict_of_paths(ComdabView.Path)
|
|
28
|
+
sequences = dict_of_paths(ComdabSequence.Path)
|
|
29
|
+
functions = dict_of_paths(ComdabFunction.Path)
|
|
30
|
+
extra = dict_of_paths(ComdabPath)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
ROOT = ComdabSchema.Path([], _ok=True)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
|
|
5
|
+
from comdab.models.base import ComdabModel
|
|
6
|
+
from comdab.path import ComdabPath, dict_of_paths, terminal_path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ComdabSequence(ComdabModel, frozen=True):
|
|
10
|
+
"""A database sequence.
|
|
11
|
+
|
|
12
|
+
Not natively handled by SQLAlchemy.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
name: str
|
|
16
|
+
type_name: str
|
|
17
|
+
start: int
|
|
18
|
+
increment: int
|
|
19
|
+
min: int
|
|
20
|
+
max: int
|
|
21
|
+
cycle: bool
|
|
22
|
+
extra: dict[str, Any] = Field(default_factory=dict)
|
|
23
|
+
|
|
24
|
+
class Path(ComdabPath):
|
|
25
|
+
name = terminal_path()
|
|
26
|
+
type_name = terminal_path()
|
|
27
|
+
start = terminal_path()
|
|
28
|
+
increment = terminal_path()
|
|
29
|
+
min = terminal_path()
|
|
30
|
+
max = terminal_path()
|
|
31
|
+
cycle = terminal_path()
|
|
32
|
+
extra = dict_of_paths(ComdabPath)
|
comdab/models/table.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
|
|
5
|
+
from comdab.models.base import ComdabModel
|
|
6
|
+
from comdab.models.column import ComdabColumn
|
|
7
|
+
from comdab.models.constraint import ComdabConstraint, ComdabConstraint_Path
|
|
8
|
+
from comdab.models.index import ComdabIndex
|
|
9
|
+
from comdab.models.trigger import ComdabTrigger
|
|
10
|
+
from comdab.path import ComdabPath, dict_of_paths, terminal_path
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ComdabTable(ComdabModel, frozen=True):
|
|
14
|
+
"""A database table.
|
|
15
|
+
|
|
16
|
+
Equivalent to a :class:`sqlalchemy.Table` object.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
name: str
|
|
20
|
+
columns: dict[str, ComdabColumn]
|
|
21
|
+
constraints: dict[str, ComdabConstraint]
|
|
22
|
+
indexes: dict[str, ComdabIndex]
|
|
23
|
+
triggers: dict[str, ComdabTrigger]
|
|
24
|
+
# TODO: custom types?
|
|
25
|
+
extra: dict[str, Any] = Field(default_factory=dict)
|
|
26
|
+
|
|
27
|
+
# PostgreSQL parameters not reflected here: global/local, logged/unlogged, inheritance, partitioning,
|
|
28
|
+
# storage parameters (fillfactor, vacuum options...), on-commit behavior, tablespace, rules,
|
|
29
|
+
# row-level security, access method, replica identity, owner, comments
|
|
30
|
+
|
|
31
|
+
class Path(ComdabPath):
|
|
32
|
+
name = terminal_path()
|
|
33
|
+
columns = dict_of_paths(ComdabColumn.Path)
|
|
34
|
+
constraints = dict_of_paths(ComdabConstraint_Path)
|
|
35
|
+
indexes = dict_of_paths(ComdabIndex.Path)
|
|
36
|
+
triggers = dict_of_paths(ComdabTrigger.Path)
|
|
37
|
+
extra = dict_of_paths(ComdabPath)
|
comdab/models/trigger.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
|
|
5
|
+
from comdab.models.base import ComdabModel
|
|
6
|
+
from comdab.path import ComdabPath, dict_of_paths, terminal_path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ComdabTrigger(ComdabModel, frozen=True):
|
|
10
|
+
"""A database trigger.
|
|
11
|
+
|
|
12
|
+
Not natively handled by SQLAlchemy.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
name: str
|
|
16
|
+
definition: str
|
|
17
|
+
extra: dict[str, Any] = Field(default_factory=dict)
|
|
18
|
+
|
|
19
|
+
class Path(ComdabPath):
|
|
20
|
+
name = terminal_path()
|
|
21
|
+
definition = terminal_path()
|
|
22
|
+
extra = dict_of_paths(ComdabPath)
|
comdab/models/type.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
from typing import Any, get_args
|
|
2
|
+
|
|
3
|
+
from pydantic import ConfigDict, Field
|
|
4
|
+
from typing_extensions import Unpack
|
|
5
|
+
|
|
6
|
+
from comdab.models.base import ComdabModel
|
|
7
|
+
from comdab.path import (
|
|
8
|
+
ComdabPath,
|
|
9
|
+
_PathDescriptor, # pyright: ignore[reportPrivateUsage] # typing usage only
|
|
10
|
+
dict_of_paths,
|
|
11
|
+
sub_path,
|
|
12
|
+
terminal_path,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class _BaseComdabType(ComdabModel, frozen=True):
|
|
17
|
+
"""Root class representing a database type.
|
|
18
|
+
|
|
19
|
+
Equivalent to a :class:`sqlalchemy.types.TypeEngine` object.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
type: str = "" # Model field, but defined in __init_subclass__
|
|
23
|
+
implem_name: str
|
|
24
|
+
extra: dict[str, Any] = Field(default_factory=dict)
|
|
25
|
+
|
|
26
|
+
def __init_subclass__(cls, **kwargs: Unpack[ConfigDict]) -> None:
|
|
27
|
+
super().__init_subclass__(**kwargs)
|
|
28
|
+
cls.type = cls.__name__
|
|
29
|
+
|
|
30
|
+
class Path(ComdabPath):
|
|
31
|
+
type = terminal_path()
|
|
32
|
+
implem_name = terminal_path()
|
|
33
|
+
extra = dict_of_paths(ComdabPath)
|
|
34
|
+
|
|
35
|
+
# Union of fields of all types
|
|
36
|
+
length = terminal_path()
|
|
37
|
+
collation = terminal_path()
|
|
38
|
+
precision = terminal_path()
|
|
39
|
+
scale = terminal_path()
|
|
40
|
+
with_timezone = terminal_path()
|
|
41
|
+
second_precision = terminal_path()
|
|
42
|
+
day_precision = terminal_path()
|
|
43
|
+
length = terminal_path()
|
|
44
|
+
item_type: _PathDescriptor["_BaseComdabType.Path"] # Defined below (references itself)
|
|
45
|
+
dimensions = terminal_path()
|
|
46
|
+
values = terminal_path()
|
|
47
|
+
type_name = terminal_path()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
_item_type_descr = sub_path(_BaseComdabType.Path)
|
|
51
|
+
_item_type_descr.__set_name__(_BaseComdabType.Path, "item_type")
|
|
52
|
+
_BaseComdabType.Path.item_type = _item_type_descr
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class ComdabTypes:
|
|
56
|
+
"""Registry of all data types handled by comdab.
|
|
57
|
+
|
|
58
|
+
Each class member represent a database type, equivalent to a :class:`sqlalchemy.types.TypeEngine` class.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
class String(_BaseComdabType, frozen=True):
|
|
62
|
+
length: int | None
|
|
63
|
+
collation: str | None
|
|
64
|
+
|
|
65
|
+
class Integer(_BaseComdabType, frozen=True):
|
|
66
|
+
pass
|
|
67
|
+
|
|
68
|
+
class Float(_BaseComdabType, frozen=True):
|
|
69
|
+
pass
|
|
70
|
+
|
|
71
|
+
class Numeric(_BaseComdabType, frozen=True):
|
|
72
|
+
precision: int | None
|
|
73
|
+
scale: int | None
|
|
74
|
+
|
|
75
|
+
class Boolean(_BaseComdabType, frozen=True):
|
|
76
|
+
pass
|
|
77
|
+
|
|
78
|
+
class DateTime(_BaseComdabType, frozen=True):
|
|
79
|
+
with_timezone: bool
|
|
80
|
+
|
|
81
|
+
class Date(_BaseComdabType, frozen=True):
|
|
82
|
+
pass
|
|
83
|
+
|
|
84
|
+
class Time(_BaseComdabType, frozen=True):
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
class Interval(_BaseComdabType, frozen=True):
|
|
88
|
+
second_precision: int | None
|
|
89
|
+
day_precision: int | None
|
|
90
|
+
|
|
91
|
+
class JSON(_BaseComdabType, frozen=True):
|
|
92
|
+
pass
|
|
93
|
+
|
|
94
|
+
class Binary(_BaseComdabType, frozen=True):
|
|
95
|
+
length: int | None
|
|
96
|
+
|
|
97
|
+
class UUID(_BaseComdabType, frozen=True):
|
|
98
|
+
pass
|
|
99
|
+
|
|
100
|
+
class Array(_BaseComdabType, frozen=True):
|
|
101
|
+
item_type: "ComdabType"
|
|
102
|
+
dimensions: int
|
|
103
|
+
|
|
104
|
+
class Enum(_BaseComdabType, frozen=True):
|
|
105
|
+
values: set[str]
|
|
106
|
+
type_name: str | None
|
|
107
|
+
collation: str | None
|
|
108
|
+
|
|
109
|
+
# === PostgreSQL-specific types ===
|
|
110
|
+
|
|
111
|
+
class HSTORE(_BaseComdabType, frozen=True):
|
|
112
|
+
pass
|
|
113
|
+
|
|
114
|
+
class Range(_BaseComdabType, frozen=True):
|
|
115
|
+
item_type: "ComdabType"
|
|
116
|
+
|
|
117
|
+
class MultiRange(_BaseComdabType, frozen=True):
|
|
118
|
+
item_type: "ComdabType"
|
|
119
|
+
|
|
120
|
+
# === Unknown type ===
|
|
121
|
+
|
|
122
|
+
class Unknown(_BaseComdabType, frozen=True):
|
|
123
|
+
"""A type not handled (yet) by comdab, when run with ``allow_unknown_types=True``."""
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
type ComdabType = ( # Union of all TYPES members
|
|
127
|
+
ComdabTypes.String
|
|
128
|
+
| ComdabTypes.Integer
|
|
129
|
+
| ComdabTypes.Float
|
|
130
|
+
| ComdabTypes.Numeric
|
|
131
|
+
| ComdabTypes.Boolean
|
|
132
|
+
| ComdabTypes.DateTime
|
|
133
|
+
| ComdabTypes.Date
|
|
134
|
+
| ComdabTypes.Time
|
|
135
|
+
| ComdabTypes.Interval
|
|
136
|
+
| ComdabTypes.JSON
|
|
137
|
+
| ComdabTypes.Binary
|
|
138
|
+
| ComdabTypes.UUID
|
|
139
|
+
| ComdabTypes.Array
|
|
140
|
+
| ComdabTypes.Enum
|
|
141
|
+
| ComdabTypes.HSTORE
|
|
142
|
+
| ComdabTypes.Range
|
|
143
|
+
| ComdabTypes.MultiRange
|
|
144
|
+
| ComdabTypes.Unknown
|
|
145
|
+
)
|
|
146
|
+
ComdabType_Path = _BaseComdabType.Path
|
|
147
|
+
|
|
148
|
+
assert set(get_args(ComdabType.__value__)) == {
|
|
149
|
+
t for t in ComdabTypes.__dict__.values() if isinstance(t, type) and issubclass(t, _BaseComdabType)
|
|
150
|
+
}, "Mismatch between ComdabType and ComdabTypes members!"
|
comdab/models/view.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
|
|
5
|
+
from comdab.models.base import ComdabModel
|
|
6
|
+
from comdab.path import ComdabPath, dict_of_paths, terminal_path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ComdabView(ComdabModel, frozen=True):
|
|
10
|
+
"""A database view.
|
|
11
|
+
|
|
12
|
+
Equivalent to a :class:`sqlalchemy.Table` object.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
name: str
|
|
16
|
+
definition: str
|
|
17
|
+
materialized: bool
|
|
18
|
+
extra: dict[str, Any] = Field(default_factory=dict)
|
|
19
|
+
|
|
20
|
+
class Path(ComdabPath):
|
|
21
|
+
name = terminal_path()
|
|
22
|
+
definition = terminal_path()
|
|
23
|
+
materialized = terminal_path()
|
|
24
|
+
extra = dict_of_paths(ComdabPath)
|
comdab/path.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
from collections.abc import Iterator, Mapping
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from types import EllipsisType, GenericAlias
|
|
4
|
+
from typing import Any, Self, get_args
|
|
5
|
+
|
|
6
|
+
from comdab.exceptions import ComdabInternalError
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(frozen=True, slots=True)
|
|
10
|
+
class PathAttr:
|
|
11
|
+
"""An attribute component of a :class:`ComdabPath`, ie. to access a field of a model."""
|
|
12
|
+
|
|
13
|
+
attr: str
|
|
14
|
+
|
|
15
|
+
def __repr__(self) -> str:
|
|
16
|
+
return f".{self.attr}"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True, slots=True)
|
|
20
|
+
class PathItem:
|
|
21
|
+
"""An item component of a :class:`ComdabPath`, ie. to access a key of a model dictionary field."""
|
|
22
|
+
|
|
23
|
+
key: str
|
|
24
|
+
|
|
25
|
+
def __repr__(self) -> str:
|
|
26
|
+
return f"[{self.key!r}]"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ComdabPath[GenericType: Any = Any]:
|
|
30
|
+
"""Represent the path of a data in a ComdabModel.
|
|
31
|
+
|
|
32
|
+
Should never in instantiated manually, but build from :attr:`~comdab.path.ROOT` attributes
|
|
33
|
+
(eg. ``ROOT.tables['foo'].columns``).
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
__slots__ = ("_components", "_generic_type")
|
|
37
|
+
|
|
38
|
+
_components: list[PathAttr | PathItem]
|
|
39
|
+
_generic_type: type[GenericType] | None
|
|
40
|
+
|
|
41
|
+
def __new__(
|
|
42
|
+
cls, _components: list[PathAttr | PathItem], *, generic_type: type[GenericType] | None = None, _ok: bool = False
|
|
43
|
+
) -> Self:
|
|
44
|
+
if not _ok:
|
|
45
|
+
raise TypeError(
|
|
46
|
+
f"{cls.__qualname__} should not be instantiated!\n\n"
|
|
47
|
+
"To build a path, start from `comdab.ROOT`, eg. ``ROOT.tables['foo'].columns``."
|
|
48
|
+
)
|
|
49
|
+
inst = super().__new__(cls)
|
|
50
|
+
inst._components = _components
|
|
51
|
+
inst._generic_type = generic_type
|
|
52
|
+
return inst
|
|
53
|
+
|
|
54
|
+
def __repr__(self) -> str:
|
|
55
|
+
return f"ROOT{''.join(repr(comp) for comp in self._components)}"
|
|
56
|
+
|
|
57
|
+
def __hash__(self) -> int:
|
|
58
|
+
return hash(tuple(self._components))
|
|
59
|
+
|
|
60
|
+
def __eq__(self, other: object) -> bool:
|
|
61
|
+
if not isinstance(other, type(self)):
|
|
62
|
+
return NotImplemented
|
|
63
|
+
return self._components == other._components
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def get_path_component(path: ComdabPath, index: int) -> PathAttr | PathItem | None:
|
|
67
|
+
try:
|
|
68
|
+
return path._components[index] # pyright: ignore[reportPrivateUsage]
|
|
69
|
+
except IndexError:
|
|
70
|
+
return None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def get_path_depth(path: ComdabPath) -> int:
|
|
74
|
+
return len(path._components) # pyright: ignore[reportPrivateUsage]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class _PathDescriptor[PathType: ComdabPath]:
|
|
78
|
+
def __init__(self, path_type: type[PathType] = ComdabPath) -> None:
|
|
79
|
+
self._path_type = path_type
|
|
80
|
+
self._name: str | None = None
|
|
81
|
+
self._cache: dict[ComdabPath, PathType] = {}
|
|
82
|
+
|
|
83
|
+
def __get__(self, obj: ComdabPath, objtype: type[Any] | None = None) -> PathType:
|
|
84
|
+
if not self._name:
|
|
85
|
+
raise ComdabInternalError(f"{type(self).__name__}.__set_name__ was not called!")
|
|
86
|
+
try:
|
|
87
|
+
return self._cache[obj]
|
|
88
|
+
except KeyError:
|
|
89
|
+
res = self._cache[obj] = self._path_type(
|
|
90
|
+
[*obj._components, PathAttr(self._name)], # pyright: ignore[reportPrivateUsage]
|
|
91
|
+
generic_type=self._path_type,
|
|
92
|
+
_ok=True,
|
|
93
|
+
)
|
|
94
|
+
return res
|
|
95
|
+
|
|
96
|
+
def __set_name__(self, owner: type[Any], name: str) -> None:
|
|
97
|
+
if self._name:
|
|
98
|
+
raise ComdabInternalError(f"A same {type(self).__name__} cannot be re-used!")
|
|
99
|
+
if not issubclass(owner, ComdabPath):
|
|
100
|
+
raise ComdabInternalError(
|
|
101
|
+
f"{type(self).__name__} must only be used on {ComdabPath.__name__} instances, got {owner}!"
|
|
102
|
+
)
|
|
103
|
+
self._name = name
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def terminal_path() -> _PathDescriptor[ComdabPath]:
|
|
107
|
+
"""Use in the class body of a :class:`ComdabPath` subclass to build a path leaf, without children."""
|
|
108
|
+
return _PathDescriptor()
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def sub_path[PathType: ComdabPath](path_type: type[PathType]) -> _PathDescriptor[PathType]:
|
|
112
|
+
"""Use in the class body of a :class:`ComdabPath` subclass to build a path branch of the given path type."""
|
|
113
|
+
return _PathDescriptor(path_type)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class ComdabPathDict[PathType: ComdabPath](ComdabPath[PathType], Mapping[str | EllipsisType, PathType]):
|
|
117
|
+
"""Represent a ComdabPath referring to a dict[str, <some ComdabModel>] attribute.
|
|
118
|
+
|
|
119
|
+
Holds paths as items, and special ``.left_only`` / ``.right_only`` path attributes.
|
|
120
|
+
|
|
121
|
+
Should never in instantiated manually, but build from :attr:`~comdab.path.ROOT` attributes
|
|
122
|
+
(eg. ``ROOT.tables['foo']``).
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
__slots__ = ("_dict",)
|
|
126
|
+
|
|
127
|
+
_dict: dict[str | EllipsisType, PathType]
|
|
128
|
+
right_only = terminal_path()
|
|
129
|
+
left_only = terminal_path()
|
|
130
|
+
|
|
131
|
+
def __class_getitem__(cls, item: Any) -> GenericAlias: # TODO[PEP 747]: type item as TypeForm?
|
|
132
|
+
return GenericAlias(cls, item)
|
|
133
|
+
|
|
134
|
+
def __new__(
|
|
135
|
+
cls, _components: list[PathAttr | PathItem], *, generic_type: type["ComdabPath"], _ok: bool = False
|
|
136
|
+
) -> Self:
|
|
137
|
+
inst = super().__new__(cls, _components, generic_type=get_args(generic_type)[0], _ok=_ok)
|
|
138
|
+
inst._dict = {}
|
|
139
|
+
return inst
|
|
140
|
+
|
|
141
|
+
def __getitem__(self, key: str | EllipsisType) -> PathType:
|
|
142
|
+
if self._generic_type is None:
|
|
143
|
+
raise ComdabInternalError(f"Non-generic {type(self).__name__} cannot be subscripted!")
|
|
144
|
+
|
|
145
|
+
key = ".*" if key is ... else key
|
|
146
|
+
try:
|
|
147
|
+
return self._dict[key]
|
|
148
|
+
except KeyError:
|
|
149
|
+
res = self._dict[key] = self._generic_type([*self._components, PathItem(key)], _ok=True)
|
|
150
|
+
return res
|
|
151
|
+
|
|
152
|
+
def __iter__(self) -> Iterator[str | EllipsisType]:
|
|
153
|
+
yield from self._dict
|
|
154
|
+
|
|
155
|
+
def __len__(self) -> int:
|
|
156
|
+
return len(self._dict)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def dict_of_paths[PathType: ComdabPath](values_type: type[PathType]) -> _PathDescriptor[ComdabPathDict[PathType]]:
|
|
160
|
+
"""Use in the class body of a :class:`ComdabPath` subclass to build a path branch of a dictionary of paths."""
|
|
161
|
+
return _PathDescriptor(ComdabPathDict[values_type])
|
comdab/py.typed
ADDED
|
File without changes
|
comdab/report.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Literal
|
|
3
|
+
|
|
4
|
+
from comdab.path import ComdabPath
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass(frozen=True, slots=True)
|
|
8
|
+
class ComdabReport:
|
|
9
|
+
"""A difference between two database schemas, reported by comdab.
|
|
10
|
+
|
|
11
|
+
Should not be instantiated manually.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
level: Whether the report is for an error (default) or a warning (caused by a custom rule).
|
|
15
|
+
path: The path to the difference between the two schemas.
|
|
16
|
+
left: The value in the left schema.
|
|
17
|
+
right: The value in the right schema.
|
|
18
|
+
|
|
19
|
+
Examples:
|
|
20
|
+
* A column is nullable in the left schema, not in the right::
|
|
21
|
+
|
|
22
|
+
ComdabReport(level="error", path=ROOT.tables['foo'].columns['bar'].nullable, left=True, right=False)
|
|
23
|
+
|
|
24
|
+
* A table is present only in the left schema::
|
|
25
|
+
|
|
26
|
+
ComdabReport(level="error", path=ROOT.tables.left_only, left={'foo': ComdabTable(...)}, right={})
|
|
27
|
+
|
|
28
|
+
* Two columns are present only in the right schema::
|
|
29
|
+
|
|
30
|
+
ComdabReport(level="error", path=ROOT.tables['foo'].right_only, left={}, right={'bar': ComdabColumn(...)})
|
|
31
|
+
ComdabReport(level="error", path=ROOT.tables['foo'].right_only, left={}, right={'baz': ComdabColumn(...)})
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
level: Literal["warning", "error"]
|
|
35
|
+
path: ComdabPath
|
|
36
|
+
left: object
|
|
37
|
+
right: object
|
comdab/source.py
ADDED
|
File without changes
|
|
File without changes
|