"""added series to invoice

Revision ID: 2d70356c6c5f
Revises: <KEY>
Create Date: 2021-01-07 23:38:39.086260

"""

# revision identifiers, used by Alembic.
revision = '2d70356c6c5f'
down_revision = '<KEY>'

from alembic import op
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class User(Base):
    __tablename__ = 'users'

    id = sa.Column(sa.Integer, primary_key=True)
    next_invoice_num = sa.Column(sa.Integer, nullable=False)


class Series(Base):
    __tablename__ = 'series'

    id = sa.Column(sa.Integer, primary_key=True)
    user_id = sa.Column(sa.Integer, sa.ForeignKey('users.id'), nullable=False)
    name = sa.Column(sa.String, nullable=False)
    next_invoice_num = sa.Column(sa.Integer, nullable=False)


class Invoice(Base):
    __tablename__ = 'invoices'

    id = sa.Column(sa.Integer, primary_key=True)
    owner_id = sa.Column(sa.Integer, sa.ForeignKey('users.id'), nullable=False)
    series_id = sa.Column(sa.Integer, sa.ForeignKey('series.id'), nullable=False)


def upgrade():
    bind = op.get_bind()
    session = orm.Session(bind=bind)

    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('invoices', sa.Column('series_id', sa.Integer(), nullable=False))

    # create Normal series for each user
    series = {user.id: Series(user_id=user.id, name='Normal', next_invoice_num=user.next_invoice_num) for user in session.query(User)}
    session.add_all(series.values())

    # update invoices with newly created series refs
    for invoice in session.query(Invoice):
        invoice.series_id = series[invoice.owner_id]

    session.commit()

    op.create_foreign_key(None, 'invoices', 'series', ['series_id'], ['id'])
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'invoices', type_='foreignkey')
    op.drop_column('invoices', 'series_id')
    ### end Alembic commands ###

"""added series to invoice

Revision ID: 2d70356c6c5f
Revises: <KEY>
Create Date: 2021-01-07 23:38:39.086260

"""

# revision identifiers, used by Alembic.
revision = '2d70356c6c5f'
down_revision = '<KEY>'

from alembic import op
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class User(Base):
    __tablename__ = 'users'

    id = sa.Column(sa.Integer, primary_key=True)
    next_invoice_num = sa.Column(sa.Integer, nullable=False)


class Series(Base):
    __tablename__ = 'series'

    id = sa.Column(sa.Integer, primary_key=True)
    user_id = sa.Column(sa.Integer, sa.ForeignKey('users.id'), nullable=False)
    name = sa.Column(sa.String, nullable=False)
    next_invoice_num = sa.Column(sa.Integer, nullable=False)


class Invoice(Base):
    __tablename__ = 'invoices'

    id = sa.Column(sa.Integer, primary_key=True)
    owner_id = sa.Column(sa.Integer, sa.ForeignKey('users.id'), nullable=False)
    series_id = sa.Column(sa.Integer, sa.ForeignKey('series.id'), nullable=False)


def upgrade():
    bind = op.get_bind()
    session = orm.Session(bind=bind)

    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('invoices', sa.Column('series_id', sa.Integer(), nullable=False))

    # create Normal series for each user
    series = {user.id: Series(user_id=user.id, name='Normal', next_invoice_num=user.next_invoice_num) for user in session.query(User)}
    session.add_all(series.values())

    # update invoices with newly created series refs
    for invoice in session.query(Invoice):
        invoice.series_id = series[invoice.owner_id]

    session.commit()

    op.create_foreign_key(None, 'invoices', 'series', ['series_id'], ['id'])
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'invoices', type_='foreignkey')
    op.drop_column('invoices', 'series_id')
    ### end Alembic commands ###

added series to invoice  Revision ID: 2d70356c6c5f Revises: <KEY> Create Date: 2021-01-07 23:38:39.086260 # revision identifiers, used by Alembic. ### commands auto generated by Alembic - please adjust! ### # create Normal series for each user # update invoices with newly created series refs ### end Alembic commands ### ### commands auto generated by Alembic - please adjust! ### ### end Alembic commands ###