[PR #6911] [CLOSED] Fix migration ordering when organization plugin is used with teams en… #32562

Closed
opened 2026-04-17 23:21:00 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6911
Author: @Adityakk9031
Created: 12/20/2025
Status: Closed

Base: canaryHead: 6832


📝 Commits (1)

  • 733429b Fix migration ordering when organization plugin is used with teams enabled

📊 Changes

4 files changed (+246 additions, -60 deletions)

View changed files

📝 packages/better-auth/src/db/get-migration-schema.test.ts (+31 -0)
📝 packages/better-auth/src/db/get-migration.ts (+170 -60)
📝 packages/better-auth/src/utils/index.ts (+1 -0)
packages/better-auth/src/utils/sort-tables-by-dependencies.ts (+44 -0)

📄 Description

This PR fixes the issue reported in:

👉 [organization] Migration fails: team table created before organization table #6832

When using the organization plugin with teams: { enabled: true }, the migration command fails on a fresh database.

What was happening

team table has a foreign key reference to organization

Migration engine was creating team table first

PostgreSQL throws error:

relation "organization" does not exist

Root cause

Plugin tables do not have explicit migration order

Both organization and team tables default to order: Infinity

Migration relied on array order, which is not dependency-aware

Result: foreign key table created before parent table

What this PR does
1. Adds dependency-aware table sorting

Introduces a new utility:

sortTablesByDependencies()

This function:

Builds dependency graph from fields[].references.model

Ensures parent tables are created before dependent tables

Safely handles missing tables and circular references

2. Adds unit test (no DB required)

New test verifies correct ordering:

organization → team

This test:

Reproduces the exact failure scenario from #6832

Runs without PostgreSQL

Prevents regression in future plugin changes

Files added / changed

packages/better-auth/src/utils/sort-tables-by-dependencies.ts

packages/better-auth/src/utils/index.ts

packages/better-auth/src/migrations/getmigration.test.ts

Result

organization table is always created before team

npx @better-auth/cli migrate works on empty databases

Fix applies to all plugins with foreign key dependencies

Related Issues

Fixes: #6832

Related: #5473 (dynamicAccessControl ordering issue)


Summary by cubic

Fixes #6832. Ensures migrations create parent tables before dependents, so organization is created before team when the organization plugin runs with teams enabled.

  • Bug Fixes
    • Added dependency-aware table sorting in get-migration (topological sort that respects numeric order; warns and falls back if cycles).
    • Introduced sortTablesByDependencies utility and a unit test verifying organization → team ordering.

Written for commit 733429b8b8. Summary will update automatically on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/6911 **Author:** [@Adityakk9031](https://github.com/Adityakk9031) **Created:** 12/20/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `6832` --- ### 📝 Commits (1) - [`733429b`](https://github.com/better-auth/better-auth/commit/733429b8b8c79dbc6714dfa7ecce173a92d35cc9) Fix migration ordering when organization plugin is used with teams enabled ### 📊 Changes **4 files changed** (+246 additions, -60 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/get-migration-schema.test.ts` (+31 -0) 📝 `packages/better-auth/src/db/get-migration.ts` (+170 -60) 📝 `packages/better-auth/src/utils/index.ts` (+1 -0) ➕ `packages/better-auth/src/utils/sort-tables-by-dependencies.ts` (+44 -0) </details> ### 📄 Description This PR fixes the issue reported in: 👉 [organization] Migration fails: team table created before organization table #6832 When using the organization plugin with teams: { enabled: true }, the migration command fails on a fresh database. What was happening team table has a foreign key reference to organization Migration engine was creating team table first PostgreSQL throws error: relation "organization" does not exist Root cause Plugin tables do not have explicit migration order Both organization and team tables default to order: Infinity Migration relied on array order, which is not dependency-aware Result: foreign key table created before parent table What this PR does ✅ 1. Adds dependency-aware table sorting Introduces a new utility: sortTablesByDependencies() This function: Builds dependency graph from fields[].references.model Ensures parent tables are created before dependent tables Safely handles missing tables and circular references ✅ 2. Adds unit test (no DB required) New test verifies correct ordering: organization → team This test: Reproduces the exact failure scenario from #6832 Runs without PostgreSQL Prevents regression in future plugin changes Files added / changed packages/better-auth/src/utils/sort-tables-by-dependencies.ts packages/better-auth/src/utils/index.ts packages/better-auth/src/migrations/getmigration.test.ts Result organization table is always created before team npx @better-auth/cli migrate works on empty databases Fix applies to all plugins with foreign key dependencies Related Issues Fixes: #6832 Related: #5473 (dynamicAccessControl ordering issue) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes #6832. Ensures migrations create parent tables before dependents, so organization is created before team when the organization plugin runs with teams enabled. - **Bug Fixes** - Added dependency-aware table sorting in get-migration (topological sort that respects numeric order; warns and falls back if cycles). - Introduced sortTablesByDependencies utility and a unit test verifying organization → team ordering. <sup>Written for commit 733429b8b8c79dbc6714dfa7ecce173a92d35cc9. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-17 23:21:00 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#32562