"""
  The MIT License (MIT)
  Copyright (c) 2016 Intel Corporation

  Permission is hereby granted, free of charge, to any person obtaining a copy of 
  this software and associated documentation files (the "Software"), to deal in 
  the Software without restriction, including without limitation the rights to 
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 
  the Software, and to permit persons to whom the Software is furnished to do so, 
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all 
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

"""Corrected triggers for concurrency handling in db_row,reference_set

Revision ID: 323f2c3ccf2f
Revises: 389236a48cd4
Create Date: 2015-12-17 17:10:54.455433

"""

# revision identifiers, used by Alembic.
revision = '323f2c3ccf2f'
down_revision = '389236a48cd4'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    # drop and re-create trigger
    op.execute(
        'DROP TRIGGER IF EXISTS increment_num_rows_in_db_array ON callset_to_db_array_association CASCADE')
    op.execute('''\
    CREATE OR REPLACE FUNCTION increment_num_rows_in_db_array_pgsql()
      RETURNS trigger AS $increment_num_rows_in_db_array_pgsql$
    DECLARE
        updated_num_rows bigint;
    BEGIN
        UPDATE db_array SET num_rows=
            CASE
               WHEN NEW.tile_row_id IS NULL THEN num_rows+1
               WHEN NEW.tile_row_id >= num_rows THEN NEW.tile_row_id+1
               ELSE num_rows
            END
        WHERE id=NEW.db_array_id RETURNING num_rows INTO updated_num_rows;
        IF NEW.tile_row_id IS NULL THEN
            NEW.tile_row_id = updated_num_rows-1;
        END IF;
        RETURN NEW;
    END;
    $increment_num_rows_in_db_array_pgsql$ LANGUAGE plpgsql;
    CREATE TRIGGER increment_num_rows_in_db_array BEFORE INSERT ON callset_to_db_array_association
    FOR EACH ROW EXECUTE PROCEDURE increment_num_rows_in_db_array_pgsql();
    ''')
    # drop and re-create trigger
    op.execute(
        'DROP TRIGGER IF EXISTS increment_next_column_in_reference_set ON reference CASCADE')
    op.execute('''\
    CREATE OR REPLACE FUNCTION increment_next_column_in_reference_set_pgsql()
      RETURNS trigger AS $increment_next_column_in_reference_set_pgsql$
    DECLARE
        updated_next_tiledb_column_offset bigint;
    BEGIN
        UPDATE reference_set SET next_tiledb_column_offset=
            CASE
                WHEN NEW.tiledb_column_offset IS NULL THEN next_tiledb_column_offset+NEW.length
                WHEN NEW.tiledb_column_offset+NEW.length>next_tiledb_column_offset THEN NEW.tiledb_column_offset+NEW.length
                ELSE next_tiledb_column_offset
            END
        WHERE id = NEW.reference_set_id RETURNING next_tiledb_column_offset INTO updated_next_tiledb_column_offset;
        IF NEW.tiledb_column_offset IS NULL THEN
            NEW.tiledb_column_offset = updated_next_tiledb_column_offset-NEW.length;
        END IF;
        RETURN NEW;
    END;
    $increment_next_column_in_reference_set_pgsql$ LANGUAGE plpgsql;
    CREATE TRIGGER increment_next_column_in_reference_set BEFORE INSERT ON reference
    FOR EACH ROW EXECUTE PROCEDURE increment_next_column_in_reference_set_pgsql();
    ''')


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    # Keep the correct updated triggers
    pass
    ### end Alembic commands ###
