From 54e7b6a5a785696c3323793d638ff75ee12fcbef Mon Sep 17 00:00:00 2001 From: Benjamin Chrobot Date: Thu, 25 Apr 2019 15:35:59 -0400 Subject: [PATCH 1/4] Add missing Spoke-style migrations. --- .../20190224000000_add_campaign_creator_id.js | 20 +++++++++++++++++++ ...5000000_add_message_campaign_contact_id.js | 15 ++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 migrations/20190224000000_add_campaign_creator_id.js create mode 100644 migrations/20190225000000_add_message_campaign_contact_id.js diff --git a/migrations/20190224000000_add_campaign_creator_id.js b/migrations/20190224000000_add_campaign_creator_id.js new file mode 100644 index 000000000..0c52b6fd3 --- /dev/null +++ b/migrations/20190224000000_add_campaign_creator_id.js @@ -0,0 +1,20 @@ +// Add creator_id column to campaign +exports.up = function(knex, Promise) { + return knex.schema.alterTable('campaign', (table) => { + table.integer('creator_id') + .unsigned() + .nullable() + .default(null) + .index() + .references('id') + .inTable('user') + }) +} + +// Drop creator_id column from campaign +exports.down = function(knex, Promise) { + return knex.schema.alterTable('campaign', (table) => { + table.dropForeign('creator_id') + table.dropColumn('creator_id') + }) +} diff --git a/migrations/20190225000000_add_message_campaign_contact_id.js b/migrations/20190225000000_add_message_campaign_contact_id.js new file mode 100644 index 000000000..88c78a34f --- /dev/null +++ b/migrations/20190225000000_add_message_campaign_contact_id.js @@ -0,0 +1,15 @@ +// Add campaign_contact_id column to message +exports.up = function(knex, Promise) { + return knex.schema.alterTable('message', table => { + table.integer('campaign_contact_id').unsigned() + table.foreign('campaign_contact_id').references('campaign_contact.id') + }) +} + +// Drop campaign_contact_id column from message +exports.down = function(knex, Promise) { + return knex.schema.alterTable('message', table => { + table.dropForeign('campaign_contact_id') + table.dropColumn('campaign_contact_id') + }) +} From fd27044dbc9a91afbd2c2b29a271022ea8027da7 Mon Sep 17 00:00:00 2001 From: Benjamin Chrobot Date: Thu, 25 Apr 2019 15:51:39 -0400 Subject: [PATCH 2/4] Add MySQL migrations not already bundled in base Knex-style migration. --- ...0408000000_add_index_message_created_at.js | 13 ++++++++ ...08010000_cascade_interaction_step_fkeys.js | 15 ++++++++++ ...20190408020000_add_deliverability_table.js | 30 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 migrations/20190408000000_add_index_message_created_at.js create mode 100644 migrations/20190408010000_cascade_interaction_step_fkeys.js create mode 100644 migrations/20190408020000_add_deliverability_table.js diff --git a/migrations/20190408000000_add_index_message_created_at.js b/migrations/20190408000000_add_index_message_created_at.js new file mode 100644 index 000000000..55f4cf4b3 --- /dev/null +++ b/migrations/20190408000000_add_index_message_created_at.js @@ -0,0 +1,13 @@ +// Add index on message.created_at +exports.up = function(knex, Promise) { + return knex.schema.alterTable('message', table => { + table.index('created_at') + }) +} + +// Drop index on message.created_at +exports.down = function(knex, Promise) { + return knex.schema.alterTable('message', table => { + table.dropIndex('created_at') + }) +} diff --git a/migrations/20190408010000_cascade_interaction_step_fkeys.js b/migrations/20190408010000_cascade_interaction_step_fkeys.js new file mode 100644 index 000000000..130c0ab47 --- /dev/null +++ b/migrations/20190408010000_cascade_interaction_step_fkeys.js @@ -0,0 +1,15 @@ +// Cascade deletes from parent interaction steps down to children +exports.up = function(knex, Promise) { + return knex.schema.alterTable('interaction_step', table => { + table.foreign('parent_interaction_id') + .references('interaction_step.id') + .onDelete('CASCADE') + }) +} + +// Remove cascading deletes from interaction steps +exports.down = function(knex, Promise) { + return knex.schema.alterTable('interaction_step', table => { + table.foreign('parent_interaction_id').references('interaction_step.id') + }) +} diff --git a/migrations/20190408020000_add_deliverability_table.js b/migrations/20190408020000_add_deliverability_table.js new file mode 100644 index 000000000..4412f5c63 --- /dev/null +++ b/migrations/20190408020000_add_deliverability_table.js @@ -0,0 +1,30 @@ +// Create deliverability_report table +exports.up = function(knex, Promise) { + return knex.schema.createTable('deliverability_report', table => { + table.increments('id').primary() + table.timestamp('period_starts_at') + table.timestamp('period_ends_at') + table.timestamp('computed_at') + table.integer('count_total') + table.integer('count_delivered') + table.integer('count_sent') + table.integer('count_error') + table.string('domain', 191) + table.string('url_path', 191) + + table.index('period_starts_at') + table.index('period_ends_at') + table.index('computed_at') + table.index('count_total') + table.index('count_delivered') + table.index('count_sent') + table.index('count_error') + table.index('domain') + table.index('url_path') + }) +} + +// Drop deliverability_report table +exports.down = function(knex, Promise) { + return knex.schema.dropTable('deliverability_report') +} From 1cb0dcfdea5b2444e4b1176d80e1e1e9f38e5215 Mon Sep 17 00:00:00 2001 From: Benjamin Chrobot Date: Thu, 25 Apr 2019 15:52:46 -0400 Subject: [PATCH 3/4] Remove old MySQL migrations. --- .../mysql-migrations/001-create_mysql.sql | 410 ------------------ .../mysql-migrations/002-mysql-utf8mb4.sql | 178 -------- .../003-message-created_at-index.sql | 1 - ...004-on-delete-cascade-interaction-step.sql | 7 - .../005-deliverability-table.sql | 22 - .../006-make-org-features-text.sql | 1 - 6 files changed, 619 deletions(-) delete mode 100644 dev-tools/mysql-migrations/001-create_mysql.sql delete mode 100644 dev-tools/mysql-migrations/002-mysql-utf8mb4.sql delete mode 100644 dev-tools/mysql-migrations/003-message-created_at-index.sql delete mode 100644 dev-tools/mysql-migrations/004-on-delete-cascade-interaction-step.sql delete mode 100644 dev-tools/mysql-migrations/005-deliverability-table.sql delete mode 100644 dev-tools/mysql-migrations/006-make-org-features-text.sql diff --git a/dev-tools/mysql-migrations/001-create_mysql.sql b/dev-tools/mysql-migrations/001-create_mysql.sql deleted file mode 100644 index f086495fc..000000000 --- a/dev-tools/mysql-migrations/001-create_mysql.sql +++ /dev/null @@ -1,410 +0,0 @@ --- Thinky does not create the MySQL columns correctly. --- Run this script instead to initially create the Spoke schema. - --- Delete all tables: --- DROP TABLE `assignment`; DROP TABLE `campaign`; DROP TABLE `campaign_contact`; DROP TABLE `canned_response`; DROP TABLE `interaction_step`; DROP TABLE `invite`; DROP TABLE `job_request`; DROP TABLE `log`; DROP TABLE `message`; DROP TABLE `migrations`; DROP TABLE `opt_out`; DROP TABLE `organization`; DROP TABLE `pending_message_part`; DROP TABLE `question_response`; DROP TABLE `user`; DROP TABLE `user_cell`; DROP TABLE `user_organization`; DROP TABLE `zip_code`; - -SET autocommit=0; -START TRANSACTION; - --- ---------------- --- Create tables --- ---------------- - -CREATE TABLE assignment ( - id int(11) NOT NULL AUTO_INCREMENT, - user_id int(11) NOT NULL, - campaign_id int(11) NOT NULL, - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - max_contacts int(11), - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE campaign ( - id int(11) NOT NULL AUTO_INCREMENT, - organization_id int(11) NOT NULL, - title varchar(255) DEFAULT '', - description varchar(255) DEFAULT '', - is_started bool, - due_by timestamp, - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - is_archived bool, - use_dynamic_assignment bool, - logo_image_url text, - intro_html text, - primary_color text, - override_organization_texting_hours bool DEFAULT 0, - texting_hours_enforced bool DEFAULT 1, - texting_hours_start int(11) DEFAULT 9, - texting_hours_end int(11) DEFAULT 21, - timezone varchar(255), - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE campaign_contact ( - id int(11) NOT NULL AUTO_INCREMENT, - campaign_id int(11) NOT NULL, - assignment_id int(11), - external_id varchar(255) DEFAULT '', - first_name varchar(255) DEFAULT '', - last_name varchar(255) DEFAULT '', - cell varchar(15) NOT NULL, - zip varchar(255) DEFAULT '', - custom_fields varchar(255) DEFAULT '{}', - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - updated_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - message_status varchar(255) DEFAULT 'needsMessage', - is_opted_out bool DEFAULT 0, - timezone_offset varchar(255) DEFAULT '', - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE canned_response ( - id int(11) NOT NULL AUTO_INCREMENT, - campaign_id int(11) NOT NULL, - `text` text NOT NULL, - title text NOT NULL, - user_id int(11), - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE interaction_step ( - id int(11) NOT NULL AUTO_INCREMENT, - campaign_id int(11) NOT NULL, - question varchar(255) DEFAULT '', - script text DEFAULT '', - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - parent_interaction_id int(11), - answer_option varchar(255) DEFAULT '', - answer_actions varchar(255) DEFAULT '', - is_deleted bool DEFAULT 0 NOT NULL, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE invite ( - id int(11) NOT NULL AUTO_INCREMENT, - is_valid bool NOT NULL, - `hash` text, - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE job_request ( - id int(11) NOT NULL AUTO_INCREMENT, - campaign_id int(11) NOT NULL, - payload text NOT NULL, - queue_name varchar(50) NOT NULL, - job_type text NOT NULL, - result_message varchar(255) DEFAULT '', - locks_queue bool DEFAULT 0, - assigned bool DEFAULT 0, - `status` int(11) DEFAULT 0, - updated_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE log ( - id int(11) NOT NULL AUTO_INCREMENT, - message_sid text NOT NULL, - body text, - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE message ( - id int(11) NOT NULL AUTO_INCREMENT, - user_id int(11), - user_number varchar(255) DEFAULT '', - contact_number varchar(15) NOT NULL, - is_from_contact bool NOT NULL, - text varchar(255) DEFAULT '', - service_response varchar(255) DEFAULT '', - assignment_id int(11) NOT NULL, - service varchar(255) DEFAULT '', - service_id varchar(255) DEFAULT '', - send_status varchar(10) NOT NULL, - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - queued_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - sent_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - service_response_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - send_before timestamp, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE migrations ( - id int(11) NOT NULL AUTO_INCREMENT, - completed int(11) NOT NULL, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE opt_out ( - id int(11) NOT NULL AUTO_INCREMENT, - cell varchar(15) NOT NULL, - assignment_id int(11) NOT NULL, - organization_id int(11) NOT NULL, - reason_code varchar(255) DEFAULT '', - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE organization ( - id int(11) NOT NULL AUTO_INCREMENT, - uuid text, - `name` text NOT NULL, - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - features varchar(255) DEFAULT '', - texting_hours_enforced bool DEFAULT 0, - texting_hours_start int(11) DEFAULT 9, - texting_hours_end int(11) DEFAULT 21, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE pending_message_part ( - id int(11) NOT NULL AUTO_INCREMENT, - `service` varchar(100) NOT NULL, - service_id text NOT NULL, - parent_id varchar(255) DEFAULT '', - service_message text NOT NULL, - user_number varchar(255) DEFAULT '', - contact_number text NOT NULL, - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE question_response ( - id int(11) NOT NULL AUTO_INCREMENT, - campaign_contact_id int(11) NOT NULL, - interaction_step_id int(11) NOT NULL, - `value` text NOT NULL, - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE `user` ( - id int(11) NOT NULL AUTO_INCREMENT, - auth0_id text NOT NULL, - first_name text NOT NULL, - last_name text NOT NULL, - cell text NOT NULL, - email text NOT NULL, - created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - assigned_cell text, - is_superadmin bool, - terms bool DEFAULT 0, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE user_cell ( - id int(11) NOT NULL AUTO_INCREMENT, - cell text NOT NULL, - user_id int(11) NOT NULL, - service text, - is_primary bool, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE user_organization ( - id int(11) NOT NULL AUTO_INCREMENT, - user_id int(11) NOT NULL, - organization_id int(11) NOT NULL, - role text NOT NULL, - PRIMARY KEY (id) -) ENGINE=MyISAM; - -CREATE TABLE zip_code ( - zip VARCHAR(5) NOT NULL, - city text NOT NULL, - `state` text NOT NULL, - latitude real NOT NULL, - longitude real NOT NULL, - timezone_offset real NOT NULL, - has_dst bool NOT NULL, - PRIMARY KEY (zip) -) ENGINE=MyISAM; - - --- ---------------- --- Create indices --- ---------------- - --- Assignment -CREATE INDEX assignment_user_id_index - ON assignment(user_id) USING btree; - -CREATE INDEX assignment_campaign_id_index - ON assignment(campaign_id) USING btree; - --- Campaign -CREATE INDEX campaign_organization_id_index - ON campaign(organization_id) USING btree; - --- Campaign Contact -CREATE INDEX campaign_contact_assignment_id_index - ON campaign_contact(assignment_id) USING btree; - -CREATE INDEX campaign_contact_campaign_id_index - ON campaign_contact(campaign_id) USING btree; - -CREATE INDEX campaign_contact_cell_index - ON campaign_contact(cell) USING btree; - -CREATE INDEX campaign_contact_campaign_id_assignment_id_index - ON campaign_contact(campaign_id,assignment_id) USING btree; - -CREATE INDEX campaign_contact_assignment_id_timezone_offset_index - ON campaign_contact(assignment_id, timezone_offset) USING btree; - --- Canned Response - -CREATE INDEX canned_response_campaign_id_index - ON canned_response(campaign_id) USING btree; - -CREATE INDEX canned_response_user_id_index - ON canned_response(user_id) USING btree; - --- Interaction Step - -CREATE INDEX interaction_step_parent_interaction_id_index - ON interaction_step(parent_interaction_id) USING btree; - -CREATE INDEX interaction_step_campaign_id_index - ON interaction_step(campaign_id) USING btree; - --- Invite - -CREATE INDEX invite_is_valid_index - ON invite(is_valid) USING btree; - --- Job Request - -CREATE INDEX job_request_queue_name_index - ON job_request(queue_name) USING btree; - --- Message - -CREATE INDEX message_assignment_id_index - ON message(assignment_id) USING btree; - -CREATE INDEX message_send_status_index - ON message(send_status) USING btree; - -CREATE INDEX message_user_number_index - ON message(user_number) USING btree; - -CREATE INDEX message_contact_number_index - ON message(contact_number) USING btree; - -CREATE INDEX message_service_id_index - ON message(service_id) USING btree; - --- Opt Out - -CREATE INDEX opt_out_cell_index - ON opt_out(cell) USING btree; - -CREATE INDEX opt_out_assignment_id_index - ON opt_out(assignment_id) USING btree; - -CREATE INDEX opt_out_organization_id_index - ON opt_out(organization_id) USING btree; - --- Question Response - -CREATE INDEX question_response_campaign_contact_id_index - ON question_response(campaign_contact_id) USING btree; - -CREATE INDEX question_response_interaction_step_id_index - ON question_response(interaction_step_id) USING btree; - --- Pending Message Part - -CREATE INDEX pending_message_part_parent_id_index - ON pending_message_part(parent_id) USING btree; - -CREATE INDEX pending_message_part_service_index - ON pending_message_part(`service`) USING btree; - --- User Organization - -CREATE INDEX user_organization_user_id_index - ON user_organization(user_id) USING btree; - -CREATE INDEX user_organization_organization_id_index - ON user_organization(organization_id) USING btree; - -CREATE INDEX user_organization_organization_id_user_id_index - ON user_organization(organization_id,user_id) USING btree; - --- ---------------- --- Add Foreign Keys --- ---------------- - -ALTER TABLE assignment - ADD CONSTRAINT assignment_campaign_id_foreign FOREIGN KEY (campaign_id) REFERENCES campaign(id); - -ALTER TABLE assignment - ADD CONSTRAINT assignment_user_id_foreign FOREIGN KEY (user_id) REFERENCES `user`(id); - -ALTER TABLE campaign_contact - ADD CONSTRAINT campaign_contact_assignment_id_foreign FOREIGN KEY (assignment_id) REFERENCES assignment(id); - -ALTER TABLE campaign_contact - ADD CONSTRAINT campaign_contact_campaign_id_foreign FOREIGN KEY (campaign_id) REFERENCES campaign(id); - -ALTER TABLE campaign - ADD CONSTRAINT campaign_organization_id_foreign FOREIGN KEY (organization_id) REFERENCES organization(id); - -ALTER TABLE canned_response - ADD CONSTRAINT canned_response_campaign_id_foreign FOREIGN KEY (campaign_id) REFERENCES campaign(id); - -ALTER TABLE canned_response - ADD CONSTRAINT canned_response_user_id_foreign FOREIGN KEY (user_id) REFERENCES `user`(id); - -ALTER TABLE interaction_step - ADD CONSTRAINT interaction_step_campaign_id_foreign FOREIGN KEY (campaign_id) REFERENCES campaign(id); - -ALTER TABLE interaction_step - ADD CONSTRAINT interaction_step_parent_interaction_id_foreign FOREIGN KEY (parent_interaction_id) REFERENCES interaction_step(id); - -ALTER TABLE job_request - ADD CONSTRAINT job_request_campaign_id_foreign FOREIGN KEY (campaign_id) REFERENCES campaign(id); - -ALTER TABLE message - ADD CONSTRAINT message_assignment_id_foreign FOREIGN KEY (assignment_id) REFERENCES assignment(id); - -ALTER TABLE message - ADD CONSTRAINT message_user_id_foreign FOREIGN KEY (user_id) REFERENCES `user`(id); - -ALTER TABLE opt_out - ADD CONSTRAINT opt_out_assignment_id_foreign FOREIGN KEY (assignment_id) REFERENCES assignment(id); - -ALTER TABLE opt_out - ADD CONSTRAINT opt_out_organization_id_foreign FOREIGN KEY (organization_id) REFERENCES organization(id); - -ALTER TABLE question_response - ADD CONSTRAINT question_response_campaign_contact_id_foreign FOREIGN KEY (campaign_contact_id) REFERENCES campaign_contact(id); - -ALTER TABLE question_response - ADD CONSTRAINT question_response_interaction_step_id_foreign FOREIGN KEY (interaction_step_id) REFERENCES interaction_step(id); - -ALTER TABLE user_cell - ADD CONSTRAINT user_cell_user_id_foreign FOREIGN KEY (user_id) REFERENCES `user`(id); - -ALTER TABLE user_organization - ADD CONSTRAINT user_organization_organization_id_foreign FOREIGN KEY (organization_id) REFERENCES organization(id); - -ALTER TABLE user_organization - ADD CONSTRAINT user_organization_user_id_foreign FOREIGN KEY (user_id) REFERENCES `user`(id); - - --- ------------- --- Peg migration --- ------------- - -INSERT INTO migrations (completed) VALUES (14); - -COMMIT; - -SET autocommit=1; diff --git a/dev-tools/mysql-migrations/002-mysql-utf8mb4.sql b/dev-tools/mysql-migrations/002-mysql-utf8mb4.sql deleted file mode 100644 index 9fda7d76a..000000000 --- a/dev-tools/mysql-migrations/002-mysql-utf8mb4.sql +++ /dev/null @@ -1,178 +0,0 @@ --- This migration updates the character set on an existing MySQL database - --- Note: changes will also need to be made to the RDS cluster parameter group: --- --- character_set_client utf8mb4 --- character_set_connection utf8mb4 --- character_set_database utf8mb4 --- character_set_server utf8mb4 --- collation_connection utf8mb4_unicode_ci --- collation_server utf8mb4_unicode_ci --- --- Source: https://stackoverflow.com/a/49963840 - --- For each database: --- Or, is this done via RDS settings? -ALTER DATABASE spoke_prod CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; - --- Table level encoding changes -ALTER TABLE assignment CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE campaign CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE campaign_contact CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE canned_response CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE interaction_step CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE invite CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE job_request CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE knex_migrations CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE knex_migrations_lock CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE log CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE lookup_tool_migrations CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE lookup_tool_migrations_lock CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE message CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE migrations CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE opt_out CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE organization CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE pending_message_part CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE phone_data CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE question_response CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE tag CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE token CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE user CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE user_cell CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE user_organization CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE utterance CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE zip_code CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ---------------------------------------------- ------- Column level encoding changes –------- ---------------------------------------------- ---------- --- When converting from utf8 to utf8mb4, the maximum length of a column or index key --- is unchanged in terms of bytes. Therefore, it is smaller in terms of characters, --- because the maximum length of a character is now four bytes instead of three. - --- For example, a TINYTEXT column can hold up to 255 bytes, which correlates to --- 85 three-byte or 63 four-byte characters. Let’s say you have a TINYTEXT column --- that uses utf8 but must be able to contain more than 63 characters. Given this requirement, --- you can’t convert this column to utf8mb4 unless you also change the data type to a longer --- type such as TEXT — because if you’d try to fill it with four-byte characters, you’d only --- be able to enter 63 characters, but not more. - --- The same goes for index keys. The InnoDB storage engine has a maximum index length of 767 bytes, --- so for utf8 or utf8mb4 columns, you can index a maximum of 255 or 191 characters, respectively. --- If you currently have utf8 columns with indexes longer than 191 characters, you will need to --- index a smaller number of characters when using utf8mb4. (Because of this, I had to change some --- indexed VARCHAR(255) columns to VARCHAR(191).) ---------- ---------- --- I checked via `show engines;` and we are using InnoDB, so we do have to watch out for too big indexes --- I'm going through table by table and finding which columns need to be changed and how --- If a table has no text or varchars, it's fine, and will be ommitted here - ----------- campaign --- campaign.title and campaign.description are varchars – we should make them of type `text` anywho --- timezone is a varchar but really it's an enum (we know the values and that they won't be 4 byte chars), so we're fine -ALTER TABLE campaign CHANGE title title TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE campaign CHANGE description description TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE campaign CHANGE timezone timezone VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- campaign - just noting which ones are charging to text -ALTER TABLE campaign_contact CHANGE external_id external_id VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE campaign_contact CHANGE first_name first_name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; --- first_name changing to text -ALTER TABLE campaign_contact CHANGE last_name last_name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; --- last_name changing to text -ALTER TABLE campaign_contact CHANGE cell cell VARCHAR(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE campaign_contact CHANGE zip zip VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE campaign_contact CHANGE custom_fields custom_fields TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; --- custom_fields changing to text -ALTER TABLE campaign_contact CHANGE message_status message_status VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE campaign_contact CHANGE timezone_offset timezone_offset VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- canned_response - both were already text -ALTER TABLE canned_response CHANGE title title TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE canned_response CHANGE `text` `text` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- interaction_step – all changed to text, except script, which was already text -ALTER TABLE interaction_step CHANGE question question TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE interaction_step CHANGE script script TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE interaction_step CHANGE answer_option answer_option TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE interaction_step CHANGE answer_actions answer_actions TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- invite – was text -ALTER TABLE invite CHANGE hash hash TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- job_request - kept varchars - queue_name has an index, but it's VARCHAR(50), so that's fine -ALTER TABLE job_request CHANGE queue_name queue_name VARCHAR(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE job_request CHANGE result_message result_message VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- knex migrations - kept varchars -ALTER TABLE knex_migrations CHANGE name name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE knex_migrations CHANGE name name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- log - was text -ALTER TABLE log CHANGE message_sid message_sid TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE log CHANGE body body TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- message - mixed, but all preserved - user_number and service are indexed 255s, making them 191s --- first two won't have emojis for sure -ALTER TABLE message CHANGE user_number user_number varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE message CHANGE contact_number contact_number varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; --- already text so no adjustments needed --- next all won't have emojis -ALTER TABLE message CHANGE `text` `text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE message CHANGE service_response varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE message CHANGE service varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE message CHANGE service_id varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- opt_out - neither would have emojis -ALTER TABLE opt_out CHANGE cell cell VARCHAR(15) SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE opt_out CHANGE reason_code reason_code VARCHAR(255) SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- organization - none have emojis -ALTER TABLE organizaiton CHANGE uuid uuid TEXT SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE organization CHANGE name name TEXT SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE organization CHANGE features features VARCHAR(255) SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ----------- pending_message_part - parts that have emojis (service_message) are already text --- parent_id is indexed, making it a 191 -ALTER TABLE pending_message_part CHANGE service service varchar(100) SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE pending_message_part CHANGE service_id service_id text SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE pending_message_part CHANGE parent_id parent_id varchar(191) SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE pending_message_part CHANGE service_message service_message text SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE pending_message_part CHANGE user_number user_number varchar(255) SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE pending_message_part CHANGE contact_number contact_number text SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ------------ question_response - already text -ALTER TABLE question_response CHANGE value value TEXT SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ------------ tag - indexed -ALTER TABLE tag CHANGE tag tag VARCHAR(191) SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ------------ token - indexed -ALTER TABLE token CHANGE tag tag VARCHAR(191) SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE token CHANGE token token VARCHAR(191) SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ------------ user - nothing indexed except id lol -ALTER TABLE user CHANGE auth0_id auth0_id text SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE user CHANGE first_name first_name text SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE user CHANGE last_name last_name text SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE user CHANGE cell cell text SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE user CHANGE email email text SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ------------ user_cell -ALTER TABLE user_cell CHANGE cell cell text SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE user_cell CHANGE service service text SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ------------ utterance - token indexed -ALTER TABLE utterance CHANGE token token varchar(191) SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ------------ zip_code - zip is indexed but only 5 so fine -ALTER TABLE zip_code CHANGE zip zip varchar(5) SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE zip_code CHANGE city city text SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE zip_code CHANGE state state text SET utf8mb4 COLLATE utf8mb4_unicode_ci; - ---------- ---------- -mysqlcheck -u root -p --auto-repair --optimize --all-databases - diff --git a/dev-tools/mysql-migrations/003-message-created_at-index.sql b/dev-tools/mysql-migrations/003-message-created_at-index.sql deleted file mode 100644 index aa7305ca6..000000000 --- a/dev-tools/mysql-migrations/003-message-created_at-index.sql +++ /dev/null @@ -1 +0,0 @@ -CREATE INDEX idx_message_created_at ON message (created_at); diff --git a/dev-tools/mysql-migrations/004-on-delete-cascade-interaction-step.sql b/dev-tools/mysql-migrations/004-on-delete-cascade-interaction-step.sql deleted file mode 100644 index 711277676..000000000 --- a/dev-tools/mysql-migrations/004-on-delete-cascade-interaction-step.sql +++ /dev/null @@ -1,7 +0,0 @@ -ALTER TABLE interaction_step DROP FOREIGN KEY `interaction_step_parent_interaction_id_foreign`; - -ALTER TABLE interaction_step - ADD CONSTRAINT `interaction_step_parent_interaction_id_foreign ` - FOREIGN KEY (`parent_interaction_id` ) - REFERENCES `interaction_step` (`id` ) - ON DELETE CASCADE; diff --git a/dev-tools/mysql-migrations/005-deliverability-table.sql b/dev-tools/mysql-migrations/005-deliverability-table.sql deleted file mode 100644 index 6b9605028..000000000 --- a/dev-tools/mysql-migrations/005-deliverability-table.sql +++ /dev/null @@ -1,22 +0,0 @@ -CREATE TABLE deliverability_report ( - id int(11) NOT NULL AUTO_INCREMENT, - period_starts_at timestamp, - period_ends_at timestamp, - computed_at timestamp, - count_total int, - count_delivered int, - count_sent int, - count_error int, - domain varchar(191), - url_path varchar(191), - primary key (id) -) ENGINE=MyISAM; - -CREATE INDEX deliverability_period_starts_at_idx ON deliverability_report(period_starts_at); -CREATE INDEX deliverability_period_ends_at_idx ON deliverability_report(period_ends_at); -CREATE INDEX deliverability_messages_total_idx ON deliverability_report(messages_total); -CREATE INDEX deliverability_messages_sent_idx ON deliverability_report(messages_sent); -CREATE INDEX deliverability_messages_unknown_idx ON deliverability_report(messages_unknown); -CREATE INDEX deliverability_messages_erred_idx ON deliverability_report(messages_erred); -CREATE INDEX deliverability_domain_idx ON deliverability_report(domain); -CREATE INDEX deliverability_url_path_idx ON deliverability_report(url_path); diff --git a/dev-tools/mysql-migrations/006-make-org-features-text.sql b/dev-tools/mysql-migrations/006-make-org-features-text.sql deleted file mode 100644 index 7d7da207d..000000000 --- a/dev-tools/mysql-migrations/006-make-org-features-text.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE organization CHANGE features features text; From a4d00c3e934c2a6296b402f344fc93edd6246aa9 Mon Sep 17 00:00:00 2001 From: Benjamin Chrobot Date: Fri, 26 Apr 2019 09:25:11 -0400 Subject: [PATCH 4/4] Recreate foreign keys rather than trying to alter them. --- migrations/20190408010000_cascade_interaction_step_fkeys.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/migrations/20190408010000_cascade_interaction_step_fkeys.js b/migrations/20190408010000_cascade_interaction_step_fkeys.js index 130c0ab47..c23949222 100644 --- a/migrations/20190408010000_cascade_interaction_step_fkeys.js +++ b/migrations/20190408010000_cascade_interaction_step_fkeys.js @@ -1,6 +1,7 @@ // Cascade deletes from parent interaction steps down to children exports.up = function(knex, Promise) { return knex.schema.alterTable('interaction_step', table => { + table.dropForeign('parent_interaction_id') table.foreign('parent_interaction_id') .references('interaction_step.id') .onDelete('CASCADE') @@ -10,6 +11,7 @@ exports.up = function(knex, Promise) { // Remove cascading deletes from interaction steps exports.down = function(knex, Promise) { return knex.schema.alterTable('interaction_step', table => { + table.dropForeign('parent_interaction_id') table.foreign('parent_interaction_id').references('interaction_step.id') }) }