Skip to content

Conversation

@mabels
Copy link
Contributor

@mabels mabels commented Jan 16, 2026

Summary

  • Fix duplicate CREATE INDEX statements when a table with indexes needs recreation
  • Track recreated tables and skip separate create_index statements (indexes already created in _moveDataStatements())
  • Add regression test with composite PK, uniqueIndex, and regular index

Problem

When running drizzle-kit push on a table that requires recreation (e.g., composite primary key changes), CREATE INDEX statements were emitted twice:

CREATE TABLE `__new_UserSlugBindings` (...);
INSERT INTO `__new_UserSlugBindings`(...) SELECT ... FROM `UserSlugBindings`;
DROP TABLE `UserSlugBindings`;
ALTER TABLE `__new_UserSlugBindings` RENAME TO `UserSlugBindings`;
CREATE UNIQUE INDEX `UserSlug_userSlug` ON `UserSlugBindings` (`userSlug`);
PRAGMA foreign_keys=ON;
CREATE UNIQUE INDEX `UserSlug_userSlug` ON `UserSlugBindings` (`userSlug`);  <-- duplicate!

Root Cause

Indexes were being generated from two separate places:

  1. _moveDataStatements() in libSqlPushUtils.ts/sqlitePushUtils.ts - generates index SQL during table recreation
  2. Separate create_index JSON statements from prepareLibSQLRecreateTable() that were also converted to SQL

Fix

Track which tables have been recreated using a Set<string> and skip create_index statements for those tables.

Test plan

  • All existing libsql push tests pass (19 tests)
  • All existing sqlite push tests pass (21 tests)
  • Added regression test that verifies:
    • First push: clean CREATE TABLE and CREATE INDEX (no ALTER/recreate)
    • Second push: no duplicate CREATE INDEX statements

When a table with indexes needed recreation (e.g., composite PK changes),
CREATE INDEX statements were being emitted twice:
- Once from _moveDataStatements() during table recreation
- Again from separate create_index JSON statements

This resulted in SQL like:
  CREATE UNIQUE INDEX `UserSlug_userSlug` ...
  PRAGMA foreign_keys=ON;
  CREATE UNIQUE INDEX `UserSlug_userSlug` ...  <-- duplicate!

Fix: Track which tables have been recreated and skip create_index
statements for those tables since indexes are already created in
_moveDataStatements().

Added regression test with UserSlugBindings table schema that has
composite primary key, uniqueIndex, and regular index.
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