pgdeclare 0.1.1__tar.gz → 0.1.2__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.1.1 → pgdeclare-0.1.2}/CHANGELOG.md +14 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/PKG-INFO +2 -15
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/README.md +1 -14
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/pyproject.toml +1 -1
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/__init__.py +1 -1
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/changes.py +35 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/differ.py +23 -4
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/introspection.py +14 -1
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/models.py +6 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_boundary.py +1 -1
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_materialized_views.py +29 -0
- pgdeclare-0.1.2/tests/test_views.py +244 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/.gitignore +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/LICENSE +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/NOTICE +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/__main__.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/cli.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/config.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/drop_directive.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/executor.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/ledger.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/migrate.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/prove.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/py.typed +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/quoting.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/retry_executor.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/schema_parser.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/src/pgdeclare/statement_parser.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/conftest.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/dbutil.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_cli.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_comments.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_default_privileges.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_diff.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_domains.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_foreign_data.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_integration.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_lifecycle.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_partitions.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_pipeline.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_prove.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_publications_rules.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_ranges_collations.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_retry_executor.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_routines.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_security.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_sequences.py +0 -0
- {pgdeclare-0.1.1 → pgdeclare-0.1.2}/tests/test_statement_parser.py +0 -0
|
@@ -5,6 +5,19 @@ 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.1.2] - 2026-10-19
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- A view whose query changes while every existing output column keeps its
|
|
13
|
+
name, type and collation (new columns may be appended) is now replaced in
|
|
14
|
+
place with `CREATE OR REPLACE VIEW`, online, instead of dropped and
|
|
15
|
+
re-created. The view keeps its OID, so its grants, comments and owner stay,
|
|
16
|
+
and the views, materialized views and SQL functions reading from it are not
|
|
17
|
+
dropped and rebuilt. A materialized view reading from it keeps its data
|
|
18
|
+
until it is refreshed. A renamed, retyped, reordered or removed column still
|
|
19
|
+
drops and re-creates the view with its dependents.
|
|
20
|
+
|
|
8
21
|
## [0.1.1] - 2026-10-18
|
|
9
22
|
|
|
10
23
|
### Added
|
|
@@ -192,6 +205,7 @@ First real release: a schema engine proven in production, extracted and made gen
|
|
|
192
205
|
|
|
193
206
|
Placeholder release to reserve the name on PyPI. No functionality.
|
|
194
207
|
|
|
208
|
+
[0.1.2]: https://github.com/powertime-org/pgdeclare/compare/v0.1.1...v0.1.2
|
|
195
209
|
[0.1.1]: https://github.com/powertime-org/pgdeclare/compare/v0.1.0...v0.1.1
|
|
196
210
|
[0.1.0]: https://github.com/powertime-org/pgdeclare/compare/v0.0.1...v0.1.0
|
|
197
211
|
[0.0.1]: https://github.com/powertime-org/pgdeclare/releases/tag/v0.0.1
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: pgdeclare
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
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.1.
|
|
50
|
+
Status: **0.1.2.** 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
|
|
|
@@ -139,19 +139,6 @@ Every command, flag and exit code is in [docs/cli.md](https://github.com/powerti
|
|
|
139
139
|
- [Configuration](https://github.com/powertime-org/pgdeclare/blob/main/docs/configuration.md): every `PgDeclareConfig` field.
|
|
140
140
|
- [Safety](https://github.com/powertime-org/pgdeclare/blob/main/docs/safety.md): previews, `--prove`, online/offline/blocked changes, quoting, masking, and the supported PostgreSQL versions (14 to 18).
|
|
141
141
|
|
|
142
|
-
## How it compares
|
|
143
|
-
|
|
144
|
-
As of October 2026, pgdeclare manages nearly every object kind [pg-delta](https://github.com/supabase/pg-toolbelt) and [pgschema](https://github.com/pgplex/pgschema) do; the exact differences are in [Coverage](https://github.com/powertime-org/pgdeclare/blob/main/docs/coverage.md#compared-with-other-tools). pgdeclare's difference is where the steps a diff cannot infer live: next to the DDL they belong to, run once, and recorded by hash.
|
|
145
|
-
|
|
146
|
-
| Tool | Renames and backfills | Records what ran | Free licence | Notes |
|
|
147
|
-
|------|-----------------------|------------------|--------------|-------|
|
|
148
|
-
| pgdeclare | `@migrate` blocks beside the DDL | Hash ledger; edited blocks are refused | Apache-2.0 | Re-diffs after applying and fails the run if anything is left; `--prove` rehearses the full run, `@migrate` blocks included, on a clone |
|
|
149
|
-
| [pgschema](https://github.com/pgplex/pgschema) | Outside the tool | No; state only | Apache-2.0 | Terraform-style plan and apply; broad object coverage |
|
|
150
|
-
| [pg-delta](https://github.com/supabase/pg-toolbelt) | Renames come out as drop plus add, edited by hand; never writes row changes | Through Supabase migration files | PostgreSQL | Alpha; very broad coverage; `prove` tests a plan on a clone first |
|
|
151
|
-
| [Atlas](https://atlasgo.io) | Versioned migration files | Yes, in versioned mode | Apache-2.0 Community Edition | The Community Edition omits views, functions, triggers and RLS policies; the full binary is under the Atlas MSA |
|
|
152
|
-
| [pg-schema-diff](https://github.com/stripe/pg-schema-diff) | Renames come out as drop plus add | No | MIT | Strong lock avoidance for online changes |
|
|
153
|
-
| [migra](https://github.com/djrobstep/migra) | Not handled | No | Unlicense | Deprecated; last release 2022 |
|
|
154
|
-
|
|
155
142
|
## Contributing
|
|
156
143
|
|
|
157
144
|
Issues and pull requests are welcome on [GitHub](https://github.com/powertime-org/pgdeclare). See [CONTRIBUTING.md](https://github.com/powertime-org/pgdeclare/blob/main/CONTRIBUTING.md) for running the tests, and [SECURITY.md](https://github.com/powertime-org/pgdeclare/blob/main/SECURITY.md) for reporting vulnerabilities.
|
|
@@ -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.1.
|
|
17
|
+
Status: **0.1.2.** 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
|
|
|
@@ -106,19 +106,6 @@ Every command, flag and exit code is in [docs/cli.md](https://github.com/powerti
|
|
|
106
106
|
- [Configuration](https://github.com/powertime-org/pgdeclare/blob/main/docs/configuration.md): every `PgDeclareConfig` field.
|
|
107
107
|
- [Safety](https://github.com/powertime-org/pgdeclare/blob/main/docs/safety.md): previews, `--prove`, online/offline/blocked changes, quoting, masking, and the supported PostgreSQL versions (14 to 18).
|
|
108
108
|
|
|
109
|
-
## How it compares
|
|
110
|
-
|
|
111
|
-
As of October 2026, pgdeclare manages nearly every object kind [pg-delta](https://github.com/supabase/pg-toolbelt) and [pgschema](https://github.com/pgplex/pgschema) do; the exact differences are in [Coverage](https://github.com/powertime-org/pgdeclare/blob/main/docs/coverage.md#compared-with-other-tools). pgdeclare's difference is where the steps a diff cannot infer live: next to the DDL they belong to, run once, and recorded by hash.
|
|
112
|
-
|
|
113
|
-
| Tool | Renames and backfills | Records what ran | Free licence | Notes |
|
|
114
|
-
|------|-----------------------|------------------|--------------|-------|
|
|
115
|
-
| pgdeclare | `@migrate` blocks beside the DDL | Hash ledger; edited blocks are refused | Apache-2.0 | Re-diffs after applying and fails the run if anything is left; `--prove` rehearses the full run, `@migrate` blocks included, on a clone |
|
|
116
|
-
| [pgschema](https://github.com/pgplex/pgschema) | Outside the tool | No; state only | Apache-2.0 | Terraform-style plan and apply; broad object coverage |
|
|
117
|
-
| [pg-delta](https://github.com/supabase/pg-toolbelt) | Renames come out as drop plus add, edited by hand; never writes row changes | Through Supabase migration files | PostgreSQL | Alpha; very broad coverage; `prove` tests a plan on a clone first |
|
|
118
|
-
| [Atlas](https://atlasgo.io) | Versioned migration files | Yes, in versioned mode | Apache-2.0 Community Edition | The Community Edition omits views, functions, triggers and RLS policies; the full binary is under the Atlas MSA |
|
|
119
|
-
| [pg-schema-diff](https://github.com/stripe/pg-schema-diff) | Renames come out as drop plus add | No | MIT | Strong lock avoidance for online changes |
|
|
120
|
-
| [migra](https://github.com/djrobstep/migra) | Not handled | No | Unlicense | Deprecated; last release 2022 |
|
|
121
|
-
|
|
122
109
|
## Contributing
|
|
123
110
|
|
|
124
111
|
Issues and pull requests are welcome on [GitHub](https://github.com/powertime-org/pgdeclare). See [CONTRIBUTING.md](https://github.com/powertime-org/pgdeclare/blob/main/CONTRIBUTING.md) for running the tests, and [SECURITY.md](https://github.com/powertime-org/pgdeclare/blob/main/SECURITY.md) for reporting vulnerabilities.
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pgdeclare"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.2"
|
|
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"
|
|
@@ -2171,6 +2171,41 @@ class CreateView(Change):
|
|
|
2171
2171
|
)
|
|
2172
2172
|
|
|
2173
2173
|
|
|
2174
|
+
@dataclass
|
|
2175
|
+
class ReplaceView(Change):
|
|
2176
|
+
"""Change an existing view's query in place with CREATE OR REPLACE VIEW.
|
|
2177
|
+
|
|
2178
|
+
The path for a definition change that keeps every existing output column
|
|
2179
|
+
(same names, types and collations, in order; new ones may only be
|
|
2180
|
+
appended). The view keeps its OID, so its ACL, comments, owner and the
|
|
2181
|
+
views, materialized views and SQL functions reading from it all stay as
|
|
2182
|
+
they are: nothing is dropped, so nothing needs rebuilding.
|
|
2183
|
+
"""
|
|
2184
|
+
|
|
2185
|
+
view: View = field(
|
|
2186
|
+
default_factory=lambda: View(schema="public", name="", definition="")
|
|
2187
|
+
)
|
|
2188
|
+
category: ChangeCategory = ChangeCategory.ONLINE
|
|
2189
|
+
description: str = ""
|
|
2190
|
+
object_type: str = "VIEW"
|
|
2191
|
+
|
|
2192
|
+
def __post_init__(self):
|
|
2193
|
+
self.object_name = self.view.qualified_name
|
|
2194
|
+
if not self.description:
|
|
2195
|
+
self.description = f"Replace view {self.view.name}" + view_options_note(
|
|
2196
|
+
self.view
|
|
2197
|
+
)
|
|
2198
|
+
|
|
2199
|
+
def to_sql(self) -> str:
|
|
2200
|
+
# CREATE OR REPLACE VIEW replaces the reloptions with the WITH clause
|
|
2201
|
+
# (none clears them), so the declared options converge in the same
|
|
2202
|
+
# statement.
|
|
2203
|
+
return (
|
|
2204
|
+
f"CREATE OR REPLACE VIEW {quote_ident(self.view.schema)}.{quote_ident(self.view.name)}"
|
|
2205
|
+
f"{self.view.options_clause} AS {self.view.definition}"
|
|
2206
|
+
)
|
|
2207
|
+
|
|
2208
|
+
|
|
2174
2209
|
@dataclass
|
|
2175
2210
|
class DropView(Change):
|
|
2176
2211
|
"""Drop a view."""
|
|
@@ -111,6 +111,7 @@ from .changes import (
|
|
|
111
111
|
RevokePublicExecute,
|
|
112
112
|
ReplacePublicationTable,
|
|
113
113
|
RevokeRoleMembership,
|
|
114
|
+
ReplaceView,
|
|
114
115
|
SetComment,
|
|
115
116
|
SetDefault,
|
|
116
117
|
SetSequenceOwner,
|
|
@@ -642,6 +643,7 @@ def _carry_recreate_dependents(
|
|
|
642
643
|
(
|
|
643
644
|
AlterViewOptions,
|
|
644
645
|
AlterMaterializedViewOptions,
|
|
646
|
+
ReplaceView,
|
|
645
647
|
),
|
|
646
648
|
)
|
|
647
649
|
and c.object_name in folded
|
|
@@ -1958,10 +1960,15 @@ def _diff_views(desired: Schema, actual: Schema) -> list[Change]:
|
|
|
1958
1960
|
if key not in actual.views:
|
|
1959
1961
|
changes.append(CreateView(view=view))
|
|
1960
1962
|
elif view.definition.strip() != actual.views[key].definition.strip():
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1963
|
+
if _view_replaceable(actual.views[key], view):
|
|
1964
|
+
# Same leading columns: replace in place, so the view keeps its
|
|
1965
|
+
# ACL and the relations reading from it are left alone.
|
|
1966
|
+
changes.append(ReplaceView(view=view))
|
|
1967
|
+
else:
|
|
1968
|
+
# A column renamed, retyped, reordered or removed: CREATE OR
|
|
1969
|
+
# REPLACE VIEW refuses those, so drop and recreate. The
|
|
1970
|
+
# recreate carries the declared WITH (...) options itself.
|
|
1971
|
+
changes.append(DropAndCreateView(view=view))
|
|
1965
1972
|
elif view.reloptions != actual.views[key].reloptions:
|
|
1966
1973
|
# Definition already matches, only the security mode drifted (say,
|
|
1967
1974
|
# `security_invoker` was declared but never written). ALTER VIEW
|
|
@@ -1981,6 +1988,18 @@ def _diff_views(desired: Schema, actual: Schema) -> list[Change]:
|
|
|
1981
1988
|
return changes
|
|
1982
1989
|
|
|
1983
1990
|
|
|
1991
|
+
def _view_replaceable(current: View, declared: View) -> bool:
|
|
1992
|
+
"""Whether CREATE OR REPLACE VIEW can turn `current` into `declared`.
|
|
1993
|
+
|
|
1994
|
+
PostgreSQL accepts it when the new query keeps every existing output
|
|
1995
|
+
column with the same name, type and collation, in the same position, and
|
|
1996
|
+
only appends new ones. Unknown columns (an empty list) never qualify.
|
|
1997
|
+
"""
|
|
1998
|
+
if not current.columns or not declared.columns:
|
|
1999
|
+
return False
|
|
2000
|
+
return declared.columns[: len(current.columns)] == current.columns
|
|
2001
|
+
|
|
2002
|
+
|
|
1984
2003
|
def _diff_materialized_views(desired: Schema, actual: Schema) -> list[Change]:
|
|
1985
2004
|
"""Diff materialized views between desired and actual schemas.
|
|
1986
2005
|
|
|
@@ -1278,7 +1278,19 @@ def _query_views(conn, schema: str) -> dict[str, View]:
|
|
|
1278
1278
|
SELECT
|
|
1279
1279
|
v.viewname AS view_name,
|
|
1280
1280
|
v.definition AS view_definition,
|
|
1281
|
-
c.reloptions AS reloptions
|
|
1281
|
+
c.reloptions AS reloptions,
|
|
1282
|
+
ARRAY(
|
|
1283
|
+
SELECT quote_ident(a.attname) || ' '
|
|
1284
|
+
|| format_type(a.atttypid, a.atttypmod)
|
|
1285
|
+
|| CASE WHEN a.attcollation <> 0
|
|
1286
|
+
AND a.attcollation <> t.typcollation
|
|
1287
|
+
THEN ' COLLATE ' || a.attcollation::regcollation::text
|
|
1288
|
+
ELSE '' END
|
|
1289
|
+
FROM pg_attribute AS a
|
|
1290
|
+
JOIN pg_type AS t ON t.oid = a.atttypid
|
|
1291
|
+
WHERE a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped
|
|
1292
|
+
ORDER BY a.attnum
|
|
1293
|
+
) AS columns
|
|
1282
1294
|
FROM pg_views AS v
|
|
1283
1295
|
JOIN pg_namespace AS n ON n.nspname = v.schemaname
|
|
1284
1296
|
JOIN pg_class AS c
|
|
@@ -1303,6 +1315,7 @@ def _query_views(conn, schema: str) -> dict[str, View]:
|
|
|
1303
1315
|
name=row["view_name"],
|
|
1304
1316
|
definition=row["view_definition"] or "",
|
|
1305
1317
|
reloptions=list(row["reloptions"] or []),
|
|
1318
|
+
columns=list(row["columns"] or []),
|
|
1306
1319
|
)
|
|
1307
1320
|
views[view.qualified_name] = view
|
|
1308
1321
|
|
|
@@ -693,6 +693,12 @@ class View:
|
|
|
693
693
|
# views owned by a BYPASSRLS superuser mean RLS on the base tables never
|
|
694
694
|
# applies to anything read through the view, so this failed open.
|
|
695
695
|
reloptions: list[str] = field(default_factory=list)
|
|
696
|
+
# Output columns as `name type [COLLATE collation]`, in order, from the
|
|
697
|
+
# catalog. Not part of equality (the definition already decides drift):
|
|
698
|
+
# they decide whether a changed definition can be applied with
|
|
699
|
+
# CREATE OR REPLACE VIEW, which keeps the view, its ACL and the views
|
|
700
|
+
# reading from it, or has to drop and recreate. Empty means unknown.
|
|
701
|
+
columns: list[str] = field(default_factory=list)
|
|
696
702
|
|
|
697
703
|
def __post_init__(self):
|
|
698
704
|
self.reloptions = normalize_view_reloptions(self.reloptions)
|
|
@@ -464,7 +464,36 @@ class TestMaterializedViewDependencies:
|
|
|
464
464
|
env.apply()
|
|
465
465
|
_admin(env.url, "INSERT INTO t VALUES (1, 10, 5)")
|
|
466
466
|
|
|
467
|
+
mid = _rows(env.url, "SELECT 'mid'::regclass::oid")
|
|
468
|
+
|
|
469
|
+
# An appended column is replaced in place (online): the matview reading
|
|
470
|
+
# from the view is left as it is, data included, as after any change to
|
|
471
|
+
# what it reads. Refreshing it is the application's call.
|
|
467
472
|
env.declare(LAYERED.replace("SELECT id, a FROM t", "SELECT id, a, b FROM t"))
|
|
473
|
+
env.apply()
|
|
474
|
+
|
|
475
|
+
assert _rows(env.url, "SELECT 'mid'::regclass::oid") == mid
|
|
476
|
+
assert _rows(env.url, "SELECT id, a FROM top_v") == []
|
|
477
|
+
_admin(env.url, "REFRESH MATERIALIZED VIEW mid")
|
|
478
|
+
assert _rows(env.url, "SELECT id, a FROM top_v") == [(1, 10)]
|
|
479
|
+
assert env.indexes("mid") == ["mid_a_idx"]
|
|
480
|
+
assert env.can_select("mid") is True
|
|
481
|
+
assert env.can_select("top_v") is True
|
|
482
|
+
assert env.residual() == []
|
|
483
|
+
|
|
484
|
+
def test_recreating_the_view_under_a_matview(self, env):
|
|
485
|
+
env.declare(LAYERED)
|
|
486
|
+
env.apply()
|
|
487
|
+
_admin(env.url, "INSERT INTO t VALUES (1, 10, 5)")
|
|
488
|
+
|
|
489
|
+
# A retyped column cannot be replaced in place: the view is recreated,
|
|
490
|
+
# which rebuilds the matview reading from it (offline).
|
|
491
|
+
env.declare(
|
|
492
|
+
LAYERED.replace("SELECT id, a FROM t", "SELECT id, a::bigint AS a FROM t")
|
|
493
|
+
)
|
|
494
|
+
(change,) = env.residual()
|
|
495
|
+
assert isinstance(change, DropAndCreateView)
|
|
496
|
+
assert change.category == ChangeCategory.OFFLINE
|
|
468
497
|
env.apply(offline=True)
|
|
469
498
|
|
|
470
499
|
assert _rows(env.url, "SELECT id, a FROM top_v") == [(1, 10)]
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"""Views: dropping a granted view, and a body change under dependent views.
|
|
2
|
+
|
|
3
|
+
The integration tests drive the whole pipeline (`apply_declared_schema`)
|
|
4
|
+
against a target and a plan database, then check that a second diff is empty;
|
|
5
|
+
they need TEST_DATABASE_URL.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import uuid
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
import psycopg2
|
|
13
|
+
import pytest
|
|
14
|
+
|
|
15
|
+
from pgdeclare import (
|
|
16
|
+
PgDeclareConfig,
|
|
17
|
+
apply_declared_schema,
|
|
18
|
+
build_desired_schema,
|
|
19
|
+
compute_diff,
|
|
20
|
+
introspect_schema,
|
|
21
|
+
)
|
|
22
|
+
from pgdeclare.changes import DropAndCreateView, ReplaceView
|
|
23
|
+
from pgdeclare.differ import _view_replaceable
|
|
24
|
+
from pgdeclare.models import View
|
|
25
|
+
|
|
26
|
+
from conftest import require_server_version
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@pytest.fixture
|
|
30
|
+
def server_url():
|
|
31
|
+
url = os.environ.get("TEST_DATABASE_URL")
|
|
32
|
+
if not url:
|
|
33
|
+
pytest.skip("TEST_DATABASE_URL not set")
|
|
34
|
+
return url
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _with_db(url: str, name: str) -> str:
|
|
38
|
+
return url.rsplit("/", 1)[0] + f"/{name}"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _admin(url: str, sql: str) -> None:
|
|
42
|
+
conn = psycopg2.connect(url)
|
|
43
|
+
conn.autocommit = True
|
|
44
|
+
try:
|
|
45
|
+
with conn.cursor() as cur:
|
|
46
|
+
cur.execute(sql)
|
|
47
|
+
finally:
|
|
48
|
+
conn.close()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _rows(url: str, sql: str, params=None) -> list[tuple]:
|
|
52
|
+
with psycopg2.connect(url) as conn:
|
|
53
|
+
with conn.cursor() as cur:
|
|
54
|
+
cur.execute(sql, params)
|
|
55
|
+
return cur.fetchall()
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@pytest.fixture
|
|
59
|
+
def env(server_url, tmp_path):
|
|
60
|
+
suffix = uuid.uuid4().hex[:8]
|
|
61
|
+
target, plan = f"pgdeclare_v_target_{suffix}", f"pgdeclare_v_plan_{suffix}"
|
|
62
|
+
role = f"v_reader_{suffix}"
|
|
63
|
+
for name in (target, plan):
|
|
64
|
+
_admin(server_url, f'CREATE DATABASE "{name}"')
|
|
65
|
+
try:
|
|
66
|
+
yield _Env(_with_db(server_url, target), plan, role, tmp_path / "sql")
|
|
67
|
+
finally:
|
|
68
|
+
for name in (target, plan):
|
|
69
|
+
_admin(server_url, f'DROP DATABASE IF EXISTS "{name}" WITH (FORCE)')
|
|
70
|
+
_admin(server_url, f'DROP ROLE IF EXISTS "{role}"')
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class _Env:
|
|
74
|
+
def __init__(self, url: str, plan: str, role: str, sql_dir: Path):
|
|
75
|
+
self.url, self.plan, self.role, self.sql_dir = url, plan, role, sql_dir
|
|
76
|
+
self.config = PgDeclareConfig(managed_roles=(role,), report=lambda _l: None)
|
|
77
|
+
|
|
78
|
+
def declare(self, sql: str) -> None:
|
|
79
|
+
schemas = self.sql_dir / "schemas"
|
|
80
|
+
schemas.mkdir(parents=True, exist_ok=True)
|
|
81
|
+
role = f'CREATE ROLE "{self.role}" NOLOGIN;\n'
|
|
82
|
+
role += f'GRANT USAGE ON SCHEMA public TO "{self.role}";\n'
|
|
83
|
+
(schemas / "schema.sql").write_text(role + sql.replace("ROLE", self.role))
|
|
84
|
+
|
|
85
|
+
def plan_changes(self) -> list:
|
|
86
|
+
desired = build_desired_schema(
|
|
87
|
+
self.sql_dir, self.url, self.plan, config=self.config
|
|
88
|
+
)
|
|
89
|
+
actual = introspect_schema(self.url, config=self.config)
|
|
90
|
+
return compute_diff(desired, actual, schemas=["public"], config=self.config)
|
|
91
|
+
|
|
92
|
+
def apply(self) -> None:
|
|
93
|
+
apply_declared_schema(self.url, self.sql_dir, self.plan, config=self.config)
|
|
94
|
+
|
|
95
|
+
def can_select(self, relation: str) -> bool:
|
|
96
|
+
return _rows(
|
|
97
|
+
self.url,
|
|
98
|
+
"SELECT has_table_privilege(%s, %s, 'SELECT')",
|
|
99
|
+
(self.role, relation),
|
|
100
|
+
)[0][0]
|
|
101
|
+
|
|
102
|
+
def oid(self, relation: str) -> int:
|
|
103
|
+
return _rows(self.url, "SELECT %s::regclass::oid", (relation,))[0][0]
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
BASE = """
|
|
107
|
+
CREATE TABLE IF NOT EXISTS people (id int PRIMARY KEY, name text, active bool);
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
PEOPLE_SET = """
|
|
111
|
+
CREATE VIEW people_set AS SELECT id, name FROM people WHERE active;
|
|
112
|
+
GRANT SELECT ON people_set TO "ROLE";
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
DEPENDENTS = """
|
|
116
|
+
CREATE VIEW people_adder AS SELECT id FROM people_set;
|
|
117
|
+
GRANT SELECT ON people_adder TO "ROLE";
|
|
118
|
+
CREATE VIEW people_picker AS SELECT a.id, s.name
|
|
119
|
+
FROM people_adder AS a JOIN people_set AS s USING (id);
|
|
120
|
+
GRANT SELECT ON people_picker TO "ROLE";
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@pytest.mark.integration
|
|
125
|
+
class TestViewLifecycle:
|
|
126
|
+
def test_dropping_a_granted_view_plans_no_revoke(self, env):
|
|
127
|
+
env.declare(BASE + PEOPLE_SET)
|
|
128
|
+
env.apply()
|
|
129
|
+
assert env.can_select("people_set")
|
|
130
|
+
|
|
131
|
+
env.declare(BASE)
|
|
132
|
+
assert not any("REVOKE" in c.to_sql() for c in env.plan_changes())
|
|
133
|
+
env.apply()
|
|
134
|
+
|
|
135
|
+
assert (
|
|
136
|
+
_rows(env.url, "SELECT viewname FROM pg_views WHERE schemaname = 'public'")
|
|
137
|
+
== []
|
|
138
|
+
)
|
|
139
|
+
assert env.plan_changes() == []
|
|
140
|
+
|
|
141
|
+
def test_body_change_under_dependents_replaces_in_place(self, env):
|
|
142
|
+
env.declare(BASE + PEOPLE_SET + DEPENDENTS)
|
|
143
|
+
env.apply()
|
|
144
|
+
_admin(env.url, "INSERT INTO people VALUES (1, 'a', true), (2, 'b', false)")
|
|
145
|
+
before = {
|
|
146
|
+
v: env.oid(v) for v in ("people_set", "people_adder", "people_picker")
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
env.declare(
|
|
150
|
+
BASE + PEOPLE_SET.replace("WHERE active", "WHERE NOT active") + DEPENDENTS
|
|
151
|
+
)
|
|
152
|
+
(change,) = env.plan_changes()
|
|
153
|
+
assert "CREATE OR REPLACE VIEW" in change.to_sql()
|
|
154
|
+
assert "DROP" not in change.to_sql()
|
|
155
|
+
env.apply()
|
|
156
|
+
|
|
157
|
+
# Replaced in place: no relation was dropped, so nothing lost its ACL.
|
|
158
|
+
assert {v: env.oid(v) for v in before} == before
|
|
159
|
+
assert _rows(env.url, "SELECT id, name FROM people_picker") == [(2, "b")]
|
|
160
|
+
for v in before:
|
|
161
|
+
assert env.can_select(v)
|
|
162
|
+
assert env.plan_changes() == []
|
|
163
|
+
|
|
164
|
+
def test_appended_column_and_options_replace_in_place(self, env):
|
|
165
|
+
require_server_version(env.url, 150000, "security_invoker views")
|
|
166
|
+
env.declare(BASE + PEOPLE_SET + DEPENDENTS)
|
|
167
|
+
env.apply()
|
|
168
|
+
before = env.oid("people_set")
|
|
169
|
+
|
|
170
|
+
env.declare(
|
|
171
|
+
BASE
|
|
172
|
+
+ PEOPLE_SET.replace(
|
|
173
|
+
"CREATE VIEW people_set AS SELECT id, name",
|
|
174
|
+
"CREATE VIEW people_set WITH (security_invoker = true)"
|
|
175
|
+
" AS SELECT id, name, active",
|
|
176
|
+
)
|
|
177
|
+
+ DEPENDENTS
|
|
178
|
+
)
|
|
179
|
+
(change,) = env.plan_changes()
|
|
180
|
+
assert isinstance(change, ReplaceView)
|
|
181
|
+
env.apply()
|
|
182
|
+
|
|
183
|
+
assert env.oid("people_set") == before
|
|
184
|
+
assert _rows(
|
|
185
|
+
env.url, "SELECT reloptions FROM pg_class WHERE relname = 'people_set'"
|
|
186
|
+
) == [(["security_invoker=true"],)]
|
|
187
|
+
assert env.plan_changes() == []
|
|
188
|
+
|
|
189
|
+
# A body change that drops the option clears it in the same statement.
|
|
190
|
+
env.declare(
|
|
191
|
+
BASE
|
|
192
|
+
+ PEOPLE_SET.replace("SELECT id, name", "SELECT id, name, active").replace(
|
|
193
|
+
"WHERE active", "WHERE NOT active"
|
|
194
|
+
)
|
|
195
|
+
+ DEPENDENTS
|
|
196
|
+
)
|
|
197
|
+
(change,) = env.plan_changes()
|
|
198
|
+
assert isinstance(change, ReplaceView)
|
|
199
|
+
env.apply()
|
|
200
|
+
assert env.oid("people_set") == before
|
|
201
|
+
assert _rows(
|
|
202
|
+
env.url, "SELECT reloptions FROM pg_class WHERE relname = 'people_set'"
|
|
203
|
+
) == [(None,)]
|
|
204
|
+
assert env.plan_changes() == []
|
|
205
|
+
|
|
206
|
+
def test_retyped_column_still_recreates_with_dependents(self, env):
|
|
207
|
+
env.declare(BASE + PEOPLE_SET + DEPENDENTS)
|
|
208
|
+
env.apply()
|
|
209
|
+
|
|
210
|
+
env.declare(
|
|
211
|
+
BASE
|
|
212
|
+
+ PEOPLE_SET.replace(
|
|
213
|
+
"SELECT id, name", "SELECT id, name::varchar(10) AS name"
|
|
214
|
+
)
|
|
215
|
+
+ DEPENDENTS
|
|
216
|
+
)
|
|
217
|
+
(change,) = env.plan_changes()
|
|
218
|
+
assert isinstance(change, DropAndCreateView)
|
|
219
|
+
env.apply()
|
|
220
|
+
|
|
221
|
+
for v in ("people_set", "people_adder", "people_picker"):
|
|
222
|
+
assert env.can_select(v)
|
|
223
|
+
assert env.plan_changes() == []
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
class TestViewReplaceable:
|
|
227
|
+
def _v(self, *columns: str) -> View:
|
|
228
|
+
return View(schema="public", name="v", definition="", columns=list(columns))
|
|
229
|
+
|
|
230
|
+
def test_same_or_appended_columns_qualify(self):
|
|
231
|
+
assert _view_replaceable(self._v("a int"), self._v("a int"))
|
|
232
|
+
assert _view_replaceable(self._v("a int"), self._v("a int", "b text"))
|
|
233
|
+
|
|
234
|
+
def test_renamed_retyped_reordered_or_removed_do_not(self):
|
|
235
|
+
current = self._v("a int", "b text")
|
|
236
|
+
assert not _view_replaceable(current, self._v("x int", "b text"))
|
|
237
|
+
assert not _view_replaceable(current, self._v("a bigint", "b text"))
|
|
238
|
+
assert not _view_replaceable(current, self._v("b text", "a int"))
|
|
239
|
+
assert not _view_replaceable(current, self._v("a int"))
|
|
240
|
+
assert not _view_replaceable(current, self._v("a int", 'b text COLLATE "C"'))
|
|
241
|
+
|
|
242
|
+
def test_unknown_columns_do_not(self):
|
|
243
|
+
assert not _view_replaceable(self._v(), self._v("a int"))
|
|
244
|
+
assert not _view_replaceable(self._v("a int"), self._v())
|
|
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
|