"""Add table auditing

Revision ID: c1197ef009a5
Revises: 3bf110ac566b
Create Date: 2019-06-23 12:25:40.796498

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'c1197ef009a5'
down_revision = '3bf110ac566b'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('test_audit',
    sa.Column('audit_operation', sa.String(length=1), nullable=False),
    sa.Column('audit_operation_timestamp', sa.DateTime(), nullable=False),
    sa.Column('audit_current_user', sa.String(length=64), nullable=False),
    sa.Column('id', sa.Integer(), nullable=True),
    sa.Column('name', sa.String(), nullable=True)
    )
    op.execute("CREATE OR REPLACE FUNCTION public_test_audit() RETURNS TRIGGER AS $public_test_audit$\nBEGIN\n    \n\n    IF (TG_OP = 'DELETE') THEN\n        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'D', now(), current_user, OLD.id, OLD.name;\n    ELSIF (TG_OP = 'UPDATE') THEN\n        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'U', now(), current_user, NEW.id, NEW.name;\n    ELSIF (TG_OP = 'INSERT') THEN\n        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'I', now(), current_user, NEW.id, NEW.name;\n    END IF;\n    RETURN NULL; -- result is ignored since this is an AFTER trigger\nEND;\n$public_test_audit$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS public_test_audit ON test;\n\nCREATE TRIGGER public_test_audit\nAFTER INSERT OR UPDATE OR DELETE ON test\nFOR EACH ROW EXECUTE PROCEDURE public_test_audit();\n")
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute('DROP TRIGGER IF EXISTS public_test_audit ON test;\nDROP FUNCTION IF EXISTS public_test_audit;\n')
    op.drop_table('test_audit')
    # ### end Alembic commands ###

10e00c43159a082e62c6aad5966975f90793a212
<mask token>


def upgrade():
    op.create_table('test_audit', sa.Column('audit_operation', sa.String(
        length=1), nullable=False), sa.Column('audit_operation_timestamp',
        sa.DateTime(), nullable=False), sa.Column('audit_current_user', sa.
        String(length=64), nullable=False), sa.Column('id', sa.Integer(),
        nullable=True), sa.Column('name', sa.String(), nullable=True))
    op.execute(
        """CREATE OR REPLACE FUNCTION public_test_audit() RETURNS TRIGGER AS $public_test_audit$
BEGIN
    

    IF (TG_OP = 'DELETE') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'D', now(), current_user, OLD.id, OLD.name;
    ELSIF (TG_OP = 'UPDATE') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'U', now(), current_user, NEW.id, NEW.name;
    ELSIF (TG_OP = 'INSERT') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'I', now(), current_user, NEW.id, NEW.name;
    END IF;
    RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$public_test_audit$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS public_test_audit ON test;

CREATE TRIGGER public_test_audit
AFTER INSERT OR UPDATE OR DELETE ON test
FOR EACH ROW EXECUTE PROCEDURE public_test_audit();
"""
        )


<mask token>

<mask token>


def upgrade():
    op.create_table('test_audit', sa.Column('audit_operation', sa.String(
        length=1), nullable=False), sa.Column('audit_operation_timestamp',
        sa.DateTime(), nullable=False), sa.Column('audit_current_user', sa.
        String(length=64), nullable=False), sa.Column('id', sa.Integer(),
        nullable=True), sa.Column('name', sa.String(), nullable=True))
    op.execute(
        """CREATE OR REPLACE FUNCTION public_test_audit() RETURNS TRIGGER AS $public_test_audit$
BEGIN
    

    IF (TG_OP = 'DELETE') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'D', now(), current_user, OLD.id, OLD.name;
    ELSIF (TG_OP = 'UPDATE') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'U', now(), current_user, NEW.id, NEW.name;
    ELSIF (TG_OP = 'INSERT') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'I', now(), current_user, NEW.id, NEW.name;
    END IF;
    RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$public_test_audit$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS public_test_audit ON test;

CREATE TRIGGER public_test_audit
AFTER INSERT OR UPDATE OR DELETE ON test
FOR EACH ROW EXECUTE PROCEDURE public_test_audit();
"""
        )


def downgrade():
    op.execute(
        """DROP TRIGGER IF EXISTS public_test_audit ON test;
DROP FUNCTION IF EXISTS public_test_audit;
"""
        )
    op.drop_table('test_audit')

<mask token>
revision = 'c1197ef009a5'
down_revision = '3bf110ac566b'
branch_labels = None
depends_on = None


def upgrade():
    op.create_table('test_audit', sa.Column('audit_operation', sa.String(
        length=1), nullable=False), sa.Column('audit_operation_timestamp',
        sa.DateTime(), nullable=False), sa.Column('audit_current_user', sa.
        String(length=64), nullable=False), sa.Column('id', sa.Integer(),
        nullable=True), sa.Column('name', sa.String(), nullable=True))
    op.execute(
        """CREATE OR REPLACE FUNCTION public_test_audit() RETURNS TRIGGER AS $public_test_audit$
BEGIN
    

    IF (TG_OP = 'DELETE') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'D', now(), current_user, OLD.id, OLD.name;
    ELSIF (TG_OP = 'UPDATE') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'U', now(), current_user, NEW.id, NEW.name;
    ELSIF (TG_OP = 'INSERT') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'I', now(), current_user, NEW.id, NEW.name;
    END IF;
    RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$public_test_audit$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS public_test_audit ON test;

CREATE TRIGGER public_test_audit
AFTER INSERT OR UPDATE OR DELETE ON test
FOR EACH ROW EXECUTE PROCEDURE public_test_audit();
"""
        )


def downgrade():
    op.execute(
        """DROP TRIGGER IF EXISTS public_test_audit ON test;
DROP FUNCTION IF EXISTS public_test_audit;
"""
        )
    op.drop_table('test_audit')

<mask token>
from alembic import op
import sqlalchemy as sa
revision = 'c1197ef009a5'
down_revision = '3bf110ac566b'
branch_labels = None
depends_on = None


def upgrade():
    op.create_table('test_audit', sa.Column('audit_operation', sa.String(
        length=1), nullable=False), sa.Column('audit_operation_timestamp',
        sa.DateTime(), nullable=False), sa.Column('audit_current_user', sa.
        String(length=64), nullable=False), sa.Column('id', sa.Integer(),
        nullable=True), sa.Column('name', sa.String(), nullable=True))
    op.execute(
        """CREATE OR REPLACE FUNCTION public_test_audit() RETURNS TRIGGER AS $public_test_audit$
BEGIN
    

    IF (TG_OP = 'DELETE') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'D', now(), current_user, OLD.id, OLD.name;
    ELSIF (TG_OP = 'UPDATE') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'U', now(), current_user, NEW.id, NEW.name;
    ELSIF (TG_OP = 'INSERT') THEN
        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'I', now(), current_user, NEW.id, NEW.name;
    END IF;
    RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$public_test_audit$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS public_test_audit ON test;

CREATE TRIGGER public_test_audit
AFTER INSERT OR UPDATE OR DELETE ON test
FOR EACH ROW EXECUTE PROCEDURE public_test_audit();
"""
        )


def downgrade():
    op.execute(
        """DROP TRIGGER IF EXISTS public_test_audit ON test;
DROP FUNCTION IF EXISTS public_test_audit;
"""
        )
    op.drop_table('test_audit')

"""Add table auditing

Revision ID: c1197ef009a5
Revises: 3bf110ac566b
Create Date: 2019-06-23 12:25:40.796498

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'c1197ef009a5'
down_revision = '3bf110ac566b'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('test_audit',
    sa.Column('audit_operation', sa.String(length=1), nullable=False),
    sa.Column('audit_operation_timestamp', sa.DateTime(), nullable=False),
    sa.Column('audit_current_user', sa.String(length=64), nullable=False),
    sa.Column('id', sa.Integer(), nullable=True),
    sa.Column('name', sa.String(), nullable=True)
    )
    op.execute("CREATE OR REPLACE FUNCTION public_test_audit() RETURNS TRIGGER AS $public_test_audit$\nBEGIN\n    \n\n    IF (TG_OP = 'DELETE') THEN\n        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'D', now(), current_user, OLD.id, OLD.name;\n    ELSIF (TG_OP = 'UPDATE') THEN\n        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'U', now(), current_user, NEW.id, NEW.name;\n    ELSIF (TG_OP = 'INSERT') THEN\n        INSERT INTO test_audit (audit_operation, audit_operation_timestamp, audit_current_user, id, name) SELECT 'I', now(), current_user, NEW.id, NEW.name;\n    END IF;\n    RETURN NULL; -- result is ignored since this is an AFTER trigger\nEND;\n$public_test_audit$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS public_test_audit ON test;\n\nCREATE TRIGGER public_test_audit\nAFTER INSERT OR UPDATE OR DELETE ON test\nFOR EACH ROW EXECUTE PROCEDURE public_test_audit();\n")
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute('DROP TRIGGER IF EXISTS public_test_audit ON test;\nDROP FUNCTION IF EXISTS public_test_audit;\n')
    op.drop_table('test_audit')
    # ### end Alembic commands ###
