This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/migrations/versions/51439cf24be8_change_trade_a...

121 lines
5.6 KiB
Python
Raw Normal View History

2021-03-16 13:31:38 +00:00
"""change trade action
Revision ID: 51439cf24be8
Revises: eca457d8b2a4
Create Date: 2021-03-15 17:40:34.410408
"""
from alembic import op
from alembic import context
from sqlalchemy.dialects import postgresql
import sqlalchemy as sa
import citext
# revision identifiers, used by Alembic.
revision = '51439cf24be8'
2021-04-20 17:12:07 +00:00
down_revision = '8cb91ad1cc40'
2021-03-16 13:31:38 +00:00
branch_labels = None
depends_on = None
def get_inv():
INV = context.get_x_argument(as_dictionary=True).get('inventory')
if not INV:
raise ValueError("Inventory value is not specified")
return INV
2021-04-08 19:22:10 +00:00
def upgrade_data():
con = op.get_bind()
sql = "update common.user set active='t';"
con.execute(sql)
sql = "update common.user set phantom='f';"
con.execute(sql)
2021-03-16 13:31:38 +00:00
def upgrade():
2021-04-19 17:32:32 +00:00
## Trade
2021-03-16 20:39:17 +00:00
op.drop_table('trade', schema=f'{get_inv()}')
2021-04-20 17:12:07 +00:00
op.create_table('trade',
2021-03-16 13:31:38 +00:00
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('price', sa.Float(decimal_return_scale=4), nullable=True),
2021-04-03 11:33:56 +00:00
sa.Column('lot_id', postgresql.UUID(as_uuid=True), nullable=True),
sa.Column('date', sa.TIMESTAMP(timezone=True), nullable=True),
2021-04-20 19:37:00 +00:00
sa.Column('currency', postgresql.ENUM(name='dbtest.currency', create_type=False), nullable=False),
2021-03-16 20:39:17 +00:00
sa.Column('user_from_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('user_to_id', postgresql.UUID(as_uuid=True), nullable=False),
2021-04-03 11:33:56 +00:00
sa.Column('document_id', citext.CIText(), nullable=True),
2021-04-03 15:46:16 +00:00
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
2021-03-19 10:53:04 +00:00
sa.ForeignKeyConstraint(['user_from_id'], ['common.user.id'], ),
sa.ForeignKeyConstraint(['user_to_id'], ['common.user.id'], ),
2021-04-03 11:33:56 +00:00
sa.ForeignKeyConstraint(['lot_id'], [f'{get_inv()}.lot.id'], ),
2021-03-19 10:53:04 +00:00
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}'
)
2021-04-08 19:22:10 +00:00
2021-04-19 17:32:32 +00:00
op.create_table('confirm',
2021-03-19 10:53:04 +00:00
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
2021-04-19 17:32:32 +00:00
sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False),
2021-04-20 17:12:07 +00:00
sa.Column('trade_id', postgresql.UUID(as_uuid=True), nullable=False),
2021-03-19 10:53:04 +00:00
2021-04-03 11:33:56 +00:00
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
2021-04-20 17:12:07 +00:00
sa.ForeignKeyConstraint(['trade_id'], [f'{get_inv()}.trade.id'], ),
2021-04-19 17:32:32 +00:00
sa.ForeignKeyConstraint(['user_id'], ['common.user.id'], ),
2021-03-16 13:31:38 +00:00
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}'
)
2021-04-19 17:32:32 +00:00
2021-04-20 17:12:07 +00:00
op.create_table('trade_note',
2021-04-20 14:26:58 +00:00
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('trade_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.ForeignKeyConstraint(['trade_id'], [f'{get_inv()}.trade.id'], ),
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}'
)
2021-04-20 17:12:07 +00:00
# ## User
2021-04-08 19:22:10 +00:00
op.add_column('user', sa.Column('active', sa.Boolean(), default=True, nullable=True),
schema='common')
op.add_column('user', sa.Column('phantom', sa.Boolean(), default=False, nullable=True),
schema='common')
upgrade_data()
op.alter_column('user', 'active', nullable=False, schema='common')
op.alter_column('user', 'phantom', nullable=False, schema='common')
2021-03-16 13:31:38 +00:00
def downgrade():
2021-04-20 14:26:58 +00:00
op.drop_table('confirm', schema=f'{get_inv()}')
2021-04-20 17:12:07 +00:00
op.drop_table('trade_note', schema=f'{get_inv()}')
op.drop_table('trade', schema=f'{get_inv()}')
2021-03-16 20:43:57 +00:00
op.create_table('trade',
sa.Column('shipping_date', sa.TIMESTAMP(timezone=True), nullable=True,
2021-03-19 10:53:04 +00:00
comment='When are the devices going to be ready \n \
for shipping?\n '),
2021-03-16 20:43:57 +00:00
sa.Column('invoice_number', citext.CIText(), nullable=True,
comment='The id of the invoice so they can be linked.'),
sa.Column('price_id', postgresql.UUID(as_uuid=True), nullable=True,
2021-03-19 10:53:04 +00:00
comment='The price set for this trade. \n \
If no price is set it is supposed that the trade was\n \
not payed, usual in donations.\n '),
2021-03-16 20:43:57 +00:00
sa.Column('to_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('confirms_id', postgresql.UUID(as_uuid=True), nullable=True,
2021-03-19 10:53:04 +00:00
comment='An organize action that this association confirms. \
\n \n For example, a ``Sell`` or ``Rent``\n \
can confirm a ``Reserve`` action.\n '),
2021-03-16 20:43:57 +00:00
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['confirms_id'], [f'{get_inv()}.organize.id'], ),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.ForeignKeyConstraint(['price_id'], [f'{get_inv()}.price.id'], ),
sa.ForeignKeyConstraint(['to_id'], [f'{get_inv()}.agent.id'], ),
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}'
)
2021-04-08 19:22:10 +00:00
op.drop_column('user', 'active', schema='common')
op.drop_column('user', 'phantom', schema='common')