remodeling table models and migration to postgres
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
from app.schemas.models import SQLModel
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
|
||||
from alembic import context
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from sqlmodel import SQLModel
|
||||
from app.schemas.models import Client, Supplier, Product, Payment, Credit
|
||||
from app.core.config import settings
|
||||
|
||||
load_dotenv()
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
config.set_main_option('sqlalchemy.url', str(settings.database_uri)) # type: ignore
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
@@ -24,7 +22,6 @@ if config.config_file_name is not None:
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
target_metadata = SQLModel.metadata
|
||||
url = os.getenv("DATABASE_URI")
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
@@ -66,7 +63,6 @@ def run_migrations_online() -> None:
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
url=url,
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ ${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
down_revision: Union[str, Sequence[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)}
|
||||
|
||||
|
||||
+6
-6
@@ -1,8 +1,8 @@
|
||||
"""Checking for updates
|
||||
"""Initial tables
|
||||
|
||||
Revision ID: e8c4300db3cb
|
||||
Revises: bfb086d8d500
|
||||
Create Date: 2025-06-08 19:06:55.200977
|
||||
Revision ID: 0aa4734ce008
|
||||
Revises:
|
||||
Create Date: 2025-08-17 16:44:05.785214
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
@@ -12,8 +12,8 @@ import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'e8c4300db3cb'
|
||||
down_revision: Union[str, None] = 'bfb086d8d500'
|
||||
revision: str = '0aa4734ce008'
|
||||
down_revision: Union[str, Sequence[str], None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
"""Date_modified columns - add default=None
|
||||
|
||||
Revision ID: 174e0494276d
|
||||
Revises: d89dba0432de
|
||||
Create Date: 2025-06-15 20:42:30.850962
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '174e0494276d'
|
||||
down_revision: Union[str, None] = 'd89dba0432de'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
||||
+11
-12
@@ -1,20 +1,19 @@
|
||||
"""Rebuild
|
||||
"""Initial tables
|
||||
|
||||
Revision ID: 5840d2b52dd8
|
||||
Revises:
|
||||
Create Date: 2025-06-01 14:27:25.657473
|
||||
Revision ID: 4966e016dd7c
|
||||
Revises: 0aa4734ce008
|
||||
Create Date: 2025-08-17 16:50:53.587969
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
import sqlmodel
|
||||
|
||||
import sqlmodel.sql.sqltypes
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '5840d2b52dd8'
|
||||
down_revision: Union[str, None] = None
|
||||
revision: str = '4966e016dd7c'
|
||||
down_revision: Union[str, Sequence[str], None] = '0aa4734ce008'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
@@ -52,8 +51,8 @@ def upgrade() -> None:
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('transcation_type', sa.Enum('BUY', 'SELL', name='tradetype'), nullable=False),
|
||||
sa.Column('product_code', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('client_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('supplier_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('client_id', sa.Integer(), nullable=False),
|
||||
sa.Column('supplier_id', sa.Integer(), nullable=False),
|
||||
sa.Column('qty', sa.Integer(), nullable=False),
|
||||
sa.Column('amount', sa.Integer(), nullable=False),
|
||||
sa.Column('date', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||||
@@ -66,8 +65,8 @@ def upgrade() -> None:
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('payment_type', sa.Enum('BUY', 'SELL', name='tradetype'), nullable=False),
|
||||
sa.Column('product_code', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('client_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('supplier_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('client_id', sa.Integer(), nullable=False),
|
||||
sa.Column('supplier_id', sa.Integer(), nullable=False),
|
||||
sa.Column('amount', sa.Integer(), nullable=False),
|
||||
sa.Column('payment_method', sqlmodel.sql.sqltypes.AutoString(length=24), nullable=False),
|
||||
sa.Column('date', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||||
@@ -1,38 +0,0 @@
|
||||
"""Product id made nullable
|
||||
|
||||
Revision ID: 8aefa882e096
|
||||
Revises: e8c4300db3cb
|
||||
Create Date: 2025-06-15 19:33:39.299803
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import mysql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '8aefa882e096'
|
||||
down_revision: Union[str, None] = 'e8c4300db3cb'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.alter_column('product', 'id',
|
||||
existing_type=mysql.INTEGER(),
|
||||
nullable=True,
|
||||
autoincrement=True)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.alter_column('product', 'id',
|
||||
existing_type=mysql.INTEGER(),
|
||||
nullable=False,
|
||||
autoincrement=True)
|
||||
# ### end Alembic commands ###
|
||||
@@ -1,32 +0,0 @@
|
||||
"""Product id made nullable using 'default=None'
|
||||
|
||||
Revision ID: b5ff3e70bd95
|
||||
Revises: 8aefa882e096
|
||||
Create Date: 2025-06-15 19:38:20.874456
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'b5ff3e70bd95'
|
||||
down_revision: Union[str, None] = '8aefa882e096'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
||||
@@ -1,60 +0,0 @@
|
||||
"""Fix client_id in Credit type to int
|
||||
|
||||
Revision ID: bfb086d8d500
|
||||
Revises: 5840d2b52dd8
|
||||
Create Date: 2025-06-01 14:53:57.095181
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
import sqlmodel
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'bfb086d8d500'
|
||||
down_revision: Union[str, None] = '5840d2b52dd8'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('credit',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('transcation_type', sa.Enum('BUY', 'SELL', name='tradetype'), nullable=False),
|
||||
sa.Column('product_code', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('client_id', sa.Integer(), nullable=False),
|
||||
sa.Column('supplier_id', sa.Integer(), nullable=False),
|
||||
sa.Column('qty', sa.Integer(), nullable=False),
|
||||
sa.Column('amount', sa.Integer(), nullable=False),
|
||||
sa.Column('date', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||||
sa.ForeignKeyConstraint(['client_id'], ['client.id'], ),
|
||||
sa.ForeignKeyConstraint(['product_code'], ['product.product_code'], ),
|
||||
sa.ForeignKeyConstraint(['supplier_id'], ['supplier.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_table('payment',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('payment_type', sa.Enum('BUY', 'SELL', name='tradetype'), nullable=False),
|
||||
sa.Column('product_code', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('client_id', sa.Integer(), nullable=False),
|
||||
sa.Column('supplier_id', sa.Integer(), nullable=False),
|
||||
sa.Column('amount', sa.Integer(), nullable=False),
|
||||
sa.Column('payment_method', sqlmodel.sql.sqltypes.AutoString(length=24), nullable=False),
|
||||
sa.Column('date', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||||
sa.ForeignKeyConstraint(['client_id'], ['client.id'], ),
|
||||
sa.ForeignKeyConstraint(['product_code'], ['product.product_code'], ),
|
||||
sa.ForeignKeyConstraint(['supplier_id'], ['supplier.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('payment')
|
||||
op.drop_table('credit')
|
||||
# ### end Alembic commands ###
|
||||
@@ -1,32 +0,0 @@
|
||||
"""Making Product.date_modified default=None
|
||||
|
||||
Revision ID: d89dba0432de
|
||||
Revises: b5ff3e70bd95
|
||||
Create Date: 2025-06-15 20:06:11.734486
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'd89dba0432de'
|
||||
down_revision: Union[str, None] = 'b5ff3e70bd95'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,26 @@
|
||||
from sqlmodel import SQLModel
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class UserRole(str, Enum):
|
||||
ADMIN = "admin"
|
||||
WRITE = "write"
|
||||
READ_ONLY = "read_only"
|
||||
|
||||
|
||||
class TransactionType(str, Enum):
|
||||
SALE = "sell"
|
||||
PURCHASE = "buy"
|
||||
CREDIT = "credit"
|
||||
|
||||
|
||||
class TransactionStatus(str, Enum):
|
||||
UNPAID = "unpaid"
|
||||
PARTIALLY_PAID = "partially_paid"
|
||||
PAID = "paid"
|
||||
CANCELLED = 'cancelled'
|
||||
|
||||
|
||||
class PartnerType(str, Enum):
|
||||
CLIENT = "client"
|
||||
SUPPLIER = "supplier"
|
||||
@@ -18,86 +18,100 @@ from datetime import datetime
|
||||
from sqlalchemy import Column, DateTime, func, Enum as SQLEnum
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
from base import UserRole, PartnerType, TransactionType, TransactionStatus
|
||||
|
||||
|
||||
class TradeType(str, Enum):
|
||||
BUY = "Buy"
|
||||
SELL = "Sell"
|
||||
class User(SQLModel, table=True):
|
||||
"""
|
||||
User table mapping, api response validation and serialisation
|
||||
"""
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
username: str = Field(nullable=False,unique=True, max_length=100)
|
||||
role: UserRole = Field(nullable=True, default=PartnerType.CLIENT)
|
||||
password_hash: str = Field(nullable=False)
|
||||
|
||||
|
||||
class Client(SQLModel, table=True):
|
||||
class Partner(SQLModel, table=True):
|
||||
"""Clients table mapping, api response validation and serialisation"""
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
tin_number: int = Field(nullable=False, unique=True)
|
||||
names: str = Field(max_length=100, nullable=False)
|
||||
phone_number: str = Field(max_length=10, nullable=False)
|
||||
|
||||
|
||||
class Supplier(SQLModel, table=True):
|
||||
"""Supplier table mapping, api response validation and serialisation"""
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
tin_number: int = Field(nullable=False, unique=True)
|
||||
names: str = Field(max_length=100, nullable=False)
|
||||
phone_number: str = Field(max_length=10, nullable=False)
|
||||
type: PartnerType = Field(nullable=False, default=PartnerType.CLIENT)
|
||||
phone_number: str = Field(max_length=10, nullable=True)
|
||||
|
||||
|
||||
class Product(SQLModel, table=True):
|
||||
"""Products table mapping, api response validation and serialisation
|
||||
|
||||
NOTE: purchase price should update every time a supplier credits us goods
|
||||
and price has changed
|
||||
NOTE: Every time a product's purchase price changes, it should be updated
|
||||
here as well
|
||||
"""
|
||||
__table_args__ = (UniqueConstraint("product_code"),)
|
||||
__table_args__ = (UniqueConstraint("product_code"))
|
||||
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
product_code: str = Field(max_length=10, nullable=False)
|
||||
product_code: str = Field(max_length=10, unique=True, nullable=False)
|
||||
product_name: str = Field(max_length=20, nullable=False, unique=True)
|
||||
purchase_price: int = Field(nullable=False)
|
||||
date_modified: datetime = Field(
|
||||
default=None,
|
||||
sa_column=Column(DateTime,
|
||||
sa_column=Column(DateTime(timezone=True),
|
||||
server_default=func.now(),
|
||||
server_onupdate=func.now())
|
||||
)
|
||||
|
||||
|
||||
class Transaction(SQLModel, table=True):
|
||||
"""
|
||||
Transaction table mapping, api response validation and serialisation
|
||||
|
||||
Include both business events to/from suppliers and to/from clients
|
||||
"""
|
||||
__tablename__: str = "transactions"
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
partner_id: Optional[int] = Field(nullable=False, foreign_key="partner.id")
|
||||
transcation_type: TransactionType = Field(
|
||||
sa_column=Column(SQLEnum(TransactionType), nullable=False)
|
||||
)
|
||||
transaction_status: TransactionStatus
|
||||
created_on: datetime = Field(
|
||||
default=None,
|
||||
sa_column=Column(DateTime(timezone=True), server_default=func.now())
|
||||
)
|
||||
updated_on: datetime = Field(
|
||||
default=None,
|
||||
sa_column=Column(
|
||||
DateTime(timezone=True),
|
||||
onupdate=func.now(),
|
||||
server_default=func.now()
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class Transaction_items(SQLModel, table=True):
|
||||
"""
|
||||
Transaction table mapping, api response validation and serialisation
|
||||
Includes transactions details from transactions
|
||||
"""
|
||||
|
||||
|
||||
class Payment(SQLModel, table=True):
|
||||
"""
|
||||
Payments table mapping, api response validation and serialisation
|
||||
|
||||
Include both payments to suppliers and from clients
|
||||
|
||||
"""
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
payment_type: TradeType = Field(
|
||||
sa_column=Column(SQLEnum(TradeType), nullable=False)
|
||||
)
|
||||
product_code: str = Field(nullable=False, foreign_key="product.product_code")
|
||||
client_id: Optional[int] = Field(nullable=False, foreign_key="client.id")
|
||||
|
||||
supplier_id: Optional[int] = Field(nullable=False, foreign_key="supplier.id")
|
||||
amount: int = Field(nullable=False)
|
||||
payment_method: str = Field(max_length=24, nullable=False)
|
||||
date: datetime = Field(
|
||||
default=None,
|
||||
sa_column=Column(DateTime, server_default=func.now())
|
||||
)
|
||||
|
||||
|
||||
class Credit(SQLModel, table=True):
|
||||
class Credit_accounts(SQLModel, table=True):
|
||||
"""Credit table mapping, api response validation and serialisation
|
||||
|
||||
Include both credit from suppliers and to clients
|
||||
"""
|
||||
__tablename__: str = "credit_accounts"
|
||||
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
transcation_type: TradeType = Field(
|
||||
sa_column=Column(SQLEnum(TradeType), nullable=False)
|
||||
)
|
||||
product_code: str = Field(nullable=False, foreign_key="product.product_code")
|
||||
client_id: Optional[int] = Field(nullable=False, foreign_key="client.id")
|
||||
supplier_id: Optional[int] = Field(nullable=False, foreign_key="supplier.id")
|
||||
client_id: Optional[int] = Field(nullable=True, foreign_key="client.id")
|
||||
supplier_id: Optional[int] = Field(nullable=True, foreign_key="supplier.id")
|
||||
qty: int = Field(nullable=False)
|
||||
amount: int = Field(nullable=False)
|
||||
date: datetime = Field(
|
||||
default=None,
|
||||
sa_column=Column(DateTime, server_default=func.now())
|
||||
sa_column=Column(DateTime(timezone=True), server_default=func.now())
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user