-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from lsst-sqre/tickets/DM-47262
DM-47262: Add Alembic support
- Loading branch information
Showing
21 changed files
with
418 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[alembic] | ||
script_location = %(here)s/alembic | ||
file_template = %%(year)d%%(month).2d%%(day).2d_%%(hour).2d%%(minute).2d_%%(rev)s_%%(slug)s | ||
prepend_sys_path = . | ||
timezone = UTC | ||
version_path_separator = os | ||
|
||
[post_write_hooks] | ||
hooks = ruff ruff_format | ||
ruff.type = exec | ||
ruff.executable = ruff | ||
ruff.options = check --fix REVISION_SCRIPT_FILENAME | ||
ruff_format.type = exec | ||
ruff_format.executable = ruff | ||
ruff_format.options = format REVISION_SCRIPT_FILENAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Wobbly Alembic configuration | ||
|
||
This directory contains the Alembic configuration for managing the Wobbly UWS database. | ||
It is installed into the Wobbly Docker image and is used to check whether the schema is up-to-date at startup of the Wobbly web service. | ||
It is also used by the Helm hook that updates the Wobbly UWS schema if `config.updateSchema` is enabled. | ||
|
||
## Generating new migrations | ||
|
||
For detailed instructions on how to generate a new Alembic migration, see [the Safir documentation](https://safir.lsst.io/user-guide/database/schema#create-migration). | ||
|
||
One of the files in this directory is here only to support creating migrations. | ||
`docker-compose.yaml` is a [docker-compose](https://docs.docker.com/compose/) configuration file that starts a PostgreSQL instance suitable for generating schema migrations. | ||
This file is not used at runtime. | ||
It is used by the tox environment described in the above documentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: "3" | ||
services: | ||
postgresql: | ||
image: "postgres:latest" | ||
hostname: "postgresql" | ||
container_name: "postgresql" | ||
environment: | ||
POSTGRES_PASSWORD: "INSECURE" | ||
POSTGRES_USER: "wobbly" | ||
POSTGRES_DB: "wobbly" | ||
ports: | ||
- "5432:5432" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Alembic migration environment.""" | ||
|
||
from safir.database import run_migrations_offline, run_migrations_online | ||
from safir.logging import configure_alembic_logging, configure_logging | ||
|
||
from alembic import context | ||
from wobbly.config import config | ||
from wobbly.schema import SchemaBase | ||
|
||
# Configure structlog. | ||
configure_logging(name="wobbly", log_level=config.log_level) | ||
configure_alembic_logging() | ||
|
||
# Run the migrations. | ||
if context.is_offline_mode(): | ||
run_migrations_offline(SchemaBase.metadata, config.database_url) | ||
else: | ||
run_migrations_online( | ||
SchemaBase.metadata, | ||
config.database_url, | ||
config.database_password, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""${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 identifiers, used by Alembic. | ||
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"} |
111 changes: 111 additions & 0 deletions
111
alembic/versions/20241112_2307_ef0ac4e1d0cb_initial_schema.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
"""Initial schema | ||
Revision ID: ef0ac4e1d0cb | ||
Revises: | ||
Create Date: 2024-11-12 23:07:29.449620+00:00 | ||
""" | ||
|
||
from collections.abc import Sequence | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "ef0ac4e1d0cb" | ||
down_revision: str | None = None | ||
branch_labels: str | Sequence[str] | None = None | ||
depends_on: str | Sequence[str] | None = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"job", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("service", sa.Text(), nullable=False), | ||
sa.Column("owner", sa.Text(), nullable=False), | ||
sa.Column( | ||
"phase", | ||
sa.Enum( | ||
"PENDING", | ||
"QUEUED", | ||
"EXECUTING", | ||
"COMPLETED", | ||
"ERROR", | ||
"UNKNOWN", | ||
"HELD", | ||
"SUSPENDED", | ||
"ABORTED", | ||
"ARCHIVED", | ||
name="executionphase", | ||
), | ||
nullable=False, | ||
), | ||
sa.Column("run_id", sa.Text(), nullable=True), | ||
sa.Column( | ||
"parameters", | ||
postgresql.JSONB(astext_type=sa.Text()), | ||
nullable=False, | ||
), | ||
sa.Column("message_id", sa.Text(), nullable=True), | ||
sa.Column("creation_time", sa.DateTime(), nullable=False), | ||
sa.Column("start_time", sa.DateTime(), nullable=True), | ||
sa.Column("end_time", sa.DateTime(), nullable=True), | ||
sa.Column("destruction_time", sa.DateTime(), nullable=False), | ||
sa.Column("execution_duration", sa.Integer(), nullable=True), | ||
sa.Column("quote", sa.DateTime(), nullable=True), | ||
sa.Column( | ||
"error_type", | ||
sa.Enum("TRANSIENT", "FATAL", name="errortype"), | ||
nullable=True, | ||
), | ||
sa.Column("error_code", sa.Text(), nullable=True), | ||
sa.Column("error_message", sa.Text(), nullable=True), | ||
sa.Column("error_detail", sa.Text(), nullable=True), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index( | ||
"by_service_owner_phase", | ||
"job", | ||
["service", "owner", "phase", "creation_time"], | ||
unique=False, | ||
) | ||
op.create_index( | ||
"by_service_owner_time", | ||
"job", | ||
["service", "owner", "creation_time"], | ||
unique=False, | ||
) | ||
op.create_table( | ||
"job_result", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("job_id", sa.Integer(), nullable=False), | ||
sa.Column("result_id", sa.Text(), nullable=False), | ||
sa.Column("sequence", sa.Integer(), nullable=False), | ||
sa.Column("url", sa.Text(), nullable=False), | ||
sa.Column("size", sa.Integer(), nullable=True), | ||
sa.Column("mime_type", sa.Text(), nullable=True), | ||
sa.ForeignKeyConstraint(["job_id"], ["job.id"], ondelete="CASCADE"), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index( | ||
"by_result_id", "job_result", ["job_id", "result_id"], unique=True | ||
) | ||
op.create_index( | ||
"by_sequence", "job_result", ["job_id", "sequence"], unique=True | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index("by_sequence", table_name="job_result") | ||
op.drop_index("by_result_id", table_name="job_result") | ||
op.drop_table("job_result") | ||
op.drop_index("by_service_owner_time", table_name="job") | ||
op.drop_index("by_service_owner_phase", table_name="job") | ||
op.drop_table("job") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,6 @@ pytest-cov | |
|
||
# Documentation | ||
scriv | ||
|
||
# Alembic | ||
ruff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.