Repository navigation
fix: resolve index generation bugs (Issue #21 + DropIndex reversibility) - #24
Merged
Merged
Conversation
This commit fixes three critical bugs affecting index generation and migration reversibility: **Issue #21: @@index not generated in migrations** - Root cause: Differ only created CreateTable operation for new tables, without separate CreateIndex operations - Fix: Generate CreateIndex operations for each index when creating new tables (differ.go:148-155) - Impact: Indexes are now correctly included in migrations for new tables **Bug #2: Migration builder loses indexes from history** - Root cause: applyCreateIndex modified table.Indexes but didn't save table back to map (Go value semantics) - Fix: Add b.tables[stmt.Table] = table after modifying indexes (builder.go:212) - Impact: Schema loaded from migration history now preserves indexes correctly **Bug #3: DropIndex with unnamed indexes generates invalid SQL** - Root cause: DropIndex operation only stored Name (empty for auto-generated indexes) - Fix: Add Index field to DropIndex struct to store full index definition (operation.go:230) - Fix: Populate Index object when creating DropIndex operations (differ.go:404) - Fix: Auto-generate index name from columns when Name is empty (generator.go:268-274) - Fix: Generate CREATE INDEX for reverse DropIndex operations (generator.go:384-405) - Impact: DROP INDEX now generates valid SQL and fully reversible Down migrations **Changes:** - internal/diff/differ.go: Generate CreateIndex ops for new tables, populate Index in DropIndex - internal/diff/operation.go: Add Index field to DropIndex struct - internal/migration/builder.go: Save modified table back to map after applying index - internal/prisma/converter.go: Extract index name from @@index attributes - internal/sql/postgres/generator.go: Auto-generate index names, implement reversible DROP INDEX - internal/sql/postgres/generator_reverse_test.go: Update test expectations for reversible DropIndex **Tests added:** - internal/diff/differ_dropindex_test.go: Unit tests for DropIndex with unnamed/named indexes - internal/sql/postgres/generator_dropindex_test.go: Unit tests for DROP INDEX SQL generation and reverse - internal/prisma/converter_test.go: Test for index name extraction - internal/diff/differ_test.go: Tests for new tables with indexes **Verification:** - All unit tests pass - E2E manual testing confirms indexes generated correctly - Down migrations fully reversible Fixes #21 Co-authored-by: Claude (Anthropic) Signed-off-by: Phathdt <phathdt189@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes three critical bugs affecting index generation and migration reversibility.
Bugs Fixed
🔴 Issue #21: @@index not generated in migrations
Root cause: Differ only created CreateTable operation for new tables, without separate CreateIndex operations.
Fix: Generate CreateIndex operations for each index when creating new tables.
Impact: Indexes are now correctly included in migrations for new tables.
🔴 Bug #2: Migration builder loses indexes from history
Root cause: applyCreateIndex modified table.Indexes but didn't save table back to map (Go value semantics issue).
Fix: Add
b.tables[stmt.Table] = tableafter modifying indexes.Impact: Schema loaded from migration history now preserves indexes correctly.
🔴 Bug #3: DropIndex with unnamed indexes generates invalid SQL
Root cause: DropIndex operation only stored Name (empty for auto-generated indexes), resulting in
DROP INDEX ;(invalid SQL).Fixes:
Impact: DROP INDEX now generates valid SQL and fully reversible Down migrations.
Changes
internal/diff/differ.go: Generate CreateIndex ops for new tables, populate Index in DropIndexinternal/diff/operation.go: Add Index field to DropIndex structinternal/migration/builder.go: Save modified table back to map after applying indexinternal/prisma/converter.go: Extract index name from @@index attributesinternal/sql/postgres/generator.go: Auto-generate index names, implement reversible DROP INDEXinternal/sql/postgres/generator_reverse_test.go: Update test expectations for reversible DropIndexTests Added
internal/diff/differ_dropindex_test.go: Unit tests for DropIndex with unnamed/named indexesinternal/sql/postgres/generator_dropindex_test.go: Unit tests for DROP INDEX SQL generation and reverseinternal/prisma/converter_test.go: Test for index name extractioninternal/diff/differ_test.go: Tests for new tables with indexesVerification
Example
Before (Bug):
After (Fixed):
Closes #21