pgdeclare 0.2.2__tar.gz → 0.3.1__tar.gz
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.
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/CHANGELOG.md +41 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/PKG-INFO +2 -2
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/README.md +1 -1
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/pyproject.toml +1 -1
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/__init__.py +1 -1
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/changes.py +16 -3
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/cli.py +4 -3
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/differ.py +255 -13
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/executor.py +28 -3
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_boundary.py +1 -1
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_comments.py +2 -1
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_diff.py +15 -11
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_expand_contract.py +352 -7
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_routines.py +14 -7
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/.gitignore +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/LICENSE +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/NOTICE +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/__main__.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/config.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/contract.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/drop_directive.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/introspection.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/ledger.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/migrate.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/models.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/prove.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/py.typed +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/quoting.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/retry_executor.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/schema_parser.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/src/pgdeclare/statement_parser.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/conftest.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/dbutil.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_cli.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_default_privileges.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_domains.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_foreign_data.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_integration.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_lifecycle.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_materialized_views.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_partitions.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_pipeline.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_prove.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_publications_rules.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_ranges_collations.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_retry_executor.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_security.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_sequences.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_statement_parser.py +0 -0
- {pgdeclare-0.2.2 → pgdeclare-0.3.1}/tests/test_views.py +0 -0
|
@@ -5,6 +5,47 @@ All notable changes to this project are documented here. The format follows
|
|
|
5
5
|
[Semantic Versioning](https://semver.org/spec/v2.0.0.html). Until 1.0 the API
|
|
6
6
|
may change in minor releases.
|
|
7
7
|
|
|
8
|
+
## [0.3.1] - 2026-11-19
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- A routine re-created because its result or its kind changes (a new
|
|
13
|
+
`RETURNS` type or `TABLE` columns, an aggregate's result, a procedure that
|
|
14
|
+
becomes a function, a window function that becomes a plain one) is now
|
|
15
|
+
offline. The drop and re-create is still one statement, but a plan the
|
|
16
|
+
serving application cached for a call to the old form fails ("cached plan
|
|
17
|
+
must not change result type"), and a call of the old kind no longer works.
|
|
18
|
+
A new body alone stays an online `CREATE OR REPLACE`.
|
|
19
|
+
|
|
20
|
+
## [0.3.0] - 2026-11-19
|
|
21
|
+
|
|
22
|
+
### Changed
|
|
23
|
+
|
|
24
|
+
- Expand/contract covers what a query reads, not only routines. Dropping an
|
|
25
|
+
undeclared view, materialized view, sequence or foreign table, and revoking
|
|
26
|
+
a privilege on a table, view, column, sequence or schema, is now a
|
|
27
|
+
contract change: `apply --expand-only` holds it back, so the version still
|
|
28
|
+
serving keeps reading the relation, calling `nextval()` and running with
|
|
29
|
+
its privileges until the run after it has stopped.
|
|
30
|
+
- A relation drop that cannot wait goes in the same run, offline: when the
|
|
31
|
+
schema files declare another relation under its name, when a view it reads
|
|
32
|
+
from is recreated in the run, or when its query names what the run drops,
|
|
33
|
+
recreates or retypes (and what depends on such a drop goes with it). A
|
|
34
|
+
routine and the view whose row type it returns now wait together instead of
|
|
35
|
+
both going offline.
|
|
36
|
+
- A table-level revoke grants the declared column-level grants of the same
|
|
37
|
+
privilege back in the same statement (`RevokePrivilege.regrant_columns`), and
|
|
38
|
+
those grants leave the plan as separate changes. The contract run's revoke
|
|
39
|
+
would otherwise strip the column grants the expand run wrote, and the run
|
|
40
|
+
after a table-wide grant narrowed to some columns no longer depends on the
|
|
41
|
+
revoke running first.
|
|
42
|
+
|
|
43
|
+
### Fixed
|
|
44
|
+
|
|
45
|
+
- A relation declared under the name of an undeclared relation of another
|
|
46
|
+
kind (a view becoming a table) no longer fails `relation already exists`:
|
|
47
|
+
the create is retried once the old relation is dropped.
|
|
48
|
+
|
|
8
49
|
## [0.2.2] - 2026-11-19
|
|
9
50
|
|
|
10
51
|
### Changed
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: pgdeclare
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
Summary: Declarative Postgres schemas: diff the live database against your SQL files, with before/after migration blocks and a hash ledger.
|
|
5
5
|
Project-URL: Homepage, https://github.com/powertime-org/pgdeclare
|
|
6
6
|
Project-URL: Source, https://github.com/powertime-org/pgdeclare
|
|
@@ -47,7 +47,7 @@ A diff cannot infer everything, such as a rename or a backfill. For those you wr
|
|
|
47
47
|
|
|
48
48
|

|
|
49
49
|
|
|
50
|
-
Status: **0.
|
|
50
|
+
Status: **0.3.0.** The engine has run the production schema of [Powertime](https://powertime.org) since it went into service on 10 December 2025. Backwards compatibility may not be preserved until 1.0.
|
|
51
51
|
|
|
52
52
|
## Design choices
|
|
53
53
|
|
|
@@ -14,7 +14,7 @@ A diff cannot infer everything, such as a rename or a backfill. For those you wr
|
|
|
14
14
|
|
|
15
15
|

|
|
16
16
|
|
|
17
|
-
Status: **0.
|
|
17
|
+
Status: **0.3.0.** The engine has run the production schema of [Powertime](https://powertime.org) since it went into service on 10 December 2025. Backwards compatibility may not be preserved until 1.0.
|
|
18
18
|
|
|
19
19
|
## Design choices
|
|
20
20
|
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pgdeclare"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.3.1"
|
|
8
8
|
description = "Declarative Postgres schemas: diff the live database against your SQL files, with before/after migration blocks and a hash ledger."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
@@ -88,8 +88,10 @@ class Change(ABC):
|
|
|
88
88
|
object_type: str = ""
|
|
89
89
|
object_name: str = ""
|
|
90
90
|
# The contract half of an expand/contract deploy: a change the previous
|
|
91
|
-
# version of the application may still depend on while it serves
|
|
92
|
-
# dropping a routine it may still call
|
|
91
|
+
# version of the application may still depend on while it serves:
|
|
92
|
+
# dropping a routine it may still call, or a view, materialized view,
|
|
93
|
+
# sequence or foreign table it may still read, or revoking a privilege on
|
|
94
|
+
# one of those, a table, column or schema. A run with `expand_only` holds
|
|
93
95
|
# it back; the run after the old version has stopped applies it.
|
|
94
96
|
contract: bool = False
|
|
95
97
|
|
|
@@ -2621,6 +2623,11 @@ class RevokePrivilege(Change):
|
|
|
2621
2623
|
description: str = ""
|
|
2622
2624
|
object_type: str = "REVOKE"
|
|
2623
2625
|
|
|
2626
|
+
# A table-level revoke strips the column-level grants of the same
|
|
2627
|
+
# privilege and grantee; the declared ones are granted again in the same
|
|
2628
|
+
# statement, so no run, held back or not, can leave them stripped.
|
|
2629
|
+
regrant_columns: list[Grant] = field(default_factory=list)
|
|
2630
|
+
|
|
2624
2631
|
def __post_init__(self):
|
|
2625
2632
|
self.object_name = self.grant.key
|
|
2626
2633
|
if not self.description:
|
|
@@ -2652,7 +2659,13 @@ class RevokePrivilege(Change):
|
|
|
2652
2659
|
f"BEGIN {revoke_sql}; EXCEPTION WHEN undefined_table THEN NULL; END"
|
|
2653
2660
|
)
|
|
2654
2661
|
else:
|
|
2655
|
-
|
|
2662
|
+
revoke_sql = f"REVOKE {self.grant.privilege} ON {quote_ident(self.grant.schema)}.{quote_ident(self.grant.object_name)} FROM {role_ident(self.grant.grantee)}"
|
|
2663
|
+
if not self.regrant_columns:
|
|
2664
|
+
return f"{revoke_sql};"
|
|
2665
|
+
regrants = " ".join(
|
|
2666
|
+
GrantPrivilege(grant=g).to_sql() for g in self.regrant_columns
|
|
2667
|
+
)
|
|
2668
|
+
return do_block(f"BEGIN {revoke_sql}; {regrants} END")
|
|
2656
2669
|
|
|
2657
2670
|
|
|
2658
2671
|
@dataclass
|
|
@@ -464,9 +464,10 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
464
464
|
apply.add_argument(
|
|
465
465
|
"--expand-only",
|
|
466
466
|
action="store_true",
|
|
467
|
-
help="the expand half of an expand/contract deploy: keep the
|
|
468
|
-
"
|
|
469
|
-
"signature
|
|
467
|
+
help="the expand half of an expand/contract deploy: keep what the "
|
|
468
|
+
"running application may still use (routines dropped or replaced by a "
|
|
469
|
+
"new signature, views, materialized views, sequences and foreign tables "
|
|
470
|
+
"dropped, privileges revoked) for a later run without this flag",
|
|
470
471
|
)
|
|
471
472
|
apply.add_argument(
|
|
472
473
|
"--prove",
|
|
@@ -6,6 +6,7 @@ a list of Change objects needed to synchronize them.
|
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
from dataclasses import replace as dataclass_replace
|
|
9
|
+
import re
|
|
9
10
|
from typing import Any, Callable, Optional, Union
|
|
10
11
|
|
|
11
12
|
from .changes import (
|
|
@@ -277,7 +278,14 @@ def compute_diff(
|
|
|
277
278
|
changes.extend(default_privilege_grants)
|
|
278
279
|
changes = _skip_revokes_on_dropped_relations(changes)
|
|
279
280
|
changes = _skip_canonical_function_drops(changes, actual)
|
|
280
|
-
|
|
281
|
+
_hold_relation_drops(changes)
|
|
282
|
+
changes = _hold_relation_revokes(changes, desired)
|
|
283
|
+
# Whether a relation drop can wait depends on which routine drops go now,
|
|
284
|
+
# and the other way round, so settle both together.
|
|
285
|
+
while _drop_now_what_relations_need(
|
|
286
|
+
changes, desired, actual
|
|
287
|
+
) | _drop_now_what_other_drops_need(changes, actual):
|
|
288
|
+
pass
|
|
281
289
|
_hold_revokes_with_their_routine(changes)
|
|
282
290
|
|
|
283
291
|
# Re-apply grants for objects being dropped and recreated.
|
|
@@ -1429,10 +1437,28 @@ def _diff_functions(desired: Schema, actual: Schema) -> list[Change]:
|
|
|
1429
1437
|
func.kind != actual_func.kind
|
|
1430
1438
|
or func.get_return_clause() != actual_func.get_return_clause()
|
|
1431
1439
|
):
|
|
1432
|
-
#
|
|
1433
|
-
|
|
1434
|
-
|
|
1440
|
+
# One statement, so nothing ever finds it missing, but a
|
|
1441
|
+
# caller of the old form reads a different answer: a cached
|
|
1442
|
+
# plan for it fails ("cached plan must not change result
|
|
1443
|
+
# type") and a procedure is not called like a function. The
|
|
1444
|
+
# application still serving must be stopped: offline.
|
|
1445
|
+
what = (
|
|
1446
|
+
f"its kind changes from {actual_func.keyword.lower()} "
|
|
1447
|
+
f"to {func.keyword.lower()}"
|
|
1448
|
+
if func.kind != actual_func.kind
|
|
1449
|
+
else "its result changes, and a cached plan for a call "
|
|
1450
|
+
"to the old form fails"
|
|
1451
|
+
)
|
|
1452
|
+
change = DropAndCreateFunction(
|
|
1453
|
+
function=func,
|
|
1454
|
+
old_kind=actual_func.kind,
|
|
1455
|
+
category=ChangeCategory.OFFLINE,
|
|
1435
1456
|
)
|
|
1457
|
+
change.description = (
|
|
1458
|
+
f"Drop and re-create {func.keyword.lower()} "
|
|
1459
|
+
f"{func.signature} (offline: {what})"
|
|
1460
|
+
)
|
|
1461
|
+
changes.append(change)
|
|
1436
1462
|
else:
|
|
1437
1463
|
changes.append(ReplaceFunction(function=func))
|
|
1438
1464
|
|
|
@@ -2020,27 +2046,31 @@ def _skip_canonical_function_drops(
|
|
|
2020
2046
|
]
|
|
2021
2047
|
|
|
2022
2048
|
|
|
2023
|
-
def _drop_now_what_other_drops_need(changes: list[Change], actual: Schema) ->
|
|
2049
|
+
def _drop_now_what_other_drops_need(changes: list[Change], actual: Schema) -> bool:
|
|
2024
2050
|
"""Make a routine drop that another drop needs a drop of this run.
|
|
2025
2051
|
|
|
2026
2052
|
A routine depends on the types its signature names: a dropped view's or
|
|
2027
2053
|
domain's or range's (these drops run without CASCADE). If an expand run
|
|
2028
|
-
held the routine back
|
|
2029
|
-
goes in the same run, and since the application that
|
|
2030
|
-
call it, that run is offline. A routine whose
|
|
2031
|
-
mention the object's name cannot depend on it;
|
|
2032
|
-
test errs towards offline.
|
|
2054
|
+
held the routine back while the other drop went now, that drop would fail
|
|
2055
|
+
on it. So the routine goes in the same run, and since the application that
|
|
2056
|
+
is serving may still call it, that run is offline. A routine whose
|
|
2057
|
+
definition does not even mention the object's name cannot depend on it;
|
|
2058
|
+
one that does may, so the test errs towards offline. A view held back as a
|
|
2059
|
+
contract change waits with the routine, so it forces nothing.
|
|
2060
|
+
|
|
2061
|
+
Returns whether it moved a routine drop to this run.
|
|
2033
2062
|
"""
|
|
2034
2063
|
names: set[str] = set()
|
|
2035
2064
|
for c in changes:
|
|
2036
|
-
if isinstance(c, DropView):
|
|
2065
|
+
if isinstance(c, DropView) and not c.contract:
|
|
2037
2066
|
names.add(c.view_name.lower())
|
|
2038
|
-
elif isinstance(c, DropMaterializedView):
|
|
2067
|
+
elif isinstance(c, DropMaterializedView) and not c.contract:
|
|
2039
2068
|
names.add(c.matview_name.lower())
|
|
2040
2069
|
elif isinstance(c, (DropDomain, DropRangeType)):
|
|
2041
2070
|
names.add(c.name.lower())
|
|
2042
2071
|
if not names:
|
|
2043
|
-
return
|
|
2072
|
+
return False
|
|
2073
|
+
moved = False
|
|
2044
2074
|
for c in changes:
|
|
2045
2075
|
if not (isinstance(c, DropFunction) and c.contract):
|
|
2046
2076
|
continue
|
|
@@ -2054,6 +2084,218 @@ def _drop_now_what_other_drops_need(changes: list[Change], actual: Schema) -> No
|
|
|
2054
2084
|
f" (offline: dropped with {', '.join(needed)}, which it may "
|
|
2055
2085
|
f"depend on, while the application may still call it)"
|
|
2056
2086
|
)
|
|
2087
|
+
moved = True
|
|
2088
|
+
return moved
|
|
2089
|
+
|
|
2090
|
+
|
|
2091
|
+
# Drops of relations a query names. The application that is serving may still
|
|
2092
|
+
# read them, so an expand run holds them back for the run after.
|
|
2093
|
+
_RelationDrop = Union[DropView, DropMaterializedView, DropSequence, DropForeignTable]
|
|
2094
|
+
|
|
2095
|
+
|
|
2096
|
+
def _relation_drop_name(c: _RelationDrop) -> str:
|
|
2097
|
+
if isinstance(c, DropView):
|
|
2098
|
+
return c.view_name
|
|
2099
|
+
if isinstance(c, DropMaterializedView):
|
|
2100
|
+
return c.matview_name
|
|
2101
|
+
if isinstance(c, DropSequence):
|
|
2102
|
+
return c.sequence_name
|
|
2103
|
+
return c.table_name
|
|
2104
|
+
|
|
2105
|
+
|
|
2106
|
+
def _hold_relation_drops(changes: list[Change]) -> None:
|
|
2107
|
+
"""A view, materialized view, sequence or foreign table no longer declared
|
|
2108
|
+
is dropped as a contract change.
|
|
2109
|
+
|
|
2110
|
+
The application that is serving may still read the relation or call
|
|
2111
|
+
nextval() on the sequence; dropping it breaks every such query until the
|
|
2112
|
+
new version takes over. `_drop_now_what_relations_need` then moves back to
|
|
2113
|
+
this run, offline, each drop that cannot wait.
|
|
2114
|
+
"""
|
|
2115
|
+
for c in changes:
|
|
2116
|
+
if (
|
|
2117
|
+
isinstance(
|
|
2118
|
+
c, (DropView, DropMaterializedView, DropSequence, DropForeignTable)
|
|
2119
|
+
)
|
|
2120
|
+
and c.category == ChangeCategory.ONLINE
|
|
2121
|
+
):
|
|
2122
|
+
c.contract = True
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
def _mentions(text: str, name: str) -> bool:
|
|
2126
|
+
return (
|
|
2127
|
+
re.search(rf"(?<![\w$]){re.escape(name)}(?![\w$])", text, re.IGNORECASE)
|
|
2128
|
+
is not None
|
|
2129
|
+
)
|
|
2130
|
+
|
|
2131
|
+
|
|
2132
|
+
def _names_taken_now(changes: list[Change]) -> set[str]:
|
|
2133
|
+
"""Names of what this run drops, recreates or retypes, as a query names them.
|
|
2134
|
+
|
|
2135
|
+
A view reading one of them holds a dependency that makes the change fail
|
|
2136
|
+
(DROP and ALTER COLUMN TYPE refuse while a view uses the object) or, for a
|
|
2137
|
+
routine re-created with CASCADE, takes the view along.
|
|
2138
|
+
"""
|
|
2139
|
+
names: set[str] = set()
|
|
2140
|
+
for c in changes:
|
|
2141
|
+
if c.contract:
|
|
2142
|
+
continue
|
|
2143
|
+
if isinstance(c, (DropView, DropMaterializedView, DropForeignTable)):
|
|
2144
|
+
names.add(_relation_drop_name(c))
|
|
2145
|
+
elif isinstance(c, DropTable):
|
|
2146
|
+
names.add(c.table_name)
|
|
2147
|
+
elif isinstance(c, (DropColumn, AlterColumnType)):
|
|
2148
|
+
names.add(c.table)
|
|
2149
|
+
elif isinstance(c, (DropFunction, DropDomain, DropRangeType)):
|
|
2150
|
+
names.add(c.name)
|
|
2151
|
+
elif isinstance(c, DropAndCreateFunction):
|
|
2152
|
+
names.add(c.function.name)
|
|
2153
|
+
elif isinstance(c, DropAndCreateDomain):
|
|
2154
|
+
names.add(c.domain.name)
|
|
2155
|
+
elif isinstance(c, DropAndCreateRangeType):
|
|
2156
|
+
names.add(c.range_type.name)
|
|
2157
|
+
elif isinstance(c, DropAndCreateForeignTable):
|
|
2158
|
+
names.add(c.table.name)
|
|
2159
|
+
return {n.lower() for n in names}
|
|
2160
|
+
|
|
2161
|
+
|
|
2162
|
+
def _declared_relation_names(desired: Schema) -> set[str]:
|
|
2163
|
+
"""`schema.name` of every declared object that lives in pg_class."""
|
|
2164
|
+
found: set[str] = set()
|
|
2165
|
+
for objects in (
|
|
2166
|
+
desired.tables,
|
|
2167
|
+
desired.views,
|
|
2168
|
+
desired.materialized_views,
|
|
2169
|
+
desired.sequences,
|
|
2170
|
+
desired.foreign_tables,
|
|
2171
|
+
desired.composite_types,
|
|
2172
|
+
desired.indexes,
|
|
2173
|
+
):
|
|
2174
|
+
for obj in objects.values():
|
|
2175
|
+
found.add(f"{obj.schema}.{obj.name}")
|
|
2176
|
+
return found
|
|
2177
|
+
|
|
2178
|
+
|
|
2179
|
+
def _drop_now_what_relations_need(
|
|
2180
|
+
changes: list[Change], desired: Schema, actual: Schema
|
|
2181
|
+
) -> bool:
|
|
2182
|
+
"""Move a held-back relation drop to this run, offline, when it cannot wait.
|
|
2183
|
+
|
|
2184
|
+
It cannot wait when:
|
|
2185
|
+
|
|
2186
|
+
- the schema files declare another relation under its name, which this
|
|
2187
|
+
run creates and which would collide with it;
|
|
2188
|
+
- this run drops and recreates a view or materialized view it reads from,
|
|
2189
|
+
directly or not: the recreate drops it on the way;
|
|
2190
|
+
- it is a view or materialized view whose query names what this run
|
|
2191
|
+
drops, recreates or retypes (or what another drop moved to this run
|
|
2192
|
+
reads from): that change fails on it, or takes it along. Like the
|
|
2193
|
+
routine rule, a name in the query errs towards offline.
|
|
2194
|
+
|
|
2195
|
+
Each one then goes now, and the serving application may still read it,
|
|
2196
|
+
so the run is offline. Returns whether it moved any.
|
|
2197
|
+
"""
|
|
2198
|
+
held = [
|
|
2199
|
+
c
|
|
2200
|
+
for c in changes
|
|
2201
|
+
if isinstance(
|
|
2202
|
+
c, (DropView, DropMaterializedView, DropSequence, DropForeignTable)
|
|
2203
|
+
)
|
|
2204
|
+
and c.contract
|
|
2205
|
+
]
|
|
2206
|
+
if not held:
|
|
2207
|
+
return False
|
|
2208
|
+
declared = _declared_relation_names(desired)
|
|
2209
|
+
taken = _names_taken_now(changes)
|
|
2210
|
+
recreated_dependents: set[str] = set()
|
|
2211
|
+
for c in changes:
|
|
2212
|
+
if isinstance(c, DropAndCreateView):
|
|
2213
|
+
root = c.view.qualified_name
|
|
2214
|
+
elif isinstance(c, DropAndCreateMaterializedView):
|
|
2215
|
+
root = c.matview.qualified_name
|
|
2216
|
+
else:
|
|
2217
|
+
continue
|
|
2218
|
+
recreated_dependents.update(
|
|
2219
|
+
_relation_dependent_order(root, actual.relation_dependents)
|
|
2220
|
+
)
|
|
2221
|
+
|
|
2222
|
+
moved = False
|
|
2223
|
+
for c in held:
|
|
2224
|
+
name = _relation_drop_name(c)
|
|
2225
|
+
qualified = f"{c.schema}.{name}"
|
|
2226
|
+
reason = ""
|
|
2227
|
+
if qualified in declared:
|
|
2228
|
+
reason = f"{qualified} is declared again as another kind of relation"
|
|
2229
|
+
elif qualified in recreated_dependents:
|
|
2230
|
+
reason = "a view it reads from is recreated in this run"
|
|
2231
|
+
elif isinstance(c, (DropView, DropMaterializedView)):
|
|
2232
|
+
relation = actual.views.get(qualified) or actual.materialized_views.get(
|
|
2233
|
+
qualified
|
|
2234
|
+
)
|
|
2235
|
+
definition = relation.definition if relation is not None else ""
|
|
2236
|
+
needed = sorted(n for n in taken if _mentions(definition, n))
|
|
2237
|
+
if needed:
|
|
2238
|
+
reason = (
|
|
2239
|
+
f"it reads {', '.join(needed)}, which this run drops or changes"
|
|
2240
|
+
)
|
|
2241
|
+
if reason:
|
|
2242
|
+
c.contract = False
|
|
2243
|
+
c.category = ChangeCategory.OFFLINE
|
|
2244
|
+
c.description += (
|
|
2245
|
+
f" (offline: {reason}, while the application may still read it)"
|
|
2246
|
+
)
|
|
2247
|
+
moved = True
|
|
2248
|
+
return moved
|
|
2249
|
+
|
|
2250
|
+
|
|
2251
|
+
# Grants on these are what the serving application's queries run on.
|
|
2252
|
+
_HELD_REVOKE_OBJECTS = frozenset({"TABLE", "COLUMN", "SEQUENCE", "SCHEMA"})
|
|
2253
|
+
|
|
2254
|
+
|
|
2255
|
+
def _hold_relation_revokes(changes: list[Change], desired: Schema) -> list[Change]:
|
|
2256
|
+
"""A revoke on a table, view, column, sequence or schema is a contract change.
|
|
2257
|
+
|
|
2258
|
+
The serving application's queries fail without the privilege exactly as
|
|
2259
|
+
they would without the object. A table-level REVOKE also strips every
|
|
2260
|
+
column-level grant of that privilege on the table, so the revoke grants
|
|
2261
|
+
the declared ones again in the same statement, and their own GRANTs leave
|
|
2262
|
+
the plan: while the table-wide grant stands they add nothing (and the
|
|
2263
|
+
catalog reports them as part of it), and once it goes they come back with
|
|
2264
|
+
the revoke, in whichever run that is.
|
|
2265
|
+
"""
|
|
2266
|
+
folded: set[str] = set()
|
|
2267
|
+
for c in changes:
|
|
2268
|
+
if (
|
|
2269
|
+
isinstance(c, RevokePrivilege)
|
|
2270
|
+
and c.grant.object_type in _HELD_REVOKE_OBJECTS
|
|
2271
|
+
):
|
|
2272
|
+
c.contract = True
|
|
2273
|
+
if c.grant.object_type == "TABLE" and not c.grant.column:
|
|
2274
|
+
c.regrant_columns = sorted(
|
|
2275
|
+
(
|
|
2276
|
+
g
|
|
2277
|
+
for g in desired.grants
|
|
2278
|
+
if g.column
|
|
2279
|
+
and g.schema == c.grant.schema
|
|
2280
|
+
and g.object_name == c.grant.object_name
|
|
2281
|
+
and g.privilege == c.grant.privilege
|
|
2282
|
+
and g.grantee == c.grant.grantee
|
|
2283
|
+
),
|
|
2284
|
+
key=lambda g: g.key,
|
|
2285
|
+
)
|
|
2286
|
+
folded.update(g.key for g in c.regrant_columns)
|
|
2287
|
+
if c.regrant_columns:
|
|
2288
|
+
c.description += (
|
|
2289
|
+
f" (re-granting it on {len(c.regrant_columns)} "
|
|
2290
|
+
f"declared column(s), which the revoke strips)"
|
|
2291
|
+
)
|
|
2292
|
+
if not folded:
|
|
2293
|
+
return changes
|
|
2294
|
+
return [
|
|
2295
|
+
c
|
|
2296
|
+
for c in changes
|
|
2297
|
+
if not (isinstance(c, GrantPrivilege) and c.grant.key in folded)
|
|
2298
|
+
]
|
|
2057
2299
|
|
|
2058
2300
|
|
|
2059
2301
|
def _hold_revokes_with_their_routine(changes: list[Change]) -> None:
|
|
@@ -27,9 +27,14 @@ from .changes import (
|
|
|
27
27
|
AlterOwner,
|
|
28
28
|
Change,
|
|
29
29
|
CreateEnum,
|
|
30
|
+
CreateForeignTable,
|
|
30
31
|
CreateIndex,
|
|
32
|
+
CreateMaterializedView,
|
|
31
33
|
CreatePartitionedIndex,
|
|
32
34
|
CreateRole,
|
|
35
|
+
CreateSequence,
|
|
36
|
+
CreateTable,
|
|
37
|
+
CreateView,
|
|
33
38
|
SetComment,
|
|
34
39
|
DropCollation,
|
|
35
40
|
DropDomain,
|
|
@@ -72,6 +77,19 @@ RETRY_WHILE_DEPENDED_ON = (
|
|
|
72
77
|
DropForeignTable,
|
|
73
78
|
)
|
|
74
79
|
|
|
80
|
+
# duplicate_table. Retryable for the creates of a relation: a relation of
|
|
81
|
+
# another kind that held the name (a view declared again as a table, say) is
|
|
82
|
+
# dropped by another change in the same run, which the differ orders after the
|
|
83
|
+
# create, and the create succeeds once it has.
|
|
84
|
+
DUPLICATE_RELATION_ERROR_CODE = "42P07"
|
|
85
|
+
RETRY_WHILE_NAME_TAKEN = (
|
|
86
|
+
CreateTable,
|
|
87
|
+
CreateView,
|
|
88
|
+
CreateMaterializedView,
|
|
89
|
+
CreateSequence,
|
|
90
|
+
CreateForeignTable,
|
|
91
|
+
)
|
|
92
|
+
|
|
75
93
|
# Changes run in the final phase, after the DDL and index phases: comments, and
|
|
76
94
|
# owners (a recreate in the DDL phase resets an object's owner).
|
|
77
95
|
FINAL_PHASE_CHANGE_TYPES = (SetComment, AlterOwner)
|
|
@@ -414,9 +432,16 @@ def _execute_with_retry_savepoints(
|
|
|
414
432
|
continue
|
|
415
433
|
|
|
416
434
|
# Check if this is a dependency error (retryable)
|
|
417
|
-
if
|
|
418
|
-
error_code
|
|
419
|
-
|
|
435
|
+
if (
|
|
436
|
+
error_code in DEPENDENCY_ERROR_CODES
|
|
437
|
+
or (
|
|
438
|
+
error_code == DEPENDENT_OBJECTS_ERROR_CODE
|
|
439
|
+
and isinstance(change, RETRY_WHILE_DEPENDED_ON)
|
|
440
|
+
)
|
|
441
|
+
or (
|
|
442
|
+
error_code == DUPLICATE_RELATION_ERROR_CODE
|
|
443
|
+
and isinstance(change, RETRY_WHILE_NAME_TAKEN)
|
|
444
|
+
)
|
|
420
445
|
):
|
|
421
446
|
# Defer for retry
|
|
422
447
|
deferred.append(
|
|
@@ -260,7 +260,8 @@ def _apply(sql_dir: Path, target_url: str, plan_db: str, content: str) -> None:
|
|
|
260
260
|
"""Apply `content`, then prove a fresh diff against it is empty."""
|
|
261
261
|
config = PgDeclareConfig(report=lambda _line: None)
|
|
262
262
|
_write(sql_dir, content)
|
|
263
|
-
|
|
263
|
+
# Recreating what a comment is on can be offline (a new result type).
|
|
264
|
+
apply_declared_schema(target_url, sql_dir, plan_db, offline=True, config=config)
|
|
264
265
|
desired = build_desired_schema(sql_dir, target_url, plan_db, config=config)
|
|
265
266
|
actual = introspect_schema(target_url, config=config)
|
|
266
267
|
assert compute_diff(desired, actual, schemas=["public"]) == []
|
|
@@ -1933,13 +1933,13 @@ class TestGrantChanges:
|
|
|
1933
1933
|
assert "REVOKE DELETE" in sql
|
|
1934
1934
|
assert "FROM app_user" in sql
|
|
1935
1935
|
|
|
1936
|
-
def
|
|
1937
|
-
"""Table-wide INSERT narrowed to per-column INSERT
|
|
1936
|
+
def test_table_revoke_grants_the_columns_back_in_one_statement(self):
|
|
1937
|
+
"""Table-wide INSERT narrowed to per-column INSERT is one statement.
|
|
1938
1938
|
|
|
1939
1939
|
PostgreSQL's table-level REVOKE also removes the column-level grants of
|
|
1940
|
-
the same privilege, so
|
|
1941
|
-
|
|
1942
|
-
|
|
1940
|
+
the same privilege, so a column GRANT run before it would be wiped and
|
|
1941
|
+
the run would never converge. The revoke therefore grants the declared
|
|
1942
|
+
columns again itself, after revoking, whichever run applies it.
|
|
1943
1943
|
"""
|
|
1944
1944
|
|
|
1945
1945
|
def g(column=None):
|
|
@@ -1961,13 +1961,17 @@ class TestGrantChanges:
|
|
|
1961
1961
|
if isinstance(c, (GrantPrivilege, RevokePrivilege))
|
|
1962
1962
|
]
|
|
1963
1963
|
|
|
1964
|
-
assert [type(c) for c in changes] == [
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1964
|
+
assert [type(c) for c in changes] == [RevokePrivilege]
|
|
1965
|
+
revoke = changes[0]
|
|
1966
|
+
assert isinstance(revoke, RevokePrivilege)
|
|
1967
|
+
assert revoke.grant.column is None
|
|
1968
|
+
assert [r.column for r in revoke.regrant_columns] == [
|
|
1969
|
+
"as_owner",
|
|
1970
|
+
"note",
|
|
1971
|
+
"team_id",
|
|
1969
1972
|
]
|
|
1970
|
-
|
|
1973
|
+
sql = revoke.to_sql()
|
|
1974
|
+
assert sql.index("REVOKE INSERT") < sql.index('GRANT INSERT ("as_owner")')
|
|
1971
1975
|
|
|
1972
1976
|
|
|
1973
1977
|
class TestFunctionPublicExecute:
|
|
@@ -27,15 +27,34 @@ from pgdeclare import (
|
|
|
27
27
|
compute_diff,
|
|
28
28
|
)
|
|
29
29
|
from pgdeclare.changes import (
|
|
30
|
+
AlterColumnType,
|
|
30
31
|
CreateFunction,
|
|
32
|
+
ReplaceFunction,
|
|
31
33
|
DropAndCreateFunction,
|
|
34
|
+
DropAndCreateView,
|
|
35
|
+
DropDomain,
|
|
36
|
+
DropForeignTable,
|
|
32
37
|
DropFunction,
|
|
38
|
+
DropMaterializedView,
|
|
39
|
+
DropSequence,
|
|
33
40
|
DropView,
|
|
34
41
|
RevokePrivilege,
|
|
35
42
|
)
|
|
36
43
|
from pgdeclare.ledger import MigrationBlock
|
|
37
44
|
from pgdeclare.migrate import is_held_back
|
|
38
|
-
from pgdeclare.models import
|
|
45
|
+
from pgdeclare.models import (
|
|
46
|
+
ChangeCategory,
|
|
47
|
+
Column,
|
|
48
|
+
Domain,
|
|
49
|
+
ForeignTable,
|
|
50
|
+
Function,
|
|
51
|
+
Grant,
|
|
52
|
+
MaterializedView,
|
|
53
|
+
Schema,
|
|
54
|
+
Sequence,
|
|
55
|
+
Table,
|
|
56
|
+
View,
|
|
57
|
+
)
|
|
39
58
|
|
|
40
59
|
|
|
41
60
|
def _fn(
|
|
@@ -156,6 +175,28 @@ class TestSignatureChanges:
|
|
|
156
175
|
assert changes[0].category == ChangeCategory.OFFLINE
|
|
157
176
|
|
|
158
177
|
|
|
178
|
+
class TestResultChanges:
|
|
179
|
+
def test_a_new_result_is_offline(self):
|
|
180
|
+
# Same identity, so one statement, but a plan the serving version
|
|
181
|
+
# cached for the old result fails: "cached plan must not change
|
|
182
|
+
# result type".
|
|
183
|
+
old = _fn("f", "a int", "integer", 1, 1, body="RETURNS TABLE (x int) AS '1'")
|
|
184
|
+
new = _fn(
|
|
185
|
+
"f", "a int", "integer", 1, 1, body="RETURNS TABLE (x int, y int) AS '1'"
|
|
186
|
+
)
|
|
187
|
+
changes = compute_diff(_schema(new), _schema(old))
|
|
188
|
+
assert [type(c) for c in changes] == [DropAndCreateFunction]
|
|
189
|
+
assert changes[0].category == ChangeCategory.OFFLINE
|
|
190
|
+
assert "result changes" in changes[0].description
|
|
191
|
+
|
|
192
|
+
def test_a_new_body_stays_online(self):
|
|
193
|
+
old = _fn("f", "a int", "integer", 1, 1, body="RETURNS int AS '1'")
|
|
194
|
+
new = _fn("f", "a int", "integer", 1, 1, body="RETURNS int AS '2'")
|
|
195
|
+
changes = compute_diff(_schema(new), _schema(old))
|
|
196
|
+
assert [type(c) for c in changes] == [ReplaceFunction]
|
|
197
|
+
assert changes[0].category == ChangeCategory.ONLINE
|
|
198
|
+
|
|
199
|
+
|
|
159
200
|
class TestDroppedRoutines:
|
|
160
201
|
def test_an_undeclared_routine_is_a_contract_drop(self):
|
|
161
202
|
gone = _fn("gone", "a int", "integer", 1, 1)
|
|
@@ -172,19 +213,196 @@ class TestDroppedRoutines:
|
|
|
172
213
|
assert len(revokes) == 1 and is_held_back(revokes[0])
|
|
173
214
|
|
|
174
215
|
def test_a_routine_another_drop_needs_goes_now_and_offline(self):
|
|
175
|
-
# DROP
|
|
176
|
-
#
|
|
216
|
+
# DROP DOMAIN runs without CASCADE, so it would fail on a function
|
|
217
|
+
# taking the domain that an expand run had held back.
|
|
218
|
+
domain = Domain(schema="public", name="cents", base_type="integer")
|
|
219
|
+
gone = _fn("gone", "a cents", "cents", 1, 1)
|
|
220
|
+
kept = _fn("kept", "", "", 0, 0)
|
|
221
|
+
changes = compute_diff(
|
|
222
|
+
_schema(kept), _schema(gone, kept, domains={"public.cents": domain})
|
|
223
|
+
)
|
|
224
|
+
assert any(isinstance(c, DropDomain) for c in changes)
|
|
225
|
+
drop = next(c for c in changes if isinstance(c, DropFunction))
|
|
226
|
+
assert drop.category == ChangeCategory.OFFLINE
|
|
227
|
+
assert not drop.contract
|
|
228
|
+
assert "cents" in drop.description
|
|
229
|
+
|
|
230
|
+
def test_a_routine_and_the_view_it_returns_wait_together(self):
|
|
177
231
|
view = View(schema="public", name="report", definition="SELECT 1 AS x")
|
|
178
232
|
gone = _fn("gone", "", "", 0, 0, body="RETURNS SETOF report")
|
|
179
233
|
kept = _fn("kept", "", "", 0, 0)
|
|
180
234
|
changes = compute_diff(
|
|
181
235
|
_schema(kept), _schema(gone, kept, views={"public.report": view})
|
|
182
236
|
)
|
|
183
|
-
|
|
184
|
-
|
|
237
|
+
drops = [c for c in changes if isinstance(c, (DropView, DropFunction))]
|
|
238
|
+
assert len(drops) == 2 and all(is_held_back(c) for c in drops)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _view(name: str, definition: str) -> View:
|
|
242
|
+
return View(schema="public", name=name, definition=definition)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _table(name: str, **columns: str) -> Table:
|
|
246
|
+
return Table(
|
|
247
|
+
schema="public",
|
|
248
|
+
name=name,
|
|
249
|
+
columns={c: Column(c, t, True) for c, t in columns.items()},
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
class TestDroppedRelations:
|
|
254
|
+
"""Views, materialized views, sequences and foreign tables a query names."""
|
|
255
|
+
|
|
256
|
+
def test_undeclared_relations_are_contract_drops(self):
|
|
257
|
+
actual = Schema(
|
|
258
|
+
views={"public.v": _view("v", "SELECT 1 AS x")},
|
|
259
|
+
materialized_views={
|
|
260
|
+
"public.m": MaterializedView("public", "m", "SELECT 1 AS x")
|
|
261
|
+
},
|
|
262
|
+
sequences={"public.s": Sequence("public", "s")},
|
|
263
|
+
foreign_tables={
|
|
264
|
+
"public.f": ForeignTable("public", "f", server="remote", columns={})
|
|
265
|
+
},
|
|
266
|
+
)
|
|
267
|
+
changes = compute_diff(Schema(), actual)
|
|
268
|
+
drops = {
|
|
269
|
+
type(c): c
|
|
270
|
+
for c in changes
|
|
271
|
+
if isinstance(
|
|
272
|
+
c, (DropView, DropMaterializedView, DropSequence, DropForeignTable)
|
|
273
|
+
)
|
|
274
|
+
}
|
|
275
|
+
assert set(drops) == {
|
|
276
|
+
DropView,
|
|
277
|
+
DropMaterializedView,
|
|
278
|
+
DropSequence,
|
|
279
|
+
DropForeignTable,
|
|
280
|
+
}
|
|
281
|
+
assert all(c.category == ChangeCategory.ONLINE for c in drops.values())
|
|
282
|
+
assert all(is_held_back(c) for c in drops.values())
|
|
283
|
+
|
|
284
|
+
def test_a_name_declared_again_as_a_table_cannot_wait(self):
|
|
285
|
+
# CREATE TABLE v would collide with the view an expand run kept.
|
|
286
|
+
actual = Schema(views={"public.v": _view("v", "SELECT 1 AS x")})
|
|
287
|
+
desired = Schema(tables={"public.v": _table("v", x="integer")})
|
|
288
|
+
drop = next(c for c in compute_diff(desired, actual) if isinstance(c, DropView))
|
|
185
289
|
assert drop.category == ChangeCategory.OFFLINE
|
|
186
|
-
assert not drop.contract
|
|
187
|
-
assert "
|
|
290
|
+
assert not drop.contract and not is_held_back(drop)
|
|
291
|
+
assert "declared again" in drop.description
|
|
292
|
+
|
|
293
|
+
def test_a_view_reading_a_retyped_column_cannot_wait(self):
|
|
294
|
+
# ALTER COLUMN TYPE refuses while a view uses the column.
|
|
295
|
+
actual = Schema(
|
|
296
|
+
tables={"public.t": _table("t", a="integer")},
|
|
297
|
+
views={"public.v": _view("v", " SELECT t.a\n FROM t;")},
|
|
298
|
+
)
|
|
299
|
+
desired = Schema(tables={"public.t": _table("t", a="bigint")})
|
|
300
|
+
changes = compute_diff(desired, actual)
|
|
301
|
+
assert any(isinstance(c, AlterColumnType) for c in changes)
|
|
302
|
+
drop = next(c for c in changes if isinstance(c, DropView))
|
|
303
|
+
assert drop.category == ChangeCategory.OFFLINE
|
|
304
|
+
assert "it reads t" in drop.description
|
|
305
|
+
|
|
306
|
+
def test_a_view_reading_another_table_waits(self):
|
|
307
|
+
actual = Schema(
|
|
308
|
+
tables={
|
|
309
|
+
"public.t": _table("t", a="integer"),
|
|
310
|
+
"public.tt": _table("tt", a="integer"),
|
|
311
|
+
},
|
|
312
|
+
views={"public.v": _view("v", " SELECT tt.a\n FROM tt;")},
|
|
313
|
+
)
|
|
314
|
+
desired = Schema(
|
|
315
|
+
tables={
|
|
316
|
+
"public.t": _table("t", a="bigint"),
|
|
317
|
+
"public.tt": _table("tt", a="integer"),
|
|
318
|
+
}
|
|
319
|
+
)
|
|
320
|
+
drop = next(c for c in compute_diff(desired, actual) if isinstance(c, DropView))
|
|
321
|
+
assert is_held_back(drop)
|
|
322
|
+
|
|
323
|
+
def test_a_view_on_a_recreated_view_cannot_wait(self):
|
|
324
|
+
# The recreate drops what reads from it on the way.
|
|
325
|
+
base = View("public", "base", "SELECT 1 AS x", columns=["x integer"])
|
|
326
|
+
actual = Schema(
|
|
327
|
+
views={
|
|
328
|
+
"public.base": base,
|
|
329
|
+
"public.top": _view("top", "SELECT x FROM base"),
|
|
330
|
+
},
|
|
331
|
+
relation_dependents={"public.base": ["public.top"]},
|
|
332
|
+
)
|
|
333
|
+
desired = Schema(
|
|
334
|
+
views={
|
|
335
|
+
"public.base": View(
|
|
336
|
+
"public", "base", "SELECT 'a'::text AS y", columns=["y text"]
|
|
337
|
+
)
|
|
338
|
+
}
|
|
339
|
+
)
|
|
340
|
+
changes = compute_diff(desired, actual)
|
|
341
|
+
assert any(isinstance(c, DropAndCreateView) for c in changes)
|
|
342
|
+
drop = next(c for c in changes if isinstance(c, DropView))
|
|
343
|
+
assert drop.category == ChangeCategory.OFFLINE
|
|
344
|
+
assert "recreated" in drop.description
|
|
345
|
+
assert not any(is_held_back(c) for c in changes)
|
|
346
|
+
|
|
347
|
+
def test_dependents_of_a_view_that_goes_now_go_too(self):
|
|
348
|
+
# `low` collides with a new table, so it goes now; `high` reads it,
|
|
349
|
+
# and DROP VIEW low would fail on it.
|
|
350
|
+
actual = Schema(
|
|
351
|
+
views={
|
|
352
|
+
"public.low": _view("low", "SELECT 1 AS x"),
|
|
353
|
+
"public.high": _view("high", " SELECT low.x\n FROM low;"),
|
|
354
|
+
},
|
|
355
|
+
relation_dependents={"public.low": ["public.high"]},
|
|
356
|
+
)
|
|
357
|
+
desired = Schema(tables={"public.low": _table("low", x="integer")})
|
|
358
|
+
drops = [c for c in compute_diff(desired, actual) if isinstance(c, DropView)]
|
|
359
|
+
assert len(drops) == 2
|
|
360
|
+
assert all(c.category == ChangeCategory.OFFLINE for c in drops)
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
class TestRevokedRelationGrants:
|
|
364
|
+
@pytest.mark.parametrize(
|
|
365
|
+
"grant",
|
|
366
|
+
[
|
|
367
|
+
Grant("TABLE", "t", "public", "SELECT", "app"),
|
|
368
|
+
Grant("COLUMN", "t", "public", "SELECT", "app", column="a"),
|
|
369
|
+
Grant("SEQUENCE", "s", "public", "USAGE", "app"),
|
|
370
|
+
Grant("SCHEMA", "public", "public", "USAGE", "app"),
|
|
371
|
+
],
|
|
372
|
+
)
|
|
373
|
+
def test_a_revoke_is_a_contract_change(self, grant):
|
|
374
|
+
actual = Schema(
|
|
375
|
+
tables={"public.t": _table("t", a="integer")},
|
|
376
|
+
sequences={"public.s": Sequence("public", "s")},
|
|
377
|
+
grants=[grant],
|
|
378
|
+
)
|
|
379
|
+
desired = Schema(
|
|
380
|
+
tables={"public.t": _table("t", a="integer")},
|
|
381
|
+
sequences={"public.s": Sequence("public", "s")},
|
|
382
|
+
)
|
|
383
|
+
changes = compute_diff(desired, actual)
|
|
384
|
+
assert [type(c) for c in changes] == [RevokePrivilege]
|
|
385
|
+
assert is_held_back(changes[0])
|
|
386
|
+
|
|
387
|
+
def test_a_table_revoke_regrants_the_declared_columns(self):
|
|
388
|
+
# Moving a role from table-wide SELECT to SELECT (a): the contract
|
|
389
|
+
# run's REVOKE strips the column grant the expand run already wrote.
|
|
390
|
+
table = _table("t", a="integer", b="integer")
|
|
391
|
+
column = Grant("COLUMN", "t", "public", "SELECT", "app", column="a")
|
|
392
|
+
actual = Schema(
|
|
393
|
+
tables={"public.t": table},
|
|
394
|
+
grants=[Grant("TABLE", "t", "public", "SELECT", "app")],
|
|
395
|
+
)
|
|
396
|
+
desired = Schema(tables={"public.t": table}, grants=[column])
|
|
397
|
+
changes = compute_diff(desired, actual)
|
|
398
|
+
# One statement: an expand run, under the table-wide grant, has no
|
|
399
|
+
# use for the column grant (the catalog reports it as part of that).
|
|
400
|
+
assert [type(c) for c in changes] == [RevokePrivilege]
|
|
401
|
+
revoke = changes[0]
|
|
402
|
+
assert isinstance(revoke, RevokePrivilege) and is_held_back(revoke)
|
|
403
|
+
assert revoke.regrant_columns == [column]
|
|
404
|
+
sql = revoke.to_sql()
|
|
405
|
+
assert sql.index("REVOKE SELECT") < sql.index('GRANT SELECT ("a")')
|
|
188
406
|
|
|
189
407
|
|
|
190
408
|
# =============================================================================
|
|
@@ -298,6 +516,39 @@ class TestDeployWhileTheOldVersionServes:
|
|
|
298
516
|
target, sql_dir, plan, expect_converged=True, config=QUIET
|
|
299
517
|
)
|
|
300
518
|
|
|
519
|
+
def test_a_new_result_is_refused_online(self, tmp_path, databases):
|
|
520
|
+
target, plan = databases
|
|
521
|
+
sql_dir = tmp_path / "sql"
|
|
522
|
+
_declare(sql_dir, V1)
|
|
523
|
+
apply_declared_schema(target, sql_dir, plan, config=QUIET)
|
|
524
|
+
|
|
525
|
+
# What a serving connection holds: a plan cached for the old result.
|
|
526
|
+
conn = psycopg2.connect(target)
|
|
527
|
+
conn.autocommit = True
|
|
528
|
+
try:
|
|
529
|
+
with conn.cursor() as cur:
|
|
530
|
+
cur.execute("PREPARE old_total AS SELECT * FROM total(1)")
|
|
531
|
+
cur.execute("EXECUTE old_total")
|
|
532
|
+
_declare(
|
|
533
|
+
sql_dir,
|
|
534
|
+
V1.replace(
|
|
535
|
+
"total(a int) RETURNS int LANGUAGE sql AS 'SELECT a'",
|
|
536
|
+
"total(a int) RETURNS bigint LANGUAGE sql AS 'SELECT a'",
|
|
537
|
+
),
|
|
538
|
+
)
|
|
539
|
+
with pytest.raises(Exception, match="Migration execution failed"):
|
|
540
|
+
apply_declared_schema(
|
|
541
|
+
target, sql_dir, plan, expand_only=True, config=QUIET
|
|
542
|
+
)
|
|
543
|
+
cur.execute("EXECUTE old_total") # untouched: still answers
|
|
544
|
+
assert cur.fetchall() == [(1,)]
|
|
545
|
+
|
|
546
|
+
apply_declared_schema(target, sql_dir, plan, offline=True, config=QUIET)
|
|
547
|
+
with pytest.raises(psycopg2.Error, match="cached plan"):
|
|
548
|
+
cur.execute("EXECUTE old_total")
|
|
549
|
+
finally:
|
|
550
|
+
conn.close()
|
|
551
|
+
|
|
301
552
|
def test_expand_keeps_old_calls_working_and_contract_drops(
|
|
302
553
|
self, tmp_path, databases
|
|
303
554
|
):
|
|
@@ -439,3 +690,97 @@ class TestExpandRefusesBreakingBlocks:
|
|
|
439
690
|
sql_dir = tmp_path / "sql"
|
|
440
691
|
_declare(sql_dir, INCIDENT_V2)
|
|
441
692
|
apply_declared_schema(target, sql_dir, plan, expand_only=True, config=QUIET)
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
# =============================================================================
|
|
696
|
+
# Relations and grants, against a real server
|
|
697
|
+
# =============================================================================
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
@pytest.fixture
|
|
701
|
+
def app_role(server_url):
|
|
702
|
+
role = f"pgdeclare_ec_app_{uuid.uuid4().hex[:8]}"
|
|
703
|
+
_run(server_url, f'CREATE ROLE "{role}"')
|
|
704
|
+
try:
|
|
705
|
+
yield role
|
|
706
|
+
finally:
|
|
707
|
+
_run(server_url, f'DROP ROLE IF EXISTS "{role}"')
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
RELATIONS_V1 = """
|
|
711
|
+
CREATE TABLE t (a int, b int);
|
|
712
|
+
CREATE VIEW v AS SELECT a FROM t;
|
|
713
|
+
CREATE MATERIALIZED VIEW m AS SELECT a FROM t;
|
|
714
|
+
CREATE SEQUENCE s;
|
|
715
|
+
GRANT USAGE ON SCHEMA public TO "{role}";
|
|
716
|
+
GRANT SELECT ON t TO "{role}";
|
|
717
|
+
GRANT SELECT ON v TO "{role}";
|
|
718
|
+
GRANT SELECT ON m TO "{role}";
|
|
719
|
+
GRANT USAGE ON SEQUENCE s TO "{role}";
|
|
720
|
+
"""
|
|
721
|
+
|
|
722
|
+
# The old version reads all of it, as the application role.
|
|
723
|
+
OLD_READS = (
|
|
724
|
+
'SET ROLE "{role}"; '
|
|
725
|
+
"SELECT (SELECT count(*) FROM t), (SELECT count(*) FROM v), "
|
|
726
|
+
"(SELECT count(*) FROM m), nextval('s') > 0, (SELECT count(b) FROM t)"
|
|
727
|
+
)
|
|
728
|
+
|
|
729
|
+
# The new version reads t's column a only, and needs none of the rest.
|
|
730
|
+
RELATIONS_V2 = """
|
|
731
|
+
CREATE TABLE t (a int, b int);
|
|
732
|
+
GRANT USAGE ON SCHEMA public TO "{role}";
|
|
733
|
+
GRANT SELECT (a) ON t TO "{role}";
|
|
734
|
+
"""
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
class TestRelationsWhileTheOldVersionServes:
|
|
738
|
+
def test_expand_keeps_what_the_old_version_reads_and_contract_drops(
|
|
739
|
+
self, tmp_path, app_role, databases
|
|
740
|
+
):
|
|
741
|
+
target, plan = databases
|
|
742
|
+
sql_dir = tmp_path / "sql"
|
|
743
|
+
config = PgDeclareConfig(report=lambda _line: None, managed_roles=(app_role,))
|
|
744
|
+
_declare(sql_dir, RELATIONS_V1.format(role=app_role))
|
|
745
|
+
apply_declared_schema(target, sql_dir, plan, config=config)
|
|
746
|
+
old_reads = OLD_READS.format(role=app_role)
|
|
747
|
+
assert _run(target, old_reads) == [(0, 0, 0, True, 0)]
|
|
748
|
+
|
|
749
|
+
_declare(sql_dir, RELATIONS_V2.format(role=app_role))
|
|
750
|
+
# Before the fix, this dropped v, m and s and revoked the table-wide
|
|
751
|
+
# SELECT, and every one of these reads failed until the switch.
|
|
752
|
+
apply_declared_schema(target, sql_dir, plan, expand_only=True, config=config)
|
|
753
|
+
assert _run(target, old_reads) == [(0, 0, 0, True, 0)]
|
|
754
|
+
|
|
755
|
+
# Contract, once the old version has stopped: the role keeps column a
|
|
756
|
+
# (the table-wide REVOKE strips it, and the same statement restores it).
|
|
757
|
+
apply_declared_schema(target, sql_dir, plan, config=config)
|
|
758
|
+
assert _run(
|
|
759
|
+
target,
|
|
760
|
+
"SELECT relname FROM pg_class WHERE relnamespace = "
|
|
761
|
+
"'public'::regnamespace ORDER BY 1",
|
|
762
|
+
) == [("t",)]
|
|
763
|
+
assert _run(target, f'SET ROLE "{app_role}"; SELECT count(a) FROM t') == [(0,)]
|
|
764
|
+
with pytest.raises(psycopg2.errors.InsufficientPrivilege):
|
|
765
|
+
_run(target, f'SET ROLE "{app_role}"; SELECT count(b) FROM t')
|
|
766
|
+
apply_declared_schema(
|
|
767
|
+
target, sql_dir, plan, expect_converged=True, config=config
|
|
768
|
+
)
|
|
769
|
+
|
|
770
|
+
def test_a_view_replaced_by_a_table_of_its_name_is_refused_online(
|
|
771
|
+
self, tmp_path, databases
|
|
772
|
+
):
|
|
773
|
+
target, plan = databases
|
|
774
|
+
sql_dir = tmp_path / "sql"
|
|
775
|
+
_declare(sql_dir, "CREATE TABLE t (a int);\nCREATE VIEW v AS SELECT a FROM t;")
|
|
776
|
+
apply_declared_schema(target, sql_dir, plan, config=QUIET)
|
|
777
|
+
|
|
778
|
+
_declare(sql_dir, "CREATE TABLE t (a int);\nCREATE TABLE v (a int);")
|
|
779
|
+
with pytest.raises(Exception, match="Migration execution failed"):
|
|
780
|
+
apply_declared_schema(target, sql_dir, plan, expand_only=True, config=QUIET)
|
|
781
|
+
assert _run(target, "SELECT count(*) FROM v") == [(0,)]
|
|
782
|
+
|
|
783
|
+
apply_declared_schema(target, sql_dir, plan, offline=True, config=QUIET)
|
|
784
|
+
assert _run(target, "SELECT relkind FROM pg_class WHERE relname = 'v'") == [
|
|
785
|
+
("r",)
|
|
786
|
+
]
|
|
@@ -407,8 +407,10 @@ class TestRoutineRoundTrip:
|
|
|
407
407
|
.replace("'SELECT a + b'", "'SELECT x + y'")
|
|
408
408
|
)
|
|
409
409
|
# The new procedure signature competes with the old one for a call
|
|
410
|
-
# of two arguments,
|
|
411
|
-
#
|
|
410
|
+
# of two arguments, `step`'s renamed parameters break a call naming
|
|
411
|
+
# the old ones, `tally`'s new result breaks a plan cached for the old
|
|
412
|
+
# one, and a procedure that becomes a function is called differently:
|
|
413
|
+
# all four swaps are offline.
|
|
412
414
|
offline = [
|
|
413
415
|
c
|
|
414
416
|
for c in _plan(sql_dir, recreated, target_url, plan_db)
|
|
@@ -416,7 +418,9 @@ class TestRoutineRoundTrip:
|
|
|
416
418
|
]
|
|
417
419
|
assert sorted(c.object_name for c in offline) == [
|
|
418
420
|
"public.book(IN amount bigint, INOUT booked bigint)",
|
|
421
|
+
'public.odd "proc"()',
|
|
419
422
|
"public.step(x integer, y integer)",
|
|
423
|
+
"public.tally()",
|
|
420
424
|
]
|
|
421
425
|
_apply(sql_dir, recreated, target_url, plan_db, offline=True)
|
|
422
426
|
routines = _routines(target_url)
|
|
@@ -459,9 +463,10 @@ class TestRoutineRoundTrip:
|
|
|
459
463
|
assert _routines(target_url) == [("row_no", "w", "")]
|
|
460
464
|
assert _pending(sql_dir, target_url, plan_db) == []
|
|
461
465
|
|
|
462
|
-
# A window function cannot be replaced by a plain one
|
|
466
|
+
# A window function cannot be replaced by a plain one, and a call
|
|
467
|
+
# with OVER fails once it is: offline.
|
|
463
468
|
plain = "CREATE FUNCTION row_no() RETURNS bigint LANGUAGE sql AS 'SELECT 1::bigint';"
|
|
464
|
-
_apply(sql_dir, plain, target_url, plan_db)
|
|
469
|
+
_apply(sql_dir, plain, target_url, plan_db, offline=True)
|
|
465
470
|
assert _routines(target_url) == [("row_no", "f", "")]
|
|
466
471
|
assert _pending(sql_dir, target_url, plan_db) == []
|
|
467
472
|
|
|
@@ -516,16 +521,18 @@ class TestRoutineGrants:
|
|
|
516
521
|
assert privileges() == [("tally",), ("work",)]
|
|
517
522
|
assert _pending(sql_dir, target_url, plan_db, config) == []
|
|
518
523
|
|
|
519
|
-
# A new result type recreates the aggregate: the grant is
|
|
524
|
+
# A new result type recreates the aggregate (offline): the grant is
|
|
525
|
+
# replayed.
|
|
520
526
|
changed = granted.replace(
|
|
521
527
|
"CREATE AGGREGATE tally(*) (SFUNC = int8inc, STYPE = int8, INITCOND = '0');",
|
|
522
528
|
"CREATE AGGREGATE tally(*) (SFUNC = int4inc, STYPE = int4, INITCOND = '0');",
|
|
523
529
|
)
|
|
524
|
-
_apply(sql_dir, changed, target_url, plan_db, config)
|
|
530
|
+
_apply(sql_dir, changed, target_url, plan_db, config, offline=True)
|
|
525
531
|
assert privileges() == [("tally",), ("work",)]
|
|
526
532
|
assert _pending(sql_dir, target_url, plan_db, config) == []
|
|
527
533
|
|
|
528
|
-
|
|
534
|
+
# Back to the base's result type: offline again.
|
|
535
|
+
_apply(sql_dir, base, target_url, plan_db, config, offline=True)
|
|
529
536
|
assert privileges() == []
|
|
530
537
|
assert _pending(sql_dir, target_url, plan_db, config) == []
|
|
531
538
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|