From fbd3f97ae02b866d8f0773c2fcadec23ec94ecda Mon Sep 17 00:00:00 2001
From: David Leek <david@getunleash.io>
Date: Thu, 24 Oct 2024 13:33:54 +0200
Subject: [PATCH] chore: add migration for release plans (#8529)

---
 ...add-release-plans-milestones-strategies.js | 62 +++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 src/migrations/20241024062521-add-release-plans-milestones-strategies.js

diff --git a/src/migrations/20241024062521-add-release-plans-milestones-strategies.js b/src/migrations/20241024062521-add-release-plans-milestones-strategies.js
new file mode 100644
index 000000000000..e1f8493bdb6c
--- /dev/null
+++ b/src/migrations/20241024062521-add-release-plans-milestones-strategies.js
@@ -0,0 +1,62 @@
+exports.up = function(db, cb) {
+    db.runSql(
+        `
+        CREATE TABLE IF NOT EXISTS release_plan_definitions
+        (
+            id TEXT PRIMARY KEY NOT NULL,
+            discriminator TEXT NOT NULL,
+            name TEXT NOT NULL,
+            description TEXT,
+            feature_name TEXT,
+			environment TEXT,
+            created_by_user_id INTEGER NOT NULL REFERENCES users(id),
+            created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() at time zone 'utc'),
+            CONSTRAINT release_plan_definitions_discriminator_values
+                CHECK (discriminator IN ('plan', 'template')),
+            CONSTRAINT feature_environments_fkey FOREIGN KEY (environment, feature_name)
+                REFERENCES feature_environments(environment, feature_name)
+                ON DELETE CASCADE
+        );
+
+        CREATE TABLE IF NOT EXISTS milestones
+        (
+            id TEXT PRIMARY KEY NOT NULL,
+            name TEXT NOT NULL,
+            sort_order INTEGER NOT NULL,
+            release_plan_definition_id TEXT NOT NULL REFERENCES release_plan_definitions(id) ON DELETE CASCADE
+        );
+
+        CREATE INDEX IF NOT EXISTS idx_milestones_release_plan_definition_id ON milestones (release_plan_definition_id);
+
+        ALTER TABLE IF EXISTS release_plan_definitions
+            ADD COLUMN IF NOT EXISTS active_milestone_id TEXT REFERENCES milestones(id);
+        
+        CREATE TABLE IF NOT EXISTS milestone_strategies
+        (
+            id TEXT PRIMARY KEY NOT NULL,
+            milestone_id TEXT NOT NULL REFERENCES milestones(id) ON DELETE CASCADE,
+            sort_order INTEGER NOT NULL,
+            title TEXT NOT NULL,
+            strategy_name TEXT NOT NULL REFERENCES strategies(name),
+            parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+            constraints JSONB
+        );
+        CREATE INDEX IF NOT EXISTS idx_milestone_strategies_strategy_name ON milestone_strategies (strategy_name);
+        `,
+        cb,
+    );
+};
+
+exports.down = function(db, cb) {
+    db.runSql(
+        `
+        DROP INDEX IF EXISTS idx_milestone_strategies_strategy_name;
+        DROP INDEX IF EXISTS idx_milestones_release_plan_definition_id;
+        ALTER TABLE IF EXISTS release_plan_definitions DROP COLUMN IF EXISTS active_milestone_id;
+        DROP TABLE IF EXISTS milestone_strategies;
+        DROP TABLE IF EXISTS milestones;
+        DROP TABLE IF EXISTS release_plan_definitions;
+        `,
+        cb,
+    );
+};
\ No newline at end of file