-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Gafaelfawr's schema management match the new recommended practices in Safir, including a stack of schema migrations that create the database from scratch. This means that direct upgrades from versions earlier than 10.0.0 are no longer supported, since they will attempt to recreate the schema unless one carefully uses `alembic stamp` first. Document that. Change the name of the SQLAlchemy declarative base to `SchemaBase`, matching the Safir documentation. Switch to the Safir method of checking for unexpected schema changes, which is much simpler than the previous approach and doesn't require keeping around old copies of the schema. Point to the Safir documentation for how to make schema migrations rather than duplicating that documentation here.
- Loading branch information
Showing
23 changed files
with
322 additions
and
252 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
231 changes: 231 additions & 0 deletions
231
alembic/versions/20240209_2309_5c28ed7092c2_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,231 @@ | ||
"""Initial schema. | ||
Revision ID: 5c28ed7092c2 | ||
Revises: | ||
Create Date: 2024-11-19 22:40:16.309715+00:00 | ||
""" | ||
|
||
from collections.abc import Sequence | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "5c28ed7092c2" | ||
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( | ||
"admin", | ||
sa.Column("username", sa.String(length=64), nullable=False), | ||
sa.PrimaryKeyConstraint("username"), | ||
) | ||
op.create_table( | ||
"admin_history", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("username", sa.String(length=64), nullable=False), | ||
sa.Column( | ||
"action", | ||
sa.Enum("add", "remove", name="adminchange"), | ||
nullable=False, | ||
), | ||
sa.Column("actor", sa.String(length=64), nullable=False), | ||
sa.Column( | ||
"ip_address", | ||
sa.String(length=64).with_variant(postgresql.INET(), "postgresql"), | ||
nullable=False, | ||
), | ||
sa.Column("event_time", sa.DateTime(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index( | ||
"admin_history_by_time", | ||
"admin_history", | ||
["event_time", "id"], | ||
unique=False, | ||
) | ||
op.create_table( | ||
"token", | ||
sa.Column( | ||
"token", sa.String(length=64, collation="C"), nullable=False | ||
), | ||
sa.Column("username", sa.String(length=64), nullable=False), | ||
sa.Column( | ||
"token_type", | ||
sa.Enum( | ||
"session", | ||
"user", | ||
"notebook", | ||
"internal", | ||
"service", | ||
name="tokentype", | ||
), | ||
nullable=False, | ||
), | ||
sa.Column("token_name", sa.String(length=64), nullable=True), | ||
sa.Column("scopes", sa.String(length=512), nullable=False), | ||
sa.Column("service", sa.String(length=64), nullable=True), | ||
sa.Column("created", sa.DateTime(), nullable=False), | ||
sa.Column("last_used", sa.DateTime(), nullable=True), | ||
sa.Column("expires", sa.DateTime(), nullable=True), | ||
sa.PrimaryKeyConstraint("token"), | ||
sa.UniqueConstraint("username", "token_name"), | ||
) | ||
op.create_index( | ||
"token_by_username", "token", ["username", "token_type"], unique=False | ||
) | ||
op.create_table( | ||
"token_auth_history", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("token", sa.String(length=64), nullable=False), | ||
sa.Column("username", sa.String(length=64), nullable=False), | ||
sa.Column( | ||
"token_type", | ||
sa.Enum( | ||
"session", | ||
"user", | ||
"notebook", | ||
"internal", | ||
"service", | ||
name="tokentype", | ||
), | ||
nullable=False, | ||
), | ||
sa.Column("token_name", sa.String(length=64), nullable=True), | ||
sa.Column("parent", sa.String(length=64), nullable=True), | ||
sa.Column("scopes", sa.String(length=512), nullable=True), | ||
sa.Column("service", sa.String(length=64), nullable=True), | ||
sa.Column( | ||
"ip_address", | ||
sa.String(length=64).with_variant(postgresql.INET(), "postgresql"), | ||
nullable=True, | ||
), | ||
sa.Column("event_time", sa.DateTime(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index( | ||
"token_auth_history_by_time", | ||
"token_auth_history", | ||
["event_time", "id"], | ||
unique=False, | ||
) | ||
op.create_index( | ||
"token_auth_history_by_token", | ||
"token_auth_history", | ||
["token", "event_time", "id"], | ||
unique=False, | ||
) | ||
op.create_index( | ||
"token_auth_history_by_username", | ||
"token_auth_history", | ||
["username", "event_time", "id"], | ||
unique=False, | ||
) | ||
op.create_table( | ||
"token_change_history", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("token", sa.String(length=64), nullable=False), | ||
sa.Column("username", sa.String(length=64), nullable=False), | ||
sa.Column( | ||
"token_type", | ||
sa.Enum( | ||
"session", | ||
"user", | ||
"notebook", | ||
"internal", | ||
"service", | ||
name="tokentype", | ||
), | ||
nullable=False, | ||
), | ||
sa.Column("token_name", sa.String(length=64), nullable=True), | ||
sa.Column("parent", sa.String(length=64), nullable=True), | ||
sa.Column("scopes", sa.String(length=512), nullable=False), | ||
sa.Column("service", sa.String(length=64), nullable=True), | ||
sa.Column("expires", sa.DateTime(), nullable=True), | ||
sa.Column("actor", sa.String(length=64), nullable=True), | ||
sa.Column( | ||
"action", | ||
sa.Enum("create", "revoke", "expire", "edit", name="tokenchange"), | ||
nullable=False, | ||
), | ||
sa.Column("old_token_name", sa.String(length=64), nullable=True), | ||
sa.Column("old_scopes", sa.String(length=512), nullable=True), | ||
sa.Column("old_expires", sa.DateTime(), nullable=True), | ||
sa.Column( | ||
"ip_address", | ||
sa.String(length=64).with_variant(postgresql.INET(), "postgresql"), | ||
nullable=True, | ||
), | ||
sa.Column("event_time", sa.DateTime(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index( | ||
"token_change_history_by_time", | ||
"token_change_history", | ||
["event_time", "id"], | ||
unique=False, | ||
) | ||
op.create_index( | ||
"token_change_history_by_token", | ||
"token_change_history", | ||
["token", "event_time", "id"], | ||
unique=False, | ||
) | ||
op.create_index( | ||
"token_change_history_by_username", | ||
"token_change_history", | ||
["username", "event_time", "id"], | ||
unique=False, | ||
) | ||
op.create_table( | ||
"subtoken", | ||
sa.Column("child", sa.String(length=64), nullable=False), | ||
sa.Column("parent", sa.String(length=64), nullable=True), | ||
sa.ForeignKeyConstraint( | ||
["child"], ["token.token"], ondelete="CASCADE" | ||
), | ||
sa.ForeignKeyConstraint( | ||
["parent"], ["token.token"], ondelete="SET NULL" | ||
), | ||
sa.PrimaryKeyConstraint("child"), | ||
) | ||
op.create_index("subtoken_by_parent", "subtoken", ["parent"], unique=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index("subtoken_by_parent", table_name="subtoken") | ||
op.drop_table("subtoken") | ||
op.drop_index( | ||
"token_change_history_by_username", table_name="token_change_history" | ||
) | ||
op.drop_index( | ||
"token_change_history_by_token", table_name="token_change_history" | ||
) | ||
op.drop_index( | ||
"token_change_history_by_time", table_name="token_change_history" | ||
) | ||
op.drop_table("token_change_history") | ||
op.drop_index( | ||
"token_auth_history_by_username", table_name="token_auth_history" | ||
) | ||
op.drop_index( | ||
"token_auth_history_by_token", table_name="token_auth_history" | ||
) | ||
op.drop_index( | ||
"token_auth_history_by_time", table_name="token_auth_history" | ||
) | ||
op.drop_table("token_auth_history") | ||
op.drop_index("token_by_username", table_name="token") | ||
op.drop_table("token") | ||
op.drop_index("admin_history_by_time", table_name="admin_history") | ||
op.drop_table("admin_history") | ||
op.drop_table("admin") | ||
# ### 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### Other changes | ||
|
||
- Gafaelfawr no longer supports direct upgrades from versions older than 10.0.0. When upgrading from an older version, upgrade to 12.1.0 or earlier first and complete the database schema migration, and then upgrade to the latest version. |
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.