Repository navigation
fix: use k8s postgres for paperclip, remove docker-postgres dep #1347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,26 +4,38 @@ set -euo pipefail | |||||||||||||||||||||||||
| INSTANCE_DIR="@instance_dir@" | ||||||||||||||||||||||||||
| CONFIG="${INSTANCE_DIR}/config.json" | ||||||||||||||||||||||||||
| TEMPLATE="@template@" | ||||||||||||||||||||||||||
| ENV_FILE="${HOME}/dotfiles/.env" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Source .env if it exists (for DATABASE_URL) | ||||||||||||||||||||||||||
| if [ -f "$ENV_FILE" ]; then | ||||||||||||||||||||||||||
| set -a | ||||||||||||||||||||||||||
| # shellcheck source=/dev/null | ||||||||||||||||||||||||||
| . "$ENV_FILE" | ||||||||||||||||||||||||||
| set +a | ||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| mkdir -p "${INSTANCE_DIR}" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Use DATABASE_URL from env if set, otherwise use the Nix default | ||||||||||||||||||||||||||
| DB_CONNECTION="${DATABASE_URL:-@database_connection_string@}" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| # On Kyber, postgres mode requires a non-empty connection string. | |
| # Fail early with a clear error instead of rendering an invalid config. | |
| # shellcheck disable=SC2050 | |
| if [ "@is_kyber@" = "true" ] && [ -z "$DB_CONNECTION" ]; then | |
| echo "error: DATABASE_URL is required on Kyber when using postgres, but no non-empty value was provided." >&2 | |
| echo "Set DATABASE_URL in ${ENV_FILE} or provide a non-empty Nix database connection string." >&2 | |
| exit 1 | |
| fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Escape DB_CONNECTION before using it in the sed replacement; unescaped &/| can corrupt the rendered database URL in config.json.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At config/paperclip/hydrate.sh, line 24:
<comment>Escape `DB_CONNECTION` before using it in the sed replacement; unescaped `&`/`|` can corrupt the rendered database URL in `config.json`.</comment>
<file context>
@@ -4,26 +4,38 @@ set -euo pipefail
@sed@ \
-e "s|__DATABASE_MODE__|@database_mode@|g" \
- -e "s|__DATABASE_CONNECTION_STRING__|@database_connection_string@|g" \
+ -e "s|__DATABASE_CONNECTION_STRING__|${DB_CONNECTION}|g" \
-e "s|__DEPLOYMENT_MODE__|@deployment_mode@|g" \
-e "s|__HOST__|@host@|g" \
</file context>
| -e "s|__DATABASE_CONNECTION_STRING__|${DB_CONNECTION}|g" \ | |
| -e "s|__DATABASE_CONNECTION_STRING__|$(printf '%s' "$DB_CONNECTION" | sed 's/[&|]/\\&/g')|g" \ |
Copilot
AI
Apr 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DB_CONNECTION is interpolated directly into the sed replacement. If DATABASE_URL contains &, backslashes, or the delimiter, sed will treat those specially and can corrupt the generated JSON (e.g., query params like ...&sslmode=...). Escape the replacement value (or switch to a templating approach that safely JSON-encodes the value) before substituting into the template.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Preserve URL query parameters when rewriting the DB name to /postgres; currently required options can be dropped from DB_BASE.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At config/paperclip/hydrate.sh, line 36:
<comment>Preserve URL query parameters when rewriting the DB name to `/postgres`; currently required options can be dropped from `DB_BASE`.</comment>
<file context>
@@ -4,26 +4,38 @@ set -euo pipefail
- echo "Created paperclip database" >&2
+if [ "@is_kyber@" = "true" ] && [ -n "$DB_CONNECTION" ]; then
+ if command -v psql >/dev/null 2>&1; then
+ DB_BASE="${DB_CONNECTION%/*}/postgres"
+ if ! psql "$DB_BASE" -tAc "SELECT 1 FROM pg_database WHERE datname='paperclip'" 2>/dev/null | grep -q 1; then
+ psql "$DB_BASE" -c "CREATE DATABASE paperclip" 2>/dev/null && echo "Created paperclip database" >&2
</file context>
| DB_BASE="${DB_CONNECTION%/*}/postgres" | |
| DB_NO_QUERY="${DB_CONNECTION%%\?*}" | |
| DB_QUERY="${DB_CONNECTION#"$DB_NO_QUERY"}" | |
| DB_BASE="${DB_NO_QUERY%/*}/postgres${DB_QUERY}" |
Copilot
AI
Apr 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DB_BASE="${DB_CONNECTION%/*}/postgres" drops any query string/options from DATABASE_URL (e.g., ?sslmode=require), which can make the subsequent psql connection fail even though the main connection string works. Consider preserving connection options when deriving the base DB connection (or parse the URL more robustly).
| DB_BASE="${DB_CONNECTION%/*}/postgres" | |
| DB_QUERY="" | |
| DB_WITHOUT_QUERY="$DB_CONNECTION" | |
| if [ "${DB_CONNECTION#*\?}" != "$DB_CONNECTION" ]; then | |
| DB_QUERY="?${DB_CONNECTION#*\?}" | |
| DB_WITHOUT_QUERY="${DB_CONNECTION%%\?*}" | |
| fi | |
| DB_BASE="${DB_WITHOUT_QUERY%/*}/postgres${DB_QUERY}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for constructing DB_BASE by stripping the last component of the URL is fragile. If the DATABASE_URL contains query parameters (e.g., ?sslmode=require), they will be lost when appending /postgres, which might cause connection failures if the server requires those parameters to connect.
A more robust approach is to use the full connection string and override the database name using the -d flag in psql. According to the psql documentation, the -d flag overrides the database name specified in a connection URI, preserving all other parameters like host, port, and options.
| DB_BASE="${DB_CONNECTION%/*}/postgres" | |
| if ! psql "$DB_BASE" -tAc "SELECT 1 FROM pg_database WHERE datname='paperclip'" 2>/dev/null | grep -q 1; then | |
| psql "$DB_BASE" -c "CREATE DATABASE paperclip" 2>/dev/null && echo "Created paperclip database" >&2 | |
| if ! psql "$DB_CONNECTION" -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='paperclip'" 2>/dev/null | grep -q 1; then | |
| psql "$DB_CONNECTION" -d postgres -c "CREATE DATABASE paperclip" 2>/dev/null && echo "Created paperclip database" >&2 | |
| fi |
Copilot
AI
Apr 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If psql is not installed, database auto-provisioning is silently skipped. Since this PR relies on provisioning against the k8s Postgres, consider emitting a warning when @is_kyber@ is true but psql is missing (or ensure psql is available via the Nix environment) so failures are discoverable.
| fi | |
| fi | |
| else | |
| echo "Warning: psql is not installed; skipping automatic paperclip database provisioning for kyber deployment" >&2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,18 @@ The output should include 'set -euo pipefail' | |
| End | ||
| End | ||
|
|
||
| Describe 'secret loading' | ||
| It 'reads DATABASE_URL from dotfiles .env' | ||
| When run bash -c "grep 'DATABASE_URL' '$SCRIPT'" | ||
| The output should include 'DATABASE_URL' | ||
| End | ||
|
|
||
| It 'sources the env file' | ||
| When run bash -c "grep 'ENV_FILE' '$SCRIPT'" | ||
| The output should include 'dotfiles/.env' | ||
| End | ||
| End | ||
|
|
||
| Describe 'config generation' | ||
| It 'uses sed to substitute values in template' | ||
| When run bash -c "grep '@sed@' '$SCRIPT'" | ||
|
|
@@ -44,13 +56,8 @@ End | |
| End | ||
|
|
||
| Describe 'database provisioning' | ||
| It 'creates paperclip database on docker-postgres' | ||
| When run bash -c "grep 'createdb' '$SCRIPT'" | ||
| The output should include 'paperclip' | ||
| End | ||
|
|
||
| It 'checks if database already exists before creating' | ||
| When run bash -c "grep 'grep -qw paperclip' '$SCRIPT'" | ||
| It 'creates paperclip database if missing' | ||
| When run bash -c "grep 'CREATE DATABASE' '$SCRIPT'" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: This assertion doesn't actually test the "if missing" condition; it only checks that a CREATE DATABASE statement exists. The spec can pass even if database creation is unconditional. Prompt for AI agents |
||
| The output should include 'paperclip' | ||
| End | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
database_modeispostgreson Kyber butdatabase_connection_stringis now always an empty string. UnlessDATABASE_URLis guaranteed to be present at hydrate time, this produces an invalid config. Either provide a sane Kyber default here or enforce/validate thatDATABASE_URLis set (and fail/warn clearly) during hydration.