This commit is contained in:
1
backend/alembic/README
Normal file
1
backend/alembic/README
Normal file
@@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
||||
81
backend/alembic/env.py
Normal file
81
backend/alembic/env.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
|
||||
from alembic import context
|
||||
|
||||
from sqlmodel import SQLModel
|
||||
from src.settings import settings
|
||||
|
||||
# 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 = SQLModel.metadata
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
config.set_main_option("sqlalchemy.url", f'postgresql://{settings.db_user}:{settings.db_pass}@{settings.db_host}:5432/{settings.db_name}')
|
||||
# ... 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()
|
||||
29
backend/alembic/script.py.mako
Normal file
29
backend/alembic/script.py.mako
Normal file
@@ -0,0 +1,29 @@
|
||||
"""${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 ""}
|
||||
import sqlmodel.sql.sqltypes
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_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)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
${downgrades if downgrades else "pass"}
|
||||
155
backend/alembic/versions/370137544ee6_initial_repository.py
Normal file
155
backend/alembic/versions/370137544ee6_initial_repository.py
Normal file
@@ -0,0 +1,155 @@
|
||||
"""Initial repository
|
||||
|
||||
Revision ID: 370137544ee6
|
||||
Revises:
|
||||
Create Date: 2026-02-19 22:55:19.804879
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
import sqlmodel.sql.sqltypes
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '370137544ee6'
|
||||
down_revision: Union[str, Sequence[str], None] = None
|
||||
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.drop_table('template')
|
||||
op.drop_table('user')
|
||||
op.drop_table('form')
|
||||
op.drop_table('usercontracttypelink')
|
||||
op.drop_table('shipment')
|
||||
op.drop_table('cheque')
|
||||
op.drop_table('productor')
|
||||
op.drop_table('paymentmethod')
|
||||
op.drop_table('contract')
|
||||
op.drop_table('contractproduct')
|
||||
op.drop_table('contracttype')
|
||||
op.drop_table('product')
|
||||
op.drop_table('shipmentproductlink')
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('shipmentproductlink',
|
||||
sa.Column('shipment_id', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.Column('product_id', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.ForeignKeyConstraint(['product_id'], ['product.id'], name=op.f('shipmentproductlink_product_id_fkey')),
|
||||
sa.ForeignKeyConstraint(['shipment_id'], ['shipment.id'], name=op.f('shipmentproductlink_shipment_id_fkey')),
|
||||
sa.PrimaryKeyConstraint('shipment_id', 'product_id', name=op.f('shipmentproductlink_pkey'))
|
||||
)
|
||||
op.create_table('product',
|
||||
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('unit', postgresql.ENUM('GRAMS', 'KILO', 'PIECE', name='unit'), autoincrement=False, nullable=False),
|
||||
sa.Column('price', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True),
|
||||
sa.Column('price_kg', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True),
|
||||
sa.Column('quantity', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True),
|
||||
sa.Column('quantity_unit', sa.VARCHAR(), autoincrement=False, nullable=True),
|
||||
sa.Column('type', postgresql.ENUM('OCCASIONAL', 'RECCURENT', name='producttype'), autoincrement=False, nullable=False),
|
||||
sa.Column('productor_id', sa.INTEGER(), autoincrement=False, nullable=True),
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.ForeignKeyConstraint(['productor_id'], ['productor.id'], name=op.f('product_productor_id_fkey')),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('product_pkey'))
|
||||
)
|
||||
op.create_table('contracttype',
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('contracttype_pkey'))
|
||||
)
|
||||
op.create_table('contractproduct',
|
||||
sa.Column('product_id', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.Column('shipment_id', sa.INTEGER(), autoincrement=False, nullable=True),
|
||||
sa.Column('quantity', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=False),
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.Column('contract_id', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.ForeignKeyConstraint(['contract_id'], ['contract.id'], name=op.f('contractproduct_contract_id_fkey'), ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['product_id'], ['product.id'], name=op.f('contractproduct_product_id_fkey'), ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['shipment_id'], ['shipment.id'], name=op.f('contractproduct_shipment_id_fkey'), ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('contractproduct_pkey'))
|
||||
)
|
||||
op.create_table('contract',
|
||||
sa.Column('firstname', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('lastname', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('email', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('phone', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('payment_method', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('cheque_quantity', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.Column('form_id', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.Column('file', postgresql.BYTEA(), autoincrement=False, nullable=True),
|
||||
sa.Column('total_price', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True),
|
||||
sa.ForeignKeyConstraint(['form_id'], ['form.id'], name=op.f('contract_form_id_fkey'), ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('contract_pkey'))
|
||||
)
|
||||
op.create_table('paymentmethod',
|
||||
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('details', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.Column('productor_id', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.ForeignKeyConstraint(['productor_id'], ['productor.id'], name=op.f('paymentmethod_productor_id_fkey'), ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('paymentmethod_pkey'))
|
||||
)
|
||||
op.create_table('productor',
|
||||
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('address', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('type', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('productor_pkey'))
|
||||
)
|
||||
op.create_table('cheque',
|
||||
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('value', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.Column('contract_id', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.ForeignKeyConstraint(['contract_id'], ['contract.id'], name=op.f('cheque_contract_id_fkey'), ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('cheque_pkey'))
|
||||
)
|
||||
op.create_table('shipment',
|
||||
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('date', sa.DATE(), autoincrement=False, nullable=False),
|
||||
sa.Column('form_id', sa.INTEGER(), autoincrement=False, nullable=True),
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.ForeignKeyConstraint(['form_id'], ['form.id'], name=op.f('shipment_form_id_fkey'), ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('shipment_pkey'))
|
||||
)
|
||||
op.create_table('usercontracttypelink',
|
||||
sa.Column('user_id', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.Column('contract_type_id', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.ForeignKeyConstraint(['contract_type_id'], ['contracttype.id'], name=op.f('usercontracttypelink_contract_type_id_fkey')),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], name=op.f('usercontracttypelink_user_id_fkey')),
|
||||
sa.PrimaryKeyConstraint('user_id', 'contract_type_id', name=op.f('usercontracttypelink_pkey'))
|
||||
)
|
||||
op.create_table('form',
|
||||
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('productor_id', sa.INTEGER(), autoincrement=False, nullable=True),
|
||||
sa.Column('referer_id', sa.INTEGER(), autoincrement=False, nullable=True),
|
||||
sa.Column('season', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('start', sa.DATE(), autoincrement=False, nullable=False),
|
||||
sa.Column('end', sa.DATE(), autoincrement=False, nullable=False),
|
||||
sa.Column('minimum_shipment_value', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True),
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.ForeignKeyConstraint(['productor_id'], ['productor.id'], name=op.f('form_productor_id_fkey')),
|
||||
sa.ForeignKeyConstraint(['referer_id'], ['user.id'], name=op.f('form_referer_id_fkey')),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('form_pkey'))
|
||||
)
|
||||
op.create_table('user',
|
||||
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('email', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('user_pkey'))
|
||||
)
|
||||
op.create_table('template',
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('template_pkey'))
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user