🚀 STABILITY PATCH v1.0: Added 13 missing tables to Golden Schema

ANALYTICS ENGINE (4 tables):
- site_analytics: GA/Ads/Pixel tracking config
- events: Custom event tracking
- pageviews: Page view analytics
- conversions: Conversion tracking with lead linking

GEO-INTELLIGENCE (3 tables):
- locations_states: 50 US states with codes
- locations_counties: 3,143 counties
- locations_cities: Cities with lat/long, zip codes

LEAD CAPTURE (2 tables):
- forms: Dynamic form builder
- form_submissions: Form data storage

SITE BUILDER (3 tables):
- navigation: Self-referential menu system
- globals: Site-wide settings (singleton per site)
- hub_pages: Hierarchical content hubs

SYSTEM (1 table):
- work_log: Activity logging

Also added Directus UI configs for all new FK fields.

Schema now has 28 tables matching TypeScript types.
This commit is contained in:
cawcenter
2025-12-14 14:32:23 -05:00
parent 846b07e080
commit bbf2127f5d

View File

@@ -8,6 +8,7 @@
-- ===================================================================================
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
-- ===================================================================================
@@ -168,33 +169,440 @@ CREATE TABLE IF NOT EXISTS link_targets (
keyword_focus VARCHAR(255)
);
-- ===================================================================================
-- 🚀 STABILITY PATCH v1.0 (Added 2024-12-14)
-- Purpose: Create missing tables for Analytics, Geo, Forms, Navigation, System
-- Author: Spark Overlord
-- ===================================================================================
-- ===================================================================================
-- 📊 ANALYTICS ENGINE (The "Proof" for Subscribers)
-- ===================================================================================
CREATE TABLE IF NOT EXISTS site_analytics (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
site_id UUID UNIQUE REFERENCES sites (id) ON DELETE CASCADE,
google_analytics_id VARCHAR(255),
google_ads_id VARCHAR(255),
fb_pixel_id VARCHAR(255),
gtm_container_id VARCHAR(255)
);
CREATE TABLE IF NOT EXISTS events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
site_id UUID REFERENCES sites (id) ON DELETE CASCADE,
event_name VARCHAR(255) NOT NULL,
page_path VARCHAR(500),
session_id VARCHAR(255),
user_agent TEXT,
timestamp TIMESTAMP DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS pageviews (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
site_id UUID REFERENCES sites (id) ON DELETE CASCADE,
page_path VARCHAR(500),
session_id VARCHAR(255),
referrer VARCHAR(500),
user_agent TEXT,
timestamp TIMESTAMP DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS conversions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
site_id UUID REFERENCES sites (id) ON DELETE CASCADE,
lead_id UUID REFERENCES leads (id) ON DELETE SET NULL,
conversion_type VARCHAR(100),
value DECIMAL(10, 2),
source VARCHAR(255),
timestamp TIMESTAMP DEFAULT NOW()
);
-- ===================================================================================
-- 🌍 GEO-INTELLIGENCE (The "Scale" Engine - 50,000+ Page Potential)
-- ===================================================================================
CREATE TABLE IF NOT EXISTS locations_states (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
name VARCHAR(100) NOT NULL,
code VARCHAR(10) NOT NULL UNIQUE,
population INTEGER,
region VARCHAR(100)
);
CREATE TABLE IF NOT EXISTS locations_counties (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
name VARCHAR(100) NOT NULL,
state_id UUID REFERENCES locations_states (id) ON DELETE CASCADE,
fips_code VARCHAR(10),
population INTEGER
);
CREATE TABLE IF NOT EXISTS locations_cities (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
name VARCHAR(100) NOT NULL,
state_id UUID REFERENCES locations_states (id) ON DELETE CASCADE,
county_id UUID REFERENCES locations_counties (id) ON DELETE SET NULL,
population INTEGER,
zip_codes JSONB,
latitude DECIMAL(10, 6),
longitude DECIMAL(10, 6)
);
-- ===================================================================================
-- 📝 LEAD CAPTURE (The "Money" Engine)
-- ===================================================================================
CREATE TABLE IF NOT EXISTS forms (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
site_id UUID REFERENCES sites (id) ON DELETE CASCADE,
name VARCHAR(255) NOT NULL,
slug VARCHAR(255),
fields JSONB NOT NULL DEFAULT '[]',
submit_action VARCHAR(50) DEFAULT 'store',
webhook_url VARCHAR(500),
email_recipients TEXT,
success_message TEXT,
redirect_url VARCHAR(500),
date_created TIMESTAMP DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS form_submissions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
form_id UUID REFERENCES forms (id) ON DELETE CASCADE,
data JSONB NOT NULL,
ip_address VARCHAR(45),
user_agent TEXT,
date_created TIMESTAMP DEFAULT NOW()
);
-- ===================================================================================
-- 🏗️ SITE BUILDER (The "Experience" Layer)
-- ===================================================================================
CREATE TABLE IF NOT EXISTS navigation (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
site_id UUID REFERENCES sites (id) ON DELETE CASCADE,
label VARCHAR(100) NOT NULL,
url VARCHAR(500) NOT NULL,
parent_id UUID REFERENCES navigation (id) ON DELETE CASCADE,
target VARCHAR(20) DEFAULT '_self',
icon VARCHAR(100),
sort_order INTEGER DEFAULT 0
);
CREATE TABLE IF NOT EXISTS globals (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
site_id UUID UNIQUE REFERENCES sites (id) ON DELETE CASCADE,
site_name VARCHAR(255),
site_tagline TEXT,
logo VARCHAR(500),
favicon VARCHAR(500),
footer_text TEXT,
scripts_head TEXT,
scripts_body TEXT,
social_links JSONB,
theme_settings JSONB
);
CREATE TABLE IF NOT EXISTS hub_pages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
site_id UUID REFERENCES sites (id) ON DELETE CASCADE,
title VARCHAR(255) NOT NULL,
slug VARCHAR(255) NOT NULL,
description TEXT,
parent_id UUID REFERENCES hub_pages (id) ON DELETE SET NULL,
sort_order INTEGER DEFAULT 0
);
-- ===================================================================================
-- 🔧 SYSTEM ADMIN
-- ===================================================================================
CREATE TABLE IF NOT EXISTS work_log (
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
site_id UUID REFERENCES sites (id) ON DELETE SET NULL,
action VARCHAR(100) NOT NULL,
entity_type VARCHAR(100),
entity_id UUID,
details JSONB,
level VARCHAR(20) DEFAULT 'info',
status VARCHAR(100),
user_id UUID,
timestamp TIMESTAMP DEFAULT NOW()
);
-- ===================================================================================
-- 🎨 DIRECTUS UI CONFIGURATION (The "Glance" Layer)
-- Fixes interfaces, dropdowns, and template issues automatically
-- ===================================================================================
-- 1. Enable 'Select Dropdown' for all Foreign Keys (Fixes "Raw UUID" UI issue)
INSERT INTO directus_fields (collection, field, interface, readonly, hidden, width)
VALUES
('campaign_masters', 'site_id', 'select-dropdown-m2o', 'false', 'false', 'half'),
('generated_articles', 'site_id', 'select-dropdown-m2o', 'false', 'false', 'half'),
('generated_articles', 'campaign_id', 'select-dropdown-m2o', 'false', 'false', 'half'),
('generation_jobs', 'site_id', 'select-dropdown-m2o', 'false', 'false', 'half'),
('pages', 'site_id', 'select-dropdown-m2o', 'false', 'false', 'half'),
('posts', 'site_id', 'select-dropdown-m2o', 'false', 'false', 'half'),
('leads', 'site_id', 'select-dropdown-m2o', 'false', 'false', 'half'),
('headline_inventory', 'campaign_id', 'select-dropdown-m2o', 'false', 'false', 'half'),
('content_fragments', 'campaign_id', 'select-dropdown-m2o', 'false', 'false', 'half'),
('link_targets', 'site_id', 'select-dropdown-m2o', 'false', 'false', 'half')
ON CONFLICT (collection, field)
DO UPDATE SET interface = 'select-dropdown-m2o';
INSERT INTO
directus_fields (
collection,
field,
interface,
readonly,
hidden,
width
)
VALUES (
'campaign_masters',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'generated_articles',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'generated_articles',
'campaign_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'generation_jobs',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'pages',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'posts',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'leads',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'headline_inventory',
'campaign_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'content_fragments',
'campaign_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'link_targets',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
-- NEW: Stability Patch FK configurations
(
'site_analytics',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'events',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'pageviews',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'conversions',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'conversions',
'lead_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'locations_counties',
'state_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'locations_cities',
'state_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'locations_cities',
'county_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'forms',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'form_submissions',
'form_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'navigation',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'navigation',
'parent_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'globals',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'hub_pages',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'hub_pages',
'parent_id',
'select-dropdown-m2o',
'false',
'false',
'half'
),
(
'work_log',
'site_id',
'select-dropdown-m2o',
'false',
'false',
'half'
) ON CONFLICT (collection, field) DO
UPDATE
SET
interface = 'select-dropdown-m2o';
-- 2. Fix the Template Mismatch (The 'campaign_name' vs 'name' bug)
UPDATE directus_collections
SET display_template = '{{campaign_id.name}}'
WHERE collection IN ('content_fragments', 'headline_inventory', 'generated_articles');
SET
display_template = '{{campaign_id.name}}'
WHERE
collection IN (
'content_fragments',
'headline_inventory',
'generated_articles'
);
-- 3. Set standard display templates for Sites
UPDATE directus_collections
SET display_template = '{{name}}'
WHERE collection IN ('sites', 'campaign_masters');
SET
display_template = '{{name}}'
WHERE
collection IN (
'sites',
'campaign_masters',
'forms',
'navigation',
'hub_pages'
);
-- 4. Set display templates for Geo tables
UPDATE directus_collections
SET
display_template = '{{name}} ({{code}})'
WHERE
collection = 'locations_states';
UPDATE directus_collections
SET
display_template = '{{name}}'
WHERE
collection IN (
'locations_counties',
'locations_cities'
);
-- 5. Set display template for globals (one per site)
UPDATE directus_collections
SET
display_template = '{{site_name}}'
WHERE
collection = 'globals';