diff --git a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PostgresHistoryDdl.java b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PostgresHistoryDdl.java index fc79ff5ca..d1b34b976 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PostgresHistoryDdl.java +++ b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PostgresHistoryDdl.java @@ -79,13 +79,21 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl { protected void createOrReplaceFunction(DdlBuffer apply, String procedureName, String historyTable, List includedColumns) throws IOException { apply - .append("create or replace function ").append(procedureName).append("() returns trigger as $$").newLine() - .append("begin").newLine(); + .append("create or replace function ").append(procedureName).append("() returns trigger as $$").newLine(); + + apply.append("declare").newLine() + .append(" lowerTs timestamptz;").newLine() + .append(" upperTs timestamptz;").newLine(); + + apply.append("begin").newLine() + .append(" lowerTs = lower(OLD.sys_period);").newLine() + .append(" upperTs = greatest(lowerTs + '1 microsecond',current_timestamp);").newLine(); + apply .append(" if (TG_OP = 'UPDATE') then").newLine(); appendInsertIntoHistory(apply, historyTable, includedColumns); apply - .append(" NEW.").append(sysPeriod).append(" = tstzrange(").append(currentTimestamp).append(",null);").newLine() + .append(" NEW.").append(sysPeriod).append(" = tstzrange(upperTs,null);").newLine() .append(" return new;").newLine(); apply .append(" elsif (TG_OP = 'DELETE') then").newLine(); @@ -124,7 +132,7 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl { buffer.append(" insert into ").append(historyTable).append(" (").append(sysPeriod).append(","); appendColumnNames(buffer, columns, ""); - buffer.append(") values (tstzrange(lower(OLD.").append(sysPeriod).append("), ").append(currentTimestamp).append("), "); + buffer.append(") values (tstzrange(lowerTs,upperTs), "); appendColumnNames(buffer, columns, "OLD."); buffer.append(");").newLine(); } diff --git a/src/test/java/org/tests/history/TestHistoryInsert.java b/src/test/java/org/tests/history/TestHistoryInsert.java index 67a1e93e4..40f95ce69 100644 --- a/src/test/java/org/tests/history/TestHistoryInsert.java +++ b/src/test/java/org/tests/history/TestHistoryInsert.java @@ -4,6 +4,7 @@ import io.ebean.BaseTestCase; import io.ebean.Ebean; import io.ebean.SqlQuery; import io.ebean.SqlRow; +import io.ebean.Transaction; import io.ebean.Version; import io.ebean.annotation.ForPlatform; import io.ebean.annotation.Platform; @@ -93,6 +94,52 @@ public class TestHistoryInsert extends BaseTestCase { assertThat(versions).hasSize(3); } + @Test + @ForPlatform({Platform.POSTGRES}) + public void test_singleTransaction_multipleHistory() { + + User user = new User(); + user.setName("First"); + user.setEmail("first@email.com"); + user.setPasswordHash("someHash"); + + try (Transaction transaction = Ebean.beginTransaction()) { + // insert and many updates inside transaction + Ebean.save(user); + user.setEmail("first2@email.com"); + Ebean.save(user); + user.setEmail("first3@email.com"); + Ebean.save(user); + user.setEmail("first4@email.com"); + Ebean.save(user); + + transaction.commit(); + } + + // a couple more updates outside of the first transaction + user.setEmail("first5@email.com"); + Ebean.save(user); + + user.setEmail("first6@email.com"); + Ebean.save(user); + + List sqlRows = + Ebean.createSqlQuery("select lower(sys_period) lowerBound, upper(sys_period) upperBound from c_user_history where id = :id order by when_modified") + .setParameter("id", user.getId()) + .findList(); + + Timestamp previousUpper = null; + + for (SqlRow sqlRow : sqlRows) { + Timestamp nextLower = sqlRow.getTimestamp("lowerBound"); + Timestamp nextUpper = sqlRow.getTimestamp("upperBound"); + if (previousUpper != null) { + assertThat(previousUpper).isEqualTo(nextLower); + } + previousUpper = nextUpper; + } + } + /** * Use SqlQuery to query the history table directly. */ diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.0__initial.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.0__initial.sql index 0ad03898d..92439eac6 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.0__initial.sql @@ -184,13 +184,18 @@ create table migtest_e_history6_history(like migtest_e_history6); create view migtest_e_history6_with_history as select * from migtest_e_history6 union all select * from migtest_e_history6_history; create or replace function migtest_e_history2_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.obsolete_string2); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.obsolete_string2); + insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2); return old; end if; end; @@ -201,13 +206,18 @@ create trigger migtest_e_history2_history_upd for each row execute procedure migtest_e_history2_history_version(); create or replace function migtest_e_history3_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history3_history (sys_period,id, test_string) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history3_history (sys_period,id, test_string) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history3_history (sys_period,id, test_string) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string); + insert into migtest_e_history3_history (sys_period,id, test_string) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string); return old; end if; end; @@ -218,13 +228,18 @@ create trigger migtest_e_history3_history_upd for each row execute procedure migtest_e_history3_history_version(); create or replace function migtest_e_history4_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number); + insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number); return old; end if; end; @@ -235,13 +250,18 @@ create trigger migtest_e_history4_history_upd for each row execute procedure migtest_e_history4_history_version(); create or replace function migtest_e_history5_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history5_history (sys_period,id, test_number) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history5_history (sys_period,id, test_number) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history5_history (sys_period,id, test_number) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number); + insert into migtest_e_history5_history (sys_period,id, test_number) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number); return old; end if; end; @@ -252,13 +272,18 @@ create trigger migtest_e_history5_history_upd for each row execute procedure migtest_e_history5_history_version(); create or replace function migtest_e_history6_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history6_history (sys_period,id, test_number1, test_number2) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number1, OLD.test_number2); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history6_history (sys_period,id, test_number1, test_number2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number1, OLD.test_number2); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history6_history (sys_period,id, test_number1, test_number2) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number1, OLD.test_number2); + insert into migtest_e_history6_history (sys_period,id, test_number1, test_number2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number1, OLD.test_number2); return old; end if; end; diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql index 40c16d978..7e13e6924 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql @@ -131,13 +131,18 @@ create view migtest_e_history4_with_history as select * from migtest_e_history4 create view migtest_e_history5_with_history as select * from migtest_e_history5 union all select * from migtest_e_history5_history; create or replace function migtest_e_history_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history_history (sys_period,id, test_string) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history_history (sys_period,id, test_string) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history_history (sys_period,id, test_string) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string); + insert into migtest_e_history_history (sys_period,id, test_string) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string); return old; end if; end; @@ -149,13 +154,18 @@ create trigger migtest_e_history_history_upd -- changes: [add test_string2, add test_string3] create or replace function migtest_e_history2_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, obsolete_string1, obsolete_string2) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, obsolete_string1, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, obsolete_string1, obsolete_string2) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2); + insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, obsolete_string1, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2); return old; end if; end; @@ -163,13 +173,18 @@ $$ LANGUAGE plpgsql; -- changes: [exclude test_string] create or replace function migtest_e_history3_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history3_history (sys_period,id) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history3_history (sys_period,id) values (tstzrange(lowerTs,upperTs), OLD.id); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history3_history (sys_period,id) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id); + insert into migtest_e_history3_history (sys_period,id) values (tstzrange(lowerTs,upperTs), OLD.id); return old; end if; end; @@ -177,13 +192,18 @@ $$ LANGUAGE plpgsql; -- changes: [alter test_number] create or replace function migtest_e_history4_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number); + insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number); return old; end if; end; @@ -191,13 +211,18 @@ $$ LANGUAGE plpgsql; -- changes: [add test_boolean] create or replace function migtest_e_history5_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history5_history (sys_period,id, test_number, test_boolean) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number, OLD.test_boolean); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history5_history (sys_period,id, test_number, test_boolean) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number, OLD.test_boolean); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history5_history (sys_period,id, test_number, test_boolean) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number, OLD.test_boolean); + insert into migtest_e_history5_history (sys_period,id, test_number, test_boolean) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number, OLD.test_boolean); return old; end if; end; diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.2__dropsFor_1.1.sql index 67f252e11..83c88afd6 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.2__dropsFor_1.1.sql @@ -21,13 +21,18 @@ create view migtest_e_history2_with_history as select * from migtest_e_history2 -- changes: [drop obsolete_string1, drop obsolete_string2] create or replace function migtest_e_history2_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history2_history (sys_period,id, test_string, test_string3) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.test_string3); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history2_history (sys_period,id, test_string, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history2_history (sys_period,id, test_string, test_string3) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.test_string3); + insert into migtest_e_history2_history (sys_period,id, test_string, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3); return old; end if; end; diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql index 89e93220f..cdd4d2bd6 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql @@ -74,13 +74,18 @@ create view migtest_e_history4_with_history as select * from migtest_e_history4 -- changes: [add obsolete_string1, add obsolete_string2] create or replace function migtest_e_history2_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3); + insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3); return old; end if; end; @@ -88,13 +93,18 @@ $$ LANGUAGE plpgsql; -- changes: [include test_string] create or replace function migtest_e_history3_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history3_history (sys_period,id, test_string) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history3_history (sys_period,id, test_string) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history3_history (sys_period,id, test_string) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string); + insert into migtest_e_history3_history (sys_period,id, test_string) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string); return old; end if; end; @@ -102,13 +112,18 @@ $$ LANGUAGE plpgsql; -- changes: [alter test_number] create or replace function migtest_e_history4_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number); + insert into migtest_e_history4_history (sys_period,id, test_number) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number); return old; end if; end; diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.4__dropsFor_1.3.sql index af25ac556..6530a3b97 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.4__dropsFor_1.3.sql @@ -50,13 +50,18 @@ create view migtest_e_history5_with_history as select * from migtest_e_history5 -- changes: [drop test_string2, drop test_string3] create or replace function migtest_e_history2_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.obsolete_string2); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.obsolete_string2); + insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2); return old; end if; end; @@ -64,13 +69,18 @@ $$ LANGUAGE plpgsql; -- changes: [drop test_boolean] create or replace function migtest_e_history5_history_version() returns trigger as $$ +declare + lowerTs timestamptz; + upperTs timestamptz; begin + lowerTs = lower(OLD.sys_period); + upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history5_history (sys_period,id, test_number) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number); - NEW.sys_period = tstzrange(current_timestamp,null); + insert into migtest_e_history5_history (sys_period,id, test_number) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number); + NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history5_history (sys_period,id, test_number) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_number); + insert into migtest_e_history5_history (sys_period,id, test_number) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_number); return old; end if; end;