accountant/migrations/versions/144929e0f5f_improve_operation_scheduling.py

145 lines
4.4 KiB
Python
Raw Normal View History

"""Improve operation scheduling.
Revision ID: 144929e0f5f
Revises: None
Create Date: 2015-07-17 15:04:01.002581
"""
# revision identifiers, used by Alembic.
revision = '144929e0f5f'
down_revision = None
from alembic import op
import sqlalchemy as sa
from accountant import session_scope
from accountant.api.models.scheduled_operations import ScheduledOperation
def upgrade():
op.get_bind().execute("DROP VIEW operation")
op.rename_table('entry', 'operation')
# Add column "canceled" in table "entry"
op.add_column(
'operation',
sa.Column(
'canceled',
sa.Boolean(),
nullable=False,
default=False,
server_default=sa.false()
)
)
# Add column "confirmed" in table "entry"
op.add_column(
'operation',
sa.Column(
'confirmed',
sa.Boolean(),
nullable=False,
default=True,
server_default=sa.true()
)
)
# Drop unused table canceled_operation.
op.drop_table('canceled_operation')
op.get_bind().execute(
"alter sequence entry_id_seq rename to operation_id_seq"
)
# TODO Alexis Lahouze 2015-07-17 Insert scheduling.
connection = op.get_bind()
Session = sa.orm.sessionmaker()
session = Session(bind=connection)
# Get all scheduled operations
scheduled_operations = ScheduledOperation.query(session).all()
for scheduled_operation in scheduled_operations:
scheduled_operation.reschedule(session)
def downgrade():
op.get_bind().execute(
"alter sequence operation_id_seq rename to entry_id_seq"
)
op.create_table(
"canceled_operation",
sa.Column("id", sa.Integer, primary_key=True),
sa.Column(
"scheduled_operation_id", sa.Integer(),
sa.ForeignKey("scheduled_operation.id")),
sa.Column("operation_date", sa.Date, nullable=False)
)
op.drop_column('operation', 'canceled')
op.drop_column('operation', 'confirmed')
op.rename_table('operation', 'entry')
op.get_bind().execute(
"""
CREATE VIEW operation AS
SELECT entry.id,
entry.operation_date,
entry.label,
entry.value,
entry.account_id,
entry.category,
entry.pointed,
entry.scheduled_operation_id,
false AS canceled
FROM entry
UNION
SELECT NULL::bigint AS id,
scheduled_operation.operation_date,
scheduled_operation.label,
scheduled_operation.value,
scheduled_operation.account_id,
scheduled_operation.category,
false AS pointed,
scheduled_operation.id AS scheduled_operation_id,
false AS canceled
FROM (
SELECT scheduled_operation_1.id,
scheduled_operation_1.start_date,
scheduled_operation_1.stop_date,
scheduled_operation_1.day,
scheduled_operation_1.frequency,
scheduled_operation_1.label,
scheduled_operation_1.value,
scheduled_operation_1.account_id,
scheduled_operation_1.category,
generate_series(scheduled_operation_1.start_date::timestamp without time zone, scheduled_operation_1.stop_date::timestamp without time zone, scheduled_operation_1.frequency::double precision * '1 mon'::interval) AS operation_date
FROM scheduled_operation scheduled_operation_1) scheduled_operation
LEFT JOIN (
SELECT entry.scheduled_operation_id,
date_trunc('MONTH'::text, entry.operation_date::timestamp with time zone) AS operation_date
FROM entry
UNION
SELECT canceled_operation.scheduled_operation_id,
date_trunc('MONTH'::text, canceled_operation.operation_date::timestamp with time zone) AS operation_date
FROM canceled_operation
) saved_operation ON saved_operation.scheduled_operation_id = scheduled_operation.id AND saved_operation.operation_date = date_trunc('MONTH'::text, scheduled_operation.operation_date)
WHERE saved_operation.scheduled_operation_id IS NULL
UNION
SELECT NULL::bigint AS id,
canceled_operation.operation_date,
scheduled_operation.label,
scheduled_operation.value,
scheduled_operation.account_id,
scheduled_operation.category,
false AS pointed,
scheduled_operation.id AS scheduled_operation_id,
true AS canceled
FROM scheduled_operation
JOIN canceled_operation ON canceled_operation.scheduled_operation_id = scheduled_operation.id;
"""
)