Merge branch 'develop' into fix/configdb-otp-instructions
[akkoma] / docs / configuration / howto_database_config.md
1 # How to activate Pleroma in-database configuration
2 ## Explanation
3
4 The configuration of Pleroma has traditionally been managed with a config file, e.g. `config/prod.secret.exs`. This method requires a restart of the application for any configuration changes to take effect. We have made it possible to control most settings in the AdminFE interface after running a migration script.
5
6 ## Migration to database config
7
8 1. Run the mix task to migrate to the database. You'll receive some debugging output and a few messages informing you of what happened.
9
10 **Source:**
11
12 ```
13 $ mix pleroma.config migrate_to_db
14 ```
15
16 or
17
18 **OTP:**
19
20 *Note: OTP users need Pleroma to be running for `pleroma_ctl` commands to work*
21
22 ```
23 $ ./bin/pleroma_ctl config migrate_to_db
24 ```
25
26 ```
27 10:04:34.155 [debug] QUERY OK source="config" db=1.6ms decode=2.0ms queue=33.5ms idle=0.0ms
28 SELECT c0."id", c0."key", c0."group", c0."value", c0."inserted_at", c0."updated_at" FROM "config" AS c0 []
29 Migrating settings from file: /home/pleroma/config/dev.secret.exs
30
31 10:04:34.240 [debug] QUERY OK db=4.5ms queue=0.3ms idle=92.2ms
32 TRUNCATE config; []
33
34 10:04:34.244 [debug] QUERY OK db=2.8ms queue=0.3ms idle=97.2ms
35 ALTER SEQUENCE config_id_seq RESTART; []
36
37 10:04:34.256 [debug] QUERY OK source="config" db=0.8ms queue=1.4ms idle=109.8ms
38 SELECT c0."id", c0."key", c0."group", c0."value", c0."inserted_at", c0."updated_at" FROM "config" AS c0 WHERE ((c0."group" = $1) AND (c0."key" = $2)) [":pleroma", ":instance"]
39
40 10:04:34.292 [debug] QUERY OK db=2.6ms queue=1.7ms idle=137.7ms
41 INSERT INTO "config" ("group","key","value","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5) RETURNING "id" [":pleroma", ":instance", <<131, 108, 0, 0, 0, 1, 104, 2, 100, 0, 4, 110, 97, 109, 101, 109, 0, 0, 0, 7, 66, 108, 101, 114, 111, 109, 97, 106>>, ~N[2020-07-12 15:04:34], ~N[2020-07-12 15:04:34]]
42 Settings for key instance migrated.
43 Settings for group :pleroma migrated.
44 ```
45
46 2. It is recommended to backup your config file now.
47
48 ```
49 cp config/dev.secret.exs config/dev.secret.exs.orig
50 ```
51
52 3. Edit your Pleroma config to enable database configuration:
53
54 ```
55 config :pleroma, configurable_from_database: true
56 ```
57
58 4. ⚠️ **THIS IS NOT REQUIRED** ⚠️
59
60 Now you can edit your config file and strip it down to the only settings which are not possible to control in the database. e.g., the Postgres (Repo) and webserver (Endpoint) settings cannot be controlled in the database because the application needs the settings to start up and access the database.
61
62 Any settings in the database will override those in the config file, but you may find it less confusing if the setting is only declared in one place.
63
64 A non-exhaustive list of settings that are only possible in the config file include the following:
65
66 * config :pleroma, Pleroma.Web.Endpoint
67 * config :pleroma, Pleroma.Repo
68 * config :pleroma, configurable\_from\_database
69 * config :pleroma, :database, rum_enabled
70 * config :pleroma, :connections_pool
71
72 Here is an example of a server config stripped down after migration:
73
74 ```
75 use Mix.Config
76
77 config :pleroma, Pleroma.Web.Endpoint,
78 url: [host: "cool.pleroma.site", scheme: "https", port: 443]
79
80 config :pleroma, Pleroma.Repo,
81 adapter: Ecto.Adapters.Postgres,
82 username: "pleroma",
83 password: "MySecretPassword",
84 database: "pleroma_prod",
85 hostname: "localhost"
86
87 config :pleroma, configurable_from_database: true
88 ```
89
90 5. Restart your instance and you can now access the Settings tab in AdminFE.
91
92
93 ## Reverting back from database config
94
95 1. Run the mix task to migrate back from the database. You'll receive some debugging output and a few messages informing you of what happened.
96
97 **Source:**
98
99 ```
100 $ mix pleroma.config migrate_from_db
101 ```
102
103 or
104
105 **OTP:**
106
107 ```
108 $ ./bin/pleroma_ctl config migrate_from_db
109 ```
110
111 ```
112 10:26:30.593 [debug] QUERY OK source="config" db=9.8ms decode=1.2ms queue=26.0ms idle=0.0ms
113 SELECT c0."id", c0."key", c0."group", c0."value", c0."inserted_at", c0."updated_at" FROM "config" AS c0 []
114
115 10:26:30.659 [debug] QUERY OK source="config" db=1.1ms idle=80.7ms
116 SELECT c0."id", c0."key", c0."group", c0."value", c0."inserted_at", c0."updated_at" FROM "config" AS c0 []
117 Database configuration settings have been saved to config/dev.exported_from_db.secret.exs
118 ```
119
120 2. Remove `config :pleroma, configurable_from_database: true` from your config. The in-database configuration still exists, but it will not be used. Future migrations will erase the database config before importing your config file again.
121
122 3. Restart your instance.
123
124 ## Debugging
125
126 ### Clearing database config
127 You can clear the database config by truncating the `config` table in the database. e.g.,
128
129 ```
130 psql -d pleroma_dev
131 pleroma_dev=# TRUNCATE config;
132 TRUNCATE TABLE
133 ```
134
135 Additionally, every time you migrate the configuration to the database the config table is automatically truncated to ensure a clean migration.
136
137 ### Manually removing a setting
138 If you encounter a situation where the server cannot run properly because of an invalid setting in the database and this is preventing you from accessing AdminFE, you can manually remove the offending setting if you know which one it is.
139
140 e.g., here is an example showing a minimal configuration in the database. Only the `config :pleroma, :instance` settings are in the table:
141
142 ```
143 psql -d pleroma_dev
144 pleroma_dev=# select * from config;
145 id | key | value | inserted_at | updated_at | group
146 ----+-----------+------------------------------------------------------------+---------------------+---------------------+----------
147 1 | :instance | \x836c0000000168026400046e616d656d00000007426c65726f6d616a | 2020-07-12 15:33:29 | 2020-07-12 15:33:29 | :pleroma
148 (1 row)
149 pleroma_dev=# delete from config where key = ':instance' and group = ':pleroma';
150 DELETE 1
151 ```
152
153 Now the `config :pleroma, :instance` settings have been removed from the database.