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

Fix attribute error when saving mod that was in deleted modpack #380

Merged
merged 1 commit into from
Aug 13, 2021
Merged
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
10 changes: 5 additions & 5 deletions KerbalStuff/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,12 @@ def __repr__(self) -> str:
class ModListItem(Base): # type: ignore
__tablename__ = 'modlistitem'
id = Column(Integer, primary_key=True)
mod_id = Column(Integer, ForeignKey('mod.id'))
mod = relationship('Mod', backref='mod_list_items')
mod_list_id = Column(Integer, ForeignKey('modlist.id'))
mod_id = Column(Integer, ForeignKey('mod.id', ondelete='CASCADE'), nullable=False)
mod = relationship('Mod', backref=backref('mod_list_items', passive_deletes=True))
mod_list_id = Column(Integer, ForeignKey('modlist.id', ondelete='CASCADE'), nullable=False)
mod_list = relationship('ModList',
backref=backref('mods', order_by="asc(ModListItem.sort_index)"))
sort_index = Column(Integer, default=0)
backref=backref('mods', passive_deletes=True, order_by="asc(ModListItem.sort_index)"))
sort_index = Column(Integer, default=0, nullable=False)

def __repr__(self) -> str:
return '<ModListItem %r %r>' % (self.mod_id, self.mod_list_id)
Expand Down
40 changes: 40 additions & 0 deletions alembic/versions/2021_07_30_16_16_44-1cbfc1acd83d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Make most of modlistitem non-nullable

Revision ID: 1cbfc1acd83d
Revises: 833ba1ee8c72
Create Date: 2021-07-30 16:16:44

"""

# revision identifiers, used by Alembic.
revision = '1cbfc1acd83d'
down_revision = '833ba1ee8c72'

from alembic import op
import sqlalchemy as sa


def upgrade() -> None:
# Remove broken rows
op.execute("DELETE FROM modlistitem WHERE mod_id is NULL OR mod_list_id is NULL OR sort_index is NULL")

# Make most of the columns non-nullable
op.alter_column('modlistitem', 'mod_id', existing_type=sa.INTEGER(), nullable=False)
op.alter_column('modlistitem', 'mod_list_id', existing_type=sa.INTEGER(), nullable=False)
op.alter_column('modlistitem', 'sort_index', existing_type=sa.INTEGER(), nullable=False)

# Cascade deletion for the mod and mod list
op.drop_constraint('modlistitem_mod_list_id_fkey', 'modlistitem', type_='foreignkey')
op.drop_constraint('modlistitem_mod_id_fkey', 'modlistitem', type_='foreignkey')
op.create_foreign_key('modlistitem_mod_list_id_fkey', 'modlistitem', 'modlist', ['mod_list_id'], ['id'], ondelete='CASCADE')
op.create_foreign_key('modlistitem_mod_id_fkey', 'modlistitem', 'mod', ['mod_id'], ['id'], ondelete='CASCADE')


def downgrade() -> None:
op.drop_constraint('modlistitem_mod_id_fkey', 'modlistitem', type_='foreignkey')
op.drop_constraint('modlistitem_mod_list_id_fkey', 'modlistitem', type_='foreignkey')
op.create_foreign_key('modlistitem_mod_id_fkey', 'modlistitem', 'mod', ['mod_id'], ['id'])
op.create_foreign_key('modlistitem_mod_list_id_fkey', 'modlistitem', 'modlist', ['mod_list_id'], ['id'])
op.alter_column('modlistitem', 'sort_index', existing_type=sa.INTEGER(), nullable=True)
op.alter_column('modlistitem', 'mod_list_id', existing_type=sa.INTEGER(), nullable=True)
op.alter_column('modlistitem', 'mod_id', existing_type=sa.INTEGER(), nullable=True)