"""asset v7

Revision ID: dba5beff556b
Revises: ac7de9ca96cd
Create Date: 2024-02-29 21:00:03.312582

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'dba5beff556b'
down_revision = 'ac7de9ca96cd'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('cmf_asset_cat_cmf_asset_type_cat',
    sa.Column('id', sa.String(length=128), nullable=False),
    sa.Column('parent_id', sa.String(length=64), nullable=True),
    sa.Column('root_id', sa.String(length=64), nullable=True),
    sa.Column('description', sa.String(length=4096), nullable=True),
    sa.Column('left_id', sa.String(length=64), nullable=True),
    sa.Column('right_id', sa.String(length=64), nullable=True),
    sa.Column('left_name_cache', sa.String(length=256), nullable=True),
    sa.Column('right_name_cache', sa.String(length=256), nullable=True),
    sa.ForeignKeyConstraint(['left_id'], ['cmf_asset_cat.id'], ),
    sa.ForeignKeyConstraint(['right_id'], ['cmf_asset_type_cat.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_cmf_asset_cat_cmf_asset_type_cat_left_id'), 'cmf_asset_cat_cmf_asset_type_cat', ['left_id'], unique=False)
    op.create_index(op.f('ix_cmf_asset_cat_cmf_asset_type_cat_parent_id'), 'cmf_asset_cat_cmf_asset_type_cat', ['parent_id'], unique=False)
    op.create_index(op.f('ix_cmf_asset_cat_cmf_asset_type_cat_right_id'), 'cmf_asset_cat_cmf_asset_type_cat', ['right_id'], unique=False)
    op.create_index(op.f('ix_cmf_asset_cat_cmf_asset_type_cat_root_id'), 'cmf_asset_cat_cmf_asset_type_cat', ['root_id'], unique=False)
    op.create_table('cmf_asset_cmf_asset',
    sa.Column('id', sa.String(length=128), nullable=False),
    sa.Column('parent_id', sa.String(length=64), nullable=True),
    sa.Column('root_id', sa.String(length=64), nullable=True),
    sa.Column('description', sa.String(length=4096), nullable=True),
    sa.Column('left_id', sa.String(length=64), nullable=True),
    sa.Column('right_id', sa.String(length=64), nullable=True),
    sa.Column('left_name_cache', sa.String(length=256), nullable=True),
    sa.Column('right_name_cache', sa.String(length=256), nullable=True),
    sa.ForeignKeyConstraint(['left_id'], ['cmf_asset.id'], ),
    sa.ForeignKeyConstraint(['right_id'], ['cmf_asset.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_cmf_asset_cmf_asset_left_id'), 'cmf_asset_cmf_asset', ['left_id'], unique=False)
    op.create_index(op.f('ix_cmf_asset_cmf_asset_parent_id'), 'cmf_asset_cmf_asset', ['parent_id'], unique=False)
    op.create_index(op.f('ix_cmf_asset_cmf_asset_right_id'), 'cmf_asset_cmf_asset', ['right_id'], unique=False)
    op.create_index(op.f('ix_cmf_asset_cmf_asset_root_id'), 'cmf_asset_cmf_asset', ['root_id'], unique=False)
    op.drop_index('ix_cmf_asset_owner_group_left_id', table_name='cmf_asset_owner_group')
    op.drop_index('ix_cmf_asset_owner_group_parent_id', table_name='cmf_asset_owner_group')
    op.drop_index('ix_cmf_asset_owner_group_right_id', table_name='cmf_asset_owner_group')
    op.drop_index('ix_cmf_asset_owner_group_root_id', table_name='cmf_asset_owner_group')
    op.drop_table('cmf_asset_owner_group')
    op.add_column('cmf_asset', sa.Column('markcode', sa.String(length=256), nullable=True))
    op.add_column('cmf_asset', sa.Column('inv_no', sa.String(length=256), nullable=True))
    op.add_column('cmf_asset', sa.Column('org_unit_id', sa.String(length=64), nullable=True))
    op.create_index('ix_cmf_asset_asset_tag_gin_trgm', 'cmf_asset', ['asset_tag'], unique=False, postgresql_using='gin', postgresql_ops={'asset_tag': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_domain_name_gin_trgm', 'cmf_asset', ['domain_name'], unique=False, postgresql_using='gin', postgresql_ops={'domain_name': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_imei_gin_trgm', 'cmf_asset', ['imei'], unique=False, postgresql_using='gin', postgresql_ops={'imei': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_inv_no_gin_trgm', 'cmf_asset', ['inv_no'], unique=False, postgresql_using='gin', postgresql_ops={'inv_no': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_invoice_number_gin_trgm', 'cmf_asset', ['invoice_number'], unique=False, postgresql_using='gin', postgresql_ops={'invoice_number': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_ip_adress_gin_trgm', 'cmf_asset', ['ip_adress'], unique=False, postgresql_using='gin', postgresql_ops={'ip_adress': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_lease_contract_gin_trgm', 'cmf_asset', ['lease_contract'], unique=False, postgresql_using='gin', postgresql_ops={'lease_contract': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_maintenance_contract_gin_trgm', 'cmf_asset', ['maintenance_contract'], unique=False, postgresql_using='gin', postgresql_ops={'maintenance_contract': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_markcode_gin_trgm', 'cmf_asset', ['markcode'], unique=False, postgresql_using='gin', postgresql_ops={'markcode': 'gin_trgm_ops'})
    op.create_index(op.f('ix_cmf_asset_org_unit_id'), 'cmf_asset', ['org_unit_id'], unique=False)
    op.create_index('ix_cmf_asset_os_version_gin_trgm', 'cmf_asset', ['os_version'], unique=False, postgresql_using='gin', postgresql_ops={'os_version': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_phone_number_gin_trgm', 'cmf_asset', ['phone_number'], unique=False, postgresql_using='gin', postgresql_ops={'phone_number': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_po_number_gin_trgm', 'cmf_asset', ['po_number'], unique=False, postgresql_using='gin', postgresql_ops={'po_number': 'gin_trgm_ops'})
    op.create_index('ix_cmf_asset_serial_number_gin_trgm', 'cmf_asset', ['serial_number'], unique=False, postgresql_using='gin', postgresql_ops={'serial_number': 'gin_trgm_ops'})
    op.create_foreign_key(None, 'cmf_asset', 'cmf_org_unit', ['org_unit_id'], ['id'])
    op.add_column('cmf_asset_cat', sa.Column('default_logic_type_id', sa.String(length=64), nullable=True))
    op.execute(
        'update cmf_asset_cat set default_logic_type_id = (select id from cmf_logic_type limit 1)')
    op.alter_column('cmf_asset_cat', 'default_logic_type_id',
               existing_type=sa.String(length=64),
               nullable=False)
    op.create_index(op.f('ix_cmf_asset_cat_default_logic_type_id'), 'cmf_asset_cat', ['default_logic_type_id'], unique=False)
    op.create_foreign_key(None, 'cmf_asset_cat', 'cmf_logic_type', ['default_logic_type_id'], ['id'])
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('cmf_asset_cat_default_logic_type_id_fkey', 'cmf_asset_cat', type_='foreignkey')
    op.drop_index(op.f('ix_cmf_asset_cat_default_logic_type_id'), table_name='cmf_asset_cat')
    op.drop_column('cmf_asset_cat', 'default_logic_type_id')
    op.drop_constraint('cmf_asset_org_unit_id_fkey', 'cmf_asset', type_='foreignkey')
    op.drop_index('ix_cmf_asset_serial_number_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'serial_number': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_po_number_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'po_number': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_phone_number_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'phone_number': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_os_version_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'os_version': 'gin_trgm_ops'})
    op.drop_index(op.f('ix_cmf_asset_org_unit_id'), table_name='cmf_asset')
    op.drop_index('ix_cmf_asset_markcode_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'markcode': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_maintenance_contract_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'maintenance_contract': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_lease_contract_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'lease_contract': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_ip_adress_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'ip_adress': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_invoice_number_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'invoice_number': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_inv_no_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'inv_no': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_imei_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'imei': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_domain_name_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'domain_name': 'gin_trgm_ops'})
    op.drop_index('ix_cmf_asset_asset_tag_gin_trgm', table_name='cmf_asset', postgresql_using='gin', postgresql_ops={'asset_tag': 'gin_trgm_ops'})
    op.drop_column('cmf_asset', 'org_unit_id')
    op.drop_column('cmf_asset', 'inv_no')
    op.drop_column('cmf_asset', 'markcode')
    op.create_table('cmf_asset_owner_group',
    sa.Column('id', sa.VARCHAR(length=128), autoincrement=False, nullable=False),
    sa.Column('parent_id', sa.VARCHAR(length=64), autoincrement=False, nullable=True),
    sa.Column('root_id', sa.VARCHAR(length=64), autoincrement=False, nullable=True),
    sa.Column('description', sa.VARCHAR(length=4096), autoincrement=False, nullable=True),
    sa.Column('left_id', sa.VARCHAR(length=64), autoincrement=False, nullable=True),
    sa.Column('right_id', sa.VARCHAR(length=64), autoincrement=False, nullable=True),
    sa.Column('left_name_cache', sa.VARCHAR(length=256), autoincrement=False, nullable=True),
    sa.Column('right_name_cache', sa.VARCHAR(length=256), autoincrement=False, nullable=True),
    sa.PrimaryKeyConstraint('id', name='cmf_asset_owner_group_pkey')
    )
    op.create_index('ix_cmf_asset_owner_group_root_id', 'cmf_asset_owner_group', ['root_id'], unique=False)
    op.create_index('ix_cmf_asset_owner_group_right_id', 'cmf_asset_owner_group', ['right_id'], unique=False)
    op.create_index('ix_cmf_asset_owner_group_parent_id', 'cmf_asset_owner_group', ['parent_id'], unique=False)
    op.create_index('ix_cmf_asset_owner_group_left_id', 'cmf_asset_owner_group', ['left_id'], unique=False)
    op.drop_index(op.f('ix_cmf_asset_cmf_asset_root_id'), table_name='cmf_asset_cmf_asset')
    op.drop_index(op.f('ix_cmf_asset_cmf_asset_right_id'), table_name='cmf_asset_cmf_asset')
    op.drop_index(op.f('ix_cmf_asset_cmf_asset_parent_id'), table_name='cmf_asset_cmf_asset')
    op.drop_index(op.f('ix_cmf_asset_cmf_asset_left_id'), table_name='cmf_asset_cmf_asset')
    op.drop_table('cmf_asset_cmf_asset')
    op.drop_index(op.f('ix_cmf_asset_cat_cmf_asset_type_cat_root_id'), table_name='cmf_asset_cat_cmf_asset_type_cat')
    op.drop_index(op.f('ix_cmf_asset_cat_cmf_asset_type_cat_right_id'), table_name='cmf_asset_cat_cmf_asset_type_cat')
    op.drop_index(op.f('ix_cmf_asset_cat_cmf_asset_type_cat_parent_id'), table_name='cmf_asset_cat_cmf_asset_type_cat')
    op.drop_index(op.f('ix_cmf_asset_cat_cmf_asset_type_cat_left_id'), table_name='cmf_asset_cat_cmf_asset_type_cat')
    op.drop_table('cmf_asset_cat_cmf_asset_type_cat')
    # ### end Alembic commands ###
