@pgpm/uuid 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/Makefile +6 -0
- package/README.md +5 -0
- package/__tests__/uuid.test.ts +90 -0
- package/deploy/schemas/uuids/procedures/pseudo_order_seed_uuid.sql +50 -0
- package/deploy/schemas/uuids/procedures/pseudo_order_uuid.sql +27 -0
- package/deploy/schemas/uuids/procedures/trigger_set_uuid_related_field.sql +29 -0
- package/deploy/schemas/uuids/procedures/trigger_set_uuid_seed.sql +24 -0
- package/deploy/schemas/uuids/schema.sql +16 -0
- package/jest.config.js +15 -0
- package/launchql-uuid.control +8 -0
- package/launchql.plan +9 -0
- package/package.json +28 -0
- package/revert/schemas/uuids/procedures/pseudo_order_seed_uuid.sql +7 -0
- package/revert/schemas/uuids/procedures/pseudo_order_uuid.sql +7 -0
- package/revert/schemas/uuids/procedures/trigger_set_uuid_related_field.sql +7 -0
- package/revert/schemas/uuids/procedures/trigger_set_uuid_seed.sql +7 -0
- package/revert/schemas/uuids/schema.sql +7 -0
- package/sqitch.plan +9 -0
- package/sql/launchql-uuid--0.4.6.sql +72 -0
- package/verify/schemas/uuids/procedures/pseudo_order_seed_uuid.sql +7 -0
- package/verify/schemas/uuids/procedures/pseudo_order_uuid.sql +7 -0
- package/verify/schemas/uuids/procedures/trigger_set_uuid_related_field.sql +7 -0
- package/verify/schemas/uuids/procedures/trigger_set_uuid_seed.sql +7 -0
- package/verify/schemas/uuids/schema.sql +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Interweb, Inc.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/Makefile
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { getConnections, PgTestClient } from 'pgsql-test';
|
|
2
|
+
|
|
3
|
+
let pg: PgTestClient;
|
|
4
|
+
let teardown: () => Promise<void>;
|
|
5
|
+
|
|
6
|
+
beforeAll(async () => {
|
|
7
|
+
({ pg, teardown } = await getConnections());
|
|
8
|
+
|
|
9
|
+
await pg.any(`
|
|
10
|
+
CREATE TABLE public.items (
|
|
11
|
+
id UUID DEFAULT uuids.pseudo_order_uuid(),
|
|
12
|
+
name TEXT,
|
|
13
|
+
seed TEXT,
|
|
14
|
+
custom_id UUID
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
CREATE TABLE public.items_seeded (
|
|
18
|
+
id UUID,
|
|
19
|
+
tenant TEXT
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
CREATE TRIGGER set_custom_id
|
|
23
|
+
BEFORE INSERT ON public.items
|
|
24
|
+
FOR EACH ROW
|
|
25
|
+
EXECUTE FUNCTION uuids.trigger_set_uuid_seed('custom_id', 'seed');
|
|
26
|
+
|
|
27
|
+
CREATE TRIGGER set_id_from_tenant
|
|
28
|
+
BEFORE INSERT ON public.items_seeded
|
|
29
|
+
FOR EACH ROW
|
|
30
|
+
EXECUTE FUNCTION uuids.trigger_set_uuid_related_field('id', 'tenant');
|
|
31
|
+
`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
afterAll(async () => {
|
|
35
|
+
try {
|
|
36
|
+
await teardown();
|
|
37
|
+
} catch (e) {
|
|
38
|
+
console.error('Teardown failed:', e);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
beforeEach(() => pg.beforeEach());
|
|
43
|
+
afterEach(() => pg.afterEach());
|
|
44
|
+
|
|
45
|
+
describe('uuids.pseudo_order_uuid()', () => {
|
|
46
|
+
it('generates a valid UUID', async () => {
|
|
47
|
+
const { pseudo_order_uuid } = await pg.one(`
|
|
48
|
+
SELECT uuids.pseudo_order_uuid() AS pseudo_order_uuid
|
|
49
|
+
`);
|
|
50
|
+
expect(pseudo_order_uuid).toMatch(
|
|
51
|
+
/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}$/
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('uuids.pseudo_order_seed_uuid(seed)', () => {
|
|
57
|
+
it('generates a valid UUID from a seed', async () => {
|
|
58
|
+
const { pseudo_order_seed_uuid } = await pg.one(
|
|
59
|
+
`SELECT uuids.pseudo_order_seed_uuid($1) AS pseudo_order_seed_uuid`,
|
|
60
|
+
['tenant123']
|
|
61
|
+
);
|
|
62
|
+
expect(pseudo_order_seed_uuid).toMatch(
|
|
63
|
+
/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}$/
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('uuids.trigger_set_uuid_seed', () => {
|
|
69
|
+
it('sets custom_id based on static seed', async () => {
|
|
70
|
+
const { custom_id } = await pg.one(
|
|
71
|
+
`INSERT INTO public.items (name, seed) VALUES ($1, $2) RETURNING custom_id`,
|
|
72
|
+
['Item A', 'my-seed']
|
|
73
|
+
);
|
|
74
|
+
expect(custom_id).toMatch(
|
|
75
|
+
/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}$/
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('uuids.trigger_set_uuid_related_field', () => {
|
|
81
|
+
it('sets id based on tenant column', async () => {
|
|
82
|
+
const { id } = await pg.one(
|
|
83
|
+
`INSERT INTO public.items_seeded (tenant) VALUES ($1) RETURNING id`,
|
|
84
|
+
['tenant-42']
|
|
85
|
+
);
|
|
86
|
+
expect(id).toMatch(
|
|
87
|
+
/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}$/
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
-- Deploy schemas/uuids/procedures/pseudo_order_seed_uuid to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/uuids/schema
|
|
4
|
+
|
|
5
|
+
-- The first 2 characters of the UUID value comes from
|
|
6
|
+
-- the MD5 hash of the uuid_seed. The next
|
|
7
|
+
-- 2 characters are the MD5 hash of the concatenation
|
|
8
|
+
-- of the current year and week number. This value is,
|
|
9
|
+
-- of course, static over a week. The remaining of the
|
|
10
|
+
-- UUID value comes from the MD5 of a random value and
|
|
11
|
+
-- the current time at a precision of 1us. The third field
|
|
12
|
+
-- is prefixed with a “4” to indicate it is a version 4 UUID type.
|
|
13
|
+
|
|
14
|
+
BEGIN;
|
|
15
|
+
|
|
16
|
+
CREATE FUNCTION uuids.pseudo_order_seed_uuid(
|
|
17
|
+
seed text
|
|
18
|
+
)
|
|
19
|
+
RETURNS uuid
|
|
20
|
+
AS $$
|
|
21
|
+
DECLARE
|
|
22
|
+
new_uuid char(36);
|
|
23
|
+
md5_str char(32);
|
|
24
|
+
md5_str2 char(32);
|
|
25
|
+
uid text;
|
|
26
|
+
BEGIN
|
|
27
|
+
md5_str := md5(concat(random(), now()));
|
|
28
|
+
md5_str2 := md5(concat(random(), now()));
|
|
29
|
+
|
|
30
|
+
new_uuid := concat(
|
|
31
|
+
LEFT (md5(seed), 2),
|
|
32
|
+
LEFT (md5(concat(extract(year FROM now()), extract(week FROM now()))), 2),
|
|
33
|
+
substring(md5_str, 1, 4),
|
|
34
|
+
'-',
|
|
35
|
+
substring(md5_str, 5, 4),
|
|
36
|
+
'-4',
|
|
37
|
+
substring(md5_str2, 9, 3),
|
|
38
|
+
'-',
|
|
39
|
+
substring(md5_str, 13, 4),
|
|
40
|
+
'-',
|
|
41
|
+
substring(md5_str2, 17, 12)
|
|
42
|
+
);
|
|
43
|
+
RETURN new_uuid;
|
|
44
|
+
END;
|
|
45
|
+
$$
|
|
46
|
+
LANGUAGE plpgsql VOLATILE;
|
|
47
|
+
|
|
48
|
+
COMMENT ON FUNCTION uuids.pseudo_order_seed_uuid IS 'Pseudo Ordered UUID with a seed. Good for multi-tenant scenarios, other wise use non-seed.';
|
|
49
|
+
|
|
50
|
+
COMMIT;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
-- Deploy schemas/uuids/procedures/pseudo_order_uuid to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/uuids/schema
|
|
4
|
+
|
|
5
|
+
BEGIN;
|
|
6
|
+
|
|
7
|
+
CREATE FUNCTION uuids.pseudo_order_uuid()
|
|
8
|
+
RETURNS uuid
|
|
9
|
+
AS $$
|
|
10
|
+
DECLARE
|
|
11
|
+
new_uuid char(36);
|
|
12
|
+
md5_str char(32);
|
|
13
|
+
md5_str2 char(32);
|
|
14
|
+
BEGIN
|
|
15
|
+
md5_str := md5(concat(random(), now()));
|
|
16
|
+
md5_str2 := md5(concat(random(), now()));
|
|
17
|
+
new_uuid := concat(
|
|
18
|
+
LEFT (md5(concat(extract(year FROM now()), extract(week FROM now()))), 4),
|
|
19
|
+
LEFT (md5_str, 4), '-', substring(md5_str, 5, 4), '-4', substring(md5_str2, 9, 3), '-', substring(md5_str, 13, 4), '-', substring(md5_str2, 17, 12));
|
|
20
|
+
RETURN new_uuid;
|
|
21
|
+
END;
|
|
22
|
+
$$
|
|
23
|
+
LANGUAGE plpgsql VOLATILE;
|
|
24
|
+
|
|
25
|
+
COMMENT ON FUNCTION uuids.pseudo_order_seed_uuid IS 'Pseudo Ordered UUID';
|
|
26
|
+
|
|
27
|
+
COMMIT;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
-- Deploy schemas/uuids/procedures/trigger_set_uuid_related_field to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/uuids/schema
|
|
4
|
+
-- requires: schemas/uuids/procedures/pseudo_order_seed_uuid
|
|
5
|
+
|
|
6
|
+
BEGIN;
|
|
7
|
+
|
|
8
|
+
-- https://dba.stackexchange.com/questions/61271/how-to-access-new-or-old-field-given-only-the-fields-name
|
|
9
|
+
-- https://dba.stackexchange.com/questions/82039/assign-to-new-by-key-in-a-postgres-trigger/82044#82044
|
|
10
|
+
-- https://stackoverflow.com/questions/7711432/how-to-set-value-of-composite-variable-field-using-dynamic-sql
|
|
11
|
+
|
|
12
|
+
-- usage CREATE TRIGGER ...
|
|
13
|
+
-- EXECUTE PROCEDURE uuids.trigger_set_uuid_seed ('id', 'database_id');
|
|
14
|
+
|
|
15
|
+
CREATE FUNCTION uuids.trigger_set_uuid_related_field()
|
|
16
|
+
RETURNS TRIGGER AS $$
|
|
17
|
+
DECLARE
|
|
18
|
+
_seed_column text := to_json(NEW) ->> TG_ARGV[1];
|
|
19
|
+
BEGIN
|
|
20
|
+
IF _seed_column IS NULL THEN
|
|
21
|
+
RAISE EXCEPTION 'UUID seed is NULL on table %', TG_TABLE_NAME;
|
|
22
|
+
END IF;
|
|
23
|
+
NEW := NEW #= (TG_ARGV[0] || '=>' || uuids.pseudo_order_seed_uuid(_seed_column))::hstore;
|
|
24
|
+
RETURN NEW;
|
|
25
|
+
END;
|
|
26
|
+
$$
|
|
27
|
+
LANGUAGE 'plpgsql' VOLATILE;
|
|
28
|
+
|
|
29
|
+
COMMIT;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
-- Deploy schemas/uuids/procedures/trigger_set_uuid_seed to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/uuids/schema
|
|
4
|
+
-- requires: schemas/uuids/procedures/pseudo_order_seed_uuid
|
|
5
|
+
|
|
6
|
+
BEGIN;
|
|
7
|
+
|
|
8
|
+
-- https://dba.stackexchange.com/questions/61271/how-to-access-new-or-old-field-given-only-the-fields-name
|
|
9
|
+
-- https://dba.stackexchange.com/questions/82039/assign-to-new-by-key-in-a-postgres-trigger/82044#82044
|
|
10
|
+
-- https://stackoverflow.com/questions/7711432/how-to-set-value-of-composite-variable-field-using-dynamic-sql
|
|
11
|
+
|
|
12
|
+
-- usage CREATE TRIGGER ...
|
|
13
|
+
-- EXECUTE PROCEDURE uuids.trigger_set_uuid_seed ('id', '41c33217-a5e5-46d1-c8db-62a563cba3af');
|
|
14
|
+
|
|
15
|
+
CREATE FUNCTION uuids.trigger_set_uuid_seed()
|
|
16
|
+
RETURNS TRIGGER AS $$
|
|
17
|
+
BEGIN
|
|
18
|
+
NEW := NEW #= (TG_ARGV[0] || '=>' || uuids.pseudo_order_seed_uuid(TG_ARGV[1]))::hstore;
|
|
19
|
+
RETURN NEW;
|
|
20
|
+
END;
|
|
21
|
+
$$
|
|
22
|
+
LANGUAGE 'plpgsql' VOLATILE;
|
|
23
|
+
|
|
24
|
+
COMMIT;
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
preset: 'ts-jest',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
|
+
|
|
6
|
+
// Match both __tests__ and colocated test files
|
|
7
|
+
testMatch: ['**/?(*.)+(test|spec).{ts,tsx,js,jsx}'],
|
|
8
|
+
|
|
9
|
+
// Ignore build artifacts and type declarations
|
|
10
|
+
testPathIgnorePatterns: ['/dist/', '\\.d\\.ts$'],
|
|
11
|
+
modulePathIgnorePatterns: ['<rootDir>/dist/'],
|
|
12
|
+
watchPathIgnorePatterns: ['/dist/'],
|
|
13
|
+
|
|
14
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
|
15
|
+
};
|
package/launchql.plan
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
%syntax-version=1.0.0
|
|
2
|
+
%project=launchql-uuid
|
|
3
|
+
%uri=launchql-uuid
|
|
4
|
+
|
|
5
|
+
schemas/uuids/schema 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/schema
|
|
6
|
+
schemas/uuids/procedures/pseudo_order_seed_uuid [schemas/uuids/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/pseudo_order_seed_uuid
|
|
7
|
+
schemas/uuids/procedures/pseudo_order_uuid [schemas/uuids/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/pseudo_order_uuid
|
|
8
|
+
schemas/uuids/procedures/trigger_set_uuid_related_field [schemas/uuids/schema schemas/uuids/procedures/pseudo_order_seed_uuid] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/trigger_set_uuid_related_field
|
|
9
|
+
schemas/uuids/procedures/trigger_set_uuid_seed [schemas/uuids/schema schemas/uuids/procedures/pseudo_order_seed_uuid] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/trigger_set_uuid_seed
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pgpm/uuid",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "UUID utilities and extensions for PostgreSQL",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"bundle": "lql package",
|
|
10
|
+
"test": "jest",
|
|
11
|
+
"test:watch": "jest --watch"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@pgpm/verify": "0.4.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@launchql/cli": "^4.9.0"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/launchql/extensions"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/launchql/extensions",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/launchql/extensions/issues"
|
|
26
|
+
},
|
|
27
|
+
"gitHead": "cc9f52a335caa6e21ee7751b04b77c84ce6cb809"
|
|
28
|
+
}
|
package/sqitch.plan
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
%syntax-version=1.0.0
|
|
2
|
+
%project=launchql-uuid
|
|
3
|
+
%uri=launchql-uuid
|
|
4
|
+
|
|
5
|
+
schemas/uuids/schema 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/schema
|
|
6
|
+
schemas/uuids/procedures/pseudo_order_seed_uuid [schemas/uuids/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/pseudo_order_seed_uuid
|
|
7
|
+
schemas/uuids/procedures/pseudo_order_uuid [schemas/uuids/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/pseudo_order_uuid
|
|
8
|
+
schemas/uuids/procedures/trigger_set_uuid_related_field [schemas/uuids/schema schemas/uuids/procedures/pseudo_order_seed_uuid] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/trigger_set_uuid_related_field
|
|
9
|
+
schemas/uuids/procedures/trigger_set_uuid_seed [schemas/uuids/schema schemas/uuids/procedures/pseudo_order_seed_uuid] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/trigger_set_uuid_seed
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
\echo Use "CREATE EXTENSION launchql-uuid" to load this file. \quit
|
|
2
|
+
CREATE SCHEMA uuids;
|
|
3
|
+
|
|
4
|
+
GRANT USAGE ON SCHEMA uuids TO PUBLIC;
|
|
5
|
+
|
|
6
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA uuids
|
|
7
|
+
GRANT EXECUTE ON FUNCTIONS TO PUBLIC;
|
|
8
|
+
|
|
9
|
+
CREATE FUNCTION uuids.pseudo_order_seed_uuid(seed text) RETURNS uuid AS $EOFCODE$
|
|
10
|
+
DECLARE
|
|
11
|
+
new_uuid char(36);
|
|
12
|
+
md5_str char(32);
|
|
13
|
+
md5_str2 char(32);
|
|
14
|
+
uid text;
|
|
15
|
+
BEGIN
|
|
16
|
+
md5_str := md5(concat(random(), now()));
|
|
17
|
+
md5_str2 := md5(concat(random(), now()));
|
|
18
|
+
|
|
19
|
+
new_uuid := concat(
|
|
20
|
+
LEFT (md5(seed), 2),
|
|
21
|
+
LEFT (md5(concat(extract(year FROM now()), extract(week FROM now()))), 2),
|
|
22
|
+
substring(md5_str, 1, 4),
|
|
23
|
+
'-',
|
|
24
|
+
substring(md5_str, 5, 4),
|
|
25
|
+
'-4',
|
|
26
|
+
substring(md5_str2, 9, 3),
|
|
27
|
+
'-',
|
|
28
|
+
substring(md5_str, 13, 4),
|
|
29
|
+
'-',
|
|
30
|
+
substring(md5_str2, 17, 12)
|
|
31
|
+
);
|
|
32
|
+
RETURN new_uuid;
|
|
33
|
+
END;
|
|
34
|
+
$EOFCODE$ LANGUAGE plpgsql VOLATILE;
|
|
35
|
+
|
|
36
|
+
COMMENT ON FUNCTION uuids.pseudo_order_seed_uuid IS 'Pseudo Ordered UUID with a seed. Good for multi-tenant scenarios, other wise use non-seed.';
|
|
37
|
+
|
|
38
|
+
CREATE FUNCTION uuids.pseudo_order_uuid() RETURNS uuid AS $EOFCODE$
|
|
39
|
+
DECLARE
|
|
40
|
+
new_uuid char(36);
|
|
41
|
+
md5_str char(32);
|
|
42
|
+
md5_str2 char(32);
|
|
43
|
+
BEGIN
|
|
44
|
+
md5_str := md5(concat(random(), now()));
|
|
45
|
+
md5_str2 := md5(concat(random(), now()));
|
|
46
|
+
new_uuid := concat(
|
|
47
|
+
LEFT (md5(concat(extract(year FROM now()), extract(week FROM now()))), 4),
|
|
48
|
+
LEFT (md5_str, 4), '-', substring(md5_str, 5, 4), '-4', substring(md5_str2, 9, 3), '-', substring(md5_str, 13, 4), '-', substring(md5_str2, 17, 12));
|
|
49
|
+
RETURN new_uuid;
|
|
50
|
+
END;
|
|
51
|
+
$EOFCODE$ LANGUAGE plpgsql VOLATILE;
|
|
52
|
+
|
|
53
|
+
COMMENT ON FUNCTION uuids.pseudo_order_seed_uuid IS 'Pseudo Ordered UUID';
|
|
54
|
+
|
|
55
|
+
CREATE FUNCTION uuids.trigger_set_uuid_related_field() RETURNS trigger AS $EOFCODE$
|
|
56
|
+
DECLARE
|
|
57
|
+
_seed_column text := to_json(NEW) ->> TG_ARGV[1];
|
|
58
|
+
BEGIN
|
|
59
|
+
IF _seed_column IS NULL THEN
|
|
60
|
+
RAISE EXCEPTION 'UUID seed is NULL on table %', TG_TABLE_NAME;
|
|
61
|
+
END IF;
|
|
62
|
+
NEW := NEW #= (TG_ARGV[0] || '=>' || uuids.pseudo_order_seed_uuid(_seed_column))::hstore;
|
|
63
|
+
RETURN NEW;
|
|
64
|
+
END;
|
|
65
|
+
$EOFCODE$ LANGUAGE plpgsql VOLATILE;
|
|
66
|
+
|
|
67
|
+
CREATE FUNCTION uuids.trigger_set_uuid_seed() RETURNS trigger AS $EOFCODE$
|
|
68
|
+
BEGIN
|
|
69
|
+
NEW := NEW #= (TG_ARGV[0] || '=>' || uuids.pseudo_order_seed_uuid(TG_ARGV[1]))::hstore;
|
|
70
|
+
RETURN NEW;
|
|
71
|
+
END;
|
|
72
|
+
$EOFCODE$ LANGUAGE plpgsql VOLATILE;
|