Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions pgdevkit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ def migrate_apply(
try:
targets = migrate.pending_migrations(migrations_dir, conninfo, tracking_table)
except migrate.TrackingTableMissing:
err_console.print(
f"[yellow]⚠[/yellow] {tracking_table} not found — treating every migration as pending"
)
targets = migrate.list_migration_files(migrations_dir)

if not targets:
Expand All @@ -324,6 +327,17 @@ def migrate_apply(
stop = threading.Event()
outcomes: list[tuple[str, str]] = []
bar = tqdm(total=len(targets), unit="migration", desc="Applying")
# tqdm isn't guaranteed thread-safe without an explicit lock, and both the worker
# thread and this (the main/--ask) thread call bar.update()/bar.write().
bar_lock = threading.Lock()

def bar_write(msg: str) -> None:
with bar_lock:
bar.write(msg)

def bar_step() -> None:
with bar_lock:
bar.update(1)

def worker() -> None:
nonlocal failure
Expand All @@ -334,18 +348,18 @@ def worker() -> None:
except Exception as e: # noqa: BLE001
failure = e
stop.set()
bar.write(f"FAILED {path.name}: {e}")
bar_write(f"FAILED {path.name}: {e}")
outcomes.append((path.name, "failed"))
else:
if result.executed:
for tbl in result.verified_tables:
bar.write(f" table {tbl} exists")
bar.write(f"Applied {path.name}")
bar_write(f" table {tbl} exists")
bar_write(f"Applied {path.name}")
outcomes.append((path.name, "applied"))
else:
bar.write(f"Recorded {path.name} as already applied (not executed)")
bar_write(f"Recorded {path.name} as already applied (not executed)")
outcomes.append((path.name, "recorded"))
bar.update(1)
bar_step()
work_q.task_done()

thread = threading.Thread(target=worker, daemon=True)
Expand All @@ -357,23 +371,23 @@ def worker() -> None:
break
already_done = False
if ask:
bar.write(f"\n=== {path.name} ===")
bar.write(path.read_text(encoding="utf-8"))
bar_write(f"\n=== {path.name} ===")
bar_write(path.read_text(encoding="utf-8"))
answer = typer.prompt("[Y]es execute / [n]o skip / [a]lready done / [q]uit", default="y").strip().lower()
if answer in ("q", "quit"):
quit_requested = True
break
if answer in ("n", "no"):
bar.write(f"Skipped {path.name}")
bar_write(f"Skipped {path.name}")
outcomes.append((path.name, "skipped"))
bar.update(1)
bar_step()
continue
if answer in ("a", "already", "already done"):
already_done = True
elif answer not in ("", "y", "yes"):
bar.write(f"Skipped {path.name}")
bar_write(f"Skipped {path.name}")
outcomes.append((path.name, "skipped"))
bar.update(1)
bar_step()
continue

work_q.put((path, already_done))
Expand Down
2 changes: 1 addition & 1 deletion pgdevkit/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _created_table_names(stmts: list[str]) -> list[str]:
names.extend(
m.group(1)
for m in re.finditer(
r"CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(\S+)", stripped, re.IGNORECASE
r'CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?([\w."]+)', stripped, re.IGNORECASE
)
)
return names
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ packages = ["pgdevkit"]

[project]
name = "pgdevkit"
version = "0.3.3"
version = "0.3.4"
description = "A helper for developing with Postgres"
readme = "README.md"
requires-python = ">=3.14"
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading