File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ exports . up = async function ( knex ) {
2+
3+ const password = process . env . DATABASE_READONLY_PASSWORD ;
4+ if ( ! password ) {
5+ throw new Error ( 'Unable to create harmony_read_only user: DATABASE_READONLY_PASSWORD variable is not set' ) ;
6+ }
7+
8+ const { rows } = await knex . raw (
9+ `SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = 'harmony_read_only'`
10+ ) ;
11+
12+ if ( rows . length === 0 ) {
13+ const escapedPassword = password . replace ( / ' / g, "''" ) ;
14+ await knex . raw ( `CREATE ROLE harmony_read_only WITH LOGIN PASSWORD '${ escapedPassword } '` ) ;
15+ }
16+
17+ await knex . raw ( `GRANT USAGE ON SCHEMA public TO harmony_read_only` ) ;
18+ await knex . raw ( `GRANT SELECT ON ALL TABLES IN SCHEMA public TO harmony_read_only` ) ;
19+ await knex . raw ( `ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO harmony_read_only` ) ;
20+ } ;
21+
22+ exports . down = async function ( knex ) {
23+
24+ const { rows } = await knex . raw (
25+ `SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = 'harmony_read_only'`
26+ ) ;
27+
28+ if ( rows . length === 0 ) {
29+ return ;
30+ }
31+
32+ await knex . raw ( `ALTER DEFAULT PRIVILEGES IN SCHEMA public REVOKE SELECT ON TABLES FROM harmony_read_only` ) ;
33+ await knex . raw ( `REVOKE SELECT ON ALL TABLES IN SCHEMA public FROM harmony_read_only` ) ;
34+ await knex . raw ( `REVOKE USAGE ON SCHEMA public FROM harmony_read_only` ) ;
35+ await knex . raw ( `DROP ROLE IF EXISTS harmony_read_only` ) ;
36+ } ;
Original file line number Diff line number Diff line change @@ -84,6 +84,9 @@ DATABASE_TYPE=postgres
8484# ignored, using a sqlite3 file instead
8585DATABASE_URL=postgresql://postgres:password@localhost:5432/postgres
8686
87+ # Password for read only database access
88+ DATABASE_READONLY_PASSWORD=changeme
89+
8790# Whether to use encryption when communicating with the database.
8891DB_USE_SSL=false
8992
You can’t perform that action at this time.
0 commit comments