forked from Kinto/kinto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Kinto#665 from mozilla-services/fix-behaviour-spec…
…ified-timestamps Fix PG behaviour when specifying timestamps in the past
- Loading branch information
Showing
10 changed files
with
326 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
cliquet/storage/postgresql/migrations/migration_009_010.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
CREATE TABLE IF NOT EXISTS timestamps ( | ||
parent_id TEXT NOT NULL, | ||
collection_id TEXT NOT NULL, | ||
last_modified TIMESTAMP NOT NULL, | ||
PRIMARY KEY (parent_id, collection_id) | ||
); | ||
|
||
|
||
CREATE OR REPLACE FUNCTION collection_timestamp(uid VARCHAR, resource VARCHAR) | ||
RETURNS TIMESTAMP AS $$ | ||
DECLARE | ||
ts TIMESTAMP; | ||
BEGIN | ||
ts := NULL; | ||
|
||
SELECT last_modified INTO ts | ||
FROM timestamps | ||
WHERE parent_id = uid | ||
AND collection_id = resource; | ||
|
||
IF ts IS NULL THEN | ||
ts := clock_timestamp(); | ||
INSERT INTO timestamps (parent_id, collection_id, last_modified) | ||
VALUES (uid, resource, ts); | ||
END IF; | ||
|
||
RETURN ts; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
|
||
CREATE OR REPLACE FUNCTION bump_timestamp() | ||
RETURNS trigger AS $$ | ||
DECLARE | ||
previous TIMESTAMP; | ||
current TIMESTAMP; | ||
|
||
BEGIN | ||
previous := NULL; | ||
SELECT last_modified INTO previous | ||
FROM timestamps | ||
WHERE parent_id = NEW.parent_id | ||
AND collection_id = NEW.collection_id; | ||
|
||
-- | ||
-- This bumps the current timestamp to 1 msec in the future if the previous | ||
-- timestamp is equal to the current one (or higher if was bumped already). | ||
-- | ||
-- If a bunch of requests from the same user on the same collection | ||
-- arrive in the same millisecond, the unicity constraint can raise | ||
-- an error (operation is cancelled). | ||
-- See https://github.com/mozilla-services/cliquet/issues/25 | ||
-- | ||
current := clock_timestamp(); | ||
IF previous IS NOT NULL AND previous >= current THEN | ||
current := previous + INTERVAL '1 milliseconds'; | ||
END IF; | ||
|
||
|
||
IF NEW.last_modified IS NULL THEN | ||
-- If record does not carry last-modified, assign it to current. | ||
NEW.last_modified := current; | ||
ELSE | ||
-- Use record last-modified as collection timestamp. | ||
IF previous IS NULL OR NEW.last_modified > previous THEN | ||
current := NEW.last_modified; | ||
END IF; | ||
END IF; | ||
|
||
-- | ||
-- Upsert current collection timestamp. | ||
-- | ||
WITH upsert AS ( | ||
UPDATE timestamps SET last_modified = current | ||
WHERE parent_id = NEW.parent_id AND collection_id = NEW.collection_id | ||
RETURNING * | ||
) | ||
INSERT INTO timestamps (parent_id, collection_id, last_modified) | ||
SELECT NEW.parent_id, NEW.collection_id, current | ||
WHERE NOT EXISTS (SELECT * FROM upsert); | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
|
||
-- Bump storage schema version. | ||
INSERT INTO metadata (name, value) VALUES ('storage_schema_version', '10'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.