|
| 1 | +"""New ORM models |
| 2 | +
|
| 3 | +Revision ID: 698fc7687b7f |
| 4 | +Revises: 3bb5cc4b5d48 |
| 5 | +Create Date: 2023-03-25 22:25:55.765694 |
| 6 | +
|
| 7 | +""" |
| 8 | +import sqlalchemy as sa |
| 9 | +from alembic import op |
| 10 | + |
| 11 | +# revision identifiers, used by Alembic. |
| 12 | +revision = "698fc7687b7f" |
| 13 | +down_revision = "3bb5cc4b5d48" |
| 14 | +branch_labels = None |
| 15 | +depends_on = None |
| 16 | + |
| 17 | + |
| 18 | +def upgrade(): |
| 19 | + # Primary key renames |
| 20 | + # "DROP CONSTRAINT ... CASCADE" is psql specific, and not supported by alembic. Need to manually call using .execute(). |
| 21 | + |
| 22 | + # infractions_pk isn't referenced by any other tables |
| 23 | + op.execute("ALTER TABLE infractions DROP CONSTRAINT infractions_pk CASCADE") |
| 24 | + op.alter_column("infractions", "id", new_column_name="infraction_id") |
| 25 | + op.create_primary_key("infractions_pk", "infractions", ["infraction_id"]) |
| 26 | + |
| 27 | + # jams_pk is used by infractions, teams & winners |
| 28 | + # winners is being deleted, so don't care about that table |
| 29 | + op.execute("ALTER TABLE jams DROP CONSTRAINT jams_pk CASCADE") |
| 30 | + op.alter_column("jams", "id", new_column_name="jam_id") |
| 31 | + op.create_primary_key("jams_pk", "jams", ["jam_id"]) |
| 32 | + # Update foreign key to jams_pk in infractions |
| 33 | + op.alter_column("infractions", "jam_id", new_column_name="issued_in_jam_id", nullable=False) |
| 34 | + op.create_foreign_key( |
| 35 | + op.f("infractions_issued_in_jam_id_jams_fk"), "infractions", "jams", ["issued_in_jam_id"], ["jam_id"] |
| 36 | + ) |
| 37 | + # Update foreign key to jams_pk in teams |
| 38 | + op.create_foreign_key(op.f("teams_jam_id_jams_fk"), "teams", "jams", ["jam_id"], ["jam_id"]) |
| 39 | + |
| 40 | + # teams_pk is used by team_has_user |
| 41 | + # team_has_user is being deleted, so don't care about that table |
| 42 | + op.execute("ALTER TABLE teams DROP CONSTRAINT teams_pk CASCADE") |
| 43 | + op.alter_column("teams", "id", new_column_name="team_id") |
| 44 | + op.create_primary_key("teams_pk", "teams", ["team_id"]) |
| 45 | + |
| 46 | + # users_pk is used by infractions, team_has_user & winners |
| 47 | + # team_has_user & winners are being deleted, so don't care about those tables |
| 48 | + op.execute("ALTER TABLE users DROP CONSTRAINT users_pk CASCADE") |
| 49 | + op.alter_column("users", "id", new_column_name="user_id") |
| 50 | + op.create_primary_key("users_pk", "users", ["user_id"]) |
| 51 | + op.alter_column("infractions", "user_id", existing_type=sa.BIGINT(), nullable=False) |
| 52 | + op.create_foreign_key(op.f("infractions_user_id_users_fk"), "infractions", "users", ["user_id"], ["user_id"]) |
| 53 | + # End primary key renames |
| 54 | + |
| 55 | + # New columns |
| 56 | + op.alter_column("infractions", "reason", existing_type=sa.TEXT(), type_=sa.String(), existing_nullable=False) |
| 57 | + |
| 58 | + op.alter_column("jams", "name", existing_type=sa.TEXT(), type_=sa.String(), existing_nullable=False) |
| 59 | + op.alter_column("jams", "ongoing", existing_type=sa.BOOLEAN(), server_default=None, existing_nullable=False) |
| 60 | + |
| 61 | + op.add_column("teams", sa.Column("leader_id", sa.BigInteger())) |
| 62 | + op.execute( |
| 63 | + "UPDATE teams SET leader_id = tu.user_id from teams t join team_has_user tu USING(team_id) where tu.is_leader" |
| 64 | + ) |
| 65 | + op.alter_column("teams", "leader_id", existing_type=sa.Integer(), nullable=False) |
| 66 | + |
| 67 | + op.add_column("teams", sa.Column("winner", sa.Boolean(), nullable=True)) |
| 68 | + op.add_column("teams", sa.Column("first_place_winner", sa.Boolean(), nullable=True)) |
| 69 | + op.alter_column("teams", "name", existing_type=sa.TEXT(), type_=sa.String(), existing_nullable=False) |
| 70 | + op.alter_column("teams", "discord_role_id", existing_type=sa.BIGINT()) |
| 71 | + op.alter_column("teams", "discord_channel_id", existing_type=sa.BIGINT()) |
| 72 | + op.execute( |
| 73 | + "UPDATE teams " |
| 74 | + "SET winner = true, " |
| 75 | + "first_place_winner = w.first_place " |
| 76 | + "from teams t " |
| 77 | + "join winners w USING(jam_id)" |
| 78 | + ) |
| 79 | + |
| 80 | + op.create_foreign_key(op.f("teams_leader_id_users_fk"), "teams", "users", ["leader_id"], ["user_id"]) |
| 81 | + |
| 82 | + op.create_table( |
| 83 | + "jam_specific_details", |
| 84 | + sa.Column("jam_specific_detail_id", sa.Integer(), nullable=False), |
| 85 | + sa.Column("user_id", sa.BigInteger(), nullable=False), |
| 86 | + sa.Column("jam_id", sa.Integer(), nullable=False), |
| 87 | + sa.Column( |
| 88 | + "experience_level_git", |
| 89 | + sa.Enum("beginner", "decent", "expierienced", "very_expierienced", name="experience_level_git_enum"), |
| 90 | + nullable=False, |
| 91 | + ), |
| 92 | + sa.Column( |
| 93 | + "experience_level_python", |
| 94 | + sa.Enum("beginner", "decent", "expierienced", "very_expierienced", name="experience_level_python_enum"), |
| 95 | + nullable=False, |
| 96 | + ), |
| 97 | + sa.Column("time_zone", sa.String(), nullable=False), |
| 98 | + sa.Column("willing_to_lead", sa.Boolean(), nullable=False), |
| 99 | + sa.ForeignKeyConstraint(["jam_id"], ["jams.jam_id"], name=op.f("jam_specific_details_jam_id_jams_fk")), |
| 100 | + sa.ForeignKeyConstraint(["user_id"], ["users.user_id"], name=op.f("jam_specific_details_user_id_users_fk")), |
| 101 | + sa.PrimaryKeyConstraint("jam_specific_detail_id", name=op.f("jam_specific_details_pk")), |
| 102 | + ) |
| 103 | + |
| 104 | + op.create_table( |
| 105 | + "team_has_users", |
| 106 | + sa.Column("team_id", sa.Integer(), nullable=False), |
| 107 | + sa.Column("user_id", sa.BigInteger(), nullable=False), |
| 108 | + sa.ForeignKeyConstraint(["team_id"], ["teams.team_id"], name=op.f("team_has_users_team_id_teams_fk")), |
| 109 | + sa.ForeignKeyConstraint(["user_id"], ["users.user_id"], name=op.f("team_has_users_user_id_users_fk")), |
| 110 | + sa.PrimaryKeyConstraint("team_id", "user_id", name=op.f("team_has_users_pk")), |
| 111 | + ) |
| 112 | + op.execute("INSERT INTO team_has_users (team_id, user_id) SELECT team_id, user_id FROM team_has_user") |
| 113 | + |
| 114 | + op.drop_table("winners") |
| 115 | + op.drop_table("team_has_user") |
| 116 | + # ### end Alembic commands ### |
| 117 | + |
| 118 | + |
| 119 | +def downgrade(): |
| 120 | + # ### commands auto generated by Alembic - please adjust! ### |
| 121 | + op.add_column("users", sa.Column("id", sa.BIGINT(), autoincrement=False, nullable=False)) |
| 122 | + op.drop_column("users", "user_id") |
| 123 | + op.add_column("teams", sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False)) |
| 124 | + op.drop_constraint(op.f("teams_jam_id_jams_fk"), "teams", type_="foreignkey") |
| 125 | + op.drop_constraint(op.f("teams_leader_id_users_fk"), "teams", type_="foreignkey") |
| 126 | + op.create_foreign_key("teams_jam_id_jams_fk", "teams", "jams", ["jam_id"], ["id"]) |
| 127 | + op.alter_column("teams", "discord_channel_id", existing_type=sa.BIGINT(), nullable=True) |
| 128 | + op.alter_column("teams", "discord_role_id", existing_type=sa.BIGINT(), nullable=True) |
| 129 | + op.alter_column("teams", "name", existing_type=sa.String(), type_=sa.TEXT(), existing_nullable=False) |
| 130 | + op.drop_column("teams", "first_place_winner") |
| 131 | + op.drop_column("teams", "winner") |
| 132 | + op.drop_column("teams", "leader_id") |
| 133 | + op.drop_column("teams", "team_id") |
| 134 | + op.add_column( |
| 135 | + "jams", |
| 136 | + sa.Column( |
| 137 | + "id", |
| 138 | + sa.INTEGER(), |
| 139 | + server_default=sa.text("nextval('jams_id_seq'::regclass)"), |
| 140 | + autoincrement=True, |
| 141 | + nullable=False, |
| 142 | + ), |
| 143 | + ) |
| 144 | + op.alter_column( |
| 145 | + "jams", "ongoing", existing_type=sa.BOOLEAN(), server_default=sa.text("false"), existing_nullable=False |
| 146 | + ) |
| 147 | + op.alter_column("jams", "name", existing_type=sa.String(), type_=sa.TEXT(), existing_nullable=False) |
| 148 | + op.drop_column("jams", "jam_id") |
| 149 | + op.add_column("infractions", sa.Column("jam_id", sa.INTEGER(), autoincrement=False, nullable=True)) |
| 150 | + op.add_column("infractions", sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False)) |
| 151 | + op.drop_constraint(op.f("infractions_issued_in_jam_id_jams_fk"), "infractions", type_="foreignkey") |
| 152 | + op.drop_constraint(op.f("infractions_user_id_users_fk"), "infractions", type_="foreignkey") |
| 153 | + op.create_foreign_key("infractions_jam_id_jams_fk", "infractions", "jams", ["jam_id"], ["id"]) |
| 154 | + op.create_foreign_key("infractions_user_id_users_fk", "infractions", "users", ["user_id"], ["id"]) |
| 155 | + op.alter_column("infractions", "reason", existing_type=sa.String(), type_=sa.TEXT(), existing_nullable=False) |
| 156 | + op.alter_column("infractions", "user_id", existing_type=sa.BIGINT(), nullable=True) |
| 157 | + op.drop_column("infractions", "issued_in_jam_id") |
| 158 | + op.drop_column("infractions", "infraction_id") |
| 159 | + op.create_table( |
| 160 | + "team_has_user", |
| 161 | + sa.Column("team_id", sa.INTEGER(), autoincrement=False, nullable=False), |
| 162 | + sa.Column("user_id", sa.BIGINT(), autoincrement=False, nullable=False), |
| 163 | + sa.Column("is_leader", sa.BOOLEAN(), autoincrement=False, nullable=False), |
| 164 | + sa.ForeignKeyConstraint(["team_id"], ["teams.id"], name="team_has_user_team_id_teams_fk"), |
| 165 | + sa.ForeignKeyConstraint(["user_id"], ["users.id"], name="team_has_user_user_id_users_fk"), |
| 166 | + sa.PrimaryKeyConstraint("team_id", "user_id", name="team_has_user_pk"), |
| 167 | + ) |
| 168 | + op.create_table( |
| 169 | + "winners", |
| 170 | + sa.Column("jam_id", sa.INTEGER(), autoincrement=False, nullable=False), |
| 171 | + sa.Column("user_id", sa.BIGINT(), autoincrement=False, nullable=False), |
| 172 | + sa.Column("first_place", sa.BOOLEAN(), autoincrement=False, nullable=False), |
| 173 | + sa.ForeignKeyConstraint(["jam_id"], ["jams.id"], name="winners_jam_id_jams_fk"), |
| 174 | + sa.ForeignKeyConstraint(["user_id"], ["users.id"], name="winners_user_id_users_fk"), |
| 175 | + sa.PrimaryKeyConstraint("jam_id", "user_id", name="winners_pk"), |
| 176 | + ) |
| 177 | + op.drop_table("team_has_users") |
| 178 | + op.drop_table("jam_specific_details") |
| 179 | + # ### end Alembic commands ### |
0 commit comments