Adjusted migration to use tasks instead of flows.

Now adding the starting task id to the campaign row.
campaign-creator
Jordan Wright 2016-04-26 01:03:30 -05:00
parent 4311e5ed84
commit 5caa02b3a0
1 changed files with 35 additions and 24 deletions

View File

@ -3,12 +3,12 @@
-- SQL in section 'Up' is executed when this migration is applied -- SQL in section 'Up' is executed when this migration is applied
ALTER TABLE campaigns RENAME TO campaigns_old; ALTER TABLE campaigns RENAME TO campaigns_old;
/* /*
The new campaigns table will rely on a "flows" table that is basically a list of tasks to accomplish. The new campaigns table will rely on a "tasks" table that is basically a list of tasks to accomplish.
This will include sending emails, hosting landing pages, or possibly taking other action as needed, This will include sending emails, hosting landing pages, or possibly taking other action as needed,
including running scripts, sending HTTP requests, etc. including running scripts, sending HTTP requests, etc.
*/ */
CREATE TABLE campaigns ("id" integer primary key autoincrement, "user_id" bigint, "name" varchar(255) NOT NULL, "created_date" datetime, "completed_date" datetime, "flow_id" bigint, "status" varchar(255)); CREATE TABLE campaigns ("id" integer primary key autoincrement, "user_id" bigint, "name" varchar(255) NOT NULL, "created_date" datetime, "completed_date" datetime, "task_id" bigint, "status" varchar(255));
INSERT INTO campaigns INSERT INTO campaigns
( (
@ -28,16 +28,16 @@ SELECT
campaigns_old.status campaigns_old.status
FROM campaigns_old; FROM campaigns_old;
/* Create our flows table */ /* Create our tasks table */
CREATE TABLE "flows" ("id" integer primary key autoincrement,"user_id" bigint,"previous_id" bigint,"next_id" bigint,"campaign_id" bigint, "metadata" blob,"task" varchar(255)); CREATE TABLE "tasks" ("id" integer primary key autoincrement,"user_id" bigint,"previous_id" bigint,"next_id" bigint,"campaign_id" bigint, "metadata" blob,"type" varchar(255));
/* Setup our email flows */ /* Setup our email tasks */
INSERT INTO flows ( INSERT INTO tasks (
user_id, user_id,
campaign_id, campaign_id,
task, type,
metadata metadata
) )
SELECT SELECT
@ -50,12 +50,20 @@ SELECT
'}' '}'
FROM campaigns_old; FROM campaigns_old;
/* Setup our landing page flows */ /* Point our campaigns to the SMTP tasks */
UPDATE campaigns
SET
task_id = (SELECT tasks.id FROM tasks
WHERE tasks.campaign_id = campaigns.id
AND tasks.type = "SEND_EMAIL"
);
INSERT INTO flows ( /* Setup our landing page tasks */
INSERT INTO tasks (
user_id, user_id,
campaign_id, campaign_id,
task, type,
metadata, metadata,
previous_id previous_id
) )
@ -67,29 +75,32 @@ SELECT
'"page_id" : ' || campaigns_old.page_id || ',' || '"page_id" : ' || campaigns_old.page_id || ',' ||
'"url" : "' || campaigns_old.url || '"' || '"url" : "' || campaigns_old.url || '"' ||
'}', '}',
flows.id as flow_id tasks.id as task_id
FROM campaigns_old, flows FROM campaigns_old, tasks
WHERE flows.id IN ( WHERE tasks.id IN (
SELECT id FROM flows SELECT id FROM tasks
WHERE campaign_id=campaigns_old.id WHERE campaign_id=campaigns_old.id
AND task="SEND_EMAIL" AND type="SEND_EMAIL"
); );
/* /*
Finally, we need to update our email flows to point to the landing page Next, we need to update our email tasks to point to the landing page
flows. tasks.
*/ */
UPDATE flows UPDATE tasks
SET SET
next_id = ( next_id = (
SELECT f2.id SELECT t2.id
FROM flows AS f2 FROM tasks AS t2
WHERE f2.previous_id=flows.id WHERE t2.previous_id=tasks.id
AND f2.campaign_id = flows.campaign_id AND t2.campaign_id = tasks.campaign_id
AND f2.task = "LANDING_PAGE" AND t2.type = "LANDING_PAGE"
) )
WHERE task = "SEND_EMAIL" WHERE type = "SEND_EMAIL";
/* Finally, we drop our temp table */
DROP TABLE campaigns_old;
-- +goose Down -- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back -- SQL section 'Down' is executed when this migration is rolled back