update release.sh to handel sql move to version catalog

This commit is contained in:
2025-03-05 14:55:57 +01:00
parent f5038a7e9e
commit 1aba25b9b3
6 changed files with 12 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
-- Role: role_administrator
-- DROP ROLE IF EXISTS role_administrator;
CREATE ROLE role_administrator;
-- Role: role_service
-- DROP ROLE IF EXISTS role_service;
CREATE ROLE role_service;
-- Role: role_maintainer
-- DROP ROLE IF EXISTS role_maintainer;
CREATE ROLE role_maintainer;
-- Role: support_role
-- DROP ROLE IF EXISTS support_role;
CREATE ROLE role_support;
-- User: services
-- DROP USER IF EXISTS services;
CREATE USER services WITH PASSWORD 'password';
-- Assign role to the user
GRANT role_service TO services;
-- User: user_maintainer
-- DROP USER IF EXISTS user_maintainer;
CREATE USER user_maintainer WITH PASSWORD 'password';
-- Assign role to the user
GRANT role_maintainer TO user_maintainer;
-- User: user_support
-- DROP USER IF EXISTS user_support;
CREATE USER user_support WITH PASSWORD 'password';
-- Assign role to the user
GRANT role_support TO user_support;
-- Database: device_registry
-- DROP DATABASE IF EXISTS device_registry;
CREATE DATABASE device_registry
WITH
OWNER = role_administrator
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
LOCALE_PROVIDER = 'libc'
TABLESPACE = pg_default
CONNECTION LIMIT = -1
IS_TEMPLATE = False;

View File

@@ -0,0 +1,43 @@
-- FUNCTION: public.gen_uuid_v7(timestamp with time zone)
-- DROP FUNCTION IF EXISTS public.gen_uuid_v7(timestamp with time zone);
CREATE OR REPLACE FUNCTION public.gen_uuid_v7(p_timestamp timestamp with time zone)
RETURNS uuid
LANGUAGE 'sql'
COST 100
VOLATILE PARALLEL UNSAFE
AS
$BODY$
-- Replace the first 48 bits of a uuid v4 with the provided timestamp (in milliseconds) since 1970-01-01 UTC, and set the version to 7
SELECT encode(set_bit(set_bit(overlay(uuid_send(gen_random_uuid()) PLACING substring(int8send((extract(EPOCH FROM p_timestamp) * 1000):: BIGINT) FROM 3) FROM 1 FOR 6), 52, 1), 53, 1), 'hex') ::uuid;
$BODY$;
ALTER FUNCTION public.gen_uuid_v7(timestamp with time zone)
OWNER TO role_administrator;
COMMENT
ON FUNCTION public.gen_uuid_v7(timestamp with time zone)
IS 'Generate a UUIDv7 value using a provided timestamp (in milliseconds since 1970-01-01 UTC) with 74 bits of randomness.';
-- FUNCTION: public.gen_uuid_v7()
-- DROP FUNCTION IF EXISTS public.gen_uuid_v7();
CREATE OR REPLACE FUNCTION public.gen_uuid_v7()
RETURNS uuid
LANGUAGE 'sql'
COST 100
VOLATILE PARALLEL UNSAFE
AS
$BODY$
SELECT gen_uuid_v7(clock_timestamp());
$BODY$;
ALTER FUNCTION public.gen_uuid_v7()
OWNER TO role_administrator;
COMMENT
ON FUNCTION public.gen_uuid_v7()
IS 'Generate a UUIDv7 value with a 48-bit timestamp (millisecond precision) and 74 bits of randomness.';

22
sql/initial/002-types.sql Normal file
View File

@@ -0,0 +1,22 @@
-- Table: public.types
-- DROP TABLE IF EXISTS public.types;
CREATE TABLE IF NOT EXISTS public.types
(
id UUID DEFAULT gen_uuid_v7(),
timestamp timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
name VARCHAR(50) UNIQUE NOT NULL,
CONSTRAINT pk_contact_types PRIMARY KEY (id)
);
ALTER TABLE IF EXISTS public.types
OWNER to role_administrator;
-- Revoke all permissions from existing roles
REVOKE ALL ON TABLE public.types FROM role_administrator, role_maintainer, role_support, role_service;
-- Grant appropriate permissions
GRANT ALL ON TABLE public.types TO role_administrator;
GRANT SELECT, INSERT, UPDATE ON TABLE public.types TO role_maintainer, role_service;
GRANT SELECT ON TABLE public.types TO role_support;

View File

@@ -0,0 +1,29 @@
-- Table: public.devices
-- DROP TABLE IF EXISTS public.devices;
CREATE TABLE IF NOT EXISTS public.devices
(
id UUID DEFAULT gen_uuid_v7(),
timestamp TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
type UUID NOT NULL,
CONSTRAINT pk_devices PRIMARY KEY (id),
CONSTRAINT fk_devices_type FOREIGN KEY (type) REFERENCES public.types (id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
ALTER TABLE IF EXISTS public.devices
OWNER to role_administrator;
-- Index: public.i_devices_type
-- DROP INDEX IF EXISTS public.i_devices_type;
CREATE INDEX IF NOT EXISTS i_devices_type ON public.devices (type);
-- Revoke all permissions from existing roles
REVOKE ALL ON TABLE public.devices FROM role_administrator, role_maintainer, role_support, role_service;
-- Grant appropriate permissions
GRANT ALL ON TABLE public.devices TO role_administrator;
GRANT SELECT, INSERT, UPDATE ON TABLE public.devices TO role_maintainer, role_service;
GRANT SELECT ON TABLE public.devices TO role_support;

31
sql/initial/004-nodes.sql Normal file
View File

@@ -0,0 +1,31 @@
-- Table: public.nodes
-- DROP TABLE IF EXISTS public.nodes;
CREATE TABLE IF NOT EXISTS public.nodes
(
id UUID DEFAULT gen_uuid_v7(),
timestamp TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
client UUID NOT NULL,
device UUID NOT NULL,
name VARCHAR(50) NOT NULL,
CONSTRAINT pk_nodes PRIMARY KEY (id),
CONSTRAINT fk_nodes_type FOREIGN KEY (device) REFERENCES public.devices (id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
ALTER TABLE IF EXISTS public.nodes
OWNER to role_administrator;
-- Index: public.i_nodes_type
-- DROP INDEX IF EXISTS public.i_nodes_type;
CREATE INDEX IF NOT EXISTS i_nodes_device ON public.nodes (device);
-- Revoke all permissions from existing roles
REVOKE ALL ON TABLE public.nodes FROM role_administrator, role_maintainer, role_support, role_service;
-- Grant appropriate permissions
GRANT ALL ON TABLE public.nodes TO role_administrator;
GRANT SELECT, INSERT, UPDATE ON TABLE public.nodes TO role_maintainer, role_service;
GRANT SELECT ON TABLE public.nodes TO role_support;