From 56c9ea070b120c5f462236fb2200c63d1e39c777 Mon Sep 17 00:00:00 2001 From: William Woodson Date: Sat, 27 Feb 2016 19:38:54 -0600 Subject: [PATCH] Fixed isses with SMTP table migration: better names on created objects, all SMTP records are imported with appropriate user_id, entries are de-duped based on user_id:host:from_address tuple --- ...160221001538_0.1.2_store-smtp-settings.sql | 41 --------------- ...160225173824_0.1.2_capture_credentials.sql | 9 ++++ ...160227180335_0.1.2_store-smtp-settings.sql | 52 +++++++++++++++++++ 3 files changed, 61 insertions(+), 41 deletions(-) delete mode 100644 db/migrations/20160221001538_0.1.2_store-smtp-settings.sql create mode 100644 db/migrations/20160225173824_0.1.2_capture_credentials.sql create mode 100644 db/migrations/20160227180335_0.1.2_store-smtp-settings.sql diff --git a/db/migrations/20160221001538_0.1.2_store-smtp-settings.sql b/db/migrations/20160221001538_0.1.2_store-smtp-settings.sql deleted file mode 100644 index 3e85cbaf..00000000 --- a/db/migrations/20160221001538_0.1.2_store-smtp-settings.sql +++ /dev/null @@ -1,41 +0,0 @@ - --- +goose Up --- SQL in section 'Up' is executed when this migration is applied -ALTER TABLE campaigns ADD COLUMN smtp_id bigint; -UPDATE campaigns - SET smtp_id = (SELECT smtp.smtp_id FROM smtp) - WHERE campaigns.id = ( - SELECT campaigns.id - FROM smtp,campaigns - WHERE smtp.campaign_id=campaigns.id - ) -; -- sure hope the current smtp table works like I think it does -ALTER TABLE smtp RENAME TO smtp_old; -CREATE TABLE smtp( - id integer primary key autoincrement, - user_id bigint, - interface_type varchar(255), - name varchar(255), - host varchar(255), - username varchar(255), - password varchar(255), - from_address varchar(255), - modified_date datetime default CURRENT_TIMESTAMP, - ignore_cert_errors BOOLEAN -); -INSERT INTO smtp (id,interface_type,name,host,username,from_address,ignore_cert_errors) - SELECT smtp_id,'SMTP',smtp_id,host,username,from_address,ignore_cert_errors - FROM smtp_old -; -UPDATE smtp - SET user_id = (SELECT campaigns.user_id FROM campaigns) - WHERE smtp.id = ( - SELECT smtp.id - FROM smtp,campaigns - WHERE smtp.id=campaigns.smtp_id - ) -; -DROP TABLE smtp_old; --- +goose Down --- SQL section 'Down' is executed when this migration is rolled back - diff --git a/db/migrations/20160225173824_0.1.2_capture_credentials.sql b/db/migrations/20160225173824_0.1.2_capture_credentials.sql new file mode 100644 index 00000000..eff45de5 --- /dev/null +++ b/db/migrations/20160225173824_0.1.2_capture_credentials.sql @@ -0,0 +1,9 @@ + +-- +goose Up +-- SQL in section 'Up' is executed when this migration is applied +ALTER TABLE pages ADD COLUMN capture_credentials BOOLEAN; +ALTER TABLE pages ADD COLUMN capture_passwords BOOLEAN; + +-- +goose Down +-- SQL section 'Down' is executed when this migration is rolled back + diff --git a/db/migrations/20160227180335_0.1.2_store-smtp-settings.sql b/db/migrations/20160227180335_0.1.2_store-smtp-settings.sql new file mode 100644 index 00000000..a59b7a7c --- /dev/null +++ b/db/migrations/20160227180335_0.1.2_store-smtp-settings.sql @@ -0,0 +1,52 @@ + +-- +goose Up +-- SQL in section 'Up' is executed when this migration is applied +-- Move the relationship between campaigns and smtp to campaigns +ALTER TABLE campaigns ADD COLUMN "smtp_id" bigint; +UPDATE campaigns + SET smtp_id = ( + SELECT smtp.smtp_id + FROM smtp,campaigns + WHERE campaigns.id=smtp.campaign_id + ) +; +-- Add the appropriate user_id to each smtp record +ALTER TABLE smtp ADD COLUMN "user_id" bigint; +UPDATE smtp + SET user_id = ( + SELECT campaigns.user_id + FROM smtp,campaigns + WHERE smtp.smtp_id=campaigns.smtp_id + ) +; +-- Create a new table to store smtp records +ALTER TABLE smtp RENAME TO smtp_old; +CREATE TABLE smtp( + id integer primary key autoincrement, + user_id bigint, + interface_type varchar(255), + name varchar(255), + host varchar(255), + username varchar(255), + password varchar(255), + from_address varchar(255), + modified_date datetime default CURRENT_TIMESTAMP, + ignore_cert_errors BOOLEAN +); +-- Import existing smtp records into new format and drop the old table +INSERT INTO smtp (id,user_id,interface_type,name,host,username,from_address,ignore_cert_errors) + SELECT smtp_id,user_id,'SMTP', + 'Imported campaign via ' || COALESCE(host,'') || ' from ' || COALESCE(from_address,''), + host,username,from_address,ignore_cert_errors + FROM smtp_old + -- Prevent insertion of duplicate records + WHERE smtp_id IN ( + SELECT smtp_id + FROM smtp_old + GROUP BY user_id,host,from_address + ) +; +DROP TABLE smtp_old; +-- +goose Down +-- SQL section 'Down' is executed when this migration is rolled back +