Closed rocketnova closed 3 weeks ago
After discussing with @lorenyu offline, we decided that a better and simpler approach is simply to hardcode the schema name. This reduces unnecessary logic. We will treat the database name, the schema name, the database user/role name, and the database migrator user/role name as special and hardcode them. Closing this PR in favor of a forthcoming PR.
Ticket
N/A
Changes
Context for reviewers
Per the documentation, PostgreSQL identifiers should be letters, underscores, digits, or dollar signs:
PostgreSQL allows identifiers to include arbitrary characters if the identifier is surrounded by double quotes:
The infra template's directory naming convention uses hyphens (
-
) to separate multi-word names (e.g.app-config
,build-repository
). Theapp-config
uses the application directory to generate thelocal.app_name
which is used throughout the configuration.When a project uses the infra template and renames the application directory to a multi-word, hyphenated name, such as
flowable-rest
, this will cause issues unless every instance of the database and schema names is double quoted. Even though we are properly using the pg8000identifier
in the role manager, while working on the platform-test-rails repo, I still ran into issues with the following lines because my application uses a hyphenated name (i.e.app-rails
):https://github.com/navapbc/template-infra/blob/6f8464ef7ff1cb20a263d0a68705a05e7c3f3206/infra/modules/database/role_manager/manage.py#L104
https://github.com/navapbc/template-infra/blob/6f8464ef7ff1cb20a263d0a68705a05e7c3f3206/infra/modules/database/role_manager/check.py#L32
This PR proposes to protect the database from hyphens by modifying the database name and the schema name to use underscores instead of hyphens.
Breaking change
⚠️ Warning! This could potentially be a breaking change for any existing projects that use hyphenated database names and have worked around the issue!
Testing
Tested in https://github.com/navapbc/platform-test/pull/104/files:
app-hyphen
rldb
A screenshot showing that the database name is correctly using an underscore when creating the database resources:![CleanShot 2024-06-14 at 14 39 05@2x](https://github.com/navapbc/template-infra/assets/67701/b864e7be-2b0e-4dc8-ad61-84423b034be9)
Snippet showing successfully using an underscore name when creating the roles:
A snippet showing successfully checking roles when using the underscore name: