about summary refs log tree commit diff
path: root/db/migrate/20191212022020_create_sharekeys.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrate/20191212022020_create_sharekeys.rb')
-rw-r--r--db/migrate/20191212022020_create_sharekeys.rb19
1 files changed, 19 insertions, 0 deletions
diff --git a/db/migrate/20191212022020_create_sharekeys.rb b/db/migrate/20191212022020_create_sharekeys.rb
new file mode 100644
index 000000000..c0027babe
--- /dev/null
+++ b/db/migrate/20191212022020_create_sharekeys.rb
@@ -0,0 +1,19 @@
+class CreateSharekeys < ActiveRecord::Migration[5.2]
+  def up
+    create_table :sharekeys do |t|
+      t.references :status, foreign_key: true, index: {unique: true}
+      t.string :key
+    end
+
+    safety_assured do
+      execute 'INSERT INTO sharekeys (status_id, key) SELECT id, sharekey FROM statuses WHERE local AND sharekey IS NOT NULL'
+      remove_column :statuses, :sharekey
+    end
+  end
+
+  def down
+    add_column :statuses, :sharekey, :string
+    execute 'UPDATE statuses SET sharekey = s.key FROM (SELECT status_id, key FROM sharekeys) AS s WHERE statuses.id = s.id'
+    drop_table :sharekeys
+  end
+end