def downgrade():
    from autonomie.alembic.utils import force_rename_table
    from autonomie.alembic.utils import rename_column

    for table, key in foreign_key_names:
        remove_foreign_key(table, key)

    for table in ('estimation', 'invoice', 'cancelinvoice', 'manualinvoice'):
        fkey = "%s_ibfk_1" % table
        query = "alter table %s drop foreign key %s;" % (table, fkey)
        op.execute(query)
        rename_column(table, 'customer_id', 'client_id')

    for table, key in foreign_key_names:
        op.create_foreign_key(
            fkey,
            table,
            'customer',
            ['client_id'],
            ['id'])
    force_rename_table('project_customer', 'project_client')
    # Rename the column
    fkey = "project_client_ibfk_2"
    remove_foreign_key("project_client", "project_customer_ibfk_2")

    rename_column('project_client', 'customer_id', 'client_id')

    op.create_foreign_key(
        "project_client_ibfk_2",
        'project_client',
        'customer',
        ['client_id'],
        ['id'])
def downgrade():
        rename_column(
        'payment',
        'bank_remittance_id',
        'remittance_amount',
        type_=mysql.VARCHAR(length=255),
    )
def update_database_structure():
    rename_column(
        'payment',
        'remittance_amount',
        'bank_remittance_id',
        type_=mysql.VARCHAR(length=255),
    )
def upgrade():
    from autonomie.alembic.utils import force_rename_table
    from autonomie.alembic.utils import rename_column
    has_fkey = {
            'estimation':False,
            "invoice": False,
            "cancelinvoice": False,
            "manualinv": False
            }


    # Remove foreign keys to be able to rename columns
    for table, fkey in foreign_key_names:
        has_fkey[table] = remove_foreign_key(table, fkey)

    # Rename columns
    for table in (
            'estimation',
            'invoice',
            'cancelinvoice',
            'manualinv',
            ):
        rename_column(table, 'client_id', 'customer_id')
        op.execute("delete from %s where customer_id=0;" % table)

    # Add the foreign key constraint again
    for table, fkey in foreign_key_names:
        if has_fkey[table]:
            op.create_foreign_key(
                fkey,
                table,
                'customer',
                ['customer_id'],
                ['id'])

    remove_foreign_key("project_client", "project_client_ibfk_2")
    # Rename the project client
    force_rename_table('project_client', 'project_customer')


    # Rename the column
    rename_column('project_customer', 'client_id', 'customer_id')

    op.create_foreign_key(
        "project_customer_ibfk_2",
        'project_customer',
        'customer',
        ['customer_id'],
        ['id'])
def upgrade():
    from autonomie.alembic.utils import force_rename_table
    from autonomie.alembic.utils import rename_column
    has_fkey = {
        'estimation': False,
        "invoice": False,
        "cancelinvoice": False,
        "manualinv": False
    }

    # Remove foreign keys to be able to rename columns
    for table, fkey in foreign_key_names:
        has_fkey[table] = remove_foreign_key(table, fkey)

    # Rename columns
    for table in (
            'estimation',
            'invoice',
            'cancelinvoice',
            'manualinv',
    ):
        rename_column(table, 'client_id', 'customer_id')
        op.execute("delete from %s where customer_id=0;" % table)

    # Add the foreign key constraint again
    for table, fkey in foreign_key_names:
        if has_fkey[table]:
            op.create_foreign_key(fkey, table, 'customer', ['customer_id'],
                                  ['id'])

    remove_foreign_key("project_client", "project_client_ibfk_2")
    # Rename the project client
    force_rename_table('project_client', 'project_customer')

    # Rename the column
    rename_column('project_customer', 'client_id', 'customer_id')

    op.create_foreign_key("project_customer_ibfk_2", 'project_customer',
                          'customer', ['customer_id'], ['id'])
def downgrade():
    from autonomie.alembic.utils import force_rename_table
    from autonomie.alembic.utils import rename_column

    for table, key in foreign_key_names:
        remove_foreign_key(table, key)

    for table in ('estimation', 'invoice', 'cancelinvoice', 'manualinvoice'):
        fkey = "%s_ibfk_1" % table
        query = "alter table %s drop foreign key %s;" % (table, fkey)
        op.execute(query)
        rename_column(table, 'customer_id', 'client_id')

    for table, key in foreign_key_names:
        op.create_foreign_key(fkey, table, 'customer', ['client_id'], ['id'])
    force_rename_table('project_customer', 'project_client')
    # Rename the column
    fkey = "project_client_ibfk_2"
    remove_foreign_key("project_client", "project_customer_ibfk_2")

    rename_column('project_client', 'customer_id', 'client_id')

    op.create_foreign_key("project_client_ibfk_2", 'project_client',
                          'customer', ['client_id'], ['id'])
def upgrade():
    force_rename_table("egw_accounts", "accounts")
    force_rename_table("egw_config", "config")
    force_rename_table("coop_customer", "customer")
    force_rename_table("coop_company", "company")
    force_rename_table("coop_company_employee", "company_employee")
    force_rename_table("coop_project", "project")
    force_rename_table("coop_phase", "phase")
    force_rename_table("coop_tva", "tva")
    force_rename_table("coop_task_status", "task_status")
    force_rename_table("coop_holliday", "holiday")
    force_rename_table("symf_operation_treso", "operation_tresorerie")
    force_rename_table("coop_task", "task")
    force_rename_table("coop_invoice", "invoice")
    force_rename_table("coop_invoice_line", "invoice_line")
    force_rename_table("coop_cancel_invoice", "cancelinvoice")
    force_rename_table("coop_cancel_invoice_line", "cancelinvoice_line")
    force_rename_table("coop_payment", "payment")
    force_rename_table("symf_facture_manuelle", "manualinvoice")
    force_rename_table("coop_estimation", "estimation")
    force_rename_table("coop_estimation_line", "estimation_line")
    force_rename_table("coop_estimation_payment", "estimation_payment")
    op.execute("""
alter table accounts change account_id id int(11) NOT NULL AUTO_INCREMENT;
""")
    op.execute("""
alter table accounts change account_lid login varchar(64) NOT NULL;
""")
    op.execute("""
alter table accounts change account_pwd password varchar(100) NOT NULL;
""")
    op.execute("""
alter table accounts change account_firstname firstname varchar(50) DEFAULT NULL;
""")
    op.execute("""
alter table accounts change account_lastname lastname varchar(50) DEFAULT NULL;
""")
    op.execute("""
alter table accounts change account_primary_group primary_group int(11) NOT NULL DEFAULT '0';
""")
    op.execute("""
alter table accounts change account_status active varchar(1) NOT NULL DEFAULT 'Y';
""")
    op.execute("""
alter table accounts change account_email email varchar(100) DEFAULT NULL;
""")
    # IDProject
    rename_column("project", "IDProject", "id", autoincrement=True)
    rename_column("invoice", "IDProject", "project_id")
    rename_column("estimation", "IDProject", "project_id")
    rename_column("cancelinvoice", "IDProject", "project_id")
    rename_column("phase", 'IDProject', 'project_id')
    # IDCompany
    rename_column("company", 'IDCompany', 'id', autoincrement=True)
    rename_column("customer", 'IDCompany', 'company_id')
    rename_column("project", 'IDCompany', 'company_id')
    rename_column("company_employee", 'IDCompany', 'company_id')
    # IDTask
    rename_column("task", 'IDTask', 'id', autoincrement=True)
    rename_column("estimation", 'IDTask', 'id', autoincrement=True)
    rename_column("invoice", 'IDTask', 'id', autoincrement=True)
    rename_column("invoice", 'IDEstimation', 'estimation_id', nullable=True)
    rename_column("cancelinvoice", 'IDTask', 'id', autoincrement=True)
    rename_column("cancelinvoice", 'IDInvoice', "invoice_id", nullable=True)

    rename_column("estimation_line", 'IDTask', 'task_id')
    rename_column("estimation_line", 'IDWorkLine', 'id', autoincrement=True)
    rename_column("estimation_payment", 'IDTask', 'task_id')
    rename_column("estimation_payment",
                  'IDPaymentLine',
                  'id',
                  autoincrement=True)
    rename_column("invoice_line", 'IDTask', 'task_id')
    rename_column("invoice_line", 'IDInvoiceLine', 'id', autoincrement=True)
    rename_column("payment", 'IDTask', 'task_id')
    rename_column("cancelinvoice_line", 'IDTask', 'task_id')

    rename_column("task_status", 'IDTask', 'task_id')

    # IDPhase
    rename_column("phase", "IDPhase", "id", autoincrement=True)
    rename_column("task", "IDPhase", "phase_id")

    # IDEmployee
    rename_column("company_employee", 'IDEmployee', 'account_id')
    rename_column("task", "IDEmployee", 'owner_id')
def update_database_structure():
    logger = logging.getLogger(__name__)
    op.add_column(
        'customer',
        sa.Column('civilite', sa.String(10), default=''),
    )
    op.add_column(
        'customer',
        sa.Column('mobile', sa.String(20), default=''),
    )
    op.add_column(
        'customer',
        sa.Column('type_', sa.String(10), default='company'),
    )
    op.add_column(
        'estimation_payment',
        sa.Column('date', sa.Date()),
    )
    rename_column("customer", 'contactLastName', 'lastname', sa.String(255))
    rename_column(
        "customer",
        'contactFirstName',
        'firstname',
        sa.String(255),
        nullable=True,
    )
    rename_column("customer",
                  'intraTVA',
                  'tva_intracomm',
                  sa.String(50),
                  nullable=True)
    rename_column(
        "customer",
        'zipCode',
        'zip_code',
        sa.String(20),
    )
    rename_column(
        'task',
        'CAEStatus',
        'status',
        sa.String(10),
    )
    rename_column(
        'task',
        'statusComment',
        'status_comment',
        sa.Text,
    )
    rename_column(
        'task',
        'statusPerson',
        'status_person_id',
        sa.Integer,
    )
    #op.create_foreign_key(
    #    None, 'task', 'accounts', ['status_person_id'], ['id']
    #)
    rename_column(
        'task_status',
        'statusPerson',
        'status_person_id',
        sa.Integer,
    )
    #op.create_foreign_key(
    #    None, 'task_status', 'accounts', ['status_person_id'], ['id']
    #)
    rename_column(
        'task_status',
        'statusCode',
        'status_code',
        sa.String(10),
    )
    rename_column(
        'task_status',
        'statusComment',
        'status_comment',
        sa.Text,
    )

    op.add_column(
        'estimation',
        sa.Column('signed_status', sa.String(10)),
    )
    op.add_column(
        'estimation',
        sa.Column('geninv', sa.Boolean()),
    )
    op.add_column(
        'invoice',
        sa.Column('paid_status', sa.String(10)),
    )
    op.add_column(
        'payment',
        sa.Column("user_id", sa.Integer, sa.ForeignKey('accounts.id')))

    op.add_column(
        'expense_sheet',
        sa.Column('paid_status', sa.String(10)),
    )
    op.add_column(
        'expense_sheet',
        sa.Column('justified', sa.Boolean()),
    )
    op.add_column(
        "expense_payment",
        sa.Column("user_id", sa.Integer, sa.ForeignKey('accounts.id')))
    logger.debug("Migrating date columns")
    logger.debug(" + Projects")
    change_custom_date_type('project', 'starting_date', 'startingDate')
    change_custom_date_type('project', 'ending_date', 'endingDate')

    logger.debug(" + Customers")
    change_custom_date_type('customer', 'updated_at', 'updateDate')
    change_custom_date_type('customer', 'created_at', 'creationDate')

    logger.debug(" + Companies")
    change_custom_date_type('company', 'updated_at', 'updateDate')
    change_custom_date_type('company', 'created_at', 'creationDate')

    logger.debug(" + Task and TaskStatus")
    change_custom_date_type("task_status", "status_date", "statusDate")
    change_custom_date_type('task', "status_date", "statusDate")
def upgrade():
    force_rename_table("egw_accounts", "accounts")
    force_rename_table("egw_config", "config")
    force_rename_table("coop_customer", "customer")
    force_rename_table("coop_company", "company")
    force_rename_table("coop_company_employee", "company_employee")
    force_rename_table("coop_project", "project")
    force_rename_table("coop_phase", "phase")
    force_rename_table("coop_tva", "tva")
    force_rename_table("coop_task_status", "task_status")
    force_rename_table("coop_holliday", "holiday")
    force_rename_table("symf_operation_treso", "operation_tresorerie")
    force_rename_table("coop_task", "task")
    force_rename_table("coop_invoice", "invoice")
    force_rename_table("coop_invoice_line", "invoice_line")
    force_rename_table("coop_cancel_invoice", "cancelinvoice")
    force_rename_table("coop_cancel_invoice_line", "cancelinvoice_line")
    force_rename_table("coop_payment", "payment")
    force_rename_table("symf_facture_manuelle", "manualinvoice")
    force_rename_table("coop_estimation", "estimation")
    force_rename_table("coop_estimation_line", "estimation_line")
    force_rename_table("coop_estimation_payment", "estimation_payment")
    op.execute("""
alter table accounts change account_id id int(11) NOT NULL AUTO_INCREMENT;
""")
    op.execute("""
alter table accounts change account_lid login varchar(64) NOT NULL;
""")
    op.execute("""
alter table accounts change account_pwd password varchar(100) NOT NULL;
""")
    op.execute("""
alter table accounts change account_firstname firstname varchar(50) DEFAULT NULL;
""")
    op.execute("""
alter table accounts change account_lastname lastname varchar(50) DEFAULT NULL;
""")
    op.execute("""
alter table accounts change account_primary_group primary_group int(11) NOT NULL DEFAULT '0';
""")
    op.execute("""
alter table accounts change account_status active varchar(1) NOT NULL DEFAULT 'Y';
""")
    op.execute("""
alter table accounts change account_email email varchar(100) DEFAULT NULL;
""")
    # IDProject
    rename_column("project", "IDProject", "id", autoincrement=True)
    rename_column("invoice", "IDProject", "project_id")
    rename_column("estimation", "IDProject", "project_id")
    rename_column("cancelinvoice", "IDProject", "project_id")
    rename_column("phase", 'IDProject', 'project_id')
    # IDCompany
    rename_column("company", 'IDCompany', 'id', autoincrement=True)
    rename_column("customer", 'IDCompany', 'company_id')
    rename_column("project", 'IDCompany', 'company_id')
    rename_column("company_employee", 'IDCompany', 'company_id')
    # IDTask
    rename_column("task", 'IDTask', 'id', autoincrement=True)
    rename_column("estimation", 'IDTask', 'id', autoincrement=True)
    rename_column("invoice", 'IDTask', 'id', autoincrement=True)
    rename_column("invoice", 'IDEstimation', 'estimation_id', nullable=True)
    rename_column("cancelinvoice", 'IDTask', 'id', autoincrement=True)
    rename_column("cancelinvoice", 'IDInvoice', "invoice_id", nullable=True)

    rename_column("estimation_line", 'IDTask', 'task_id')
    rename_column("estimation_line", 'IDWorkLine', 'id', autoincrement=True)
    rename_column("estimation_payment", 'IDTask', 'task_id')
    rename_column("estimation_payment", 'IDPaymentLine', 'id', autoincrement=True)
    rename_column("invoice_line", 'IDTask', 'task_id')
    rename_column("invoice_line", 'IDInvoiceLine', 'id', autoincrement=True)
    rename_column("payment", 'IDTask', 'task_id')
    rename_column("cancelinvoice_line", 'IDTask', 'task_id')

    rename_column("task_status", 'IDTask', 'task_id')

    # IDPhase
    rename_column("phase", "IDPhase", "id", autoincrement=True)
    rename_column("task", "IDPhase", "phase_id")

    # IDEmployee
    rename_column("company_employee", 'IDEmployee', 'account_id')
    rename_column("task", "IDEmployee", 'owner_id')
def update_database_structure():
    logger = logging.getLogger(__name__)
    op.add_column(
        'customer',
        sa.Column('civilite', sa.String(10), default=''),
    )
    op.add_column(
        'customer',
        sa.Column('mobile', sa.String(20), default=''),
    )
    op.add_column(
        'customer',
        sa.Column('type_', sa.String(10), default='company'),
    )
    op.add_column(
        'estimation_payment',
        sa.Column('date', sa.Date()),
    )
    rename_column(
        "customer",
        'contactLastName',
        'lastname',
        sa.String(255)
    )
    rename_column(
        "customer",
        'contactFirstName',
        'firstname',
        sa.String(255),
        nullable=True,
    )
    rename_column(
        "customer",
        'intraTVA',
        'tva_intracomm',
        sa.String(50),
        nullable=True
    )
    rename_column(
        "customer",
        'zipCode',
        'zip_code',
        sa.String(20),
    )
    rename_column(
        'task',
        'CAEStatus',
        'status',
        sa.String(10),
    )
    rename_column(
        'task',
        'statusComment',
        'status_comment',
        sa.Text,
    )
    rename_column(
        'task',
        'statusPerson',
        'status_person_id',
        sa.Integer,
    )
    #op.create_foreign_key(
    #    None, 'task', 'accounts', ['status_person_id'], ['id']
    #)
    rename_column(
        'task_status',
        'statusPerson',
        'status_person_id',
        sa.Integer,
    )
    #op.create_foreign_key(
    #    None, 'task_status', 'accounts', ['status_person_id'], ['id']
    #)
    rename_column(
        'task_status',
        'statusCode',
        'status_code',
        sa.String(10),
    )
    rename_column(
        'task_status',
        'statusComment',
        'status_comment',
        sa.Text,
    )

    op.add_column(
        'estimation',
        sa.Column('signed_status', sa.String(10)),
    )
    op.add_column(
        'estimation',
        sa.Column('geninv', sa.Boolean()),
    )
    op.add_column(
        'invoice',
        sa.Column('paid_status', sa.String(10)),
    )
    op.add_column(
        'payment',
        sa.Column("user_id", sa.Integer, sa.ForeignKey('accounts.id'))
    )

    op.add_column(
        'expense_sheet',
        sa.Column('paid_status', sa.String(10)),
    )
    op.add_column(
        'expense_sheet',
        sa.Column('justified', sa.Boolean()),
    )
    op.add_column(
        "expense_payment",
        sa.Column("user_id", sa.Integer, sa.ForeignKey('accounts.id'))
    )
    logger.debug("Migrating date columns")
    logger.debug(" + Projects")
    change_custom_date_type('project', 'starting_date', 'startingDate')
    change_custom_date_type('project', 'ending_date', 'endingDate')

    logger.debug(" + Customers")
    change_custom_date_type('customer', 'updated_at', 'updateDate')
    change_custom_date_type('customer', 'created_at', 'creationDate')

    logger.debug(" + Companies")
    change_custom_date_type('company', 'updated_at', 'updateDate')
    change_custom_date_type('company', 'created_at', 'creationDate')

    logger.debug(" + Task and TaskStatus")
    change_custom_date_type("task_status", "status_date", "statusDate")
    change_custom_date_type('task', "status_date", "statusDate")