@pgpm/pg18-shim 0.0.0-stage → 0.47.15
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 +11 -2
- package/__tests__/uuidv7.test.ts +67 -0
- package/deploy/functions/uuidv7.sql +39 -0
- package/jest.config.js +5 -0
- package/package.json +16 -4
- package/pgpm-pg18-shim.control +7 -0
- package/pgpm.plan +5 -0
- package/revert/functions/uuidv7.sql +14 -0
- package/sql/pgpm-pg18-shim--0.47.15.bundle.tar.gz +0 -0
- package/sql/pgpm-pg18-shim--0.47.15.sql +34 -0
- package/verify/functions/uuidv7.sql +23 -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 Constructive
|
|
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
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @pgpm/pg18-shim
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Opt-in UUIDv7 support for PostgreSQL servers without the built-in `uuidv7()`.
|
|
4
|
+
Declare `pgpm-pg18-shim` explicitly and deploy it before modules that use
|
|
5
|
+
`uuidv7()` in column defaults.
|
|
6
|
+
|
|
7
|
+
On PostgreSQL 18, the package leaves the native function untouched. On older
|
|
8
|
+
servers without a UUIDv7 generator, it creates `public.uuidv7()` with a Unix
|
|
9
|
+
millisecond timestamp and RFC 9562 version, variant, and random bits. It does
|
|
10
|
+
not implement PostgreSQL 18's optional time-shift argument or guarantee order
|
|
11
|
+
within a millisecond. The package does not replace an existing
|
|
12
|
+
`public.uuidv7()` and only reverts its own function.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { getConnections, PgTestClient } from 'pgsql-test';
|
|
4
|
+
|
|
5
|
+
let pg: PgTestClient;
|
|
6
|
+
let teardown: () => Promise<void>;
|
|
7
|
+
|
|
8
|
+
const migration = (phase: string) =>
|
|
9
|
+
readFileSync(join(__dirname, '..', phase, 'functions', 'uuidv7.sql'), 'utf8')
|
|
10
|
+
.replace(/^BEGIN;$/m, '').replace(/^(COMMIT|ROLLBACK);$/m, '');
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
({ pg, teardown } = await getConnections({}, []));
|
|
14
|
+
});
|
|
15
|
+
afterAll(async () => teardown());
|
|
16
|
+
beforeEach(async () => pg.beforeEach());
|
|
17
|
+
afterEach(async () => pg.afterEach());
|
|
18
|
+
|
|
19
|
+
test('provides usable UUIDv7 and reverts without removing native UUIDv7', async () => {
|
|
20
|
+
const [{ native }] = await pg.any(
|
|
21
|
+
"SELECT to_regprocedure('pg_catalog.uuidv7(interval)') IS NOT NULL AS native"
|
|
22
|
+
);
|
|
23
|
+
await pg.query(migration('deploy'));
|
|
24
|
+
await pg.query(migration('verify'));
|
|
25
|
+
if (!native) {
|
|
26
|
+
const [{ configured }] = await pg.any(`SELECT proconfig IS NOT NULL AS configured
|
|
27
|
+
FROM pg_catalog.pg_proc WHERE oid = pg_catalog.to_regprocedure('public.uuidv7()')`);
|
|
28
|
+
expect(configured).toBe(false);
|
|
29
|
+
}
|
|
30
|
+
const uuidv7 = native ? 'pg_catalog.uuidv7()' : 'public.uuidv7()';
|
|
31
|
+
const [{ id }] = await pg.any(`SELECT ${uuidv7}::text AS id`);
|
|
32
|
+
expect(id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
|
|
33
|
+
const timestamp = parseInt(id.slice(0, 13).replace('-', ''), 16);
|
|
34
|
+
expect(Math.abs(Date.now() - timestamp)).toBeLessThan(5000);
|
|
35
|
+
const [{ count }] = await pg.any(`SELECT count(DISTINCT ${uuidv7})::int AS count FROM generate_series(1, 100)`);
|
|
36
|
+
expect(count).toBe(100);
|
|
37
|
+
await pg.query(migration('revert'));
|
|
38
|
+
const [{ shim, usable }] = await pg.any(`SELECT
|
|
39
|
+
to_regprocedure('public.uuidv7()') IS NOT NULL AS shim,
|
|
40
|
+
to_regprocedure('pg_catalog.uuidv7(interval)') IS NOT NULL AS usable`);
|
|
41
|
+
expect(shim).toBe(false);
|
|
42
|
+
expect(usable).toBe(native);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('does not replace or remove an existing public.uuidv7()', async () => {
|
|
46
|
+
await pg.query("CREATE FUNCTION public.uuidv7() RETURNS uuid LANGUAGE sql AS 'SELECT gen_random_uuid()'");
|
|
47
|
+
await pg.query(migration('deploy'));
|
|
48
|
+
await pg.query(migration('revert'));
|
|
49
|
+
const [{ existing }] = await pg.any("SELECT to_regprocedure('public.uuidv7()') IS NOT NULL AS existing");
|
|
50
|
+
expect(existing).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('leaves an existing defaulted public.uuidv7 alone without calling it during verify', async () => {
|
|
54
|
+
await pg.query(`CREATE FUNCTION public.uuidv7(interval DEFAULT '0 seconds')
|
|
55
|
+
RETURNS uuid LANGUAGE plpgsql AS $$BEGIN
|
|
56
|
+
RAISE EXCEPTION 'verify must not execute this function';
|
|
57
|
+
END;$$`);
|
|
58
|
+
await pg.query(migration('deploy'));
|
|
59
|
+
await pg.query(migration('verify'));
|
|
60
|
+
const [{ shim }] = await pg.any("SELECT to_regprocedure('public.uuidv7()') IS NOT NULL AS shim");
|
|
61
|
+
expect(shim).toBe(false);
|
|
62
|
+
await pg.query(migration('revert'));
|
|
63
|
+
const [{ existing }] = await pg.any(
|
|
64
|
+
"SELECT to_regprocedure('public.uuidv7(interval)') IS NOT NULL AS existing"
|
|
65
|
+
);
|
|
66
|
+
expect(existing).toBe(true);
|
|
67
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
-- Deploy pgpm-pg18-shim:functions/uuidv7 to pg
|
|
2
|
+
|
|
3
|
+
BEGIN;
|
|
4
|
+
|
|
5
|
+
DO $install$
|
|
6
|
+
BEGIN
|
|
7
|
+
IF EXISTS (
|
|
8
|
+
SELECT 1
|
|
9
|
+
FROM pg_catalog.pg_proc AS p
|
|
10
|
+
JOIN pg_catalog.pg_namespace AS n ON n.oid OPERATOR(pg_catalog.=) p.pronamespace
|
|
11
|
+
WHERE (n.nspname OPERATOR(pg_catalog.=) 'pg_catalog' OR n.nspname OPERATOR(pg_catalog.=) 'public')
|
|
12
|
+
AND p.proname OPERATOR(pg_catalog.=) 'uuidv7'
|
|
13
|
+
AND p.prokind OPERATOR(pg_catalog.=) 'f'
|
|
14
|
+
AND p.pronargs OPERATOR(pg_catalog.=) p.pronargdefaults
|
|
15
|
+
) THEN
|
|
16
|
+
RETURN;
|
|
17
|
+
END IF;
|
|
18
|
+
|
|
19
|
+
CREATE FUNCTION public.uuidv7()
|
|
20
|
+
RETURNS pg_catalog.uuid
|
|
21
|
+
LANGUAGE sql VOLATILE PARALLEL SAFE
|
|
22
|
+
AS $uuid$
|
|
23
|
+
SELECT pg_catalog.concat(
|
|
24
|
+
pg_catalog.substr(ms, 1, 8), '-', pg_catalog.substr(ms, 9), '-7', pg_catalog.substr(random_uuid, 16)
|
|
25
|
+
)::pg_catalog.uuid
|
|
26
|
+
FROM (
|
|
27
|
+
SELECT pg_catalog.lpad(pg_catalog.to_hex(pg_catalog.floor(
|
|
28
|
+
extract(epoch FROM pg_catalog.clock_timestamp()) OPERATOR(pg_catalog.*) 1000
|
|
29
|
+
)::pg_catalog.int8), 12, '0') AS ms,
|
|
30
|
+
pg_catalog.gen_random_uuid()::pg_catalog.text AS random_uuid
|
|
31
|
+
) AS parts;
|
|
32
|
+
$uuid$;
|
|
33
|
+
|
|
34
|
+
COMMENT ON FUNCTION public.uuidv7() IS 'Installed by pgpm-pg18-shim:functions/uuidv7';
|
|
35
|
+
GRANT EXECUTE ON FUNCTION public.uuidv7() TO PUBLIC;
|
|
36
|
+
END;
|
|
37
|
+
$install$;
|
|
38
|
+
|
|
39
|
+
COMMIT;
|
package/jest.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgpm/pg18-shim",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.47.15",
|
|
4
|
+
"description": "Opt-in PostgreSQL 18 UUIDv7 fallback",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"bundle": "pgpm package",
|
|
10
|
+
"test": "jest"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/constructive-io/pgpm-modules"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"gitHead": "2d039957d3f34d71fd579838e0f11467e43766c5"
|
|
18
|
+
}
|
package/pgpm.plan
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
-- Revert pgpm-pg18-shim:functions/uuidv7 from pg
|
|
2
|
+
|
|
3
|
+
BEGIN;
|
|
4
|
+
|
|
5
|
+
DO $revert$
|
|
6
|
+
BEGIN
|
|
7
|
+
IF pg_catalog.obj_description(pg_catalog.to_regprocedure('public.uuidv7()'), 'pg_proc')
|
|
8
|
+
OPERATOR(pg_catalog.=) 'Installed by pgpm-pg18-shim:functions/uuidv7' THEN
|
|
9
|
+
DROP FUNCTION public.uuidv7();
|
|
10
|
+
END IF;
|
|
11
|
+
END;
|
|
12
|
+
$revert$;
|
|
13
|
+
|
|
14
|
+
COMMIT;
|
|
Binary file
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
\echo Use "CREATE EXTENSION pgpm-pg18-shim" to load this file. \quit
|
|
2
|
+
DO $EOFCODE$
|
|
3
|
+
BEGIN
|
|
4
|
+
IF EXISTS (
|
|
5
|
+
SELECT 1
|
|
6
|
+
FROM pg_catalog.pg_proc AS p
|
|
7
|
+
JOIN pg_catalog.pg_namespace AS n ON n.oid OPERATOR(pg_catalog.=) p.pronamespace
|
|
8
|
+
WHERE (n.nspname OPERATOR(pg_catalog.=) 'pg_catalog' OR n.nspname OPERATOR(pg_catalog.=) 'public')
|
|
9
|
+
AND p.proname OPERATOR(pg_catalog.=) 'uuidv7'
|
|
10
|
+
AND p.prokind OPERATOR(pg_catalog.=) 'f'
|
|
11
|
+
AND p.pronargs OPERATOR(pg_catalog.=) p.pronargdefaults
|
|
12
|
+
) THEN
|
|
13
|
+
RETURN;
|
|
14
|
+
END IF;
|
|
15
|
+
|
|
16
|
+
CREATE FUNCTION public.uuidv7()
|
|
17
|
+
RETURNS pg_catalog.uuid
|
|
18
|
+
LANGUAGE sql VOLATILE PARALLEL SAFE
|
|
19
|
+
AS $uuid$
|
|
20
|
+
SELECT pg_catalog.concat(
|
|
21
|
+
pg_catalog.substr(ms, 1, 8), '-', pg_catalog.substr(ms, 9), '-7', pg_catalog.substr(random_uuid, 16)
|
|
22
|
+
)::pg_catalog.uuid
|
|
23
|
+
FROM (
|
|
24
|
+
SELECT pg_catalog.lpad(pg_catalog.to_hex(pg_catalog.floor(
|
|
25
|
+
extract(epoch FROM pg_catalog.clock_timestamp()) OPERATOR(pg_catalog.*) 1000
|
|
26
|
+
)::pg_catalog.int8), 12, '0') AS ms,
|
|
27
|
+
pg_catalog.gen_random_uuid()::pg_catalog.text AS random_uuid
|
|
28
|
+
) AS parts;
|
|
29
|
+
$uuid$;
|
|
30
|
+
|
|
31
|
+
COMMENT ON FUNCTION public.uuidv7() IS 'Installed by pgpm-pg18-shim:functions/uuidv7';
|
|
32
|
+
GRANT EXECUTE ON FUNCTION public.uuidv7() TO PUBLIC;
|
|
33
|
+
END;
|
|
34
|
+
$EOFCODE$;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
-- Verify pgpm-pg18-shim:functions/uuidv7 on pg
|
|
2
|
+
|
|
3
|
+
BEGIN;
|
|
4
|
+
|
|
5
|
+
DO $verify$
|
|
6
|
+
BEGIN
|
|
7
|
+
IF NOT EXISTS (
|
|
8
|
+
SELECT 1
|
|
9
|
+
FROM pg_catalog.pg_proc AS p
|
|
10
|
+
JOIN pg_catalog.pg_namespace AS n ON n.oid OPERATOR(pg_catalog.=) p.pronamespace
|
|
11
|
+
WHERE (n.nspname OPERATOR(pg_catalog.=) 'pg_catalog' OR n.nspname OPERATOR(pg_catalog.=) 'public')
|
|
12
|
+
AND p.proname OPERATOR(pg_catalog.=) 'uuidv7'
|
|
13
|
+
AND p.prokind OPERATOR(pg_catalog.=) 'f'
|
|
14
|
+
AND p.pronargs OPERATOR(pg_catalog.=) p.pronargdefaults
|
|
15
|
+
AND p.prorettype OPERATOR(pg_catalog.=) 'pg_catalog.uuid'::pg_catalog.regtype
|
|
16
|
+
AND NOT p.proretset
|
|
17
|
+
) THEN
|
|
18
|
+
RAISE EXCEPTION 'no zero-argument uuidv7() returning uuid found in pg_catalog or public';
|
|
19
|
+
END IF;
|
|
20
|
+
END;
|
|
21
|
+
$verify$;
|
|
22
|
+
|
|
23
|
+
ROLLBACK;
|