Add postgres sql file formatting via pg_format

This commit is contained in:
thiloho
2024-08-08 22:29:04 +02:00
parent 8534b2d783
commit 2fd934ed86
9 changed files with 526 additions and 397 deletions

View File

@@ -1,20 +1,27 @@
-- migrate:up
CREATE VIEW api.account
WITH (security_invoker = on)
AS
SELECT id, username
FROM internal.user
WHERE id = (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID;
CREATE VIEW api.account WITH ( security_invoker = ON
) AS
SELECT
id,
username
FROM
internal.user
WHERE
id = (
CURRENT_SETTING(
'request.jwt.claims', TRUE
)::JSON ->> 'user_id')::UUID;
CREATE VIEW api.user
WITH (security_invoker = on)
AS
SELECT id, username
FROM internal.user;
CREATE VIEW api.user WITH ( security_invoker = ON
) AS
SELECT
id,
username
FROM
internal.user;
CREATE VIEW api.website
WITH (security_invoker = on)
AS
CREATE VIEW api.website WITH ( security_invoker = ON
) AS
SELECT
id,
user_id,
@@ -23,11 +30,11 @@ SELECT
created_at,
last_modified_at,
last_modified_by
FROM internal.website;
FROM
internal.website;
CREATE VIEW api.media
WITH (security_invoker = on)
AS
CREATE VIEW api.media WITH ( security_invoker = ON
) AS
SELECT
id,
website_id,
@@ -35,11 +42,11 @@ SELECT
original_name,
file_system_path,
created_at
FROM internal.media;
FROM
internal.media;
CREATE VIEW api.settings
WITH (security_invoker = on)
AS
CREATE VIEW api.settings WITH ( security_invoker = ON
) AS
SELECT
website_id,
accent_color_light_theme,
@@ -47,11 +54,11 @@ SELECT
favicon_image,
last_modified_at,
last_modified_by
FROM internal.settings;
FROM
internal.settings;
CREATE VIEW api.header
WITH (security_invoker = on)
AS
CREATE VIEW api.header WITH ( security_invoker = ON
) AS
SELECT
website_id,
logo_type,
@@ -59,21 +66,21 @@ SELECT
logo_image,
last_modified_at,
last_modified_by
FROM internal.header;
FROM
internal.header;
CREATE view api.home
WITH (security_invoker = on)
AS
CREATE VIEW api.home WITH ( security_invoker = ON
) AS
SELECT
website_id,
main_content,
last_modified_at,
last_modified_by
FROM internal.home;
FROM
internal.home;
CREATE VIEW api.article
WITH (security_invoker = on)
AS
CREATE VIEW api.article WITH ( security_invoker = ON
) AS
SELECT
id,
website_id,
@@ -87,21 +94,21 @@ SELECT
created_at,
last_modified_at,
last_modified_by
FROM internal.article;
FROM
internal.article;
CREATE VIEW api.footer
WITH (security_invoker = on)
AS
CREATE VIEW api.footer WITH ( security_invoker = ON
) AS
SELECT
website_id,
additional_text,
last_modified_at,
last_modified_by
FROM internal.footer;
FROM
internal.footer;
CREATE VIEW api.collab
WITH (security_invoker = on)
AS
CREATE VIEW api.collab WITH ( security_invoker = ON
) AS
SELECT
website_id,
user_id,
@@ -109,11 +116,11 @@ SELECT
added_at,
last_modified_at,
last_modified_by
FROM internal.collab;
FROM
internal.collab;
CREATE VIEW api.change_log
WITH (security_invoker = on)
AS
CREATE VIEW api.change_log WITH ( security_invoker = ON
) AS
SELECT
website_id,
user_id,
@@ -121,29 +128,26 @@ SELECT
previous_value,
new_value,
timestamp
FROM internal.change_log;
FROM
internal.change_log;
CREATE FUNCTION
api.create_website(content_type VARCHAR(10), title VARCHAR(50), OUT website_id UUID) AS $$
CREATE FUNCTION api.create_website (content_type VARCHAR(10), title VARCHAR(50), OUT website_id UUID)
AS $$
DECLARE
_website_id UUID;
_user_id UUID;
BEGIN
_user_id := (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID;
_user_id := (CURRENT_SETTING('request.jwt.claims', TRUE)::JSON ->> 'user_id')::UUID;
INSERT INTO internal.website (content_type, title)
VALUES (create_website.content_type, create_website.title)
RETURNING id INTO _website_id;
VALUES (create_website.content_type, create_website.title)
RETURNING
id INTO _website_id;
INSERT INTO internal.settings (website_id)
VALUES (_website_id);
VALUES (_website_id);
INSERT INTO internal.header (website_id, logo_text)
VALUES (_website_id, 'archtika ' || create_website.content_type);
VALUES (_website_id, 'archtika ' || create_website.content_type);
INSERT INTO internal.home (website_id, main_content)
VALUES
(_website_id, '
VALUES (_website_id, '
## About
archtika is a FLOSS, modern, performant and lightweight CMS (Content Mangement System) in the form of a web application. It allows you to easily create, manage and publish minimal, responsive and SEO friendly blogging and documentation websites with official, professionally designed templates.
@@ -158,61 +162,99 @@ The web application uses SvelteKit with SSR (Server Side Rendering) and Svelte v
NGINX is used to deploy the websites, serving the static site files from the `/var/www/archtika-websites` directory. The static files can be found in this directory via the path `<user_id>/<website_id>`, which is dynamically created by the web application.
');
INSERT INTO internal.footer (website_id, additional_text)
VALUES (_website_id, 'archtika is a free, open, modern, performant and lightweight CMS');
VALUES (_website_id, 'archtika is a free, open, modern, performant and lightweight CMS');
website_id := _website_id;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
GRANT EXECUTE ON FUNCTION api.create_website(VARCHAR(10), VARCHAR(50)) TO authenticated_user;
$$
LANGUAGE plpgsql
SECURITY DEFINER;
GRANT EXECUTE ON FUNCTION api.create_website (VARCHAR(10), VARCHAR(50)) TO authenticated_user;
-- Security invoker only works on views if the user has access to the underlying table
GRANT SELECT ON internal.user TO authenticated_user;
GRANT SELECT ON api.account TO authenticated_user;
GRANT SELECT ON api.user TO authenticated_user;
GRANT SELECT, UPDATE, DELETE ON internal.website TO authenticated_user;
GRANT SELECT, UPDATE, DELETE ON api.website TO authenticated_user;
GRANT SELECT, INSERT ON internal.media TO authenticated_user;
GRANT SELECT, INSERT ON api.media TO authenticated_user;
GRANT SELECT, UPDATE ON internal.settings TO authenticated_user;
GRANT SELECT, UPDATE ON api.settings TO authenticated_user;
GRANT SELECT, UPDATE ON internal.header TO authenticated_user;
GRANT SELECT, UPDATE ON api.header TO authenticated_user;
GRANT SELECT, UPDATE ON internal.home TO authenticated_user;
GRANT SELECT, UPDATE ON api.home TO authenticated_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON internal.article TO authenticated_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON api.article TO authenticated_user;
GRANT SELECT, UPDATE ON internal.footer TO authenticated_user;
GRANT SELECT, UPDATE ON api.footer TO authenticated_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON internal.collab TO authenticated_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON api.collab TO authenticated_user;
GRANT SELECT ON internal.change_log TO authenticated_user;
GRANT SELECT ON api.change_log TO authenticated_user;
GRANT SELECT ON api.change_log TO authenticated_user;
-- migrate:down
REVOKE SELECT ON internal.user FROM authenticated_user;
REVOKE SELECT, UPDATE, DELETE ON internal.website FROM authenticated_user;
REVOKE SELECT, INSERT ON internal.media FROM authenticated_user;
REVOKE SELECT, UPDATE ON internal.settings FROM authenticated_user;
REVOKE SELECT, UPDATE ON internal.header FROM authenticated_user;
REVOKE SELECT, INSERT, UPDATE, DELETE ON internal.article FROM authenticated_user;
REVOKE SELECT, UPDATE ON internal.footer FROM authenticated_user;
REVOKE SELECT, INSERT, UPDATE, DELETE ON internal.collab FROM authenticated_user;
REVOKE SELECT ON internal.change_log FROM authenticated_user;
DROP FUNCTION api.create_website(VARCHAR(10), VARCHAR(50));
DROP FUNCTION api.create_website (VARCHAR(10), VARCHAR(50));
DROP VIEW api.change_log;
DROP VIEW api.collab;
DROP VIEW api.footer;
DROP VIEW api.home;
DROP VIEW api.article;
DROP VIEW api.header;
DROP VIEW api.settings;
DROP VIEW api.media;
DROP VIEW api.website;
DROP VIEW api.user;
DROP VIEW api.account;
DROP VIEW api.account;