-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7ab772
commit 871c905
Showing
6 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# db.py | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
from .models import Base | ||
|
||
# Database URL (adjust accordingly) | ||
DATABASE_URL = 'postgresql://username:password@localhost/dbname' | ||
|
||
# Create engine | ||
engine = create_engine(DATABASE_URL, echo=True) | ||
|
||
# Create a session | ||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
||
# Create tables in the database | ||
def init_db(): | ||
Base.metadata.create_all(bind=engine) | ||
|
||
# Dependency to get DB session | ||
def get_db(): | ||
db = SessionLocal() | ||
try: | ||
yield db | ||
finally: | ||
db.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# env.py (Generated by Alembic) | ||
from __future__ import with_statement | ||
from alembic import context | ||
from sqlalchemy import engine_from_config, pool | ||
from logging.config import fileConfig | ||
import os | ||
import sys | ||
|
||
# Add model path to sys.path for access | ||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src'))) | ||
|
||
from models import Base # Ensure your models are imported here | ||
|
||
# Configuring Alembic context | ||
config = context.config | ||
fileConfig(config.config_file_name) | ||
|
||
def run_migrations_online(): | ||
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) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
if __name__ == '__main__': | ||
run_migrations_online() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# xxxx_add_user_table.py (Generated by Alembic) | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
def upgrade(): | ||
op.create_table( | ||
'users', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('username', sa.String(length=100), nullable=False), | ||
sa.Column('email', sa.String(length=100), nullable=False), | ||
sa.Column('password_hash', sa.String(length=255), nullable=False), | ||
sa.Column('created_at', sa.DateTime(), nullable=True), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('email'), | ||
sa.UniqueConstraint('username') | ||
) | ||
|
||
def downgrade(): | ||
op.drop_table('users') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from sqlalchemy import Column, Integer, String, Float, DateTime | ||
from sqlalchemy.ext.declarative import declarative_base | ||
import datetime | ||
|
||
Base = declarative_base() | ||
|
||
class Product(Base): | ||
__tablename__ = 'products' | ||
|
||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
name = Column(String(100), nullable=False) | ||
description = Column(String(255)) | ||
price = Column(Float, nullable=False) | ||
created_at = Column(DateTime, default=datetime.datetime.utcnow) | ||
|
||
def __repr__(self): | ||
return f"<Product(id={self.id}, name={self.name}, price={self.price})>" | ||
|
||
def __init__(self, name, description, price): | ||
self.name = name | ||
self.description = description | ||
self.price = price |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from sqlalchemy.orm import Session | ||
from .user_model import User | ||
from .product_model import Product | ||
|
||
def get_user_by_id(db: Session, user_id: int): | ||
return db.query(User).filter(User.id == user_id).first() | ||
|
||
def get_all_products(db: Session): | ||
return db.query(Product).all() | ||
|
||
def create_user(db: Session, username: str, email: str, password_hash: str): | ||
db_user = User(username=username, email=email, password_hash=password_hash) | ||
db.add(db_user) | ||
db.commit() | ||
db.refresh(db_user) | ||
return db_user | ||
|
||
def create_product(db: Session, name: str, description: str, price: float): | ||
db_product = Product(name=name, description=description, price=price) | ||
db.add(db_product) | ||
db.commit() | ||
db.refresh(db_product) | ||
return db_product |