|
| 1 | +"""Removing modifier auto increment |
| 2 | +
|
| 3 | +Revision ID: 17daa1c96438 |
| 4 | +Revises: cc39d4eb113b |
| 5 | +Create Date: 2026-05-09 16:05:14.813497 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +from alembic import op |
| 12 | +import sqlalchemy as sa |
| 13 | + |
| 14 | +from app.alembic.replaceable_objects.main import ReplaceableTrigger |
| 15 | + |
| 16 | +# revision identifiers, used by Alembic. |
| 17 | +revision: str = "17daa1c96438" |
| 18 | +down_revision: Union[str, None] = "cc39d4eb113b" |
| 19 | +branch_labels: Union[str, Sequence[str], None] = None |
| 20 | +depends_on: Union[str, Sequence[str], None] = None |
| 21 | + |
| 22 | +modifier_id_trigger = ReplaceableTrigger( |
| 23 | + "increment_modifier_id", |
| 24 | + "modifier", |
| 25 | + """ |
| 26 | + RETURNS trigger AS ${name}$ |
| 27 | + DECLARE |
| 28 | + exists boolean; |
| 29 | + BEGIN |
| 30 | + exists := EXISTS(SELECT 1 FROM modifier WHERE "effect" = NEW.effect); |
| 31 | + IF NOT exists THEN |
| 32 | + NEW."modifierId" := nextval('modifier_id_seq'); |
| 33 | +
|
| 34 | + ELSIF exists THEN |
| 35 | + NEW."modifierId" := (SELECT "modifierId" FROM modifier WHERE "effect" = NEW.effect LIMIT 1); |
| 36 | + END IF; |
| 37 | +
|
| 38 | + RETURN NEW; |
| 39 | + END; |
| 40 | + ${name}$ LANGUAGE plpgsql; |
| 41 | + """, |
| 42 | + """ |
| 43 | + BEFORE INSERT ON {table} |
| 44 | + FOR EACH ROW |
| 45 | + EXECUTE FUNCTION {name}(); |
| 46 | + """, |
| 47 | +) |
| 48 | + |
| 49 | + |
| 50 | +def upgrade() -> None: |
| 51 | + # ### commands auto generated by Alembic - please adjust! ### |
| 52 | + |
| 53 | + # droping old item_modifier key |
| 54 | + op.drop_constraint( |
| 55 | + op.f("item_modifier_modifierId_fkey"), "item_modifier", type_="foreignkey" |
| 56 | + ) |
| 57 | + op.drop_constraint( |
| 58 | + op.f("item_modifier_modifierId_fkey1"), "item_modifier", type_="foreignkey" |
| 59 | + ) |
| 60 | + |
| 61 | + # Dropping old modifier keys |
| 62 | + op.f("""ALTER TABLE modifier ALTER COLUMN "modifierId" DROP IDENTITY;""") |
| 63 | + op.drop_constraint(op.f("modifier_pkey"), "modifier", type_="primary") |
| 64 | + op.drop_constraint( |
| 65 | + op.f("modifier_modifierId_position_key"), "modifier", type_="unique" |
| 66 | + ) |
| 67 | + |
| 68 | + # creating new keys |
| 69 | + op.create_primary_key("modifier_pkey", "modifier", ["modifierId", "position"]) |
| 70 | + op.add_column( |
| 71 | + "item_modifier", sa.Column("position", sa.SmallInteger(), nullable=False) |
| 72 | + ) |
| 73 | + op.create_foreign_key( |
| 74 | + "fk_item_modifier_modfierId_position", |
| 75 | + "item_modifier", |
| 76 | + "modifier", |
| 77 | + ["modifierId", "position"], |
| 78 | + ["modifierId", "position"], |
| 79 | + ondelete="CASCADE", |
| 80 | + onupdate="CASCADE", |
| 81 | + ) |
| 82 | + |
| 83 | + op.execute(""" |
| 84 | + UPDATE modifier AS m |
| 85 | + SET "modifierId" = "modifierId" + 10000; |
| 86 | + """) |
| 87 | + |
| 88 | + op.execute(""" |
| 89 | + WITH subquery as (SELECT |
| 90 | + DENSE_RANK() OVER ( |
| 91 | + ORDER BY m.effect |
| 92 | + ) AS newModifierId, |
| 93 | + m."position", |
| 94 | + m."modifierId" AS modifierId |
| 95 | + FROM modifier AS m |
| 96 | + ORDER BY newModifierId, m.position) |
| 97 | +
|
| 98 | + UPDATE modifier AS m |
| 99 | + SET "modifierId" = sq.newModifierId |
| 100 | + FROM subquery AS sq |
| 101 | + WHERE "modifierId" = sq.modifierId; |
| 102 | + """) |
| 103 | + |
| 104 | + op.execute("CREATE SEQUENCE modifier_id_seq;") |
| 105 | + op.execute( |
| 106 | + """SELECT setval('modifier_id_seq', (SELECT MAX("modifierId") FROM modifier));""" |
| 107 | + ) |
| 108 | + |
| 109 | + op.create_trigger(modifier_id_trigger) |
| 110 | + # ### end Alembic commands ### |
| 111 | + |
| 112 | + |
| 113 | +def downgrade() -> None: |
| 114 | + # ### commands auto generated by Alembic - please adjust! ### |
| 115 | + op.drop_trigger(modifier_id_trigger) |
| 116 | + |
| 117 | + # dropping new modifier keys |
| 118 | + op.drop_constraint( |
| 119 | + op.f("fk_item_modifier_modfierId_position"), "item_modifier", type_="foreignkey" |
| 120 | + ) |
| 121 | + op.drop_column("item_modifier", "position") |
| 122 | + op.drop_constraint(op.f("modifier_pkey"), "modifier", type_="primary") |
| 123 | + |
| 124 | + op.execute(""" |
| 125 | + WITH subquery as (SELECT |
| 126 | + ROW_NUMBER() OVER(ORDER BY "modifierId", position ASC) AS newModifierId, |
| 127 | + m."position", |
| 128 | + m."modifierId" AS modifierId |
| 129 | + FROM modifier AS m |
| 130 | + ORDER BY newModifierId, m.position) |
| 131 | + |
| 132 | + UPDATE modifier AS m |
| 133 | + SET "modifierId" = sq.newModifierId |
| 134 | + FROM subquery AS sq |
| 135 | + WHERE "modifierId" = sq.modifierId |
| 136 | + AND m.position = sq.position; |
| 137 | + """) |
| 138 | + |
| 139 | + op.execute(""" |
| 140 | + DROP SEQUENCE modifier_id_seq; |
| 141 | + """) |
| 142 | + |
| 143 | + # adding old keys |
| 144 | + op.create_primary_key("modifier_pkey", "modifier", ["modifierId"]) |
| 145 | + op.create_unique_constraint( |
| 146 | + op.f("modifier_modifierId_position_key"), |
| 147 | + "modifier", |
| 148 | + ["modifierId", "position"], |
| 149 | + postgresql_nulls_not_distinct=False, |
| 150 | + ) |
| 151 | + |
| 152 | + op.create_foreign_key( |
| 153 | + "item_modifier_modifierId_fkey", |
| 154 | + "item_modifier", |
| 155 | + "modifier", |
| 156 | + ["modifierId"], |
| 157 | + ["modifierId"], |
| 158 | + ) |
| 159 | + op.create_foreign_key( |
| 160 | + "item_modifier_modifierId_fkey1", |
| 161 | + "item_modifier", |
| 162 | + "modifier", |
| 163 | + ["modifierId"], |
| 164 | + ["modifierId"], |
| 165 | + ) |
| 166 | + |
| 167 | + op.alter_column( |
| 168 | + "modifier", |
| 169 | + "modifierId", |
| 170 | + existing_type=sa.SMALLINT(), |
| 171 | + server_default=sa.Identity( |
| 172 | + always=False, |
| 173 | + start=1, |
| 174 | + increment=1, |
| 175 | + minvalue=1, |
| 176 | + maxvalue=32767, |
| 177 | + cycle=True, |
| 178 | + cache=1, |
| 179 | + ), |
| 180 | + existing_nullable=False, |
| 181 | + ) |
| 182 | + op.execute( |
| 183 | + """SELECT setval(pg_get_serial_sequence('modifier', 'modifierId'), (SELECT MAX("modifierId") FROM modifier));""" |
| 184 | + ) |
| 185 | + # ### end Alembic commands ### |
0 commit comments