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)`
This commit is contained in:
Krzysztof Kotlarek 2020-07-28 13:31:51 +10:00 committed by GitHub
parent 1ad270965c
commit 14003abc37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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