gitscope
← marzneshin / v0.7.1 / app/db/migrations/versions/20241218_57eba0a293f2_unbound_host.py
https://dev.kronoz.win/r/marzneshin.git
app/db/migrations/versions/20241218_57eba0a293f2_unbound_host.py · Python
Blame Raw
  1"""unbound host
  2
  3Revision ID: 57eba0a293f2
  4Revises: 982867b533e6
  5Create Date: 2024-12-18 20:22:11.860923
  6
  7"""
  8
  9import sqlalchemy as sa
 10from alembic import op
 11
 12# revision identifiers, used by Alembic.
 13revision = "57eba0a293f2"
 14down_revision = "982867b533e6"
 15branch_labels = None
 16depends_on = None
 17
 18
 19def upgrade() -> None:
 20    # ### commands auto generated by Alembic - please adjust! ###
 21    op.create_table(
 22        "hosts_services",
 23        sa.Column("host_id", sa.Integer(), nullable=False),
 24        sa.Column("service_id", sa.Integer(), nullable=False),
 25        sa.ForeignKeyConstraint(
 26            ["host_id"],
 27            ["hosts.id"],
 28        ),
 29        sa.ForeignKeyConstraint(
 30            ["service_id"],
 31            ["services.id"],
 32        ),
 33        sa.PrimaryKeyConstraint("host_id", "service_id"),
 34    )
 35    op.add_column(
 36        "hosts",
 37        sa.Column("host_protocol", sa.String(length=32), nullable=True),
 38    )
 39    op.add_column(
 40        "hosts", sa.Column("host_network", sa.String(length=32), nullable=True)
 41    )
 42    op.add_column(
 43        "hosts", sa.Column("uuid", sa.String(length=36), nullable=True)
 44    )
 45    op.add_column(
 46        "hosts", sa.Column("password", sa.String(length=128), nullable=True)
 47    )
 48    op.add_column(
 49        "hosts", sa.Column("header_type", sa.String(length=32), nullable=True)
 50    )
 51    op.add_column(
 52        "hosts",
 53        sa.Column("reality_public_key", sa.String(length=128), nullable=True),
 54    )
 55    op.add_column(
 56        "hosts", sa.Column("reality_short_ids", sa.JSON(), nullable=True)
 57    )
 58    op.add_column(
 59        "hosts", sa.Column("flow", sa.String(length=32), nullable=True)
 60    )
 61    op.add_column(
 62        "hosts", sa.Column("shadowtls_version", sa.Integer(), nullable=True)
 63    )
 64    op.add_column(
 65        "hosts",
 66        sa.Column("shadowsocks_method", sa.String(length=32), nullable=True),
 67    )
 68    op.add_column(
 69        "hosts", sa.Column("splithttp_settings", sa.JSON(), nullable=True)
 70    )
 71    op.drop_column("hosts", "mux")
 72    op.add_column("hosts", sa.Column("mux_settings", sa.JSON(), nullable=True))
 73    op.add_column(
 74        "hosts", sa.Column("early_data", sa.Integer(), nullable=True)
 75    )
 76    op.add_column(
 77        "hosts",
 78        sa.Column(
 79            "universal",
 80            sa.Boolean(),
 81            server_default=sa.sql.false(),
 82            nullable=False,
 83        ),
 84    )
 85    if op.get_bind().dialect.name == "sqlite":
 86        op.add_column(
 87            "hosts", sa.Column("inbound_id_new", sa.Integer, nullable=True)
 88        )
 89
 90        # Copy data from the old column to the new column
 91        op.execute(
 92            """
 93            UPDATE hosts
 94            SET inbound_id_new = inbound_id
 95            """
 96        )
 97
 98        # Drop the old column (SQLite requires recreating the table for this step)
 99        with op.batch_alter_table("hosts") as batch_op:
100            batch_op.drop_column("inbound_id")
101
102        # Rename the new column to the old column's name
103        op.alter_column(
104            "hosts", "inbound_id_new", new_column_name="inbound_id"
105        )
106    else:
107        op.alter_column(
108            "hosts", "inbound_id", existing_type=sa.INTEGER(), nullable=True
109        )
110    # ### end Alembic commands ###
111
112
113def downgrade() -> None:
114    # ### commands auto generated by Alembic - please adjust! ###
115    op.drop_column("hosts", "universal")
116    op.drop_column("hosts", "early_data")
117    op.add_column(
118        "hosts",
119        sa.Column(
120            "mux", sa.Boolean(), server_default=sa.sql.true(), nullable=False
121        ),
122    )
123    op.drop_column("hosts", "mux_settings")
124    op.drop_column("hosts", "splithttp_settings")
125    op.drop_column("hosts", "shadowsocks_method")
126    op.drop_column("hosts", "shadowtls_version")
127    op.drop_column("hosts", "flow")
128    op.drop_column("hosts", "reality_short_ids")
129    op.drop_column("hosts", "reality_public_key")
130    op.drop_column("hosts", "header_type")
131    op.drop_column("hosts", "password")
132    op.drop_column("hosts", "uuid")
133    op.drop_column("hosts", "host_network")
134    op.drop_column("hosts", "host_protocol")
135    op.drop_table("hosts_services")
136    if op.get_bind().dialect.name == "sqlite":
137        op.add_column(
138            "hosts", sa.Column("inbound_id_old", sa.Integer, nullable=False)
139        )
140
141        # Copy data from the new column back to the old column
142        op.execute(
143            """
144            UPDATE hosts
145            SET inbound_id_old = inbound_id
146            """
147        )
148
149        # Drop the modified column
150        with op.batch_alter_table("hosts") as batch_op:
151            batch_op.drop_column("inbound_id")
152
153        # Rename the old column back to the original name
154        op.alter_column(
155            "hosts", "inbound_id_old", new_column_name="inbound_id"
156        )
157    else:
158        op.alter_column(
159            "hosts", "inbound_id", existing_type=sa.INTEGER(), nullable=False
160        )
161    # ### end Alembic commands ###