From 1176f0abf43c356f982e648800e39ad1a7317b96 Mon Sep 17 00:00:00 2001 From: James Nyamweya Date: Thu, 5 Jun 2025 04:43:43 +0300 Subject: [PATCH] Models done --- lib/freebies.db | Bin 20480 -> 28672 bytes .../9223ee4121cb_create_freebies_table.py | 68 ++++++++++++++++++ .../aef5ef5809a8_create_freebies_table.py | 36 ++++++++++ lib/models.py | 38 +++++++--- 4 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 lib/migrations/versions/9223ee4121cb_create_freebies_table.py create mode 100644 lib/migrations/versions/aef5ef5809a8_create_freebies_table.py diff --git a/lib/freebies.db b/lib/freebies.db index 12beb1c963e832db481e7a7493e3029e691ac4dc..6054723e70c7cdc297b73812d940d1beb6d51478 100644 GIT binary patch delta 683 zcmZozz}WDBae}lUCj$cmI}o!0F(VN3PSi1$)A2e~?ixGID= zI{COND8a>*CU56c=Tz|X4^i+7_3@c}k55t=&eKug;tUA#^mPo1RPc68nVExKTvnE`H4YF~gB8GvK*5E_JcP^Q z;n4vy7;Y{?0gBmB#m1?{&$S^r+lfoB%6oEws3Y!%S9`H|`ps>h6002qa$9@0+ delta 208 zcmZp8z}T>Wae}lUD+2=q2*UvLL>*&MRtCLzSzi7h3@m(74E((OJNfGQq&5o*EaTNk zW?>f>6=iJHEJ;ktNli&DD+Xa!=O9 None: + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('companies', schema=None) as batch_op: + batch_op.alter_column('name', + existing_type=sa.VARCHAR(), + nullable=False) + batch_op.alter_column('founding_year', + existing_type=sa.INTEGER(), + nullable=False) + + with op.batch_alter_table('devs', schema=None) as batch_op: + batch_op.alter_column('name', + existing_type=sa.VARCHAR(), + nullable=False) + + with op.batch_alter_table('freebies', schema=None) as batch_op: + batch_op.alter_column('dev_id', + existing_type=sa.INTEGER(), + nullable=False) + batch_op.alter_column('company_id', + existing_type=sa.INTEGER(), + nullable=False) + + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('freebies', schema=None) as batch_op: + batch_op.alter_column('company_id', + existing_type=sa.INTEGER(), + nullable=True) + batch_op.alter_column('dev_id', + existing_type=sa.INTEGER(), + nullable=True) + + with op.batch_alter_table('devs', schema=None) as batch_op: + batch_op.alter_column('name', + existing_type=sa.VARCHAR(), + nullable=True) + + with op.batch_alter_table('companies', schema=None) as batch_op: + batch_op.alter_column('founding_year', + existing_type=sa.INTEGER(), + nullable=True) + batch_op.alter_column('name', + existing_type=sa.VARCHAR(), + nullable=True) + + # ### end Alembic commands ### diff --git a/lib/migrations/versions/aef5ef5809a8_create_freebies_table.py b/lib/migrations/versions/aef5ef5809a8_create_freebies_table.py new file mode 100644 index 000000000..9a6a67131 --- /dev/null +++ b/lib/migrations/versions/aef5ef5809a8_create_freebies_table.py @@ -0,0 +1,36 @@ +"""Create freebies table + +Revision ID: aef5ef5809a8 +Revises: 5f72c58bf48c +Create Date: 2025-06-05 04:38:33.446196 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'aef5ef5809a8' +down_revision = '5f72c58bf48c' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + 'freebies', + sa.Column('id', sa.Integer(), primary_key=True), + sa.Column('item_name', sa.String(), nullable=False), + sa.Column('value', sa.Integer(), nullable=False), + sa.Column('dev_id', sa.Integer(), sa.ForeignKey('devs.id')), + sa.Column('company_id', sa.Integer(), sa.ForeignKey('companies.id')) +) + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/models.py b/lib/models.py index 2681bee5a..a90691f7a 100644 --- a/lib/models.py +++ b/lib/models.py @@ -8,22 +8,44 @@ metadata = MetaData(naming_convention=convention) Base = declarative_base(metadata=metadata) - class Company(Base): - __tablename__ = 'companies' + __tablename__ = "companies" id = Column(Integer(), primary_key=True) - name = Column(String()) - founding_year = Column(Integer()) + name = Column(String(), nullable=False) + founding_year = Column(Integer(), nullable=False) + + freebies = relationship("Freebie", back_populates="company") + devs = relationship("Dev", secondary="freebies", back_populates="companies") def __repr__(self): - return f'' + return f"" class Dev(Base): - __tablename__ = 'devs' + __tablename__ = "devs" + + id = Column(Integer(), primary_key=True) + name = Column(String(), nullable=False) + freebies = relationship("Freebie", back_populates="dev") + companies = relationship("Company", secondary="freebies", back_populates="devs") + + def __repr__(self): + return f"" + + +class Freebie(Base): + __tablename__ = 'freebies' + id = Column(Integer(), primary_key=True) - name= Column(String()) + item_name = Column(String(), nullable=False) + value = Column(Integer(), nullable=False) + + dev_id = Column(Integer(), ForeignKey("devs.id"), nullable=False) + company_id = Column(Integer(), ForeignKey("companies.id"), nullable=False) + + dev = relationship("Dev", back_populates="freebies") + company = relationship("Company", back_populates="freebies") def __repr__(self): - return f'' + return f""