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 000000000..01f8b0e71 Binary files /dev/null and b/freebies.db differ diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/lib/debug.py b/lib/debug.py index 4f922eb69..1eb360e3d 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -1,9 +1,31 @@ #!/usr/bin/env python3 +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) + from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker -from models import Company, Dev +from lib.models import Base, Company, Dev, Freebie # <--- This line relies on the above fix! if __name__ == '__main__': engine = create_engine('sqlite:///freebies.db') - import ipdb; ipdb.set_trace() + Base.metadata.create_all(engine) + Session = sessionmaker(bind=engine) + session = Session() + + print("Ready for debugging!") + print("You have access to: session, Company, Dev, Freebie") + + import ipdb; ipdb.set_trace() # Ensure this is AFTER session is defined + + session.close() \ No newline at end of file diff --git a/lib/migrations/env.py b/lib/migrations/env.py index c7aab9656..d76047814 100644 --- a/lib/migrations/env.py +++ b/lib/migrations/env.py @@ -18,7 +18,7 @@ # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata -from models import Base +from lib.models import Base target_metadata = Base.metadata # other values from the config, defined by the needs of env.py, diff --git a/lib/migrations/versions/a004433866c9_create_freebies_table.py b/lib/migrations/versions/a004433866c9_create_freebies_table.py new file mode 100644 index 000000000..fce4d1104 --- /dev/null +++ b/lib/migrations/versions/a004433866c9_create_freebies_table.py @@ -0,0 +1,28 @@ +"""create freebies table + +Revision ID: a004433866c9 +Revises: 5f72c58bf48c +Create Date: 2025-05-29 06:43:45.137130 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'a004433866c9' +down_revision = '5f72c58bf48c' +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..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