Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drizzle-orm/src/d1/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function migrate<TSchema extends Record<string, unknown>>(

const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
id SERIAL PRIMARY KEY,
id integer PRIMARY KEY AUTOINCREMENT,
hash text NOT NULL,
created_at numeric
)
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/durable-sqlite/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function migrate<

const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
id SERIAL PRIMARY KEY,
id integer PRIMARY KEY AUTOINCREMENT,
hash text NOT NULL,
created_at numeric
)
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/libsql/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function migrate<TSchema extends Record<string, unknown>>(

const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
id SERIAL PRIMARY KEY,
id integer PRIMARY KEY AUTOINCREMENT,
hash text NOT NULL,
created_at numeric
)
Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/sqlite-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ export class SQLiteSyncDialect extends SQLiteDialect {

const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
id SERIAL PRIMARY KEY,
id integer PRIMARY KEY AUTOINCREMENT,
hash text NOT NULL,
created_at numeric
)
Expand Down Expand Up @@ -895,7 +895,7 @@ export class SQLiteAsyncDialect extends SQLiteDialect {

const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
id SERIAL PRIMARY KEY,
id integer PRIMARY KEY AUTOINCREMENT,
hash text NOT NULL,
created_at numeric
)
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/sqlite-proxy/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function migrate<TSchema extends Record<string, unknown>>(

const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
id SERIAL PRIMARY KEY,
id integer PRIMARY KEY AUTOINCREMENT,
hash text NOT NULL,
created_at numeric
)
Expand Down
26 changes: 26 additions & 0 deletions integration-tests/tests/sqlite/better-sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ test('migrator', async () => {
db.run(sql`drop table __drizzle_migrations`);
});

test('migrator: migrations table has correct schema for SQLite', async () => {
db.run(sql`drop table if exists another_users`);
db.run(sql`drop table if exists users12`);
db.run(sql`drop table if exists __drizzle_migrations`);

migrate(db, { migrationsFolder: './drizzle2/sqlite' });

// Verify the __drizzle_migrations table uses proper SQLite syntax
// The id column should be "integer PRIMARY KEY" (which auto-increments in SQLite),
// not "SERIAL PRIMARY KEY" (which is PostgreSQL syntax)
const tableInfo = db.all<{ cid: number; name: string; type: string; notnull: number; pk: number }>(
sql`PRAGMA table_info(__drizzle_migrations)`,
);

const idColumn = tableInfo.find((col: { cid: number; name: string; type: string; notnull: number; pk: number }) => col.name === 'id');
expect(idColumn).toBeDefined();
// In SQLite, the type should be "integer" (case-insensitive) for proper auto-increment behavior
// "SERIAL" is PostgreSQL syntax and should not be used
expect(idColumn!.type.toLowerCase()).toBe('integer');
expect(idColumn!.pk).toBe(1);

db.run(sql`drop table another_users`);
db.run(sql`drop table users12`);
db.run(sql`drop table __drizzle_migrations`);
});

skipTests([
/**
* doesn't work properly:
Expand Down
26 changes: 26 additions & 0 deletions integration-tests/tests/sqlite/d1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ test('migrator : migrate with custom table', async () => {
await db.run(sql`drop table ${sql.identifier(customTable)}`);
});

test('migrator: migrations table has correct schema for SQLite', async () => {
await db.run(sql`drop table if exists another_users`);
await db.run(sql`drop table if exists users12`);
await db.run(sql`drop table if exists __drizzle_migrations`);

await migrate(db, { migrationsFolder: './drizzle2/sqlite' });

// Verify the __drizzle_migrations table uses proper SQLite syntax
// The id column should be "integer PRIMARY KEY" (which auto-increments in SQLite),
// not "SERIAL PRIMARY KEY" (which is PostgreSQL syntax)
const tableInfo = await db.all<{ cid: number; name: string; type: string; notnull: number; pk: number }>(
sql`PRAGMA table_info(__drizzle_migrations)`,
);

const idColumn = tableInfo.find((col: { name: string }) => col.name === 'id');
expect(idColumn).toBeDefined();
// In SQLite, the type should be "integer" (case-insensitive) for proper auto-increment behavior
// "SERIAL" is PostgreSQL syntax and should not be used
expect(idColumn!.type.toLowerCase()).toBe('integer');
expect(idColumn!.pk).toBe(1);

await db.run(sql`drop table another_users`);
await db.run(sql`drop table users12`);
await db.run(sql`drop table __drizzle_migrations`);
});

skipTests([
// Cannot convert 49,50,55 to a BigInt
'insert bigint values',
Expand Down
30 changes: 30 additions & 0 deletions integration-tests/tests/sqlite/durable-objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,36 @@ export class MyDurableObject extends DurableObject {
}
}

async migratorSchemaTest(): Promise<void> {
try {
this.db.run(sql`drop table if exists another_users`);
this.db.run(sql`drop table if exists users12`);
this.db.run(sql`drop table if exists __drizzle_migrations`);

migrate(this.db, migrations);

// Verify the __drizzle_migrations table uses proper SQLite syntax
// The id column should be "integer PRIMARY KEY" (which auto-increments in SQLite),
// not "SERIAL PRIMARY KEY" (which is PostgreSQL syntax)
const tableInfo = this.db.all<{ cid: number; name: string; type: string; notnull: number; pk: number }>(
sql`PRAGMA table_info(__drizzle_migrations)`,
);

const idColumn = tableInfo.find((col: { name: string }) => col.name === 'id');
expect(idColumn).to.not.be.undefined;
// In SQLite, the type should be "integer" (case-insensitive) for proper auto-increment behavior
// "SERIAL" is PostgreSQL syntax and should not be used
expect(idColumn!.type.toLowerCase()).to.equal('integer');
expect(idColumn!.pk).to.equal(1);

this.db.run(sql`drop table another_users`);
this.db.run(sql`drop table users12`);
this.db.run(sql`drop table __drizzle_migrations`);
} catch {
throw new Error('migratorSchemaTest has broken');
}
}

async beforeEach(): Promise<void> {
this.db.run(sql`drop table if exists ${usersTable}`);
this.db.run(sql`drop table if exists ${users2Table}`);
Expand Down
26 changes: 26 additions & 0 deletions integration-tests/tests/sqlite/libsql-http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ test('migrator : migrate with custom table', async () => {
await db.run(sql`drop table ${sql.identifier(customTable)}`);
});

test('migrator: migrations table has correct schema for SQLite', async () => {
await db.run(sql`drop table if exists another_users`);
await db.run(sql`drop table if exists users12`);
await db.run(sql`drop table if exists __drizzle_migrations`);

await migrate(db, { migrationsFolder: './drizzle2/sqlite' });

// Verify the __drizzle_migrations table uses proper SQLite syntax
// The id column should be "integer PRIMARY KEY" (which auto-increments in SQLite),
// not "SERIAL PRIMARY KEY" (which is PostgreSQL syntax)
const tableInfo = await db.all<{ cid: number; name: string; type: string; notnull: number; pk: number }>(
sql`PRAGMA table_info(__drizzle_migrations)`,
);

const idColumn = tableInfo.find((col: { name: string }) => col.name === 'id');
expect(idColumn).toBeDefined();
// In SQLite, the type should be "integer" (case-insensitive) for proper auto-increment behavior
// "SERIAL" is PostgreSQL syntax and should not be used
expect(idColumn!.type.toLowerCase()).toBe('integer');
expect(idColumn!.pk).toBe(1);

await db.run(sql`drop table another_users`);
await db.run(sql`drop table users12`);
await db.run(sql`drop table __drizzle_migrations`);
});

test('test $onUpdateFn and $onUpdate works as $default', async (ctx) => {
const { db } = ctx.sqlite;

Expand Down
26 changes: 26 additions & 0 deletions integration-tests/tests/sqlite/libsql-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ test('migrator : migrate with custom table', async () => {
await db.run(sql`drop table ${sql.identifier(customTable)}`);
});

test('migrator: migrations table has correct schema for SQLite', async () => {
await db.run(sql`drop table if exists another_users`);
await db.run(sql`drop table if exists users12`);
await db.run(sql`drop table if exists __drizzle_migrations`);

await migrate(db, { migrationsFolder: './drizzle2/sqlite' });

// Verify the __drizzle_migrations table uses proper SQLite syntax
// The id column should be "integer PRIMARY KEY" (which auto-increments in SQLite),
// not "SERIAL PRIMARY KEY" (which is PostgreSQL syntax)
const tableInfo = await db.all<{ cid: number; name: string; type: string; notnull: number; pk: number }>(
sql`PRAGMA table_info(__drizzle_migrations)`,
);

const idColumn = tableInfo.find((col: { name: string }) => col.name === 'id');
expect(idColumn).toBeDefined();
// In SQLite, the type should be "integer" (case-insensitive) for proper auto-increment behavior
// "SERIAL" is PostgreSQL syntax and should not be used
expect(idColumn!.type.toLowerCase()).toBe('integer');
expect(idColumn!.pk).toBe(1);

await db.run(sql`drop table another_users`);
await db.run(sql`drop table users12`);
await db.run(sql`drop table __drizzle_migrations`);
});

skipTests([
'delete with limit and order by',
'update with limit and order by',
Expand Down
26 changes: 26 additions & 0 deletions integration-tests/tests/sqlite/libsql-sqlite3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ test('migrator : migrate with custom table', async () => {
await db.run(sql`drop table ${sql.identifier(customTable)}`);
});

test('migrator: migrations table has correct schema for SQLite', async () => {
await db.run(sql`drop table if exists another_users`);
await db.run(sql`drop table if exists users12`);
await db.run(sql`drop table if exists __drizzle_migrations`);

await migrate(db, { migrationsFolder: './drizzle2/sqlite' });

// Verify the __drizzle_migrations table uses proper SQLite syntax
// The id column should be "integer PRIMARY KEY" (which auto-increments in SQLite),
// not "SERIAL PRIMARY KEY" (which is PostgreSQL syntax)
const tableInfo = await db.all<{ cid: number; name: string; type: string; notnull: number; pk: number }>(
sql`PRAGMA table_info(__drizzle_migrations)`,
);

const idColumn = tableInfo.find((col: { name: string }) => col.name === 'id');
expect(idColumn).toBeDefined();
// In SQLite, the type should be "integer" (case-insensitive) for proper auto-increment behavior
// "SERIAL" is PostgreSQL syntax and should not be used
expect(idColumn!.type.toLowerCase()).toBe('integer');
expect(idColumn!.pk).toBe(1);

await db.run(sql`drop table another_users`);
await db.run(sql`drop table users12`);
await db.run(sql`drop table __drizzle_migrations`);
});

skipTests([
'delete with limit and order by',
'update with limit and order by',
Expand Down
26 changes: 26 additions & 0 deletions integration-tests/tests/sqlite/libsql-ws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ test('migrator : migrate with custom table', async () => {
await db.run(sql`drop table ${sql.identifier(customTable)}`);
});

test('migrator: migrations table has correct schema for SQLite', async () => {
await db.run(sql`drop table if exists another_users`);
await db.run(sql`drop table if exists users12`);
await db.run(sql`drop table if exists __drizzle_migrations`);

await migrate(db, { migrationsFolder: './drizzle2/sqlite' });

// Verify the __drizzle_migrations table uses proper SQLite syntax
// The id column should be "integer PRIMARY KEY" (which auto-increments in SQLite),
// not "SERIAL PRIMARY KEY" (which is PostgreSQL syntax)
const tableInfo = await db.all<{ cid: number; name: string; type: string; notnull: number; pk: number }>(
sql`PRAGMA table_info(__drizzle_migrations)`,
);

const idColumn = tableInfo.find((col: { name: string }) => col.name === 'id');
expect(idColumn).toBeDefined();
// In SQLite, the type should be "integer" (case-insensitive) for proper auto-increment behavior
// "SERIAL" is PostgreSQL syntax and should not be used
expect(idColumn!.type.toLowerCase()).toBe('integer');
expect(idColumn!.pk).toBe(1);

await db.run(sql`drop table another_users`);
await db.run(sql`drop table users12`);
await db.run(sql`drop table __drizzle_migrations`);
});

test('test $onUpdateFn and $onUpdate works as $default', async (ctx) => {
const { db } = ctx.sqlite;

Expand Down
26 changes: 26 additions & 0 deletions integration-tests/tests/sqlite/libsql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ test('migrator : migrate with custom table', async () => {
await db.run(sql`drop table ${sql.identifier(customTable)}`);
});

test('migrator: migrations table has correct schema for SQLite', async () => {
await db.run(sql`drop table if exists another_users`);
await db.run(sql`drop table if exists users12`);
await db.run(sql`drop table if exists __drizzle_migrations`);

await migrate(db, { migrationsFolder: './drizzle2/sqlite' });

// Verify the __drizzle_migrations table uses proper SQLite syntax
// The id column should be "integer PRIMARY KEY" (which auto-increments in SQLite),
// not "SERIAL PRIMARY KEY" (which is PostgreSQL syntax)
const tableInfo = await db.all<{ cid: number; name: string; type: string; notnull: number; pk: number }>(
sql`PRAGMA table_info(__drizzle_migrations)`,
);

const idColumn = tableInfo.find((col: { name: string }) => col.name === 'id');
expect(idColumn).toBeDefined();
// In SQLite, the type should be "integer" (case-insensitive) for proper auto-increment behavior
// "SERIAL" is PostgreSQL syntax and should not be used
expect(idColumn!.type.toLowerCase()).toBe('integer');
expect(idColumn!.pk).toBe(1);

await db.run(sql`drop table another_users`);
await db.run(sql`drop table users12`);
await db.run(sql`drop table __drizzle_migrations`);
});

skipTests([
'delete with limit and order by',
'update with limit and order by',
Expand Down
26 changes: 26 additions & 0 deletions integration-tests/tests/sqlite/sql-js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ test('migrator', async () => {
db.run(sql`drop table __drizzle_migrations`);
});

test('migrator: migrations table has correct schema for SQLite', async () => {
db.run(sql`drop table if exists another_users`);
db.run(sql`drop table if exists users12`);
db.run(sql`drop table if exists __drizzle_migrations`);

migrate(db, { migrationsFolder: './drizzle2/sqlite' });

// Verify the __drizzle_migrations table uses proper SQLite syntax
// The id column should be "integer PRIMARY KEY" (which auto-increments in SQLite),
// not "SERIAL PRIMARY KEY" (which is PostgreSQL syntax)
const tableInfo = db.all<{ cid: number; name: string; type: string; notnull: number; pk: number }>(
sql`PRAGMA table_info(__drizzle_migrations)`,
);

const idColumn = tableInfo.find((col: { name: string }) => col.name === 'id');
expect(idColumn).toBeDefined();
// In SQLite, the type should be "integer" (case-insensitive) for proper auto-increment behavior
// "SERIAL" is PostgreSQL syntax and should not be used
expect(idColumn!.type.toLowerCase()).toBe('integer');
expect(idColumn!.pk).toBe(1);

db.run(sql`drop table another_users`);
db.run(sql`drop table users12`);
db.run(sql`drop table __drizzle_migrations`);
});

skipTests([
/**
* doesn't work properly:
Expand Down