Skip to content

Commit

Permalink
fix: postgres perms script (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
k3yss authored Aug 29, 2024
1 parent 2766306 commit 51b0e42
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
17 changes: 6 additions & 11 deletions docs/pg-migration-guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ Before proceeding, please review the [known limitations](https://cloud.google.co

# Step 1: Configure Source Instance and create connection profile
- Decide upon a instance to upgrade:

- We are choosing the `rishi-pg14-volcano-staging-pg-a34e9984` instance, a PostgreSQL 14 instance managed via the `galoy-infra/modules/postgresql/gcp` Terraform module.

![decide-source](./assets/decide-source-instance.png)

- On the terraform file of the decided instance, enable the `prep_upgrade_as_source_db` flag

```hcl
Expand Down Expand Up @@ -150,17 +147,17 @@ terraform output --json migration_destination_database_creds
```sh
$ psql <value of connection string from above>
postgres=> ALTER ROLE cloudsqlexternalsync RENAME TO '<instance-admin-name>';
postgres=> ALTER ROLE "volcano-staging-pg-admin" PASSWORD '<source-instance-password>'
postgres=> ALTER ROLE "<instance-admin-name>" PASSWORD '<source-instance-password>';
```

#### Step 3.5.2
Manipulate the old state to reflect the new state by running the two scripts located at `galoy-infra/examples/gcp/bin`

```sh
$ ./terraform-db-swap.sh
$ ./terraform-db-swap.sh <main.tf directory> <module-name>
# This will ask for your terraform module name
# And swap the state between the newer and old instance
$ ./terraform-state-rm.sh
$ ./terraform-state-rm.sh <main.tf directory> <module-name>
# This will ask for your terraform module name, give it the same name as you gave before
# This will remove all the conflicting state which terraform will try to remove manually
```
Expand Down Expand Up @@ -193,8 +190,6 @@ module "postgresql" {
}
```



#### Step 3.5.4

Finally, do a
Expand All @@ -210,8 +205,8 @@ The destination instance should be exactly as with the source PostgreSQL instanc
Change the owners of the tables and schemas to the correct owner using the psql command:

```sh
#TODO after the dry run

#TODO | Need to do a dry run
./postgres-perms-update.sh
```

# Step 4: Promote the instance
Expand Down Expand Up @@ -253,7 +248,7 @@ Do a `terraform apply`

### Delete the Database Migration Service that we used for migration.
```sh
$ gcloud database-migration migration-jobs describe "test-job" --region=us-east1
$ gcloud database-migration migration-jobs delete "test-job" --region=us-east1
```

### Delete the source and external replica instance
Expand Down
5 changes: 3 additions & 2 deletions modules/postgresql/gcp/bin/postgres-perms-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ read -p "Enter PostgreSQL connection string: " PG_CON

PSQL_CMD="psql $PG_CON -d $DB_NAME -At -c"

$PSQL_CMD "ALTER DATABASE postgres OWNER TO cloudsqlsuperuser;"
$PSQL_CMD "ALTER SCHEMA public OWNER TO cloudsqlsuperuser;"

$PSQL_CMD "GRANT \"$NEW_OWNER\" TO \"postgres\";"
# Get list of all tables in the database
tables=$($PSQL_CMD "SELECT tablename FROM pg_tables WHERE schemaname = 'public';")
Expand All @@ -17,5 +20,3 @@ for table in $tables; do
done

echo "Ownership of all tables in $DB_NAME has been granted to $NEW_OWNER."

#$PSQL_CMD "ALTER SCHEMA public OWNER TO \"$NEW_OWNER\";"

0 comments on commit 51b0e42

Please sign in to comment.