FIX: Improve allowed_path column migration (#10321)

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)
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 0000000..17dd899
--- /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 3e55cab..0000000
--- 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 0000000..56771b4
--- /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

GitHub sha: 14003abc

1 Like

This commit appears in #10321 which was approved by tgxworld. It was merged by lis2.