Stelle den ersten nutzbaren Ablauf zum Organisieren und Nachverfolgen bereit.
Deploy Development / deploy (push) Successful in 23s
Deploy Development / deploy (push) Successful in 23s
Anmeldung, Datenraeume, Akten, Vorgaenge, gemeinsame Notizen, Dateien, manuelle Paperless-Referenzen, Aufgaben und unabhaengige Fristen laufen damit in der Entwicklungsumgebung. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
cfe17ff909
commit
2558239bfd
60 files changed
+8467
-71
No files matched your search
@@ -0,0 +1,90 @@
|
||||
"""SQL migrations with a checksum ledger, then a runtime role without DDL."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
|
||||
import psycopg
|
||||
from psycopg import ClientCursor, sql
|
||||
|
||||
from app.core.settings import Settings, get_settings
|
||||
|
||||
MIGRATIONS = Path(__file__).resolve().parent.parent.parent / "migrations"
|
||||
|
||||
|
||||
def _checksum(path: Path) -> str:
|
||||
return hashlib.sha256(path.read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def migrate(settings: Settings | None = None) -> list[str]:
|
||||
settings = settings or get_settings()
|
||||
applied: list[str] = []
|
||||
with psycopg.connect(settings.dsn(), autocommit=True, cursor_factory=ClientCursor) as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version text PRIMARY KEY,
|
||||
checksum text NOT NULL,
|
||||
applied_at timestamptz NOT NULL DEFAULT now()
|
||||
)
|
||||
"""
|
||||
)
|
||||
for path in sorted(MIGRATIONS.glob("*.sql")):
|
||||
version = path.name
|
||||
checksum = _checksum(path)
|
||||
row = conn.execute(
|
||||
"SELECT checksum FROM schema_migrations WHERE version = %s",
|
||||
(version,),
|
||||
).fetchone()
|
||||
if row is not None:
|
||||
if row[0] != checksum:
|
||||
raise RuntimeError(f"checksum mismatch for migration {version}")
|
||||
continue
|
||||
with conn.transaction():
|
||||
conn.execute(path.read_text(encoding="utf-8"))
|
||||
conn.execute(
|
||||
"INSERT INTO schema_migrations (version, checksum) VALUES (%s, %s)",
|
||||
(version, checksum),
|
||||
)
|
||||
applied.append(version)
|
||||
_ensure_runtime_role(conn, settings)
|
||||
return applied
|
||||
|
||||
|
||||
def _ensure_runtime_role(conn: psycopg.Connection, settings: Settings) -> None:
|
||||
role = sql.Identifier(settings.db_runtime_user)
|
||||
database = sql.Identifier(settings.db_name)
|
||||
owner = sql.Identifier(settings.db_user)
|
||||
password = sql.Literal(settings.db_password)
|
||||
exists = conn.execute(
|
||||
"SELECT 1 FROM pg_roles WHERE rolname = %s",
|
||||
(settings.db_runtime_user,),
|
||||
).fetchone()
|
||||
if exists is None:
|
||||
conn.execute(
|
||||
sql.SQL(
|
||||
"CREATE ROLE {} LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT PASSWORD {}"
|
||||
).format(role, password)
|
||||
)
|
||||
else:
|
||||
conn.execute(
|
||||
sql.SQL(
|
||||
"ALTER ROLE {} WITH LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT PASSWORD {}"
|
||||
).format(role, password)
|
||||
)
|
||||
conn.execute(sql.SQL("GRANT CONNECT ON DATABASE {} TO {}").format(database, role))
|
||||
conn.execute(sql.SQL("GRANT USAGE ON SCHEMA public TO {}").format(role))
|
||||
conn.execute(sql.SQL("REVOKE CREATE ON SCHEMA public FROM {}").format(role))
|
||||
conn.execute(
|
||||
sql.SQL(
|
||||
"GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO {}"
|
||||
).format(role)
|
||||
)
|
||||
conn.execute(sql.SQL("REVOKE ALL ON TABLE schema_migrations FROM {}").format(role))
|
||||
conn.execute(
|
||||
sql.SQL(
|
||||
"ALTER DEFAULT PRIVILEGES FOR ROLE {} IN SCHEMA public "
|
||||
"GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO {}"
|
||||
).format(owner, role)
|
||||
)
|
||||
Reference in new issue
Block a user