First step of mitigation migration

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-03-27 23:34:35 +01:00
parent 449bd620da
commit 7321862ba9
62 changed files with 4025 additions and 2141 deletions

View File

@@ -0,0 +1,18 @@
-- Add organization_id column to mitigations table
ALTER TABLE mitigations ADD COLUMN organization_id TEXT;
-- Update mitigations to set organization_id based on framework's organization_id
UPDATE mitigations m
SET organization_id = f.organization_id
FROM frameworks f
WHERE m.framework_id = f.id;
-- Make organization_id NOT NULL after update
ALTER TABLE mitigations ALTER COLUMN organization_id SET NOT NULL;
-- Add foreign key constraint
ALTER TABLE mitigations ADD CONSTRAINT fk_mitigations_organization_id
FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE;
-- Add index for performance
CREATE INDEX idx_mitigations_organization_id ON mitigations(organization_id);

View File

@@ -0,0 +1,8 @@
-- Drop the foreign key constraint first
ALTER TABLE mitigations DROP CONSTRAINT IF EXISTS fk_mitigations_framework_id;
-- Drop any indices on framework_id
DROP INDEX IF EXISTS idx_mitigations_framework_id;
-- Remove the framework_id column
ALTER TABLE mitigations DROP COLUMN framework_id;

View File

@@ -0,0 +1 @@
ALTER TABLE frameworks ADD COLUMN reference_id TEXT;

View File

@@ -0,0 +1,2 @@
ALTER TABLE controls ADD COLUMN reference_id TEXT NOT NULL DEFAULT '';
ALTER TABLE controls ALTER COLUMN reference_id DROP DEFAULT;

View File

@@ -0,0 +1 @@
ALTER TABLE frameworks DROP COLUMN content_ref;

View File

@@ -0,0 +1 @@
ALTER TABLE frameworks DROP COLUMN version;

View File

@@ -0,0 +1,19 @@
-- Create a new table for controls with string IDs
CREATE TABLE controls (
id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
framework_id TEXT NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
version INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE controls_mitigations (
control_id TEXT NOT NULL,
mitigation_id TEXT NOT NULL,
tenant_id TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
PRIMARY KEY (control_id, mitigation_id)
);