sqlpush 0.4.1__tar.gz → 0.4.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlpush
3
- Version: 0.4.1
3
+ Version: 0.4.2
4
4
  Summary: Schema lifecycle tool for production PostgreSQL/TimescaleDB on SQLAlchemy 2.0 (SQLModel included): diff, push and check schema drift straight from your models
5
5
  Keywords: sqlalchemy,alembic,postgresql,timescaledb,prisma,schema,migrations,database,drift,cli
6
6
  Author: Juan Miguel Contreras
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sqlpush"
3
- version = "0.4.1"
3
+ version = "0.4.2"
4
4
  description = "Schema lifecycle tool for production PostgreSQL/TimescaleDB on SQLAlchemy 2.0 (SQLModel included): diff, push and check schema drift straight from your models"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sqlpush"
3
- version = "0.4.1"
3
+ version = "0.4.2"
4
4
  description = "Schema lifecycle tool for production PostgreSQL/TimescaleDB on SQLAlchemy 2.0 (SQLModel included): diff, push and check schema drift straight from your models"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -139,7 +139,12 @@ def revision(
139
139
  risk = max((op.risk for op in p.operations), key=lambda r: RISK_RANK[r])
140
140
  ops = [(f"[{op.risk.name}] {op.type} {op.table or '?'}", op.sql) for op in p.operations]
141
141
  out = Path(out_dir)
142
- out.mkdir(parents=True, exist_ok=True)
142
+ try:
143
+ out.mkdir(parents=True, exist_ok=True)
144
+ except OSError as exc:
145
+ # same typing as the write below: only SqlpushError subclasses
146
+ # escape the API surface (e.g. out_dir under a file)
147
+ raise SqlpushError(f"cannot create migrations directory {out}: {exc}") from exc
143
148
  rev_id = next_revision_id(out)
144
149
  slug = re.sub(r"[^a-z0-9_]+", "_", (message or "migration").lower())[:40]
145
150
  path = out / f"{rev_id}_{slug}.sql"
@@ -163,16 +168,32 @@ def revision(
163
168
  return path
164
169
 
165
170
 
166
- def migrate(target, *, chain_dir="migrations/versions", allow_destructive=False) -> MigrateReport:
171
+ def migrate(
172
+ target,
173
+ *,
174
+ chain_dir="migrations/versions",
175
+ allow_destructive=False,
176
+ advisory_wait=30.0,
177
+ lock_timeout=5.0,
178
+ ) -> MigrateReport:
167
179
  """Replay annotated-SQL chain files with gates + same-txn bookkeeping.
168
180
 
169
181
  ``target`` is a DSN string, sync ``Engine`` or ``AsyncEngine`` (resolved
170
- via ``_sync_engine_from``; engines created here are disposed). See
182
+ via ``_sync_engine_from``; engines created here are disposed).
183
+ ``advisory_wait`` bounds the advisory-lock wait and ``lock_timeout``
184
+ bounds each per-file transaction's lock wait (seconds; 0 = fail
185
+ immediately — same contract as ``push``). See
171
186
  ``chain.migrate.run_migrate`` for the execution contract.
172
187
  """
173
188
  engine, dispose = _sync_engine_from(target)
174
189
  try:
175
- return run_migrate(engine, chain_dir=chain_dir, allow_destructive=allow_destructive)
190
+ return run_migrate(
191
+ engine,
192
+ chain_dir=chain_dir,
193
+ allow_destructive=allow_destructive,
194
+ advisory_wait=advisory_wait,
195
+ lock_timeout=lock_timeout,
196
+ )
176
197
  except SQLAlchemyError as exc:
177
198
  # MigrationFileError/SqlpushError (typed) pass through untouched
178
199
  _raise_typed(exc)
@@ -181,16 +202,19 @@ def migrate(target, *, chain_dir="migrations/versions", allow_destructive=False)
181
202
  engine.dispose()
182
203
 
183
204
 
184
- def stamp(target, *, chain_dir="migrations/versions") -> MigrateReport:
205
+ def stamp(target, *, chain_dir="migrations/versions", force: bool = False) -> MigrateReport:
185
206
  """Bootstrap: register chain files as applied WITHOUT executing SQL.
186
207
 
187
- For adopting a DB whose schema already reflects the chain. ``target``
188
- resolution and error typing match :func:`migrate`. See
189
- ``chain.migrate.run_stamp`` for the report convention.
208
+ For adopting a DB whose schema already reflects the chain. A file
209
+ already registered with a different checksum (edited after apply)
210
+ is refused unless ``force`` is set — edit detection survives
211
+ re-stamping. ``target`` resolution and error typing match
212
+ :func:`migrate`. See ``chain.migrate.run_stamp`` for the report
213
+ convention.
190
214
  """
191
215
  engine, dispose = _sync_engine_from(target)
192
216
  try:
193
- return run_stamp(engine, chain_dir=chain_dir)
217
+ return run_stamp(engine, chain_dir=chain_dir, force=force)
194
218
  except SQLAlchemyError as exc:
195
219
  # MigrationFileError/SqlpushError (typed) pass through untouched
196
220
  _raise_typed(exc)
@@ -199,18 +223,35 @@ def stamp(target, *, chain_dir="migrations/versions") -> MigrateReport:
199
223
  engine.dispose()
200
224
 
201
225
 
226
+ def _translate_asyncpg(url):
227
+ """Map a ``postgresql+asyncpg`` URL onto the sync psycopg driver.
228
+
229
+ An asyncpg URL cannot back a SYNC engine (the dialect is
230
+ async-only), but psycopg is a runtime dependency — the translated
231
+ engine actually connects. asyncpg itself is never required in this
232
+ process. Everything but the driver (host/db/credentials/query
233
+ options) is preserved via the URL API, no string surgery.
234
+ """
235
+ if url.drivername == "postgresql+asyncpg":
236
+ return url.set(drivername="postgresql+psycopg")
237
+ return url
238
+
239
+
202
240
  def _sync_engine_from(target):
203
241
  if isinstance(target, AsyncEngine):
204
242
  from sqlalchemy import create_engine
205
243
  from sqlalchemy.pool import NullPool
206
244
 
207
- dsn = target.url.render_as_string(hide_password=False)
208
- return create_engine(dsn, poolclass=NullPool), True
245
+ dsn = _translate_asyncpg(target.url)
246
+ return create_engine(dsn.render_as_string(hide_password=False), poolclass=NullPool), True
209
247
  if isinstance(target, str):
210
248
  from sqlalchemy import create_engine
249
+ from sqlalchemy.engine import make_url
211
250
  from sqlalchemy.pool import NullPool
212
251
 
213
- return create_engine(target, poolclass=NullPool), True
252
+ # make_url raises the same ArgumentError a bad DSN would raise
253
+ # inside create_engine — error typing downstream is unchanged
254
+ return create_engine(_translate_asyncpg(make_url(target)), poolclass=NullPool), True
214
255
  return target, False
215
256
 
216
257
 
@@ -11,13 +11,15 @@ let a crash between apply and registry re-apply the file forever
11
11
 
12
12
  Fail-loud ordering: any blocked file (parse error, checksum mismatch,
13
13
  destructive gate, SQL failure) stops the chain — nothing later runs (R4).
14
- A hung migrate (stuck waiting on the advisory lock) is diagnosed via
15
- pg_locks / pg_stat_activity.
14
+ The advisory lock wait is BOUNDED (--advisory-wait, default 30s): an
15
+ exhausted budget raises a typed error instead of hanging on a stuck
16
+ holder — diagnose the holder via pg_locks / pg_stat_activity.
16
17
  """
17
18
 
18
19
  from __future__ import annotations
19
20
 
20
21
  import contextlib
22
+ import time
21
23
  from collections.abc import Iterator
22
24
  from pathlib import Path
23
25
 
@@ -26,7 +28,7 @@ from sqlalchemy.engine import Connection, Engine
26
28
 
27
29
  from sqlpush.apply.executor import advisory_key
28
30
  from sqlpush.chain.format import MigrationFileError, checksum, parse_migration_file
29
- from sqlpush.types import MigrateReport, RiskClass
31
+ from sqlpush.types import MigrateReport, RiskClass, SqlpushError
30
32
 
31
33
  _VERSIONS_DDL = (
32
34
  "CREATE TABLE IF NOT EXISTS public.sqlpush_versions ("
@@ -45,18 +47,33 @@ def _chain_files(chain_dir: str | Path) -> list[Path]:
45
47
 
46
48
 
47
49
  @contextlib.contextmanager
48
- def _chain_session(engine: Engine) -> Iterator[Connection]:
50
+ def _chain_session(engine: Engine, *, advisory_wait: float = 30.0) -> Iterator[Connection]:
49
51
  """Session-scoped advisory lock + versions table, shared by every verb.
50
52
 
51
53
  Same key derivation as push (fnv1a_32(b"sqlpush") ^ db oid): serializes
52
54
  concurrent chain workers and excludes push on the same database. The
53
- txn opened by the key query is committed right away — session advisory
54
- locks survive COMMIT/ROLLBACK, and an idle-in-transaction session is
55
- exposed to idle_in_transaction_session_timeout (executor.py note).
55
+ wait is BOUNDED, mirroring executor.with_advisory_lock:
56
+ ``pg_try_advisory_lock`` polled every 0.5 s against a
57
+ ``time.monotonic()`` deadline; an exhausted budget raises
58
+ :class:`SqlpushError` — a blocking ``pg_advisory_lock`` would hang
59
+ forever on a stuck holder. The txn opened by the key/probe queries
60
+ is committed right away — session advisory locks survive
61
+ COMMIT/ROLLBACK, and an idle-in-transaction session is exposed to
62
+ idle_in_transaction_session_timeout (executor.py note).
56
63
  """
64
+ if advisory_wait < 0:
65
+ raise SqlpushError(f"advisory_wait must be >= 0, got {advisory_wait}")
57
66
  with engine.connect() as conn:
58
67
  key = advisory_key(conn)
59
- conn.execute(text("SELECT pg_advisory_lock(:k)"), {"k": key})
68
+ deadline = time.monotonic() + advisory_wait
69
+ locked = conn.execute(text("SELECT pg_try_advisory_lock(:k)"), {"k": key}).scalar()
70
+ while not locked and time.monotonic() < deadline:
71
+ time.sleep(0.5)
72
+ locked = conn.execute(text("SELECT pg_try_advisory_lock(:k)"), {"k": key}).scalar()
73
+ if not locked:
74
+ raise SqlpushError(
75
+ f"another sqlpush worker holds the advisory lock after {advisory_wait}s"
76
+ )
60
77
  conn.commit()
61
78
  try:
62
79
  conn.execute(text(_VERSIONS_DDL))
@@ -73,14 +90,26 @@ def _chain_session(engine: Engine) -> Iterator[Connection]:
73
90
  conn.commit()
74
91
 
75
92
 
76
- def run_migrate(engine: Engine, *, chain_dir: str | Path, allow_destructive: bool) -> MigrateReport:
93
+ def run_migrate(
94
+ engine: Engine,
95
+ *,
96
+ chain_dir: str | Path,
97
+ allow_destructive: bool,
98
+ advisory_wait: float = 30.0,
99
+ lock_timeout: float = 5.0,
100
+ ) -> MigrateReport:
101
+ if lock_timeout < 0:
102
+ # same contract as push (executor.with_advisory_lock): budgets
103
+ # are typed floats, never user input, and a negative one must
104
+ # fail before any file or connection work
105
+ raise SqlpushError(f"lock_timeout must be >= 0, got {lock_timeout}")
77
106
  applied: list[str] = []
78
107
  skipped: list[str] = []
79
108
  blocked: list[str] = []
80
109
  notes: list[str] = []
81
110
  partial = False
82
111
  chain = _chain_files(chain_dir)
83
- with _chain_session(engine) as conn:
112
+ with _chain_session(engine, advisory_wait=advisory_wait) as conn:
84
113
  recorded = {
85
114
  row[0]: row[1]
86
115
  for row in conn.execute(text("SELECT name, sha256 FROM public.sqlpush_versions"))
@@ -107,6 +136,14 @@ def run_migrate(engine: Engine, *, chain_dir: str | Path, allow_destructive: boo
107
136
  break
108
137
  try:
109
138
  with conn.begin():
139
+ # txn-scoped: SET LOCAL dies with the per-file txn
140
+ # (style: push's transactional segment, executor.py).
141
+ # NOTE: PostgreSQL does not accept bind parameters for
142
+ # SET (utility statement), so the int is inlined;
143
+ # lock_timeout is a typed float parameter, not user
144
+ # input. A chain file blocked behind another
145
+ # transaction's lock fails fast instead of queuing.
146
+ conn.execute(text(f"SET LOCAL lock_timeout = {int(lock_timeout * 1000)}"))
110
147
  # whole-file replay: exec_driver_sql bypasses text()'s
111
148
  # bind-param parsing entirely — ":casts" and ":=" in
112
149
  # hand-edited SQL must reach the server verbatim
@@ -130,14 +167,18 @@ def run_migrate(engine: Engine, *, chain_dir: str | Path, allow_destructive: boo
130
167
  )
131
168
 
132
169
 
133
- def run_stamp(engine: Engine, *, chain_dir: str | Path) -> MigrateReport:
170
+ def run_stamp(engine: Engine, *, chain_dir: str | Path, force: bool = False) -> MigrateReport:
134
171
  """Register every parseable chain file WITHOUT executing any SQL.
135
172
 
136
173
  Bootstrap seam (spec §5 R7): adopt a DB whose schema already reflects
137
174
  the chain. Only the header must parse (fail-loud on THAT) — invalid SQL
138
- in a body still registers, because stamp never executes anything. Each
139
- registration is an idempotent upsert (``ON CONFLICT (name) DO UPDATE``),
140
- so re-stamping refreshes checksums instead of failing.
175
+ in a body still registers, because stamp never executes anything. A
176
+ file already recorded with a DIFFERENT checksum is refused — it was
177
+ edited after apply/stamp, and silently refreshing would wipe the
178
+ edit-detection integrity — unless ``force`` is set; the first mismatch
179
+ raises and nothing after it registers (strict order, same as migrate).
180
+ Unrecorded files, unchanged re-stamps and forced re-stamps upsert
181
+ idempotently (``ON CONFLICT (name) DO UPDATE``).
141
182
 
142
183
  Report convention: registered files are listed in ``skipped`` (stamp
143
184
  never populates ``applied`` and never sets ``partial_failure``); a
@@ -149,6 +190,11 @@ def run_stamp(engine: Engine, *, chain_dir: str | Path) -> MigrateReport:
149
190
  notes: list[str] = []
150
191
  chain = _chain_files(chain_dir)
151
192
  with _chain_session(engine) as conn:
193
+ recorded = {
194
+ row[0]: row[1]
195
+ for row in conn.execute(text("SELECT name, sha256 FROM public.sqlpush_versions"))
196
+ }
197
+ conn.commit()
152
198
  for f in chain:
153
199
  raw = f.read_text()
154
200
  try:
@@ -157,6 +203,11 @@ def run_stamp(engine: Engine, *, chain_dir: str | Path) -> MigrateReport:
157
203
  blocked.append(f.name)
158
204
  notes.append(f"{f.name}: {exc}")
159
205
  break # orden estricto: nada posterior se registra
206
+ if f.name in recorded and recorded[f.name] != checksum(raw) and not force:
207
+ raise SqlpushError(
208
+ f"{f.name}: checksum mismatch (file edited after apply?); "
209
+ "pass force=True/--force to accept the new content"
210
+ )
160
211
  with conn.begin():
161
212
  conn.execute(
162
213
  text(
@@ -4,7 +4,8 @@
4
4
  Exit codes: diff always 0; check 0 clean / 2 drift / 3 destructive drift;
5
5
  push 0 applied / 1 destructive blocked / 2 error (incl. partial failure);
6
6
  revision 0 written / 1 error (empty drift refuses); migrate 0 clean /
7
- 1 blocked or partial failure.
7
+ 1 blocked or partial failure; stamp 0 registered / 1 blocked or refused
8
+ (an edited file without --force raises a typed error: exit 1).
8
9
  """
9
10
 
10
11
  from __future__ import annotations
@@ -250,12 +251,20 @@ def revision(
250
251
  def migrate(
251
252
  dsn: str | None = typer.Option(None),
252
253
  allow_destructive: bool = typer.Option(False, "--allow-destructive"),
254
+ advisory_wait: float = typer.Option(30.0, "--advisory-wait"),
255
+ lock_timeout: float = typer.Option(5.0, "--lock-timeout"),
253
256
  out_dir: DirOpt = Path("migrations/versions"),
254
257
  ):
255
258
  """Replay pending migration files (gates + checksum bookkeeping)."""
256
259
  engine = _engine(dsn)
257
260
  try:
258
- report = api.migrate(engine, chain_dir=out_dir, allow_destructive=allow_destructive)
261
+ report = api.migrate(
262
+ engine,
263
+ chain_dir=out_dir,
264
+ allow_destructive=allow_destructive,
265
+ advisory_wait=advisory_wait,
266
+ lock_timeout=lock_timeout,
267
+ )
259
268
  finally:
260
269
  engine.dispose()
261
270
  typer.echo(
@@ -272,12 +281,13 @@ def migrate(
272
281
  @app.command()
273
282
  def stamp(
274
283
  dsn: str | None = typer.Option(None),
284
+ force: bool = typer.Option(False, "--force"),
275
285
  out_dir: DirOpt = Path("migrations/versions"),
276
286
  ):
277
287
  """Adopt an existing DB: register chain files without executing SQL."""
278
288
  engine = _engine(dsn)
279
289
  try:
280
- report = api.stamp(engine, chain_dir=out_dir)
290
+ report = api.stamp(engine, chain_dir=out_dir, force=force)
281
291
  finally:
282
292
  engine.dispose()
283
293
  typer.echo(
@@ -405,7 +405,9 @@ def _dedup_enum_types(ops: list[PlannedOperation]) -> list[PlannedOperation]:
405
405
  copy stays embedded in its original add_table render. A metadata
406
406
  that defines one type name two DIFFERENT ways keeps both statements
407
407
  and still fails loudly at apply time: silent first-win would mask a
408
- genuine contradiction.
408
+ genuine contradiction. Ops whose statements all survive pass
409
+ through as the ORIGINAL object (identity) — a re-joined copy of
410
+ unchanged SQL buys nothing.
409
411
  """
410
412
  seen: set[str] = set()
411
413
 
@@ -420,10 +422,12 @@ def _dedup_enum_types(ops: list[PlannedOperation]) -> list[PlannedOperation]:
420
422
  continue
421
423
  stmts = _split_statements(op.sql)
422
424
  kept: list[str] = []
425
+ dropped = False
423
426
  for stmt in stmts:
424
427
  norm = " ".join(stmt.split())
425
428
  if _is_type_stmt(norm):
426
429
  if norm in seen:
430
+ dropped = True
427
431
  continue
428
432
  seen.add(norm)
429
433
  if norm:
@@ -433,6 +437,9 @@ def _dedup_enum_types(ops: list[PlannedOperation]) -> list[PlannedOperation]:
433
437
  # survives); guard anyway — an empty op.sql is worse than
434
438
  # the duplicate it replaced
435
439
  continue
440
+ if not dropped:
441
+ out.append(op)
442
+ continue
436
443
  out.append(
437
444
  PlannedOperation(
438
445
  type=op.type,
File without changes
File without changes
File without changes