From 14003abc37e5b2f7897a81b873f6642d4c864518 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Tue, 28 Jul 2020 13:31:51 +1000 Subject: [PATCH] FIX: Improve allowed_path column migration (#10321) Because previous migration was already deployed and some databases were already migrated, I needed to add some conditions to the migration. Previous migration - https://github.com/discourse/discourse/blob/master/db/post_migrate/20200629232159_rename_path_whitelist_to_allowed_paths.rb What will happen in a scenario when previous migration was not run. 1. column allowed_paths will be created 2. allowed_path will be populated with data from path_whitelist 3. path_whitelist column will be dropped What will happen in a scenario when previous migration was already run. 1. column allowed_paths will not be created because already exists - `unless column_exists?(:embeddable_hosts, :allowed_paths)` 2. Data will not be copied because path_whitelist is missing - `if column_exists?(:embeddable_hosts, :path_whitelist) && column_exists?(:embeddable_hosts, :allowed_paths)` 3. path_whitelist column deletion will be skipped - `if column_exists?(:embeddable_hosts, :path_whitelist)` --- ...icate_allowed_paths_from_path_whitelist.rb | 24 +++++++++++++++++++ ..._rename_path_whitelist_to_allowed_paths.rb | 7 ------ ...op_path_whitelist_from_embeddable_hosts.rb | 17 +++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20200728000854_duplicate_allowed_paths_from_path_whitelist.rb delete mode 100644 db/post_migrate/20200629232159_rename_path_whitelist_to_allowed_paths.rb create mode 100644 db/post_migrate/20200728004302_drop_path_whitelist_from_embeddable_hosts.rb diff --git a/db/migrate/20200728000854_duplicate_allowed_paths_from_path_whitelist.rb b/db/migrate/20200728000854_duplicate_allowed_paths_from_path_whitelist.rb new file mode 100644 index 00000000000..17dd89960e5 --- /dev/null +++ b/db/migrate/20200728000854_duplicate_allowed_paths_from_path_whitelist.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class DuplicateAllowedPathsFromPathWhitelist < ActiveRecord::Migration[6.0] + def up + unless column_exists?(:embeddable_hosts, :allowed_paths) + add_column :embeddable_hosts, :allowed_paths, :string + end + + if column_exists?(:embeddable_hosts, :path_whitelist) + Migration::ColumnDropper.mark_readonly('embeddable_hosts', 'path_whitelist') + + if column_exists?(:embeddable_hosts, :allowed_paths) + DB.exec <<~SQL + UPDATE embeddable_hosts + SET allowed_paths = path_whitelist + SQL + end + end + end + + def down + remove_column :embeddable_hosts, :allowed_paths + end +end diff --git a/db/post_migrate/20200629232159_rename_path_whitelist_to_allowed_paths.rb b/db/post_migrate/20200629232159_rename_path_whitelist_to_allowed_paths.rb deleted file mode 100644 index 3e55cabe2db..00000000000 --- a/db/post_migrate/20200629232159_rename_path_whitelist_to_allowed_paths.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -class RenamePathWhitelistToAllowedPaths < ActiveRecord::Migration[6.0] - def change - rename_column :embeddable_hosts, :path_whitelist, :allowed_paths - end -end diff --git a/db/post_migrate/20200728004302_drop_path_whitelist_from_embeddable_hosts.rb b/db/post_migrate/20200728004302_drop_path_whitelist_from_embeddable_hosts.rb new file mode 100644 index 00000000000..56771b4c075 --- /dev/null +++ b/db/post_migrate/20200728004302_drop_path_whitelist_from_embeddable_hosts.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class DropPathWhitelistFromEmbeddableHosts < ActiveRecord::Migration[6.0] + DROPPED_COLUMNS ||= { + embeddable_hosts: %i{path_whitelist} + } + + def up + DROPPED_COLUMNS.each do |table, columns| + Migration::ColumnDropper.execute_drop(table, columns) + end + end + + def down + add_column :embeddable_hosts, :path_whitelist, :string + end +end