FIX: Remove duplicate site settings and add unique index

FIX: Remove duplicate site settings and add unique index

diff --git a/db/migrate/20190325162154_make_site_settings_unique.rb b/db/migrate/20190325162154_make_site_settings_unique.rb
new file mode 100644
index 0000000..27baec0
--- /dev/null
+++ b/db/migrate/20190325162154_make_site_settings_unique.rb
@@ -0,0 +1,15 @@
+class MakeSiteSettingsUnique < ActiveRecord::Migration[5.2]
+  def up
+    execute <<~SQL
+      DELETE
+      FROM site_settings a USING site_settings b
+      WHERE a.id < b.id AND a.name = b.name
+    SQL
+
+    add_index :site_settings, [:name], unique: true
+  end
+
+  def down
+    remove_index :site_settings, [:name]
+  end
+end

GitHub sha: cb70a328

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: