From d651af9040dbfa5b072f4111a80c8103cc6645f6 Mon Sep 17 00:00:00 2001 From: Gabrielle Date: Tue, 26 Dec 2023 16:49:25 +0300 Subject: [PATCH 1/2] file init --- lib/alembic/README | 1 + lib/alembic/env.py | 78 ++++++++++++++++++++++++++++++++++++++ lib/alembic/script.py.mako | 26 +++++++++++++ lib/debug.py | 0 lib/models.py | 4 +- lib/seed.py | 1 + 6 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 lib/alembic/README create mode 100644 lib/alembic/env.py create mode 100644 lib/alembic/script.py.mako mode change 100644 => 100755 lib/debug.py diff --git a/lib/alembic/README b/lib/alembic/README new file mode 100644 index 000000000..98e4f9c44 --- /dev/null +++ b/lib/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/lib/alembic/env.py b/lib/alembic/env.py new file mode 100644 index 000000000..36112a3c6 --- /dev/null +++ b/lib/alembic/env.py @@ -0,0 +1,78 @@ +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool + +from alembic import context + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = None + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, target_metadata=target_metadata + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/lib/alembic/script.py.mako b/lib/alembic/script.py.mako new file mode 100644 index 000000000..fbc4b07dc --- /dev/null +++ b/lib/alembic/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/lib/debug.py b/lib/debug.py old mode 100644 new mode 100755 diff --git a/lib/models.py b/lib/models.py index 2681bee5a..5c4562c8e 100644 --- a/lib/models.py +++ b/lib/models.py @@ -1,7 +1,9 @@ -from sqlalchemy import ForeignKey, Column, Integer, String, MetaData +from sqlalchemy import ForeignKey, Column, Integer, String, MetaData, create_engine from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declarative_base +engine= create_engine('sqlite:///freebies.db') + convention = { "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", } diff --git a/lib/seed.py b/lib/seed.py index b16becbbb..bb1b1ca61 100644 --- a/lib/seed.py +++ b/lib/seed.py @@ -1,3 +1,4 @@ #!/usr/bin/env python3 # Script goes here! + From 49d006d86797141a45d4aee190f696577f24af06 Mon Sep 17 00:00:00 2001 From: Gabrielle Date: Wed, 27 Dec 2023 00:04:54 +0300 Subject: [PATCH 2/2] mock code challenge --- lib/freebies.db | Bin 20480 -> 20480 bytes .../70b2d1525f1e_added_founding_year.py | 28 +++++++++++++ ...36536134dd_instance_tester_in_seed_file.py | 28 +++++++++++++ lib/models.py | 38 +++++++++++++++++- lib/seed.py | 20 +++++++++ 5 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 lib/migrations/versions/70b2d1525f1e_added_founding_year.py create mode 100644 lib/migrations/versions/fd36536134dd_instance_tester_in_seed_file.py diff --git a/lib/freebies.db b/lib/freebies.db index 12beb1c963e832db481e7a7493e3029e691ac4dc..ab0bb23d9804291b64d8d2b5ca8f96901d059e33 100644 GIT binary patch delta 56 zcmZozz}T>Wae_1>+e8^>RyGE`tP2}cCdl)c8zdQ}7@8WHrWvMgQuxA;BCyCn0RYv+ B5K906 delta 56 zcmZozz}T>Wae_1>>qHr6R#pbRc-f696XbbJ)69*MO)ZkrOe~T&DSY8a5m;oP006@2 B5D@?X diff --git a/lib/migrations/versions/70b2d1525f1e_added_founding_year.py b/lib/migrations/versions/70b2d1525f1e_added_founding_year.py new file mode 100644 index 000000000..7398fab25 --- /dev/null +++ b/lib/migrations/versions/70b2d1525f1e_added_founding_year.py @@ -0,0 +1,28 @@ +"""Added Founding Year + +Revision ID: 70b2d1525f1e +Revises: 5f72c58bf48c +Create Date: 2023-12-26 16:54:21.422798 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '70b2d1525f1e' +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/migrations/versions/fd36536134dd_instance_tester_in_seed_file.py b/lib/migrations/versions/fd36536134dd_instance_tester_in_seed_file.py new file mode 100644 index 000000000..f5a4a7a8f --- /dev/null +++ b/lib/migrations/versions/fd36536134dd_instance_tester_in_seed_file.py @@ -0,0 +1,28 @@ +"""instance tester in seed file + +Revision ID: fd36536134dd +Revises: 70b2d1525f1e +Create Date: 2023-12-26 17:12:17.096989 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'fd36536134dd' +down_revision = '70b2d1525f1e' +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 5c4562c8e..cad670025 100644 --- a/lib/models.py +++ b/lib/models.py @@ -18,8 +18,18 @@ class Company(Base): name = Column(String()) founding_year = Column(Integer()) + freebies = relationship('Freebie', backref='company') + devs= relationship('Dev') + + def give_freebie(self, dev, name, session): + freebie = Freebie(name=name, dev=dev, company=self) + session.add(freebie) + session.commit() + def __repr__(self): - return f'' + return f'' \ + + f"{self.founding_year}" + class Dev(Base): __tablename__ = 'devs' @@ -27,5 +37,31 @@ class Dev(Base): id = Column(Integer(), primary_key=True) name= Column(String()) + freebies = relationship('Freebie', backref='dev') + companies = relationship('Company') + + def give_away(self, dev, freebie , session): + if freebie.dev ==self: + freebie.dev ==dev + session.commit() + def __repr__(self): return f'' +class Freebie(Base): + __tablename__='freebies' + + id= Column(Integer(), primary_key=True) + name= Column(String()) + + Dev_id= Column(Integer(), ForeignKey('devs.id')) + Company_id= Column(Integer, ForeignKey('companies.id')) + + dev = relationship('Dev', backref='freebies') + company = relationship('Company', backref='freebies') + + print_details=print(f'{dev.name}' "owns a" f'{name}' "from"f'{company.name}' ) + + + +Freebie.print_details() + \ No newline at end of file diff --git a/lib/seed.py b/lib/seed.py index bb1b1ca61..be3b3044f 100644 --- a/lib/seed.py +++ b/lib/seed.py @@ -1,4 +1,24 @@ #!/usr/bin/env python3 # Script goes here! +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from models import Company, Dev, Base +from sqlalchemy.ext.declarative import declarative_base + +engine = create_engine('sqlite:///freebies.db') +Base.metadata.create_engine_all(engine) + +Session = sessionmaker(bind=engine) +session = Session() + +instance1 = Company(name='MoringaTech', founding_year='2023') +instance2= Dev(name='Justin Sibudi') + +session.add(instance1) +session.add(instance2) + +session.commit() + +session.close() \ No newline at end of file