Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [ENH] - Add User table to database #975

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions conda-store-server/conda_store_server/_internal/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
script_location = {alembic_dir}
sqlalchemy.url = {db_url}

# script_location = alembic
# sqlalchemy.url = postgresql+psycopg2://postgres:password@localhost:5432/conda-store

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

Expand Down

This file was deleted.

22 changes: 22 additions & 0 deletions conda-store-server/conda_store_server/_internal/alembic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Alembic

## Generating a database migration

If the database needs to be changed, `alembic` can be used to generate the migration scripts to do
so. This is usually painless, and follows these steps:

1. Make changes to the database models in
`conda-store-server/conda_store_server/_internal/orm.py`.
2. `alembic` will examine a running `conda-store` database and compare the
tables it finds there with changes to tables it finds in `orm.py` in your
feature branch. To do this, you need a running `conda-store` database from
the `main` branch, so clone the `conda-store` somewhere else on disk.
3. `cd` to the `main` branch in the _other_ copy of `conda-store`, and start
all the services with `docker compose up --build`. This will get a local
`conda-store` instance up and running, including the database that `alembic`
needs.
4. In another terminal, `cd` to your working branch and run `alembic revision
--autogenerate -m "<some useful comment about what changed in the database>`.
`alembic` will compare the running `postgres` database you started in step 3
with what it finds in your working branch, and create a database migration.
5. If the migration looks good, add and commit it to the repository.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import sys # noqa E402


sys.path.append(str(pathlib.Path(__file__).parent.parent.parent))
sys.path.append(str(pathlib.Path(__file__).parent.parent.parent.parent))

from conda_store_server._internal.orm import Base # noqa E402

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) conda-store development team. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
# Copyright (c) conda-store development team. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

"""${message}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "03c839888c82"
down_revision = "57cd11b949d5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "0f7e23ff24ee"
down_revision = "771180018e1b"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "16f65805dc8f"
down_revision = "5ad723de2abd"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "30b37e725c32"
down_revision = "d78e9889566a"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "48be4072fe58"
down_revision = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "57cd11b949d5"
down_revision = "0f7e23ff24ee"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "5ad723de2abd"
down_revision = "8d63a091aff8"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "771180018e1b"
down_revision = "30b37e725c32"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "8d63a091aff8"
down_revision = "48be4072fe58"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "abd7248d5327"
down_revision = "16f65805dc8f"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "b387747ca9b7"
down_revision = "abd7248d5327"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright (c) conda-store development team. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

"""add user table

Revision ID: b3aa6f2abe04
Revises: bf065abf375b
Create Date: 2024-11-11 19:05:34.416019

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'b3aa6f2abe04'
down_revision = 'bf065abf375b'
branch_labels = None
depends_on = None


def upgrade():
op.create_table(
'user',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.Unicode(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table(
'rolebinding',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('pattern', sa.Unicode(length=255), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('role', sa.Enum('NONE', 'VIEWER', 'EDITOR', 'ADMIN', name='role'), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)


def downgrade():
op.drop_table('rolebinding')
op.drop_table('user')
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "bf065abf375b"
down_revision = "e17b4cc6e086"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "d78e9889566a"
down_revision = "b387747ca9b7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"""

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision = "e17b4cc6e086"
down_revision = "03c839888c82"
Expand Down
56 changes: 56 additions & 0 deletions conda-store-server/conda_store_server/_internal/dbutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

import json
import os
import sys
import textwrap
from contextlib import contextmanager
from functools import partial
from tempfile import TemporaryDirectory

from alembic import command
from alembic.config import Config
from alembic.script import Script
from sqlalchemy import create_engine, inspect

from conda_store_server._internal import utils
Expand Down Expand Up @@ -106,3 +109,56 @@ def upgrade(db_url, revision="head"):

# run the upgrade.
command.upgrade(config=alembic_cfg, revision=revision)


def autogenerate_migrations(
message,
running_db: str = "postgresql+psycopg2://postgres:password@localhost:5432/conda-store",
):
"""Automatically generate new database migrations.

Make sure a postgres database is running with the previous version of the database at
the `running_db`.

Parameters
----------
running_db : str
URI of a running (current) version of the database pre-migration. The state of
this database will be compared to what is in `orm.py` to automatically generate
migrations.
"""
print(
f"Automatically generating migrations by comparing database at {running_db} to "
"this repository's table definitions in `orm.py`..."
)

with _temp_alembic_ini(db_url=running_db) as alembic_ini:
scripts = command.revision(
Config(alembic_ini),
autogenerate=True,
message=message,
)

if isinstance(scripts, Script):
scripts = [scripts]

if scripts:
print("Automatically generated migrations:")
print(
textwrap.indent(
"\n".join(script.path for script in list(scripts)),
prefix=" ",
)
)
else:
print("No migrations generated.")


if __name__ == "__main__":
if len(sys.argv) < 2:
print(
"Usage: `python -m conda_store_server._internal.dbutil '<migration message here>'"
)
sys.exit(1)

autogenerate_migrations(message=sys.argv[1])
Loading
Loading