From 573b4bd6fe4fb47ba1e5aede0f414154f5267cba Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Sun, 29 Dec 2024 06:11:24 +0100 Subject: [PATCH] Set up postgres --- .editorconfig | 2 +- README.md | 3 ++ build.gradle.kts | 4 ++ sql/000-initizalise.sql | 65 ++++++++++++++++++++++++++++++ sql/001-uuid_v7.sql | 43 ++++++++++++++++++++ src/main/resources/application.yml | 8 ++++ 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 sql/000-initizalise.sql create mode 100644 sql/001-uuid_v7.sql diff --git a/.editorconfig b/.editorconfig index 5224d99..14efb1a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,7 +9,7 @@ insert_final_newline = true max_line_length = 120 tab_width = 4 -[*.{md,sh,yaml,yml}] +[*.{md,sh,sql,yaml,yml}] max_line_length = 1024 indent_size = 2 tab_width = 2 diff --git a/README.md b/README.md index 022fee8..0ac45ab 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,9 @@ In twilight's hush, where mythic tales unfold, A ledger of legends, the bravest | name | required | info | |------------------------|:--------:|-------------------------| | spring.profiles.active | ✓ | Spring Boot environment | +| spring.r2dbc.url | ✓ | Postgres host url | +| spring.r2dbc.username | ✓ | Postgres username | +| spring.r2dbc.password | ✗ | Postgres password | *Required: ✓ can be stored as text, and ✗ need to be stored as secret.* diff --git a/build.gradle.kts b/build.gradle.kts index 990e610..21069aa 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,8 +11,12 @@ dependencies { implementation(hlaeja.kotlin.reflect) implementation(hlaeja.kotlinx.coroutines) implementation(hlaeja.springboot.starter.actuator) + implementation(hlaeja.springboot.starter.r2dbc) implementation(hlaeja.springboot.starter.webflux) + runtimeOnly(hlaeja.postgresql) + runtimeOnly(hlaeja.postgresql.r2dbc) + testImplementation(hlaeja.assertj.core) testImplementation(hlaeja.mockk) testImplementation(hlaeja.projectreactor.reactor.test) diff --git a/sql/000-initizalise.sql b/sql/000-initizalise.sql new file mode 100644 index 0000000..23781bc --- /dev/null +++ b/sql/000-initizalise.sql @@ -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 account_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; + diff --git a/sql/001-uuid_v7.sql b/sql/001-uuid_v7.sql new file mode 100644 index 0000000..6172b7c --- /dev/null +++ b/sql/001-uuid_v7.sql @@ -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.'; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4fc7d7a..d36450e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -18,6 +18,10 @@ spring: config: activate: on-profile: development + r2dbc: + url: r2dbc:postgresql://localhost:5432/account_registry + username: services + password: password --- ########################## @@ -27,6 +31,10 @@ spring: config: activate: on-profile: docker + r2dbc: + url: r2dbc:postgresql://PostgreSQL:5432/account_registry + username: services + password: password --- ##############################