# How to Add a Database to studio_api

# Setup the cluster in RDS

`<core team does some stuff>`

`<something something user permissions>`

# Setup the DB in studio_api

After DB setup, we should have (maybe) two database urls to setup:

- `DATABASE_URL_*` - This would be the raw password based login URL and is the simplest to setup that you use in most places
- `IAM_DATABASE_URL_*` - This one is optional, and would be for setting up IAM
- Note that you would have 2 versions of each of these URLs, one for staging, and one for prod
- For local dev testing, you won't have an IAM URL. You instead will instead just use a `DATABASE_URL_*` pointing to a new local DB `postgresql://suno:suno@localhost/<your new db>` . We'll get that local DB setup in later steps

### Add the secrets to AWS

1. Add the local `DATABASE_URL_*` to [studio-api-dev-envs](https://us-east-2.console.aws.amazon.com/secretsmanager/secret?name=studio-api-dev-envs&region=us-east-2) (prod AWS account)
2. Add both URLS to [studio-api-prod-envs](https://us-east-2.console.aws.amazon.com/secretsmanager/secret?name=studio-api-prod-envs&region=us-east-2) (prod AWS account)
3. Add both to [studio-api-staging-envs](https://us-east-2.console.aws.amazon.com/secretsmanager/secret?name=studio-api-staging-envs&region=us-east-2) (staging AWS account)

### Ensure all the env vars are added in code

⚠️ Remember, any time we're checking a url into git, it must always be the [`localhost`](http://localhost) url ⚠️

1. Add the secrets to the CDK
    1. If you need access to this DB from both `studio_api` and celery, add the secret [here](https://github.com/suno-ai/glockenspiel/blob/main/suno-cdk/lib/backend-infra-stack/secrets/studio-secrets.ts)
    2. If you just need access from `studio_api`, add it [here](https://github.com/suno-ai/glockenspiel/blob/main/suno-cdk/lib/backend-infra-stack/secrets/studio-api-service-only-secret.ts)
    3. **Deploy the CDK**
        1. We want to make sure the CDK gets access to these secrets before we actually attempt to use them
2. Misc places to add the `DATABASE_URL_*`env var
    1. I'm not entirely sure if the url is necessary in all these places, but I just added the env var to all the places we also added `DATABASE_URL`
    2. [billing e2e test](https://github.com/suno-ai/glockenspiel/blob/main/studio_api/studio_api/billing/e2e_tests/server.py)
    3. [studio_api_flute](https://github.com/suno-ai/glockenspiel/blob/main/studio_api_flute/cmd/app/.env.local)
3. Search for any other new/undocumented places in the codebase that use the current `DATABASE_URL` and add your URL there too

### Ensure proper DB creation for setup and testing

1. Ensure DB is created for testing
    1. Initialize the DB in [this config](https://github.com/suno-ai/glockenspiel/blob/main/.github/workflows/api-test-reusable.yml)

    ```yaml
    - name: Create <db name> database
    	run: |
    		PGPASSWORD=postgres psql -h localhost -U postgres -c "CREATE DATABASE <db name>;"
    ```

2. Create the new db in the project [setup script](https://github.com/suno-ai/glockenspiel/blob/main/setup.sh)

### Actually wire up the new DB for Django

Ok now that we have all the basic secret and env var config setup, we can actually setup django to use the DB.

1. Create the Django module that you want to route to the new DB.
2. Add a new [DB router](https://github.com/suno-ai/glockenspiel/blob/main/studio_api/studio_api/database_router.py#L1) similar to the `Suno2DBRouter` that directs queries for your new django module to the new DB.
3. Hook up the new DB and router in `settings.py`. You can see an example in [this PR](https://github.com/suno-ai/glockenspiel/pull/16066/files#diff-f9fe7f57e1212ab153032af5ce6191b404d112254ea6fd459a82b55943e79b9e)

### Misc Notes

- In order to access the DB in Django tests, you'll need update what databases are available in the conftest.py file in studio_api

### ‼️Remember to announce this new database

When you merge your new config, you will potentially break local testing of `studio_api` for people until they create the new db locally with `createdb <db name>`. Remember to warn people!
