summaryrefslogtreecommitdiff
path: root/db/migrations/0216_petite_la_nuit.sql
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrations/0216_petite_la_nuit.sql')
-rw-r--r--db/migrations/0216_petite_la_nuit.sql58
1 files changed, 58 insertions, 0 deletions
diff --git a/db/migrations/0216_petite_la_nuit.sql b/db/migrations/0216_petite_la_nuit.sql
new file mode 100644
index 00000000..67eefe78
--- /dev/null
+++ b/db/migrations/0216_petite_la_nuit.sql
@@ -0,0 +1,58 @@
+CREATE VIEW "public"."template_detail_view" AS (
+ SELECT
+ t.id,
+ t.name,
+ t.slug,
+ t.content,
+ t.description,
+ t.category,
+ t.sample_data,
+ t.is_active,
+ t.version,
+ t.created_by,
+ t.created_at,
+ t.updated_at,
+ COALESCE(
+ json_agg(
+ json_build_object(
+ 'id', v.id,
+ 'variableName', v.variable_name,
+ 'variableType', v.variable_type,
+ 'defaultValue', v.default_value,
+ 'isRequired', v.is_required,
+ 'description', v.description,
+ 'displayOrder', v.display_order
+ )
+ ORDER BY v.display_order
+ ) FILTER (WHERE v.id IS NOT NULL),
+ '[]'::json
+ ) as variables
+ FROM "templates" t
+ LEFT JOIN "template_variables" v ON t.id = v.template_id
+ GROUP BY t.id, t.name, t.slug, t.content, t.description, t.category,
+ t.sample_data, t.is_active, t.version, t.created_by, t.created_at, t.updated_at
+);--> statement-breakpoint
+CREATE VIEW "public"."template_list_view" AS (
+ SELECT
+ t.id,
+ t.name,
+ t.slug,
+ t.description,
+ t.category,
+ t.is_active,
+ t.version,
+ t.created_by,
+ t.created_at,
+ t.updated_at,
+ COALESCE(v.variable_count, 0) as variable_count,
+ COALESCE(v.required_variable_count, 0) as required_variable_count
+ FROM "templates" t
+ LEFT JOIN (
+ SELECT
+ template_id,
+ COUNT(*) as variable_count,
+ COUNT(*) FILTER (WHERE is_required = true) as required_variable_count
+ FROM "template_variables"
+ GROUP BY template_id
+ ) v ON t.id = v.template_id
+); \ No newline at end of file