#1400 - Postgres @History table sys_period empty with multiple updates

This commit is contained in:
rob bygrave committed 2018-06-11 22:10:43 +12:00
1 parent 3238c0def4
commit 2427630c07
7 files changed
+187 -52

No files matched your search

@@ -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<SqlRow> 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.
*/
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;