From 0be9cb086b070858b041cd15ee149d1323952aab Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Sun, 27 Oct 2019 19:29:35 +0300 Subject: [PATCH 1/4] Add migration --- ...91027143434_add_defaults_to_all_tables.exs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs diff --git a/priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs b/priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs new file mode 100644 index 000000000..ab60f1313 --- /dev/null +++ b/priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs @@ -0,0 +1,52 @@ +defmodule Pleroma.Repo.Migrations.AddDefaultsToAllTables do + use Ecto.Migration + + def up do + execute("ALTER TABLE activities + ALTER COLUMN recipients SET DEFAULT ARRAY[]::character varying[]") + + execute("ALTER TABLE filters + ALTER COLUMN whole_word SET DEFAULT true") + + execute("ALTER TABLE push_subscriptions + ALTER COLUMN data SET DEFAULT '{}'::jsonb") + + execute(~s(ALTER TABLE users + ALTER COLUMN following SET DEFAULT ARRAY[]::character varying[], + ALTER COLUMN tags SET DEFAULT ARRAY[]::character varying[], + ALTER COLUMN notification_settings SET DEFAULT + '{"followers": true, "follows": true, "non_follows": true, "non_followers": true}'::jsonb)) + + # irreversible updates + + execute( + "UPDATE activities SET recipients = ARRAY[]::character varying[] WHERE recipients IS NULL" + ) + + execute("UPDATE filters SET whole_word = true WHERE whole_word IS NULL") + + execute("UPDATE push_subscriptions SET data = '{}'::jsonb WHERE data IS NULL") + + execute("UPDATE users SET following = ARRAY[]::character varying[] WHERE following IS NULL") + execute("UPDATE users SET tags = ARRAY[]::character varying[] WHERE tags IS NULL") + execute(~s(UPDATE users SET notification_settings = + '{"followers": true, "follows": true, "non_follows": true, "non_followers": true}'::jsonb + WHERE notification_settings = '{}'::jsonb)) + end + + def down do + execute("ALTER TABLE activities + ALTER COLUMN recipients DROP DEFAULT") + + execute("ALTER TABLE filters + ALTER COLUMN whole_word DROP DEFAULT") + + execute("ALTER TABLE push_subscriptions + ALTER COLUMN data DROP DEFAULT") + + execute("ALTER TABLE users + ALTER COLUMN following DROP DEFAULT, + ALTER COLUMN tags DROP DEFAULT, + ALTER COLUMN notification_settings SET DEFAULT '{}'::jsonb") + end +end From 4b0893631f2d9f157c200f2787fe5154dc0b811e Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Mon, 28 Oct 2019 19:16:19 +0300 Subject: [PATCH 2/4] Complete defaults --- ...91027143434_add_defaults_to_all_tables.exs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs b/priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs index ab60f1313..0d2794ad3 100644 --- a/priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs +++ b/priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs @@ -27,6 +27,25 @@ def up do execute("UPDATE push_subscriptions SET data = '{}'::jsonb WHERE data IS NULL") + execute("UPDATE users SET source_data = '{}'::jsonb where source_data IS NULL") + execute("UPDATE users SET note_count = 0 where note_count IS NULL") + execute("UPDATE users SET background = '{}'::jsonb where background IS NULL") + execute("UPDATE users SET follower_count = 0 where follower_count IS NULL") + + execute( + "UPDATE users SET unread_conversation_count = 0 where unread_conversation_count IS NULL" + ) + + execute( + ~s(UPDATE users SET email_notifications = '{"digest": false}'::jsonb where email_notifications IS NULL) + ) + + execute("UPDATE users SET default_scope = 'public' where default_scope IS NULL") + + execute( + "UPDATE users SET pleroma_settings_store = '{}'::jsonb where pleroma_settings_store IS NULL" + ) + execute("UPDATE users SET following = ARRAY[]::character varying[] WHERE following IS NULL") execute("UPDATE users SET tags = ARRAY[]::character varying[] WHERE tags IS NULL") execute(~s(UPDATE users SET notification_settings = From 435d220700c694f1312bf213d0591054a309489a Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Mon, 28 Oct 2019 19:17:50 +0300 Subject: [PATCH 3/4] Move setDefaultsToTables to past to run before notNull migrations --- ...all_tables.exs => 20191025143434_add_defaults_to_tables.exs} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename priv/repo/migrations/{20191027143434_add_defaults_to_all_tables.exs => 20191025143434_add_defaults_to_tables.exs} (97%) diff --git a/priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs b/priv/repo/migrations/20191025143434_add_defaults_to_tables.exs similarity index 97% rename from priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs rename to priv/repo/migrations/20191025143434_add_defaults_to_tables.exs index 0d2794ad3..d16ab19f9 100644 --- a/priv/repo/migrations/20191027143434_add_defaults_to_all_tables.exs +++ b/priv/repo/migrations/20191025143434_add_defaults_to_tables.exs @@ -1,4 +1,4 @@ -defmodule Pleroma.Repo.Migrations.AddDefaultsToAllTables do +defmodule Pleroma.Repo.Migrations.AddDefaultsToTables do use Ecto.Migration def up do From 8bb6da7cd604359afb5eeabeeac0207edff07ce3 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Wed, 30 Oct 2019 18:34:14 +0300 Subject: [PATCH 4/4] Remove following column from the migrations --- priv/repo/migrations/20191025143434_add_defaults_to_tables.exs | 3 --- priv/repo/migrations/20191026191910_set_not_null_for_users.exs | 2 -- 2 files changed, 5 deletions(-) diff --git a/priv/repo/migrations/20191025143434_add_defaults_to_tables.exs b/priv/repo/migrations/20191025143434_add_defaults_to_tables.exs index d16ab19f9..a5bc82335 100644 --- a/priv/repo/migrations/20191025143434_add_defaults_to_tables.exs +++ b/priv/repo/migrations/20191025143434_add_defaults_to_tables.exs @@ -12,7 +12,6 @@ def up do ALTER COLUMN data SET DEFAULT '{}'::jsonb") execute(~s(ALTER TABLE users - ALTER COLUMN following SET DEFAULT ARRAY[]::character varying[], ALTER COLUMN tags SET DEFAULT ARRAY[]::character varying[], ALTER COLUMN notification_settings SET DEFAULT '{"followers": true, "follows": true, "non_follows": true, "non_followers": true}'::jsonb)) @@ -46,7 +45,6 @@ def up do "UPDATE users SET pleroma_settings_store = '{}'::jsonb where pleroma_settings_store IS NULL" ) - execute("UPDATE users SET following = ARRAY[]::character varying[] WHERE following IS NULL") execute("UPDATE users SET tags = ARRAY[]::character varying[] WHERE tags IS NULL") execute(~s(UPDATE users SET notification_settings = '{"followers": true, "follows": true, "non_follows": true, "non_followers": true}'::jsonb @@ -64,7 +62,6 @@ def down do ALTER COLUMN data DROP DEFAULT") execute("ALTER TABLE users - ALTER COLUMN following DROP DEFAULT, ALTER COLUMN tags DROP DEFAULT, ALTER COLUMN notification_settings SET DEFAULT '{}'::jsonb") end diff --git a/priv/repo/migrations/20191026191910_set_not_null_for_users.exs b/priv/repo/migrations/20191026191910_set_not_null_for_users.exs index f145a89ab..9d8d0ccf8 100644 --- a/priv/repo/migrations/20191026191910_set_not_null_for_users.exs +++ b/priv/repo/migrations/20191026191910_set_not_null_for_users.exs @@ -8,7 +8,6 @@ def up do execute("UPDATE users SET follower_count = 0 WHERE follower_count IS NULL") execute("ALTER TABLE users - ALTER COLUMN following SET NOT NULL, ALTER COLUMN local SET NOT NULL, ALTER COLUMN source_data SET NOT NULL, ALTER COLUMN note_count SET NOT NULL, @@ -27,7 +26,6 @@ def up do def down do execute("ALTER TABLE users - ALTER COLUMN following DROP NOT NULL, ALTER COLUMN local DROP NOT NULL, ALTER COLUMN source_data DROP NOT NULL, ALTER COLUMN note_count DROP NOT NULL,