feat: initialize database module with multi-schema support and define master, tenant, and cohort entities
This commit is contained in:
+59
-53
@@ -15,10 +15,16 @@
|
||||
|
||||
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
|
||||
|
||||
-- ─── Schemas ────────────────────────────────────────────────────────────────
|
||||
|
||||
CREATE SCHEMA IF NOT EXISTS tenant;
|
||||
CREATE SCHEMA IF NOT EXISTS masters;
|
||||
CREATE SCHEMA IF NOT EXISTS cohort;
|
||||
|
||||
-- ─── Enum Types ─────────────────────────────────────────────────────────────
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE cohort_high_value_passenger_enum AS ENUM (
|
||||
CREATE TYPE cohort.cohort_high_value_passenger_enum AS ENUM (
|
||||
'Any',
|
||||
'Yes(VIP/Strategic)',
|
||||
'No'
|
||||
@@ -28,7 +34,7 @@ EXCEPTION
|
||||
END $$;
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE cohort_flight_type_enum AS ENUM (
|
||||
CREATE TYPE cohort.cohort_flight_type_enum AS ENUM (
|
||||
'Domestic Only',
|
||||
'International Only',
|
||||
'Both (All)'
|
||||
@@ -39,7 +45,7 @@ END $$;
|
||||
|
||||
-- ─── Tenants ────────────────────────────────────────────────────────────────
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_tenants (
|
||||
CREATE TABLE IF NOT EXISTS tenant.tbl_tenants (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
slug VARCHAR NOT NULL,
|
||||
name VARCHAR NOT NULL,
|
||||
@@ -52,152 +58,152 @@ CREATE TABLE IF NOT EXISTS tbl_tenants (
|
||||
|
||||
-- ─── Master Data ────────────────────────────────────────────────────────────
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_membership_tiers (
|
||||
CREATE TABLE IF NOT EXISTS masters.tbl_membership_tiers (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"tenantId" UUID NOT NULL REFERENCES tbl_tenants(id) ON DELETE CASCADE,
|
||||
"tenantId" UUID NOT NULL REFERENCES tenant.tbl_tenants(id) ON DELETE CASCADE,
|
||||
label VARCHAR NOT NULL,
|
||||
value VARCHAR NOT NULL,
|
||||
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
|
||||
"updatedAt" TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_membership_tiers_tenant ON tbl_membership_tiers("tenantId");
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_membership_tiers_tenant ON masters.tbl_membership_tiers("tenantId");
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_customer_values (
|
||||
CREATE TABLE IF NOT EXISTS masters.tbl_customer_values (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"tenantId" UUID NOT NULL REFERENCES tbl_tenants(id) ON DELETE CASCADE,
|
||||
"tenantId" UUID NOT NULL REFERENCES tenant.tbl_tenants(id) ON DELETE CASCADE,
|
||||
label VARCHAR NOT NULL,
|
||||
value VARCHAR NOT NULL,
|
||||
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
|
||||
"updatedAt" TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_customer_values_tenant ON tbl_customer_values("tenantId");
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_customer_values_tenant ON masters.tbl_customer_values("tenantId");
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_regions (
|
||||
CREATE TABLE IF NOT EXISTS masters.tbl_regions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"tenantId" UUID NOT NULL REFERENCES tbl_tenants(id) ON DELETE CASCADE,
|
||||
"tenantId" UUID NOT NULL REFERENCES tenant.tbl_tenants(id) ON DELETE CASCADE,
|
||||
label VARCHAR NOT NULL,
|
||||
value VARCHAR NOT NULL,
|
||||
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
|
||||
"updatedAt" TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_regions_tenant ON tbl_regions("tenantId");
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_regions_tenant ON masters.tbl_regions("tenantId");
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_trip_purposes (
|
||||
CREATE TABLE IF NOT EXISTS masters.tbl_trip_purposes (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"tenantId" UUID NOT NULL REFERENCES tbl_tenants(id) ON DELETE CASCADE,
|
||||
"tenantId" UUID NOT NULL REFERENCES tenant.tbl_tenants(id) ON DELETE CASCADE,
|
||||
label VARCHAR NOT NULL,
|
||||
value VARCHAR NOT NULL,
|
||||
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
|
||||
"updatedAt" TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_trip_purposes_tenant ON tbl_trip_purposes("tenantId");
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_trip_purposes_tenant ON masters.tbl_trip_purposes("tenantId");
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_cabin_classes (
|
||||
CREATE TABLE IF NOT EXISTS masters.tbl_cabin_classes (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"tenantId" UUID NOT NULL REFERENCES tbl_tenants(id) ON DELETE CASCADE,
|
||||
"tenantId" UUID NOT NULL REFERENCES tenant.tbl_tenants(id) ON DELETE CASCADE,
|
||||
label VARCHAR NOT NULL,
|
||||
value VARCHAR NOT NULL,
|
||||
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
|
||||
"updatedAt" TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_cabin_classes_tenant ON tbl_cabin_classes("tenantId");
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_cabin_classes_tenant ON masters.tbl_cabin_classes("tenantId");
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_passenger_types (
|
||||
CREATE TABLE IF NOT EXISTS masters.tbl_passenger_types (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"tenantId" UUID NOT NULL REFERENCES tbl_tenants(id) ON DELETE CASCADE,
|
||||
"tenantId" UUID NOT NULL REFERENCES tenant.tbl_tenants(id) ON DELETE CASCADE,
|
||||
label VARCHAR NOT NULL,
|
||||
value VARCHAR NOT NULL,
|
||||
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
|
||||
"updatedAt" TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_passenger_types_tenant ON tbl_passenger_types("tenantId");
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_passenger_types_tenant ON masters.tbl_passenger_types("tenantId");
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_ancillary_purchases (
|
||||
CREATE TABLE IF NOT EXISTS masters.tbl_ancillary_purchases (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"tenantId" UUID NOT NULL REFERENCES tbl_tenants(id) ON DELETE CASCADE,
|
||||
"tenantId" UUID NOT NULL REFERENCES tenant.tbl_tenants(id) ON DELETE CASCADE,
|
||||
label VARCHAR NOT NULL,
|
||||
value VARCHAR NOT NULL,
|
||||
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
|
||||
"updatedAt" TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_ancillary_purchases_tenant ON tbl_ancillary_purchases("tenantId");
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_ancillary_purchases_tenant ON masters.tbl_ancillary_purchases("tenantId");
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_revenue_segments (
|
||||
CREATE TABLE IF NOT EXISTS masters.tbl_revenue_segments (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"tenantId" UUID NOT NULL REFERENCES tbl_tenants(id) ON DELETE CASCADE,
|
||||
"tenantId" UUID NOT NULL REFERENCES tenant.tbl_tenants(id) ON DELETE CASCADE,
|
||||
label VARCHAR NOT NULL,
|
||||
value VARCHAR NOT NULL,
|
||||
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
|
||||
"updatedAt" TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_revenue_segments_tenant ON tbl_revenue_segments("tenantId");
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_revenue_segments_tenant ON masters.tbl_revenue_segments("tenantId");
|
||||
|
||||
-- ─── Cohorts ────────────────────────────────────────────────────────────────
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_cohorts (
|
||||
CREATE TABLE IF NOT EXISTS cohort.tbl_cohorts (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"tenantId" UUID NOT NULL REFERENCES tbl_tenants(id) ON DELETE CASCADE,
|
||||
"tenantId" UUID NOT NULL REFERENCES tenant.tbl_tenants(id) ON DELETE CASCADE,
|
||||
"scopeType" VARCHAR,
|
||||
name VARCHAR NOT NULL,
|
||||
description VARCHAR,
|
||||
status VARCHAR NOT NULL DEFAULT 'Draft',
|
||||
"highValuePassenger" cohort_high_value_passenger_enum NOT NULL DEFAULT 'Any',
|
||||
"flightType" cohort_flight_type_enum,
|
||||
"highValuePassenger" cohort.cohort_high_value_passenger_enum NOT NULL DEFAULT 'Any',
|
||||
"flightType" cohort.cohort_flight_type_enum,
|
||||
"originAirport" TEXT,
|
||||
"destinationAirport" TEXT,
|
||||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
|
||||
"updatedAt" TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_cohorts_tenant ON tbl_cohorts("tenantId");
|
||||
CREATE INDEX IF NOT EXISTS idx_tbl_cohorts_tenant ON cohort.tbl_cohorts("tenantId");
|
||||
|
||||
-- ─── Cohort Join Tables (Many-to-Many) ──────────────────────────────────────
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_cohort_cabin_classes (
|
||||
"cohortId" UUID NOT NULL REFERENCES tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"cabinClassId" UUID NOT NULL REFERENCES tbl_cabin_classes(id) ON DELETE CASCADE,
|
||||
CREATE TABLE IF NOT EXISTS cohort.tbl_cohort_cabin_classes (
|
||||
"cohortId" UUID NOT NULL REFERENCES cohort.tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"cabinClassId" UUID NOT NULL REFERENCES masters.tbl_cabin_classes(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY ("cohortId", "cabinClassId")
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_cohort_passenger_types (
|
||||
"cohortId" UUID NOT NULL REFERENCES tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"passengerTypeId" UUID NOT NULL REFERENCES tbl_passenger_types(id) ON DELETE CASCADE,
|
||||
CREATE TABLE IF NOT EXISTS cohort.tbl_cohort_passenger_types (
|
||||
"cohortId" UUID NOT NULL REFERENCES cohort.tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"passengerTypeId" UUID NOT NULL REFERENCES masters.tbl_passenger_types(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY ("cohortId", "passengerTypeId")
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_cohort_ancillary_purchases (
|
||||
"cohortId" UUID NOT NULL REFERENCES tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"ancillaryPurchaseId" UUID NOT NULL REFERENCES tbl_ancillary_purchases(id) ON DELETE CASCADE,
|
||||
CREATE TABLE IF NOT EXISTS cohort.tbl_cohort_ancillary_purchases (
|
||||
"cohortId" UUID NOT NULL REFERENCES cohort.tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"ancillaryPurchaseId" UUID NOT NULL REFERENCES masters.tbl_ancillary_purchases(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY ("cohortId", "ancillaryPurchaseId")
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_cohort_loyalty_tiers (
|
||||
"cohortId" UUID NOT NULL REFERENCES tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"membershipTierId" UUID NOT NULL REFERENCES tbl_membership_tiers(id) ON DELETE CASCADE,
|
||||
CREATE TABLE IF NOT EXISTS cohort.tbl_cohort_loyalty_tiers (
|
||||
"cohortId" UUID NOT NULL REFERENCES cohort.tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"membershipTierId" UUID NOT NULL REFERENCES masters.tbl_membership_tiers(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY ("cohortId", "membershipTierId")
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_cohort_revenue_segments (
|
||||
"cohortId" UUID NOT NULL REFERENCES tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"revenueSegmentId" UUID NOT NULL REFERENCES tbl_revenue_segments(id) ON DELETE CASCADE,
|
||||
CREATE TABLE IF NOT EXISTS cohort.tbl_cohort_revenue_segments (
|
||||
"cohortId" UUID NOT NULL REFERENCES cohort.tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"revenueSegmentId" UUID NOT NULL REFERENCES masters.tbl_revenue_segments(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY ("cohortId", "revenueSegmentId")
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_cohort_regions (
|
||||
"cohortId" UUID NOT NULL REFERENCES tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"regionId" UUID NOT NULL REFERENCES tbl_regions(id) ON DELETE CASCADE,
|
||||
CREATE TABLE IF NOT EXISTS cohort.tbl_cohort_regions (
|
||||
"cohortId" UUID NOT NULL REFERENCES cohort.tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"regionId" UUID NOT NULL REFERENCES masters.tbl_regions(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY ("cohortId", "regionId")
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tbl_cohort_trip_purposes (
|
||||
"cohortId" UUID NOT NULL REFERENCES tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"tripPurposeId" UUID NOT NULL REFERENCES tbl_trip_purposes(id) ON DELETE CASCADE,
|
||||
CREATE TABLE IF NOT EXISTS cohort.tbl_cohort_trip_purposes (
|
||||
"cohortId" UUID NOT NULL REFERENCES cohort.tbl_cohorts(id) ON DELETE CASCADE,
|
||||
"tripPurposeId" UUID NOT NULL REFERENCES masters.tbl_trip_purposes(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY ("cohortId", "tripPurposeId")
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user