Skip to content

Commit

Permalink
feat!: Support timestamp input for set_system_time function and align…
Browse files Browse the repository at this point in the history
… its implementation with the original extension (#80)

* Support timestamp input for set_system_time function and align its implementation with the original extension

* Fix CI failure

* Added back set_system_time tests

* Update documentation
  • Loading branch information
bilalshareef authored Dec 2, 2024
1 parent 923ffb5 commit 6dd5ad8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 75 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ Should return something similar to:
## Setting custom system time

If you want to take advantage of setting a custom system time you can use the `set_system_time` function. It is a port of the original [set_system_time](https://github.com/arkhipov/temporal_tables#advanced-usage).
The function accepts string representation of timestamp in the following format: `YYYY-MM-DD HH:MI:SS.MS.US` - where hours are in 24-hour format 00-23 and the MS (milliseconds) and US (microseconds) portions are optional.
The function accepts a timestamp as input. It also accepts string representation of a timestamp in the following formats.
- `YYYY-MM-DD HH:MI:SS`
- `YYYY-MM-DD`

Same as the original function, calling it with `null` will reset to default setting (using the CURRENT_TIMESTAMP):

```sql
Expand All @@ -128,7 +131,7 @@ psql temporal_test < system_time_function.sql
Set a custom value for the system time:

```sql
SELECT set_system_time('1999-12-31 23:59:59');
SELECT set_system_time('1999-12-31 23:59:59'::timestamptz);
```

Now test with some data:
Expand Down
20 changes: 5 additions & 15 deletions system_time_function.sql
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
-- version 0.6.1

CREATE OR REPLACE FUNCTION set_system_time(user_timestamp text)
RETURNS text AS $$
CREATE OR REPLACE FUNCTION set_system_time(user_timestamp timestamptz)
RETURNS void AS $$
DECLARE
custom_system_time text;
BEGIN
IF user_timestamp IS NULL THEN
custom_system_time := null;
ELSE
PERFORM
REGEXP_MATCHES(user_timestamp,
'(\d){4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1]) ([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d{1,3})?(\.\d{1,6})?',
'g');
IF NOT FOUND THEN
RAISE 'You must enter a timestamp in the following format: YYYY-MM-DD HH24:MI:SS.MS.US (hours are in 24-hour format 00-23, MS and US are optional)';
ELSE
custom_system_time := user_timestamp;
END IF;
custom_system_time := TO_CHAR(user_timestamp, 'YYYY-MM-DD HH24:MI:SS');
END IF;

PERFORM set_config('user_defined.system_time', custom_system_time, false);

return custom_system_time;
PERFORM set_config('user_defined.system_time', custom_system_time, false);

END;
END;
$$ LANGUAGE plpgsql;
74 changes: 37 additions & 37 deletions test/expected/set_system_time.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ BEFORE INSERT OR UPDATE OR DELETE ON versioning
FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'versioning_history', false);
-- Insert.
BEGIN;
SELECT set_system_time('2001-01-01 22:59:59.001.000234');
set_system_time
--------------------------------
2001-01-01 22:59:59.001.000234
SELECT set_system_time('2001-01-01 22:59:59');
set_system_time
-----------------

(1 row)

INSERT INTO versioning (a) VALUES (3);
SELECT a, "b b", sys_period FROM versioning ORDER BY a, sys_period;
a | b b | sys_period
---+-----+------------------------------------------
---+-----+-----------------------------------
1 | | [-infinity,)
2 | | ["Sat Jan 01 00:00:00 2000 UTC",)
3 | | ["Mon Jan 01 22:59:59.001234 2001 UTC",)
3 | | ["Mon Jan 01 22:59:59 2001 UTC",)
(3 rows)

SELECT * FROM versioning_history ORDER BY a, sys_period;
Expand All @@ -31,75 +31,75 @@ SELECT * FROM versioning_history ORDER BY a, sys_period;
COMMIT;
-- Update.
BEGIN;
SELECT set_system_time('2001-02-01 22:59:59.001.000234');
SELECT set_system_time('2001-02-01 22:59:59');
set_system_time
--------------------------------
2001-02-01 22:59:59.001.000234
-----------------

(1 row)

UPDATE versioning SET a = 4 WHERE a = 3;
SELECT a, "b b", sys_period FROM versioning ORDER BY a, sys_period;
a | b b | sys_period
---+-----+------------------------------------------
a | b b | sys_period
---+-----+-----------------------------------
1 | | [-infinity,)
2 | | ["Sat Jan 01 00:00:00 2000 UTC",)
4 | | ["Thu Feb 01 22:59:59.001234 2001 UTC",)
4 | | ["Thu Feb 01 22:59:59 2001 UTC",)
(3 rows)

SELECT a, c, sys_period FROM versioning_history ORDER BY a, sys_period;
a | c | sys_period
---+---+-------------------------------------------------------------------------------
3 | | ["Mon Jan 01 22:59:59.001234 2001 UTC","Thu Feb 01 22:59:59.001234 2001 UTC")
a | c | sys_period
---+---+-----------------------------------------------------------------
3 | | ["Mon Jan 01 22:59:59 2001 UTC","Thu Feb 01 22:59:59 2001 UTC")
(1 row)

COMMIT;
BEGIN;
SELECT set_system_time('2001-03-01 22:59:59.001.000234');
set_system_time
--------------------------------
2001-03-01 22:59:59.001.000234
SELECT set_system_time('2001-03-01 22:59:59');
set_system_time
-----------------

(1 row)

UPDATE versioning SET a = 5 WHERE a = 4;
UPDATE versioning SET "b b" = '2012-01-01' WHERE a = 5;
SELECT a, "b b", sys_period FROM versioning ORDER BY a, sys_period;
a | b b | sys_period
---+------------+------------------------------------------
a | b b | sys_period
---+------------+-----------------------------------
1 | | [-infinity,)
2 | | ["Sat Jan 01 00:00:00 2000 UTC",)
5 | 01-01-2012 | ["Thu Mar 01 22:59:59.001234 2001 UTC",)
5 | 01-01-2012 | ["Thu Mar 01 22:59:59 2001 UTC",)
(3 rows)

SELECT a, c, sys_period FROM versioning_history ORDER BY a, sys_period;
a | c | sys_period
---+---+-------------------------------------------------------------------------------
3 | | ["Mon Jan 01 22:59:59.001234 2001 UTC","Thu Feb 01 22:59:59.001234 2001 UTC")
4 | | ["Thu Feb 01 22:59:59.001234 2001 UTC","Thu Mar 01 22:59:59.001234 2001 UTC")
a | c | sys_period
---+---+-----------------------------------------------------------------
3 | | ["Mon Jan 01 22:59:59 2001 UTC","Thu Feb 01 22:59:59 2001 UTC")
4 | | ["Thu Feb 01 22:59:59 2001 UTC","Thu Mar 01 22:59:59 2001 UTC")
(2 rows)

COMMIT;
-- Delete.
BEGIN;
SELECT set_system_time('2001-04-01 22:59:59.001.000234');
set_system_time
--------------------------------
2001-04-01 22:59:59.001.000234
SELECT set_system_time('2001-04-01 22:59:59');
set_system_time
-----------------

(1 row)

DELETE FROM versioning WHERE a = 4;
SELECT a, "b b", sys_period FROM versioning ORDER BY a, sys_period;
a | b b | sys_period
---+------------+------------------------------------------
a | b b | sys_period
---+------------+-----------------------------------
1 | | [-infinity,)
2 | | ["Sat Jan 01 00:00:00 2000 UTC",)
5 | 01-01-2012 | ["Thu Mar 01 22:59:59.001234 2001 UTC",)
5 | 01-01-2012 | ["Thu Mar 01 22:59:59 2001 UTC",)
(3 rows)

SELECT a, c, sys_period FROM versioning_history ORDER BY a, sys_period;
a | c | sys_period
---+---+-------------------------------------------------------------------------------
3 | | ["Mon Jan 01 22:59:59.001234 2001 UTC","Thu Feb 01 22:59:59.001234 2001 UTC")
4 | | ["Thu Feb 01 22:59:59.001234 2001 UTC","Thu Mar 01 22:59:59.001234 2001 UTC")
a | c | sys_period
---+---+-----------------------------------------------------------------
3 | | ["Mon Jan 01 22:59:59 2001 UTC","Thu Feb 01 22:59:59 2001 UTC")
4 | | ["Thu Feb 01 22:59:59 2001 UTC","Thu Mar 01 22:59:59 2001 UTC")
(2 rows)

COMMIT;
Expand Down
3 changes: 2 additions & 1 deletion test/runTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ REMOTE_TESTS="
combinations structure versioning
invalid_system_period invalid_system_period_values invalid_types
no_history_system_period no_history_table no_system_period
versioning_custom_system_time
"

./test/runRemoteTests.sh "$REMOTE_TESTS"
Expand All @@ -27,7 +28,7 @@ fi
TESTS="
upper_case different_schema unchanged_values unchanged_version_values
non_equality_types non_equality_types_unchanged_values
set_system_time invalid_set_system_time versioning_including_current_version_in_history
set_system_time versioning_including_current_version_in_history
versioning_rollback_include_current_version_in_history noop_update
"

Expand Down
16 changes: 0 additions & 16 deletions test/sql/invalid_set_system_time.sql

This file was deleted.

8 changes: 4 additions & 4 deletions test/sql/set_system_time.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'versioning_history', fa
-- Insert.
BEGIN;

SELECT set_system_time('2001-01-01 22:59:59.001.000234');
SELECT set_system_time('2001-01-01 22:59:59');

INSERT INTO versioning (a) VALUES (3);

Expand All @@ -25,7 +25,7 @@ COMMIT;
-- Update.
BEGIN;

SELECT set_system_time('2001-02-01 22:59:59.001.000234');
SELECT set_system_time('2001-02-01 22:59:59');

UPDATE versioning SET a = 4 WHERE a = 3;

Expand All @@ -36,7 +36,7 @@ SELECT a, c, sys_period FROM versioning_history ORDER BY a, sys_period;
COMMIT;
BEGIN;

SELECT set_system_time('2001-03-01 22:59:59.001.000234');
SELECT set_system_time('2001-03-01 22:59:59');

UPDATE versioning SET a = 5 WHERE a = 4;
UPDATE versioning SET "b b" = '2012-01-01' WHERE a = 5;
Expand All @@ -50,7 +50,7 @@ COMMIT;
-- Delete.
BEGIN;

SELECT set_system_time('2001-04-01 22:59:59.001.000234');
SELECT set_system_time('2001-04-01 22:59:59');

DELETE FROM versioning WHERE a = 4;

Expand Down

0 comments on commit 6dd5ad8

Please sign in to comment.