feat: prepare postgres sbom cutover

Assistant: codex
Assistant-Model: gpt-5.6-sol
Assistant-Session: 01a028f0-a42f-7582-89a8-ebaad7343834
This commit is contained in:
tegwick 2026-08-22 13:14:24 +02:00
parent cf7e3acb78
commit ba535e1f8f
26 changed files with 1573 additions and 411 deletions

59
migrations/env.py Normal file
View file

@ -0,0 +1,59 @@
from __future__ import annotations
import os
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
from sbom_nexus.database import database_url, metadata
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = metadata
configured_target = os.getenv("SBOM_NEXUS_DATABASE_URL") or os.getenv(
"SBOM_NEXUS_DATABASE_PATH"
)
if configured_target:
config.set_main_option(
"sqlalchemy.url",
database_url(configured_target).replace("%", "%%"),
)
def run_migrations_offline() -> None:
context.configure(
url=config.get_main_option("sqlalchemy.url"),
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
compare_type=True,
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
compare_type=True,
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

25
migrations/script.py.mako Normal file
View file

@ -0,0 +1,25 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View file

@ -0,0 +1,88 @@
"""Initial SBOM Nexus repository, snapshot, and entry schema.
Revision ID: 0001
Revises:
Create Date: 2026-08-22
"""
from __future__ import annotations
from alembic import op
import sqlalchemy as sa
revision = "0001"
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table(
"repositories",
sa.Column("id", sa.String(length=36), nullable=False),
sa.Column("slug", sa.String(length=200), nullable=False),
sa.Column("checkout_path", sa.Text(), nullable=True),
sa.Column("active", sa.Boolean(), nullable=False),
sa.Column("last_attempt_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("last_success_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("last_status", sa.String(length=50), nullable=True),
sa.Column("last_source", sa.String(length=200), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint("id", name=op.f("pk_repositories")),
sa.UniqueConstraint("slug", name=op.f("uq_repositories_slug")),
)
op.create_table(
"snapshots",
sa.Column("id", sa.String(length=36), nullable=False),
sa.Column("repo_id", sa.String(length=36), nullable=False),
sa.Column("snapshot_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("source", sa.String(length=200), nullable=False),
sa.Column("status", sa.String(length=50), nullable=False),
sa.Column("entry_count", sa.Integer(), nullable=False),
sa.Column("source_revision", sa.String(length=200), nullable=True),
sa.Column("sources_json", sa.JSON(), nullable=False),
sa.Column("errors_json", sa.JSON(), nullable=False),
sa.Column("legacy_id", sa.String(length=100), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(
["repo_id"], ["repositories.id"], name=op.f("fk_snapshots_repo_id_repositories"), ondelete="RESTRICT"
),
sa.PrimaryKeyConstraint("id", name=op.f("pk_snapshots")),
sa.UniqueConstraint("legacy_id", name=op.f("uq_snapshots_legacy_id")),
)
op.create_index("ix_snapshots_repo_time", "snapshots", ["repo_id", "snapshot_at"])
op.create_table(
"entries",
sa.Column("id", sa.String(length=36), nullable=False),
sa.Column("repo_id", sa.String(length=36), nullable=False),
sa.Column("snapshot_id", sa.String(length=36), nullable=False),
sa.Column("package_name", sa.String(length=300), nullable=False),
sa.Column("package_version", sa.String(length=100), nullable=True),
sa.Column("ecosystem", sa.String(length=50), nullable=False),
sa.Column("license_spdx", sa.String(length=100), nullable=True),
sa.Column("is_direct", sa.Boolean(), nullable=False),
sa.Column("is_dev", sa.Boolean(), nullable=False),
sa.Column("source_path", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(
["repo_id"], ["repositories.id"], name=op.f("fk_entries_repo_id_repositories"), ondelete="RESTRICT"
),
sa.ForeignKeyConstraint(
["snapshot_id"], ["snapshots.id"], name=op.f("fk_entries_snapshot_id_snapshots"), ondelete="RESTRICT"
),
sa.PrimaryKeyConstraint("id", name=op.f("pk_entries")),
)
op.create_index("ix_entries_license", "entries", ["license_spdx"])
op.create_index("ix_entries_repo", "entries", ["repo_id"])
op.create_index("ix_entries_snapshot", "entries", ["snapshot_id"])
def downgrade() -> None:
op.drop_index("ix_entries_snapshot", table_name="entries")
op.drop_index("ix_entries_repo", table_name="entries")
op.drop_index("ix_entries_license", table_name="entries")
op.drop_table("entries")
op.drop_index("ix_snapshots_repo_time", table_name="snapshots")
op.drop_table("snapshots")
op.drop_table("repositories")