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():
    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():
    from autonomie.models.task.invoice import ManualInvoice
    # Fix an error in table names for some installations
    class OldManualInvoice(DBBASE):
        """
            Modèle pour les factures manuelles (ancienne version)
        """
        __tablename__ = 'manualinvoice'
        id = Column('id', BigInteger, primary_key=True)
        officialNumber = Column('sequence_id', BigInteger)
        description = Column('libelle', String(255))
        montant_ht = Column("montant_ht", Integer)
        tva = Column("tva", Integer)
        payment_ok = Column("paiement_ok", Integer)
        statusDate = Column("paiement_date", Date())
        paymentMode = Column("paiement_comment", String(255))
        taskDate = Column("date_emission", Date(),
                                    default=datetime.datetime.now)
        created_at = Column("created_at", DateTime,
                                        default=datetime.datetime.now)
        updated_at = Column("updated_at", DateTime,
                                        default=datetime.datetime.now,
                                        onupdate=datetime.datetime.now)
        client_id = Column('client_id', Integer,
                                ForeignKey('customer.code'))

        company_id = Column('compagnie_id', Integer,
                                ForeignKey('company.id'))


    if not table_exists("manualinvoice"):
        force_rename_table('manual_invoice', 'manualinvoice')
    from autonomie_base.models.base import DBSESSION
    for manualinv in OldManualInvoice.query().all():
        m = ManualInvoice()
        m.montant_ht = manualinv.montant_ht
        m.tva = manualinv.tva
        m.client_id = manualinv.client_id
        m.company_id = manualinv.company_id
        m.description = manualinv.description
        m.CAEStatus = 'valid'
        if manualinv.payment_ok == '1' or manualinv.montant_ht < 0:
            m.CAEStatus = "resulted"
        if manualinv.montant_ht < 0:
            if manualinv.paymentMode == u"chèque":
                payment_mode = "CHEQUE"
            elif manualinv.paymentMode == u"virement":
                payment_mode = "VIREMENT"
            else:
                payment_mode = None
            if payment_mode:
                # We don't care about amounts since there is only one payment
                payment = Payment(mode=payment_mode, date=manualinv.statusDate,
                                amount=0)
                m.payments.append(payment)
        m.statusDate = manualinv.statusDate
        m.taskDate = manualinv.taskDate
        m.creationDate = manualinv.created_at
        m.updateDate = manualinv.updated_at
        m.phase_id = 0
        m.name = u"Facture manuelle %s" % manualinv.officialNumber
        m.officialNumber = manualinv.officialNumber
        m.owner_id = 0
        DBSESSION.add(m)
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 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')