First
[anni] / priv / repo / migrations / 20191025143434_add_defaults_to_tables.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Repo.Migrations.AddDefaultsToTables do
6   use Ecto.Migration
7
8   def up do
9     execute("ALTER TABLE activities
10     ALTER COLUMN recipients SET DEFAULT ARRAY[]::character varying[]")
11
12     execute("ALTER TABLE filters
13     ALTER COLUMN whole_word SET DEFAULT true")
14
15     execute("ALTER TABLE push_subscriptions
16     ALTER COLUMN data SET DEFAULT '{}'::jsonb")
17
18     execute(~s(ALTER TABLE users
19     ALTER COLUMN tags SET DEFAULT ARRAY[]::character varying[],
20     ALTER COLUMN notification_settings SET DEFAULT
21       '{"followers": true, "follows": true, "non_follows": true, "non_followers": true}'::jsonb))
22
23     # irreversible updates
24
25     execute(
26       "UPDATE activities SET recipients = ARRAY[]::character varying[] WHERE recipients IS NULL"
27     )
28
29     execute("UPDATE filters SET whole_word = true WHERE whole_word IS NULL")
30
31     execute("UPDATE push_subscriptions SET data = '{}'::jsonb WHERE data IS NULL")
32
33     execute("UPDATE users SET source_data = '{}'::jsonb where source_data IS NULL")
34     execute("UPDATE users SET note_count = 0 where note_count IS NULL")
35     execute("UPDATE users SET background = '{}'::jsonb where background IS NULL")
36     execute("UPDATE users SET follower_count = 0 where follower_count IS NULL")
37
38     execute(
39       "UPDATE users SET unread_conversation_count = 0 where unread_conversation_count IS NULL"
40     )
41
42     execute(
43       ~s(UPDATE users SET email_notifications = '{"digest": false}'::jsonb where email_notifications IS NULL)
44     )
45
46     execute("UPDATE users SET default_scope = 'public' where default_scope IS NULL")
47
48     execute(
49       "UPDATE users SET pleroma_settings_store = '{}'::jsonb where pleroma_settings_store IS NULL"
50     )
51
52     execute("UPDATE users SET tags = ARRAY[]::character varying[] WHERE tags IS NULL")
53     execute(~s(UPDATE users SET notification_settings =
54       '{"followers": true, "follows": true, "non_follows": true, "non_followers": true}'::jsonb
55       WHERE notification_settings = '{}'::jsonb))
56   end
57
58   def down do
59     execute("ALTER TABLE activities
60     ALTER COLUMN recipients DROP DEFAULT")
61
62     execute("ALTER TABLE filters
63     ALTER COLUMN whole_word DROP DEFAULT")
64
65     execute("ALTER TABLE push_subscriptions
66     ALTER COLUMN data DROP DEFAULT")
67
68     execute("ALTER TABLE users
69     ALTER COLUMN tags DROP DEFAULT,
70     ALTER COLUMN notification_settings SET DEFAULT '{}'::jsonb")
71   end
72 end