Skip to content

Conversation

@danlapid
Copy link

@danlapid danlapid commented Jan 5, 2026

Fixes #4928
This has already been tested on my own database and it did fix the issues I was having with migrations not being applied.

The __drizzle_migrations table was being created with PostgreSQL syntax (SERIAL PRIMARY KEY) instead of proper SQLite syntax (integer PRIMARY KEY AUTOINCREMENT).
While SQLite's flexible typing allowed the table to be created, the id column was stored with type "SERIAL" rather than "INTEGER", meaning it wouldn't behave as a proper auto-incrementing integer primary key.

Added tests to verify the migrations table schema uses correct SQLite syntax in all SQLite test files.
The tests were validated to indeed fail before this change.

As to existing deployments that have a broken migrations table we have four options as I see it:

  1. Do nothing - Existing DBs technically still work because SQLite is permissive. Inserts without an id value will get NULL for the id column, but the table still functions.
  2. Detect and warn - Check the schema after table creation and log a warning if it's wrong:
const tableInfo = session.all(sql`PRAGMA table_info(${migrationsTable})`);
const idCol = tableInfo.find(c => c.name === 'id');
if (idCol?.type.toLowerCase() !== 'integer') {
  console.warn('Warning: __drizzle_migrations table has incorrect schema...');
}
  1. Detect and auto-migrate - Recreate the table with correct schema:
// If table exists with wrong schema, migrate it
// 1. Create temp table with correct schema
// 2. Copy data
// 3. Drop old table
// 4. Rename temp to original
  1. Document manual fix - Users can run:
CREATE TABLE __drizzle_migrations_new (
  id integer PRIMARY KEY AUTOINCREMENT,
  hash text NOT NULL,
  created_at numeric
);
INSERT INTO __drizzle_migrations_new SELECT * FROM __drizzle_migrations;
DROP TABLE __drizzle_migrations;
ALTER TABLE __drizzle_migrations_new RENAME TO __drizzle_migrations;

At the moment this PR does 1+4, I can do 2/3 as well but I chose not to do it for the initial review part because I couldn't find existing examples where either option was chose.

Thanks for taking the time to review this.

The __drizzle_migrations table was being created with PostgreSQL syntax
(SERIAL PRIMARY KEY) instead of proper SQLite syntax (integer PRIMARY KEY
AUTOINCREMENT). While SQLite's flexible typing allowed the table to be
created, the id column was stored with type "SERIAL" rather than "INTEGER",
meaning it wouldn't behave as a proper auto-incrementing integer primary key.

Added tests to verify the migrations table schema uses correct SQLite
syntax in all SQLite test files.
The tests were validated to indeed fail before this change.
@danlapid danlapid force-pushed the dlapid/fix_sqlite_migrations_table branch from 4b58b8a to 16f3b1b Compare January 5, 2026 20:13
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.

The __drizzle_migrations table looks not right on Durable Objects

1 participant