diff --git a/lib/__pycache__/models.cpython-38.pyc b/lib/__pycache__/models.cpython-38.pyc new file mode 100644 index 000000000..1e0a0e705 Binary files /dev/null and b/lib/__pycache__/models.cpython-38.pyc differ diff --git a/lib/debug.py b/lib/debug.py index 4f922eb69..31ef4791d 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -1,9 +1,51 @@ -#!/usr/bin/env python3 - from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from models import Base, Company, Dev, Freebie + +# Create an SQLite database (you can replace 'sqlite:///your_database.db' with your preferred database URL). +engine = create_engine('sqlite:///freebies.db') + +# Create the tables in the database. +Base.metadata.create_all(engine) + +# Create a session to interact with the database. +Session = sessionmaker(bind=engine) +session = Session() + +# Create sample data (replace this with your actual data population logic). +company1 = Company(name='Company A', founding_year=2000) +company2 = Company(name='Company B', founding_year=2010) + +dev1 = Dev(name='Dev 1') +dev2 = Dev(name='Dev 2') + +# Add the objects to the session and commit them to the database. +session.add_all([company1, company2, dev1, dev2]) +session.commit() + +# Create freebies with company and dev relationships based on your structure. +freebie1 = Freebie(item_name='Item 1', value=100, company_id=company1.id, dev_id=dev1.id) +freebie2 = Freebie(item_name='Item 2', value=50, company_id=company2.id, dev_id=dev1.id) +freebie3 = Freebie(item_name='Item 3', value=75, company_id=company1.id, dev_id=dev2.id) + +# Add the freebies to the session and commit them to the database. +session.add_all([freebie1, freebie2, freebie3]) +session.commit() + +# Now you can test the methods you mentioned: + +# Get all freebies for a company. +company1_freebies = session.query(Freebie).filter_by(company_id=company1.id).all() +print("Company 1 Freebies:", company1_freebies) + +# Get all devs who collected freebies from a company. +company1_devs = session.query(Dev).filter(Dev.freebies.any(company_id=company1.id)).all() +print("Devs from Company 1:", company1_devs) -from models import Company, Dev +# Get all freebies collected by a dev. +dev1_freebies = session.query(Freebie).filter_by(dev_id=dev1.id).all() +print("Dev 1 Freebies:", dev1_freebies) -if __name__ == '__main__': - engine = create_engine('sqlite:///freebies.db') - import ipdb; ipdb.set_trace() +# Get all companies that a dev has collected freebies from. +dev1_companies = session.query(Company).filter(Company.devs.any(id=dev1.id)).all() +print("Companies for Dev 1:", dev1_companies) diff --git a/lib/freebies.db b/lib/freebies.db index 12beb1c96..08582a05a 100644 Binary files a/lib/freebies.db and b/lib/freebies.db differ diff --git a/lib/migrations/__pycache__/env.cpython-38.pyc b/lib/migrations/__pycache__/env.cpython-38.pyc new file mode 100644 index 000000000..df36cd94a Binary files /dev/null and b/lib/migrations/__pycache__/env.cpython-38.pyc differ diff --git a/lib/migrations/versions/1b04dacf764a_seeding_4_freebie_model.py b/lib/migrations/versions/1b04dacf764a_seeding_4_freebie_model.py new file mode 100644 index 000000000..8012af4b5 --- /dev/null +++ b/lib/migrations/versions/1b04dacf764a_seeding_4_freebie_model.py @@ -0,0 +1,28 @@ +"""Seeding 4 freebie model + +Revision ID: 1b04dacf764a +Revises: 8954dbace60b +Create Date: 2023-09-02 11:58:37.206591 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '1b04dacf764a' +down_revision = '8954dbace60b' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/5f72c58bf48c_create_companies_devs.py b/lib/migrations/versions/5f72c58bf48c_create_companies_devs.py index c191bb2f9..66084b285 100644 --- a/lib/migrations/versions/5f72c58bf48c_create_companies_devs.py +++ b/lib/migrations/versions/5f72c58bf48c_create_companies_devs.py @@ -29,6 +29,16 @@ def upgrade() -> None: sa.Column('name', sa.String(), nullable=True), sa.PrimaryKeyConstraint('id') ) + + op.create_table( + 'freebies', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('item_name', sa.String(length=255), nullable=True), + sa.Column('value', sa.Integer(), nullable=True), + sa.Column('dev_id', sa.Integer(), sa.ForeignKey('dev.id'), nullable=True), + sa.Column('company_id', sa.Integer(), sa.ForeignKey('company.id'), nullable=True), + sa.PrimaryKeyConstraint('id') + ) # ### end Alembic commands ### diff --git a/lib/migrations/versions/7a71dbf71c64_create_db.py b/lib/migrations/versions/7a71dbf71c64_create_db.py index 23e0a655b..395fe7be3 100644 --- a/lib/migrations/versions/7a71dbf71c64_create_db.py +++ b/lib/migrations/versions/7a71dbf71c64_create_db.py @@ -17,7 +17,15 @@ def upgrade() -> None: - pass + op.create_table( + 'freebies', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('item_name', sa.String(length=255), nullable=True), + sa.Column('value', sa.Integer(), nullable=True), + sa.Column('dev_id', sa.Integer(), sa.ForeignKey('dev.id'), nullable=True), + sa.Column('company_id', sa.Integer(), sa.ForeignKey('company.id'), nullable=True), + sa.PrimaryKeyConstraint('id') + ) def downgrade() -> None: diff --git a/lib/migrations/versions/8954dbace60b_seeding_3_freebie_model.py b/lib/migrations/versions/8954dbace60b_seeding_3_freebie_model.py new file mode 100644 index 000000000..21041a752 --- /dev/null +++ b/lib/migrations/versions/8954dbace60b_seeding_3_freebie_model.py @@ -0,0 +1,28 @@ +"""Seeding 3 freebie model + +Revision ID: 8954dbace60b +Revises: ed6dd4d46944 +Create Date: 2023-09-02 11:54:58.756855 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '8954dbace60b' +down_revision = 'ed6dd4d46944' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/90acd96a56c6_create_freebie_model.py b/lib/migrations/versions/90acd96a56c6_create_freebie_model.py new file mode 100644 index 000000000..4b816bf9b --- /dev/null +++ b/lib/migrations/versions/90acd96a56c6_create_freebie_model.py @@ -0,0 +1,33 @@ +"""Create Freebie Model + +Revision ID: 90acd96a56c6 +Revises: 5f72c58bf48c +Create Date: 2023-09-02 11:33:27.263903 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '90acd96a56c6' +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(), nullable=False), + sa.Column('item_name', sa.String(), nullable=True), + sa.Column('value', sa.Integer(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('freebies') + # ### end Alembic commands ### diff --git a/lib/migrations/versions/a357d3974d95_seeding_55_freebie_model.py b/lib/migrations/versions/a357d3974d95_seeding_55_freebie_model.py new file mode 100644 index 000000000..23aefad40 --- /dev/null +++ b/lib/migrations/versions/a357d3974d95_seeding_55_freebie_model.py @@ -0,0 +1,28 @@ +"""Seeding 55 freebie model + +Revision ID: a357d3974d95 +Revises: a999e21a9763 +Create Date: 2023-09-02 12:03:30.480276 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'a357d3974d95' +down_revision = 'a999e21a9763' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/a999e21a9763_seeding_54_freebie_model.py b/lib/migrations/versions/a999e21a9763_seeding_54_freebie_model.py new file mode 100644 index 000000000..89461fbdf --- /dev/null +++ b/lib/migrations/versions/a999e21a9763_seeding_54_freebie_model.py @@ -0,0 +1,28 @@ +"""Seeding 54 freebie model + +Revision ID: a999e21a9763 +Revises: 1b04dacf764a +Create Date: 2023-09-02 12:01:16.615509 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'a999e21a9763' +down_revision = '1b04dacf764a' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/ae0c1a34c35e_polished_relationships_and_added_forign_.py b/lib/migrations/versions/ae0c1a34c35e_polished_relationships_and_added_forign_.py new file mode 100644 index 000000000..074edf300 --- /dev/null +++ b/lib/migrations/versions/ae0c1a34c35e_polished_relationships_and_added_forign_.py @@ -0,0 +1,38 @@ +"""Polished relationships and added forign keys to Freebie model + +Revision ID: ae0c1a34c35e +Revises: dd36c80e9ece +Create Date: 2023-09-02 22:27:33.695026 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ae0c1a34c35e' +down_revision = 'dd36c80e9ece' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('freebies', schema=None) as batch_op: + batch_op.add_column(sa.Column('company_id', sa.Integer(), nullable=True)) + batch_op.add_column(sa.Column('dev_id', sa.Integer(), nullable=True)) + batch_op.create_foreign_key(batch_op.f('fk_freebies_dev_id_devs'), 'devs', ['dev_id'], ['id']) + batch_op.create_foreign_key(batch_op.f('fk_freebies_company_id_companies'), 'companies', ['company_id'], ['id']) + + # ### 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.drop_constraint(batch_op.f('fk_freebies_company_id_companies'), type_='foreignkey') + batch_op.drop_constraint(batch_op.f('fk_freebies_dev_id_devs'), type_='foreignkey') + batch_op.drop_column('dev_id') + batch_op.drop_column('company_id') + + # ### end Alembic commands ### diff --git a/lib/migrations/versions/d45d79e35116_seed_data.py b/lib/migrations/versions/d45d79e35116_seed_data.py new file mode 100644 index 000000000..57688be0c --- /dev/null +++ b/lib/migrations/versions/d45d79e35116_seed_data.py @@ -0,0 +1,28 @@ +"""Seed Data + +Revision ID: d45d79e35116 +Revises: 90acd96a56c6 +Create Date: 2023-09-02 11:50:44.919728 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'd45d79e35116' +down_revision = '90acd96a56c6' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/dd36c80e9ece_update_models.py b/lib/migrations/versions/dd36c80e9ece_update_models.py new file mode 100644 index 000000000..c8169f97a --- /dev/null +++ b/lib/migrations/versions/dd36c80e9ece_update_models.py @@ -0,0 +1,40 @@ +"""Update Models + +Revision ID: dd36c80e9ece +Revises: ff8003d387e3 +Create Date: 2023-09-02 12:31:35.588055 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'dd36c80e9ece' +down_revision = 'ff8003d387e3' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('dev_companies', + sa.Column('dev_id', sa.Integer(), nullable=True), + sa.Column('company_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['company_id'], ['companies.id'], name=op.f('fk_dev_companies_company_id_companies')), + sa.ForeignKeyConstraint(['dev_id'], ['devs.id'], name=op.f('fk_dev_companies_dev_id_devs')) + ) + op.create_table('dev_freebies', + sa.Column('dev_id', sa.Integer(), nullable=True), + sa.Column('freebie_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['dev_id'], ['devs.id'], name=op.f('fk_dev_freebies_dev_id_devs')), + sa.ForeignKeyConstraint(['freebie_id'], ['freebies.id'], name=op.f('fk_dev_freebies_freebie_id_freebies')) + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('dev_freebies') + op.drop_table('dev_companies') + # ### end Alembic commands ### diff --git a/lib/migrations/versions/ed6dd4d46944_seeding_freebie_model.py b/lib/migrations/versions/ed6dd4d46944_seeding_freebie_model.py new file mode 100644 index 000000000..57b53caba --- /dev/null +++ b/lib/migrations/versions/ed6dd4d46944_seeding_freebie_model.py @@ -0,0 +1,28 @@ +"""Seeding freebie model + +Revision ID: ed6dd4d46944 +Revises: d45d79e35116 +Create Date: 2023-09-02 11:53:10.081778 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ed6dd4d46944' +down_revision = 'd45d79e35116' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/ff8003d387e3_seeding_6_freebie_model.py b/lib/migrations/versions/ff8003d387e3_seeding_6_freebie_model.py new file mode 100644 index 000000000..276a79969 --- /dev/null +++ b/lib/migrations/versions/ff8003d387e3_seeding_6_freebie_model.py @@ -0,0 +1,28 @@ +"""Seeding 6 freebie model + +Revision ID: ff8003d387e3 +Revises: a357d3974d95 +Create Date: 2023-09-02 12:06:40.253039 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ff8003d387e3' +down_revision = 'a357d3974d95' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + 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..fb9a3a5f7 100644 --- a/lib/models.py +++ b/lib/models.py @@ -1,4 +1,4 @@ -from sqlalchemy import ForeignKey, Column, Integer, String, MetaData +from sqlalchemy import ForeignKey, Column, Integer, String, MetaData, Table from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declarative_base @@ -9,6 +9,21 @@ Base = declarative_base(metadata=metadata) +##Association tables +dev_freebies = Table('dev_freebies', Base.metadata, + Column('dev_id', Integer(), ForeignKey('devs.id')), + Column('freebie_id', Integer(), ForeignKey('freebies.id')) +) + + +dev_companies = Table('dev_companies', Base.metadata, + Column('dev_id', Integer, ForeignKey('devs.id')), + Column('company_id', Integer, ForeignKey('companies.id')) +) + + + + class Company(Base): __tablename__ = 'companies' @@ -16,9 +31,17 @@ class Company(Base): name = Column(String()) founding_year = Column(Integer()) + devs = relationship('Dev', secondary='dev_companies', backref='companies') + def __repr__(self): return f'' + def give_freebie(self, dev, item_name, value): + # Create a new Freebie instance associated with this company and the given dev. + freebie = Freebie(item_name=item_name, value=value, company=self, dev=dev) + session.add(freebie) + session.commit() + class Dev(Base): __tablename__ = 'devs' @@ -27,3 +50,30 @@ class Dev(Base): def __repr__(self): return f'' + + freebies = relationship('Freebie', secondary='dev_freebies', backref = 'devs') + + def received_one(self, item_name): + # Check if any of the freebies associated with the dev has the specified item_name. + return any(freebie.item_name == item_name for freebie in self.freebies) + + def give_away(self, other_dev, freebie): + # Ensure that the freebie belongs to the dev who's giving it away. + if freebie.dev == self: + freebie.dev = other_dev + session.commit() + +class Freebie(Base): + __tablename__ = 'freebies' + + id = Column(Integer(), primary_key=True) + item_name= Column(String()) + value=Column(Integer()) + company_id=Column(Integer(), ForeignKey('companies.id')) + dev_id = Column(Integer(), ForeignKey('devs.id')) + + def __repr__(self): + return f'' + + def print_details(self): + return f'{self.dev.name} owns a {self.item_name} from {self.company.name}' \ No newline at end of file diff --git a/lib/seed.py b/lib/seed.py index b16becbbb..edd681093 100644 --- a/lib/seed.py +++ b/lib/seed.py @@ -1,3 +1,33 @@ #!/usr/bin/env python3 # Script goes here! +from faker import Faker + +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from models import Freebie, Dev, Company + + +engine=create_engine('sqlite:///freebies.db') +Session = sessionmaker(bind=engine) +session=Session() + +session.query(Freebie).delete() +session.query(Dev).delete() +session.query(Company).delete() + +fake = Faker() + +freebies =[] +for i in range(50): + freebie=Freebie( + item_name =fake.unique.name(), + value=fake.random_int(min=1, max=50) + + ) + session.add(freebie) + freebies.append(freebie) +session.commit() +session.close() + + \ No newline at end of file