Skip to content

fix: make auth SQL bootstrap idempotent#165

Open
bhudevbhanpuriya wants to merge 1 commit into
istSOS:mainfrom
bhudevbhanpuriya:fix/auth-bootstrap-idempotent
Open

fix: make auth SQL bootstrap idempotent#165
bhudevbhanpuriya wants to merge 1 commit into
istSOS:mainfrom
bhudevbhanpuriya:fix/auth-bootstrap-idempotent

Conversation

@bhudevbhanpuriya
Copy link
Copy Markdown
Contributor

@bhudevbhanpuriya bhudevbhanpuriya commented Apr 21, 2026

Summary

This PR makes the auth database bootstrap script idempotent.

In practice, that means database/istsos_auth.sql can be run more than once without failing just because the expected auth objects already exist.

Problem

The script previously assumed it was always running on a fresh database.

Some statements used plain creation commands such as:

ALTER TABLE ... ADD COLUMN ...
CREATE ROLE ...

Those commands succeed the first time, but fail on a later redeploy or retry because PostgreSQL reports that the column, role, or policy already exists.

What Changed

  • The initial administrator user insert now uses ON CONFLICT ("username") DO NOTHING.

    • Prevents duplicate admin-user insert failures on rerun.
  • Auth-related columns now use ADD COLUMN IF NOT EXISTS.

    • Prevents reruns from failing when user_id or commit_id already exists.
  • The user, guest, and sensor database roles are now created only if they do not already exist.

    • Avoids "role already exists" errors.
  • Anonymous RLS policies are now checked before creation.

    • Avoids duplicate policy creation errors on rerun.

Expected Result

Running database/istsos_auth.sql multiple times should no longer fail because of already-existing auth bootstrap objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant