Skip to content

Commit

Permalink
chore: fix migration
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Jan 2, 2024
1 parent f4f3eb9 commit fff4f4d
Showing 1 changed file with 54 additions and 58 deletions.
112 changes: 54 additions & 58 deletions views/005_component_views.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- components by check
DROP FUNCTION IF EXISTS lookup_components_by_check;

CREATE OR REPLACE FUNCTION lookup_components_by_check (id uuid) RETURNS setof components AS
$$
SELECT * FROM components WHERE id IN (
Expand All @@ -20,8 +19,7 @@ $$ language plpgsql;
-- lookup_components_by_config
DROP FUNCTION IF EXISTS lookup_components_by_config;

CREATE
OR REPLACE function lookup_components_by_config (id text) returns table (
CREATE OR REPLACE function lookup_components_by_config (id text) returns table (
component_id UUID,
name TEXT,
type TEXT,
Expand All @@ -37,29 +35,30 @@ begin
end;
$$ language plpgsql;

CREATE OR REPLACE VIEW
component_names AS
SELECT
id,
path,
external_id,
type,
name,
created_at,
updated_at,
icon,
parent_id
FROM
components
WHERE
deleted_at is null
AND hidden != true
ORDER BY
name,
external_id;

CREATE OR REPLACE VIEW
component_names_all AS
DROP VIEW IF EXISTS component_names;
CREATE OR REPLACE VIEW component_names AS
SELECT
id,
path,
external_id,
type,
name,
created_at,
updated_at,
icon,
parent_id
FROM
components
WHERE
deleted_at is null
AND hidden != true
ORDER BY
name,
external_id;

DROP VIEW IF EXISTS component_names_all;
CREATE OR REPLACE VIEW component_names_all AS
SELECT
id,
path,
Expand All @@ -79,42 +78,39 @@ ORDER BY
name,
external_id;

CREATE OR REPLACE VIEW
component_labels AS
SELECT
d.key,
d.value
FROM
components
JOIN json_each_text(labels::json) d on true
GROUP BY
d.key,
d.value
ORDER BY
key,
value;
CREATE OR REPLACE VIEW component_labels AS
SELECT
d.key,
d.value
FROM
components
JOIN json_each_text(labels::json) d on true
GROUP BY
d.key,
d.value
ORDER BY
key,
value;

DROP VIEW IF EXISTS components_with_logs;

CREATE OR REPLACE VIEW
components_with_logs AS
SELECT
id,
name,
agent_id,
icon,
type
FROM
components
WHERE
deleted_at IS NULL
AND log_selectors IS NOT NULL;
CREATE OR REPLACE VIEW components_with_logs AS
SELECT
id,
name,
agent_id,
icon,
type
FROM
components
WHERE
deleted_at IS NULL
AND log_selectors IS NOT NULL;

-- TODO stop the recursion once max_depth is reached.level <= max_depth;
DROP FUNCTION if exists lookup_component_children;

CREATE
OR REPLACE FUNCTION lookup_component_children (id text, max_depth int) RETURNS TABLE (child_id UUID, parent_id UUID, level int) AS $$
CREATE OR REPLACE FUNCTION lookup_component_children (id text, max_depth int)
RETURNS TABLE (child_id UUID, parent_id UUID, level int) AS $$
BEGIN
IF max_depth < 0 THEN
max_depth = 10;
Expand All @@ -136,8 +132,8 @@ $$ language plpgsql;

DROP FUNCTION if exists lookup_component_relations;

CREATE
OR REPLACE FUNCTION lookup_component_relations (component_id text) RETURNS TABLE (id UUID) AS $$
CREATE OR REPLACE FUNCTION lookup_component_relations (component_id text)
RETURNS TABLE (id UUID) AS $$
BEGIN
RETURN QUERY
SELECT cr.relationship_id AS id FROM component_relationships cr WHERE cr.component_id = $1::UUID
Expand All @@ -148,4 +144,4 @@ $$ language plpgsql;

CREATE OR REPLACE VIEW component_types AS
SELECT distinct on (type) type
FROM components ORDER BY type asc;
FROM components ORDER BY type asc;

0 comments on commit fff4f4d

Please sign in to comment.