Skip to content

feat: add user creation migration scripts for Hasura#83

Open
BigJohn-dev wants to merge 1 commit intosafetrustcr:mainfrom
BigJohn-dev:feat/copy-create_user-migration-into-infra-hasura
Open

feat: add user creation migration scripts for Hasura#83
BigJohn-dev wants to merge 1 commit intosafetrustcr:mainfrom
BigJohn-dev:feat/copy-create_user-migration-into-infra-hasura

Conversation

@BigJohn-dev
Copy link
Copy Markdown

@BigJohn-dev BigJohn-dev commented Mar 28, 2026

Closes #77

Summary

This PR adds the missing Hasura migration for the users table required by the Firebase auth sync flow.

Files Added

  • up.sql
  • down.sql

What it does

  • Creates public.users with:
    • id TEXT PRIMARY KEY (Firebase UID)
    • email TEXT NOT NULL
    • last_seen TIMESTAMP WITH TIME ZONE DEFAULT NOW()
  • Adds users_email_unique unique constraint on email
  • Creates idx_users_email index
  • Provides a clean rollback in down.sql

Testing

  1. Start services:
    • docker compose up -d
  2. Confirm Hasura migration applied:
    • Open http://localhost:8080/console
    • Verify users table exists under public
  3. Validate schema in Postgres:
    • Connect to the DB via psql
    • Run \d users
    • Expect:
      • id TEXT primary key
      • email TEXT unique
      • last_seen timestamptz
  4. Check index:
    • Run \di idx_users_email
  5. Optional:
    • docker compose down -v
    • docker compose up -d
    • Confirm fresh migration still applies cleanly

Notes

  • This migration is ordered to follow the existing UUID extension migration as required by the assignment.
  • It is intended to support backend auth sync and prevent login state from dropping user persistence.

Summary by CodeRabbit

  • Chores
    • Added database migration infrastructure with forward and rollback scripts to establish foundational backend data schema. Enables reliable database updates while maintaining system stability and supporting future feature development.

@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 28, 2026

@BigJohn-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

📝 Walkthrough

Walkthrough

This PR adds a Hasura database migration that creates a users table with a TEXT primary key for Firebase UIDs, a non-null email column with unique constraint, and a timestamp column for last login tracking. The migration includes both up and down scripts for forward and rollback operations.

Changes

Cohort / File(s) Summary
Users Table Migration
infra/hasura/migrations/safetrust/1731908676359_create_users/up.sql, infra/hasura/migrations/safetrust/1731908676359_create_users/down.sql
Introduces users table with id (Firebase UID), email (unique, non-null), and last_seen columns. Includes unique constraint and email index. Down script cleanly removes all dependent objects via CASCADE.

Possibly related issues

  • dApp-SafeTrust #78: The user_wallets migration references users(id) as a foreign key, making it directly dependent on this PR's users table creation.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐰 wiggles nose at the schema
A table blooms in the database ground,
Firebase UIDs nest safe and sound,
Emails unique, timestamps take flight—
The burrow of users springs into sight! ✨🏗️

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding user creation migration scripts for Hasura, which matches the changeset's purpose.
Linked Issues check ✅ Passed The PR successfully implements all requirements from issue #77: creates up.sql and down.sql with exact SQL structure, proper column definitions, constraints, and index as specified.
Out of Scope Changes check ✅ Passed The pull request contains only the two migration files (up.sql and down.sql) required by issue #77 with no extraneous changes outside the stated objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
infra/hasura/migrations/safetrust/1731908676359_create_users/up.sql (2)

8-12: Avoid double-indexing email; reuse one index for both uniqueness and lookup.

UNIQUE (email) already creates an index in Postgres, so idx_users_email adds redundant write/storage overhead. If you need the explicit idx_users_email name, create a unique index first and bind the constraint to it.

Proposed migration adjustment
 ALTER TABLE users
-    ADD CONSTRAINT users_email_unique UNIQUE (email);
+    ADD CONSTRAINT users_email_unique UNIQUE USING INDEX idx_users_email;

 -- Add indices for common queries
-CREATE INDEX idx_users_email ON users(email);
+CREATE UNIQUE INDEX idx_users_email ON users(email);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@infra/hasura/migrations/safetrust/1731908676359_create_users/up.sql` around
lines 8 - 12, The migration currently creates a UNIQUE constraint
users_email_unique which already creates an index, then adds a redundant CREATE
INDEX idx_users_email; remove the separate CREATE INDEX idx_users_email (or
alternatively create a UNIQUE INDEX first and attach the constraint to it with
ADD CONSTRAINT ... USING INDEX) so the email uniqueness index is not duplicated;
update the users ALTER TABLE/constraint/index lines accordingly around
users_email_unique and idx_users_email to either rely on the unique constraint's
index or to create a single unique index and bind the constraint to that index.

1-12: Schema-qualify objects in migrations to avoid search_path surprises.

Using bare users / idx_users_email can target the wrong schema in non-default environments. Prefer public.users and public.idx_users_email.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@infra/hasura/migrations/safetrust/1731908676359_create_users/up.sql` around
lines 1 - 12, Schema-qualify the objects to avoid search_path issues: change
CREATE TABLE users to CREATE TABLE public.users, change ALTER TABLE users to
ALTER TABLE public.users, and change CREATE INDEX idx_users_email to CREATE
INDEX public.idx_users_email (keep the constraint name users_email_unique as-is
or rename if you want schema-qualified naming); update references to the
table/index names in this migration (symbols: users, public.users,
users_email_unique, idx_users_email, public.idx_users_email).
infra/hasura/migrations/safetrust/1731908676359_create_users/down.sql (1)

5-8: DROP INDEX after DROP TABLE is redundant.

Dropping users already drops its indexes. Keeping line 8 is harmless due IF EXISTS, but unnecessary noise in rollback logic.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@infra/hasura/migrations/safetrust/1731908676359_create_users/down.sql` around
lines 5 - 8, The rollback drops the users table which already removes its
indexes, so remove the redundant DROP INDEX IF EXISTS idx_users_email CASCADE
line from the down migration; update the down SQL in 1731908676359_create_users
to only DROP TABLE IF EXISTS users CASCADE and remove the DROP INDEX statement
referring to idx_users_email.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@infra/hasura/migrations/safetrust/1731908676359_create_users/down.sql`:
- Around line 2-5: Remove the unnecessary CASCADEs and the redundant index drop:
delete the CASCADE modifiers from the DROP CONSTRAINT and DROP TABLE statements
for the users table (symbols: users, users_email_unique, DROP TABLE users) and
also remove the separate DROP INDEX IF EXISTS idx_users_email statement (the
table drop already removes its indexes), leaving only the simplified DROP
CONSTRAINT and DROP TABLE lines in down.sql.

---

Nitpick comments:
In `@infra/hasura/migrations/safetrust/1731908676359_create_users/down.sql`:
- Around line 5-8: The rollback drops the users table which already removes its
indexes, so remove the redundant DROP INDEX IF EXISTS idx_users_email CASCADE
line from the down migration; update the down SQL in 1731908676359_create_users
to only DROP TABLE IF EXISTS users CASCADE and remove the DROP INDEX statement
referring to idx_users_email.

In `@infra/hasura/migrations/safetrust/1731908676359_create_users/up.sql`:
- Around line 8-12: The migration currently creates a UNIQUE constraint
users_email_unique which already creates an index, then adds a redundant CREATE
INDEX idx_users_email; remove the separate CREATE INDEX idx_users_email (or
alternatively create a UNIQUE INDEX first and attach the constraint to it with
ADD CONSTRAINT ... USING INDEX) so the email uniqueness index is not duplicated;
update the users ALTER TABLE/constraint/index lines accordingly around
users_email_unique and idx_users_email to either rely on the unique constraint's
index or to create a single unique index and bind the constraint to that index.
- Around line 1-12: Schema-qualify the objects to avoid search_path issues:
change CREATE TABLE users to CREATE TABLE public.users, change ALTER TABLE users
to ALTER TABLE public.users, and change CREATE INDEX idx_users_email to CREATE
INDEX public.idx_users_email (keep the constraint name users_email_unique as-is
or rename if you want schema-qualified naming); update references to the
table/index names in this migration (symbols: users, public.users,
users_email_unique, idx_users_email, public.idx_users_email).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 359a23ac-9574-4fc6-909f-3c5712e0c043

📥 Commits

Reviewing files that changed from the base of the PR and between decc85f and 9c18213.

📒 Files selected for processing (2)
  • infra/hasura/migrations/safetrust/1731908676359_create_users/down.sql
  • infra/hasura/migrations/safetrust/1731908676359_create_users/up.sql

Comment on lines +2 to +5
ALTER TABLE IF EXISTS users
DROP CONSTRAINT IF EXISTS users_email_unique CASCADE;

DROP TABLE IF EXISTS users CASCADE;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

# First, explore the migration directory structure
find infra/hasura/migrations/safetrust -type f -name "*.sql" | head -20

Repository: safetrustcr/dApp-SafeTrust

Length of output: 207


🏁 Script executed:

# Read the full down.sql file to see the complete context
cat infra/hasura/migrations/safetrust/1731908676359_create_users/down.sql

Repository: safetrustcr/dApp-SafeTrust

Length of output: 285


🏁 Script executed:

# Read the corresponding up.sql file to understand what's being created
cat infra/hasura/migrations/safetrust/1731908676359_create_users/up.sql

Repository: safetrustcr/dApp-SafeTrust

Length of output: 401


🏁 Script executed:

# Search for any foreign key constraints or references to the users table in other migrations
rg "CONSTRAINT.*users|FOREIGN KEY.*users|references.*users" infra/hasura/migrations/ -i

Repository: safetrustcr/dApp-SafeTrust

Length of output: 319


🏁 Script executed:

# Check all migrations to understand dependencies and ordering
git ls-files infra/hasura/migrations/safetrust/ | grep -E '\.sql$' | sort

Repository: safetrustcr/dApp-SafeTrust

Length of output: 207


🏁 Script executed:

# Search for CASCADE patterns across all migrations
rg "DROP.*CASCADE|CASCADE" infra/hasura/migrations/ --type sql | head -30

Repository: safetrustcr/dApp-SafeTrust

Length of output: 419


Simplify rollback: remove unused CASCADE and fix DROP INDEX ordering.

The CASCADE clauses can be removed (no foreign key dependencies exist), but the actual issue is that DROP INDEX IF EXISTS idx_users_email CASCADE; runs after DROP TABLE, making it redundant—the table drop already removes its indexes. Remove the separate index drop or reorder before the table drop if needed for clarity.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@infra/hasura/migrations/safetrust/1731908676359_create_users/down.sql` around
lines 2 - 5, Remove the unnecessary CASCADEs and the redundant index drop:
delete the CASCADE modifiers from the DROP CONSTRAINT and DROP TABLE statements
for the users table (symbols: users, users_email_unique, DROP TABLE users) and
also remove the separate DROP INDEX IF EXISTS idx_users_email statement (the
table drop already removes its indexes), leaving only the simplified DROP
CONSTRAINT and DROP TABLE lines in down.sql.

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.

🏗️📦feat: copy create_users migration into infra/hasura/

1 participant