From d6cda18621dba059a004813d40e0227e6c3a5ff7 Mon Sep 17 00:00:00 2001 From: DOROTHY CHEPKOECH Date: Thu, 29 May 2025 07:27:02 +0300 Subject: [PATCH] Debugging --- lib/alembic.ini => alembic.ini | 2 +- freebies.db | Bin 0 -> 24576 bytes lib/__init__.py | 0 lib/debug.py | 26 ++++++- lib/migrations/env.py | 2 +- .../a004433866c9_create_freebies_table.py | 28 ++++++++ lib/models.py | 29 +++++++- lib/seed.py | 67 +++++++++++++++++- 8 files changed, 148 insertions(+), 6 deletions(-) rename lib/alembic.ini => alembic.ini (98%) create mode 100644 freebies.db create mode 100644 lib/__init__.py create mode 100644 lib/migrations/versions/a004433866c9_create_freebies_table.py diff --git a/lib/alembic.ini b/alembic.ini similarity index 98% rename from lib/alembic.ini rename to alembic.ini index 953863ddd..a256866c9 100644 --- a/lib/alembic.ini +++ b/alembic.ini @@ -2,7 +2,7 @@ [alembic] # path to migration scripts -script_location = migrations +script_location = lib/migrations # template used to generate migration file names; The default value is %%(rev)s_%%(slug)s # Uncomment the line below if you want the files to be prepended with date and time diff --git a/freebies.db b/freebies.db new file mode 100644 index 0000000000000000000000000000000000000000..01f8b0e71e395c8efa3be1c0494e0019007e13c8 GIT binary patch literal 24576 zcmeI(O=}ZD7zgl~-Mns+7*9kWiIsO3{O08n(5Xrgj@&dRjKgBwdp1#_Xop zleFNs@#dj;(XZgaPhjyV2qJiNCh4|G5Va?%{D%xP`|Rw@>~D4t!;+otf)j97^Exd% zU|EtRl#+Xl5ke9o$3zZOL{wxB6AdfBDHG(z(+89K4-%btPxMLs?bOSuE24@70SG_< z0uX=z1Rwwb2teSX0!PoH@zmTLeWeC=xyfrC&dUz>!!njPjaN(b`Pxc>t(dc_eUa+ z%#F>Qxyv3JyDaTgGqP2FtGH7#b7EXpYgplItn)5UtKtUO+Lmc-tQY0=>GK8|W*TdT zX%zFu4(o?y<39}0pPeO&;ePw^p+0+UWOs>7B@$1~%+SM3Pgb6gIXWcTZ~oVsqbP~Y z;kX)4&CkfxL z>{P6M-tisJ9XV8nW;SwC&hPXx%k8$pM=G7Yl^J^81|Er#9;|kw!iJ_e(4SbIyGksz zy-11UR{0D6c~k7-ExQ|dy<=;%vzsFURlI0p@@1P8nd)DO_#r_60uX=z1Rwwb2tWV= z5P$##An>0EOi^XQURqjS&Sr0~tW@sEkNnvG|0j!!+kgNBAOHafKmY;|fB*y_009V` z3h3&BHn{(n-~UzpBhkO#2)2m)tqbiv-{9uoFbu-nOe_YBC}tRBP~q%AW1Gijq`g 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..daa1a1efc 100644 --- a/lib/models.py +++ b/lib/models.py @@ -2,6 +2,7 @@ from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declarative_base +# Define naming convention for foreign keys convention = { "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", } @@ -16,8 +17,16 @@ class Company(Base): name = Column(String()) founding_year = Column(Integer()) + # Establish relationship with Freebie: one Company can have many Freebies + freebies = relationship('Freebie', backref='company') + + def give_freebie(self, dev, name, value): + # Method to create a new Freebie instance and associate it with the company and dev + new_freebie = Freebie(company=self, dev=dev, name=name, value=value) + return new_freebie + def __repr__(self): - return f'' + return f'' class Dev(Base): __tablename__ = 'devs' @@ -25,5 +34,23 @@ class Dev(Base): id = Column(Integer(), primary_key=True) name= Column(String()) + # Establish relationship with Freebie: one Dev can receive many Freebies + freebies = relationship('Freebie', backref='dev') + def __repr__(self): return f'' + +class Freebie(Base): + __tablename__ = 'freebies' + + id = Column(Integer(), primary_key=True) + name = Column(String()) + value = Column(Integer()) # e.g., points, monetary value + + # Foreign key for Company + company_id = Column(Integer(), ForeignKey('companies.id')) + # Foreign key for Dev + dev_id = Column(Integer(), ForeignKey('devs.id')) + + def __repr__(self): + return f'' \ No newline at end of file diff --git a/lib/seed.py b/lib/seed.py index b16becbbb..5d94a6dfc 100644 --- a/lib/seed.py +++ b/lib/seed.py @@ -1,3 +1,68 @@ #!/usr/bin/env python3 -# Script goes here! +import sys +import os + +# Get the absolute path to the directory containing this script (which is 'lib/') +current_script_dir = os.path.dirname(os.path.abspath(__file__)) + +# Get the absolute path to the project root (the parent directory of 'lib/') +project_root_dir = os.path.join(current_script_dir, '..') + +# Add the project root to sys.path +sys.path.insert(0, project_root_dir) + +# --- You can add these lines for temporary debugging to see what's on your path --- +# print(f"sys.path now includes: {sys.path[0]}") +# print(f"Expected project root: {project_root_dir}") +# ---------------------------------------------------------------------------------- + +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + + +from lib.models import Base, Company, Dev, Freebie # Corrected import path + +if __name__ == '__main__': + engine = create_engine('sqlite:///freebies.db') + Base.metadata.create_all(engine) + Session = sessionmaker(bind=engine) + session = Session() + + print("Deleting old data...") + + session.query(Freebie).delete() + session.query(Company).delete() + session.query(Dev).delete() + session.commit() + print("Old data deleted.") + + print("Creating sample companies and devs...") + # Sample companies here + apple = Company(name='Apple', founding_year=1971) + google = Company(name='Google', founding_year=1975) + microsoft = Company(name='Microsoft', founding_year=1975) + + # Sample devs here + esther = Dev(name='Esther') + bob = Dev(name='Bob') + reign = Dev(name='Reign') + + session.add_all([apple, google, microsoft, esther, bob, reign]) + session.commit() + print("Companies and devs created.") + + print("Creating sample freebies...") + # Sample freebies here (using the give_freebie method from Company) + sticker = apple.give_freebie(esther, 'sticker', 10) + mug = google.give_freebie(bob, 'mug', 5) + t_shirt = microsoft.give_freebie(reign, 't-shirt', 3) + keychain = google.give_freebie(esther, 'keychain', 2) # Added another freebie for Esther + mousepad = apple.give_freebie(reign, 'mousepad', 8) + + session.add_all([sticker, mug, t_shirt, keychain, mousepad]) + session.commit() + print("Freebies created.") + + print('Database seeded successfully!') + session.close() \ No newline at end of file