All checks were successful
Build and deploy / Validate source (push) Successful in 7s
Build and deploy / Integration suite on a real stack (push) Successful in 1m25s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images and notify Portainer (push) Successful in 1m31s
local/ held six unrelated things under a name that stopped being true once it
became the production runtime: the service, the frontend, the tests, the ops
commands, the container definitions and the dependency lock, 65 files with
nothing to tell them apart.
app/ the service: api/ routers, core/ for identity, database, models,
prices and secret loading, and the worker, bootstrap and schema
tests/ the twelve suites, no longer inside the shipped package
ops/ backup, readiness, dependency audit, security summary
infra/ Dockerfiles, gateway templates, ClamAV and storage configuration,
the requirements and their hash lock
web/ the Site, Kanban and portal pages with their scripts
deploy/Dockerfile.api now copies app/ alone, so the tests stop shipping to
production; the local image still carries them, because the suites run inside
the stack's network.
Five kinds of reference had to follow, and each was found by something different
rather than by reading. Imports of the form "from . import db" survived a rewrite
that only matched "from .db import". Tests kept relative imports of modules that
had left the package. A mock.patch target names its module in a string, where no
import rewriting can see it. The browser test resolves a fixture by path. And the
release gate's markers pointed at local/runtime.py and local/worker.py, which is
the decay its new marker test exists to catch — it caught it.
Verified from docker compose down -v: the stack starts, all six integration
suites, both browser suites and the twenty-nine unit tests pass.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
59 lines
2.8 KiB
Python
59 lines
2.8 KiB
Python
"""One-shot schema/role setup. Only this job receives database admin credentials."""
|
|
import os
|
|
from pathlib import Path
|
|
from urllib.parse import urlparse
|
|
import psycopg
|
|
from psycopg import sql
|
|
from .core.secrets import load as load_secret_files
|
|
from .operators import seed_from_environment
|
|
|
|
|
|
def admin_connect():
|
|
if os.environ.get('DATABASE_ADMIN_HOST'):
|
|
return psycopg.connect(
|
|
host=os.environ['DATABASE_ADMIN_HOST'],
|
|
dbname=os.environ['DATABASE_ADMIN_NAME'],
|
|
user=os.environ['DATABASE_ADMIN_USER'],
|
|
password=os.environ['DATABASE_ADMIN_PASSWORD'],
|
|
)
|
|
return psycopg.connect(os.environ['DATABASE_ADMIN_URL'])
|
|
|
|
|
|
def admin_password():
|
|
if os.environ.get('DATABASE_ADMIN_HOST'):
|
|
return os.environ.get('DATABASE_ADMIN_PASSWORD')
|
|
url = os.environ.get('DATABASE_ADMIN_URL', '')
|
|
return urlparse(url).password
|
|
|
|
|
|
def main():
|
|
load_secret_files()
|
|
role = os.environ['APP_DB_USER']
|
|
password = os.environ['APP_DB_PASSWORD']
|
|
if password == admin_password():
|
|
raise RuntimeError('Application and database administrator passwords must differ')
|
|
with admin_connect() as c:
|
|
admin, database = c.execute('SELECT current_user,current_database()').fetchone()
|
|
if role == admin:
|
|
raise RuntimeError('Application and database administrator must differ')
|
|
if not c.execute('SELECT 1 FROM pg_roles WHERE rolname=%s', (role,)).fetchone():
|
|
c.execute(sql.SQL('CREATE ROLE {} LOGIN').format(sql.Identifier(role)))
|
|
c.execute(sql.SQL('ALTER ROLE {} NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS PASSWORD {}').format(
|
|
sql.Identifier(role), sql.Literal(password)))
|
|
c.execute(Path(__file__).with_name('schema.sql').read_text())
|
|
c.execute(sql.SQL('REVOKE CREATE ON SCHEMA public FROM PUBLIC'))
|
|
c.execute(sql.SQL('GRANT CONNECT ON DATABASE {} TO {}').format(sql.Identifier(database),sql.Identifier(role)))
|
|
c.execute(sql.SQL('GRANT USAGE ON SCHEMA dtf_local TO {}').format(sql.Identifier(role)))
|
|
c.execute(sql.SQL('GRANT SELECT,INSERT,UPDATE,DELETE ON ALL TABLES IN SCHEMA dtf_local TO {}').format(sql.Identifier(role)))
|
|
c.execute(sql.SQL('GRANT USAGE,SELECT ON ALL SEQUENCES IN SCHEMA dtf_local TO {}').format(sql.Identifier(role)))
|
|
# Turn the configured credential into a real account so an existing
|
|
# deployment keeps logging in exactly as before. Inserts only when that
|
|
# email is absent, so a password changed with local.operators is never
|
|
# reverted by a stale environment variable on the next deploy.
|
|
seeded = seed_from_environment(c)
|
|
print('Local schema migrated; runtime role has DML only.')
|
|
if seeded:
|
|
print(f'Seeded operator account {seeded} from OPERATOR_EMAIL.')
|
|
|
|
if __name__ == '__main__': main()
|