pgdevkit 0.3.4__py3-none-any.whl → 0.3.6__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.
- pgdevkit/cli.py +30 -18
- pgdevkit/migrate.py +77 -0
- {pgdevkit-0.3.4.dist-info → pgdevkit-0.3.6.dist-info}/METADATA +1 -1
- {pgdevkit-0.3.4.dist-info → pgdevkit-0.3.6.dist-info}/RECORD +6 -6
- {pgdevkit-0.3.4.dist-info → pgdevkit-0.3.6.dist-info}/WHEEL +0 -0
- {pgdevkit-0.3.4.dist-info → pgdevkit-0.3.6.dist-info}/entry_points.txt +0 -0
pgdevkit/cli.py
CHANGED
|
@@ -371,24 +371,36 @@ def migrate_apply(
|
|
|
371
371
|
break
|
|
372
372
|
already_done = False
|
|
373
373
|
if ask:
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
374
|
+
already_done = migrate.already_fully_applied(conninfo, path)
|
|
375
|
+
if already_done:
|
|
376
|
+
bar_write(f"Auto: {path.name} is already fully present in the database — marking as already done")
|
|
377
|
+
else:
|
|
378
|
+
# Hold the lock across the whole show-file+prompt step (not just each write) so
|
|
379
|
+
# the worker's bar_write() calls block until the question is answered instead
|
|
380
|
+
# of interleaving with the prompt or the migration text while it's being read.
|
|
381
|
+
with bar_lock:
|
|
382
|
+
bar.write(f"\n=== {path.name} ===")
|
|
383
|
+
bar.write(path.read_text(encoding="utf-8"))
|
|
384
|
+
answer = (
|
|
385
|
+
typer.prompt("[Y]es execute / [n]o skip / [a]lready done / [q]uit", default="y")
|
|
386
|
+
.strip()
|
|
387
|
+
.lower()
|
|
388
|
+
)
|
|
389
|
+
if answer in ("q", "quit"):
|
|
390
|
+
quit_requested = True
|
|
391
|
+
break
|
|
392
|
+
if answer in ("n", "no"):
|
|
393
|
+
bar_write(f"Skipped {path.name}")
|
|
394
|
+
outcomes.append((path.name, "skipped"))
|
|
395
|
+
bar_step()
|
|
396
|
+
continue
|
|
397
|
+
if answer in ("a", "already", "already done"):
|
|
398
|
+
already_done = True
|
|
399
|
+
elif answer not in ("", "y", "yes"):
|
|
400
|
+
bar_write(f"Skipped {path.name}")
|
|
401
|
+
outcomes.append((path.name, "skipped"))
|
|
402
|
+
bar_step()
|
|
403
|
+
continue
|
|
392
404
|
|
|
393
405
|
work_q.put((path, already_done))
|
|
394
406
|
|
pgdevkit/migrate.py
CHANGED
|
@@ -197,6 +197,83 @@ def _created_table_names(stmts: list[str]) -> list[str]:
|
|
|
197
197
|
return names
|
|
198
198
|
|
|
199
199
|
|
|
200
|
+
_ADD_COLUMN_RE = re.compile(
|
|
201
|
+
rf"ALTER\s+TABLE\s+(?:IF\s+EXISTS\s+)?([\w.\"]+)\s+ADD\s+COLUMN\s+"
|
|
202
|
+
rf"(?:IF\s+NOT\s+EXISTS\s+)?\"?({_IDENTIFIER})\"?",
|
|
203
|
+
re.IGNORECASE,
|
|
204
|
+
)
|
|
205
|
+
_CREATE_RELATION_RE = re.compile(
|
|
206
|
+
r"CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+NOT\s+EXISTS\s+)?([\w.\"]+)"
|
|
207
|
+
r"|CREATE\s+SEQUENCE\s+(?:IF\s+NOT\s+EXISTS\s+)?([\w.\"]+)"
|
|
208
|
+
r"|CREATE\s+(?:MATERIALIZED\s+)?VIEW\s+(?:IF\s+NOT\s+EXISTS\s+)?([\w.\"]+)",
|
|
209
|
+
re.IGNORECASE,
|
|
210
|
+
)
|
|
211
|
+
_CREATE_SCHEMA_RE = re.compile(r"CREATE\s+SCHEMA\s+(?:IF\s+NOT\s+EXISTS\s+)?([\w\"]+)", re.IGNORECASE)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _idempotent_target(stmt: str) -> tuple[str, ...] | None:
|
|
215
|
+
"""Classify a single statement as one of the create-if-missing DDL shapes this module
|
|
216
|
+
can check for "already applied" with no ambiguity: ("relation", name) for a table,
|
|
217
|
+
index, sequence or view; ("schema", name); or ("column", table, column) for an ADD
|
|
218
|
+
COLUMN. None if the statement isn't one of these shapes — including any CREATE OR
|
|
219
|
+
REPLACE, which is never safe to treat as a no-op just because the object exists, since
|
|
220
|
+
the migration could be replacing it with different content."""
|
|
221
|
+
stripped = _strip_line_comments(stmt).strip()
|
|
222
|
+
if re.search(r"\bOR\s+REPLACE\b", stripped, re.IGNORECASE):
|
|
223
|
+
return None
|
|
224
|
+
|
|
225
|
+
if re.match(r"CREATE\s+TABLE\b", stripped, re.IGNORECASE):
|
|
226
|
+
names = _created_table_names([stmt])
|
|
227
|
+
return ("relation", names[0]) if names else None
|
|
228
|
+
|
|
229
|
+
m = _CREATE_RELATION_RE.match(stripped)
|
|
230
|
+
if m:
|
|
231
|
+
name = next(g for g in m.groups() if g is not None)
|
|
232
|
+
return ("relation", name)
|
|
233
|
+
|
|
234
|
+
m = _CREATE_SCHEMA_RE.match(stripped)
|
|
235
|
+
if m:
|
|
236
|
+
return ("schema", m.group(1))
|
|
237
|
+
|
|
238
|
+
m = _ADD_COLUMN_RE.match(stripped)
|
|
239
|
+
if m:
|
|
240
|
+
return ("column", m.group(1), m.group(2))
|
|
241
|
+
|
|
242
|
+
return None
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _target_exists(con: psycopg.Connection, target: tuple[str, ...]) -> bool:
|
|
246
|
+
kind = target[0]
|
|
247
|
+
if kind == "relation":
|
|
248
|
+
row = con.execute("select to_regclass(%s)", (target[1],)).fetchone()
|
|
249
|
+
elif kind == "schema":
|
|
250
|
+
row = con.execute("select to_regnamespace(%s)", (target[1],)).fetchone()
|
|
251
|
+
else: # column
|
|
252
|
+
row = con.execute(
|
|
253
|
+
"select 1 from pg_attribute where attrelid = to_regclass(%s) "
|
|
254
|
+
"and attname = %s and not attisdropped",
|
|
255
|
+
(target[1], target[2]),
|
|
256
|
+
).fetchone()
|
|
257
|
+
return bool(row and row[0])
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def already_fully_applied(conninfo: str, path: Path) -> bool:
|
|
261
|
+
"""Whether every statement in this migration is a recognized create-if-missing shape
|
|
262
|
+
(table/index/sequence/view/schema, or add-column) AND its target already exists in the
|
|
263
|
+
database — i.e. re-running the migration would do nothing. Used by `--ask` to
|
|
264
|
+
auto-answer "already done" without prompting, so migrations that are trivially no-ops
|
|
265
|
+
don't interrupt review. A single unrecognized or not-yet-applied statement means
|
|
266
|
+
False — this never guesses."""
|
|
267
|
+
stmts = _split_sql(path.read_text(encoding="utf-8"))
|
|
268
|
+
if not stmts:
|
|
269
|
+
return False
|
|
270
|
+
targets = [_idempotent_target(s) for s in stmts]
|
|
271
|
+
if any(t is None for t in targets):
|
|
272
|
+
return False
|
|
273
|
+
with psycopg.connect(conninfo) as con:
|
|
274
|
+
return all(_target_exists(con, cast(tuple[str, ...], t)) for t in targets)
|
|
275
|
+
|
|
276
|
+
|
|
200
277
|
def list_migration_files(migrations_dir: Path) -> list[Path]:
|
|
201
278
|
return sorted(migrations_dir.glob("*.sql"))
|
|
202
279
|
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
pgdevkit/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
pgdevkit/cli.py,sha256=
|
|
2
|
+
pgdevkit/cli.py,sha256=xP4fZB0Dg3wivhb00DcP2w2Vnp8BRyaR2o1wD7jopFc,17574
|
|
3
3
|
pgdevkit/connection.py,sha256=x49aXKsJfyOsDTlqURs9idQUWaD-RO-XdtAqsG3guBI,4000
|
|
4
4
|
pgdevkit/dialect.py,sha256=PL5Y8QPZNnoHIvAPomczGQX7IjZwxMgKGuREX80SwFA,3105
|
|
5
5
|
pgdevkit/diff.py,sha256=Y2TbjwpFHYik0wZDhoI--9EAlIfp4AUg8BUubkOXyUg,9295
|
|
6
6
|
pgdevkit/fetch_missing.py,sha256=1-2NBDCQNtirywO9Ucm775UKSACfAzcnQlp-4Jh2weE,8136
|
|
7
7
|
pgdevkit/introspect.py,sha256=QYIlRuZk4lBHGxaW7zb_kFRIuNfi3AxH0Q2F_GKzlVI,7870
|
|
8
8
|
pgdevkit/lakebase.py,sha256=0PABOk4p-2klLTY8QAUibQf9QeyNHKywUWEWGdkUY8k,3165
|
|
9
|
-
pgdevkit/migrate.py,sha256=
|
|
9
|
+
pgdevkit/migrate.py,sha256=pU7xVssJiAHfFFU60DP2cwpjQ1Tf8Hscc_Q6wIqeSfM,13677
|
|
10
10
|
pgdevkit/models.py,sha256=dAZ8f7pgDsxoLVrDWsM4h0qwJ0pOzXDdWMGsnjrW80E,2103
|
|
11
11
|
pgdevkit/mssql_introspect.py,sha256=Fhv1N4zHYEDZ0PEy0OIpawUmoIFeoi-Lm97pmEyDmYY,12031
|
|
12
12
|
pgdevkit/parser.py,sha256=X1P8RbogRhsWc-H9w3IPlDo2blKpI33cJTvDzOXze1Y,15367
|
|
@@ -39,7 +39,7 @@ pgdevkit/docs/database-layout.md,sha256=4GBwv4PA2cKHl2BxwLd-awLOe9he9ie4X6CpQWjI
|
|
|
39
39
|
pgdevkit/skills/pgdevkit/SKILL.md,sha256=6uxe6BQwOLexGiBpf00VTFTHOiWJo5k2Qqzu172rb4I,12156
|
|
40
40
|
pgdevkit/skills/pgdevkit/references/dynamic-sql.md,sha256=aTR1sx81moiTwaMaiOecaqk-9rT8y5Zxz7W1BoC3vK0,2774
|
|
41
41
|
pgdevkit/skills/pgdevkit/references/temporal-tables.md,sha256=LwZnItvQWI7bB0oRHxIfUc0ynnq7N0V5Mnv-QHvFN3Q,1364
|
|
42
|
-
pgdevkit-0.3.
|
|
43
|
-
pgdevkit-0.3.
|
|
44
|
-
pgdevkit-0.3.
|
|
45
|
-
pgdevkit-0.3.
|
|
42
|
+
pgdevkit-0.3.6.dist-info/METADATA,sha256=DxmlfQZ0M5gVVrHhHHz-748Eu6-leFPurFjplxOZyQU,11346
|
|
43
|
+
pgdevkit-0.3.6.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
|
|
44
|
+
pgdevkit-0.3.6.dist-info/entry_points.txt,sha256=FK2P55lf0WaGlOZJIINOGgCt8WB7ep0OnPsgnrp80bg,42
|
|
45
|
+
pgdevkit-0.3.6.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|