|
| 1 | +"""expand and separate environments from session launchers |
| 2 | +
|
| 3 | +Revision ID: 584598f3b769 |
| 4 | +Revises: 9058bf0a1a12 |
| 5 | +Create Date: 2024-08-12 14:25:24.292285 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import sqlalchemy as sa |
| 10 | +from alembic import op |
| 11 | +from sqlalchemy.dialects import postgresql |
| 12 | + |
| 13 | +# revision identifiers, used by Alembic. |
| 14 | +revision = "584598f3b769" |
| 15 | +down_revision = "9058bf0a1a12" |
| 16 | +branch_labels = None |
| 17 | +depends_on = None |
| 18 | + |
| 19 | +default_url: str = "/lab" |
| 20 | +working_dir: str = "/home/jovyan/work" |
| 21 | +mount_dir: str = "/home/jovyan/work" |
| 22 | +uid: int = 1000 |
| 23 | +gid: int = 1000 |
| 24 | +port: int = 8888 |
| 25 | + |
| 26 | + |
| 27 | +def upgrade() -> None: |
| 28 | + # ### commands auto generated by Alembic - please adjust! ### |
| 29 | + op.execute("DELETE FROM sessions.launchers") |
| 30 | + op.drop_column("launchers", "default_url", schema="sessions") |
| 31 | + op.drop_column("launchers", "environment_kind", schema="sessions") |
| 32 | + op.drop_column("launchers", "container_image", schema="sessions") |
| 33 | + op.execute("DROP TYPE environmentkind CASCADE") |
| 34 | + op.execute("CREATE TYPE environmentkind AS ENUM ('GLOBAL', 'CUSTOM')") |
| 35 | + op.add_column("environments", sa.Column("port", sa.Integer(), nullable=True), schema="sessions") |
| 36 | + op.add_column("environments", sa.Column("working_directory", sa.String(), nullable=True), schema="sessions") |
| 37 | + op.add_column("environments", sa.Column("mount_directory", sa.String(), nullable=True), schema="sessions") |
| 38 | + op.add_column("environments", sa.Column("uid", sa.Integer(), nullable=True), schema="sessions") |
| 39 | + op.add_column("environments", sa.Column("gid", sa.Integer(), nullable=True), schema="sessions") |
| 40 | + op.add_column( |
| 41 | + "environments", |
| 42 | + sa.Column("environment_kind", sa.Enum("GLOBAL", "CUSTOM", name="environmentkind"), nullable=True), |
| 43 | + schema="sessions", |
| 44 | + ) |
| 45 | + op.execute(sa.text("UPDATE sessions.environments SET port = :port WHERE port is NULL").bindparams(port=port)) |
| 46 | + op.execute( |
| 47 | + sa.text( |
| 48 | + "UPDATE sessions.environments SET working_directory = :working_dir WHERE working_directory is NULL" |
| 49 | + ).bindparams(working_dir=working_dir) |
| 50 | + ) |
| 51 | + op.execute( |
| 52 | + sa.text( |
| 53 | + "UPDATE sessions.environments SET mount_directory = :mount_dir WHERE mount_directory is NULL" |
| 54 | + ).bindparams(mount_dir=mount_dir) |
| 55 | + ) |
| 56 | + op.execute(sa.text("UPDATE sessions.environments SET uid = :uid WHERE uid is NULL").bindparams(uid=uid)) |
| 57 | + op.execute(sa.text("UPDATE sessions.environments SET gid = :gid WHERE gid is NULL").bindparams(gid=gid)) |
| 58 | + op.execute("UPDATE sessions.environments SET environment_kind = 'GLOBAL' WHERE environment_kind is NULL") |
| 59 | + op.execute( |
| 60 | + sa.text("UPDATE sessions.environments SET default_url = :default_url WHERE default_url is NULL").bindparams( |
| 61 | + default_url=default_url |
| 62 | + ) |
| 63 | + ) |
| 64 | + op.alter_column("environments", "port", nullable=False, schema="sessions") |
| 65 | + op.alter_column("environments", "working_directory", nullable=False, schema="sessions") |
| 66 | + op.alter_column("environments", "mount_directory", nullable=False, schema="sessions") |
| 67 | + op.alter_column("environments", "uid", nullable=False, schema="sessions") |
| 68 | + op.alter_column("environments", "gid", nullable=False, schema="sessions") |
| 69 | + op.alter_column("environments", "environment_kind", nullable=False, schema="sessions") |
| 70 | + op.alter_column( |
| 71 | + "environments", "default_url", existing_type=sa.VARCHAR(length=200), nullable=False, schema="sessions" |
| 72 | + ) |
| 73 | + # ### end Alembic commands ### |
| 74 | + |
| 75 | + |
| 76 | +def downgrade() -> None: |
| 77 | + # ### commands auto generated by Alembic - please adjust! ### |
| 78 | + op.drop_column("environments", "environment_kind", schema="sessions") |
| 79 | + op.drop_column("environments", "gid", schema="sessions") |
| 80 | + op.drop_column("environments", "uid", schema="sessions") |
| 81 | + op.drop_column("environments", "mount_directory", schema="sessions") |
| 82 | + op.drop_column("environments", "working_directory", schema="sessions") |
| 83 | + op.drop_column("environments", "port", schema="sessions") |
| 84 | + op.execute("DROP TYPE environmentkind") |
| 85 | + op.execute("CREATE TYPE environmentkind AS ENUM ('global_environment', 'container_image')") |
| 86 | + op.add_column( |
| 87 | + "launchers", |
| 88 | + sa.Column("container_image", sa.VARCHAR(length=500), autoincrement=False, nullable=True), |
| 89 | + schema="sessions", |
| 90 | + ) |
| 91 | + op.add_column( |
| 92 | + "launchers", |
| 93 | + sa.Column( |
| 94 | + "environment_kind", |
| 95 | + postgresql.ENUM("global_environment", "container_image", name="environmentkind"), |
| 96 | + autoincrement=False, |
| 97 | + nullable=False, |
| 98 | + ), |
| 99 | + schema="sessions", |
| 100 | + ) |
| 101 | + op.add_column( |
| 102 | + "launchers", |
| 103 | + sa.Column("default_url", sa.VARCHAR(length=200), autoincrement=False, nullable=True), |
| 104 | + schema="sessions", |
| 105 | + ) |
| 106 | + op.alter_column( |
| 107 | + "environments", "default_url", existing_type=sa.VARCHAR(length=200), nullable=True, schema="sessions" |
| 108 | + ) |
| 109 | + # ### end Alembic commands ### |
0 commit comments