-
-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed SQLite onConflict clauses being overwritten instead of stacked,…
… added related tests, removed unused import
- Loading branch information
1 parent
f36e3ea
commit 348e496
Showing
5 changed files
with
192 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,14 @@ const pkExampleTable = sqliteTable('pk_example', { | |
compositePk: primaryKey({ columns: [table.id, table.name] }), | ||
})); | ||
|
||
const conflictChainExampleTable = sqliteTable('conflict_chain_example', { | ||
id: integer('id').notNull().unique(), | ||
name: text('name').notNull(), | ||
email: text('email').notNull(), | ||
}, (table) => ({ | ||
compositePk: primaryKey({ columns: [table.id, table.name] }), | ||
})); | ||
|
||
const bigIntExample = sqliteTable('big_int_example', { | ||
id: integer('id').primaryKey(), | ||
name: text('name').notNull(), | ||
|
@@ -154,6 +162,7 @@ export function tests() { | |
await db.run(sql`drop table if exists ${orders}`); | ||
await db.run(sql`drop table if exists ${bigIntExample}`); | ||
await db.run(sql`drop table if exists ${pkExampleTable}`); | ||
await db.run(sql`drop table if exists ${conflictChainExampleTable}`); | ||
await db.run(sql`drop table if exists user_notifications_insert_into`); | ||
await db.run(sql`drop table if exists users_insert_into`); | ||
await db.run(sql`drop table if exists notifications_insert_into`); | ||
|
@@ -212,6 +221,14 @@ export function tests() { | |
primary key (id, name) | ||
) | ||
`); | ||
await db.run(sql` | ||
create table ${conflictChainExampleTable} ( | ||
id integer not null unique, | ||
name text not null, | ||
email text not null, | ||
primary key (id, name) | ||
) | ||
`); | ||
await db.run(sql` | ||
create table ${bigIntExample} ( | ||
id integer primary key, | ||
|
@@ -2037,6 +2054,165 @@ export function tests() { | |
expect(res).toEqual([{ id: 1, name: 'John', email: '[email protected]' }]); | ||
}); | ||
|
||
test('insert with onConflict chained (.update -> .nothing)', async (ctx) => { | ||
const { db } = ctx.sqlite; | ||
|
||
await db.insert(conflictChainExampleTable).values([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'John Second', | ||
email: '[email protected]', | ||
}]).run(); | ||
|
||
await db | ||
.insert(conflictChainExampleTable) | ||
.values([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'Anthony', | ||
email: '[email protected]', | ||
}]) | ||
.onConflictDoUpdate({ | ||
target: [conflictChainExampleTable.id, conflictChainExampleTable.name], | ||
set: { email: '[email protected]' }, | ||
}) | ||
.onConflictDoNothing({ target: conflictChainExampleTable.id }) | ||
.run(); | ||
|
||
const res = await db | ||
.select({ | ||
id: conflictChainExampleTable.id, | ||
name: conflictChainExampleTable.name, | ||
email: conflictChainExampleTable.email, | ||
}) | ||
.from(conflictChainExampleTable) | ||
.orderBy(conflictChainExampleTable.id) | ||
.all(); | ||
|
||
expect(res).toEqual([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'John Second', | ||
email: '[email protected]', | ||
}]); | ||
}); | ||
|
||
test('insert with onConflict chained (.nothing -> .update)', async (ctx) => { | ||
const { db } = ctx.sqlite; | ||
|
||
await db.insert(conflictChainExampleTable).values([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'John Second', | ||
email: '[email protected]', | ||
}]).run(); | ||
|
||
await db | ||
.insert(conflictChainExampleTable) | ||
.values([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'Anthony', | ||
email: '[email protected]', | ||
}]) | ||
.onConflictDoUpdate({ | ||
target: [conflictChainExampleTable.id, conflictChainExampleTable.name], | ||
set: { email: '[email protected]' }, | ||
}) | ||
.onConflictDoNothing({ target: conflictChainExampleTable.id }) | ||
.run(); | ||
|
||
const res = await db | ||
.select({ | ||
id: conflictChainExampleTable.id, | ||
name: conflictChainExampleTable.name, | ||
email: conflictChainExampleTable.email, | ||
}) | ||
.from(conflictChainExampleTable) | ||
.orderBy(conflictChainExampleTable.id) | ||
.all(); | ||
|
||
expect(res).toEqual([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'John Second', | ||
email: '[email protected]', | ||
}]); | ||
}); | ||
|
||
test('insert with onConflict chained (.update -> .update)', async (ctx) => { | ||
const { db } = ctx.sqlite; | ||
|
||
await db.insert(conflictChainExampleTable).values([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'John Second', | ||
email: '[email protected]', | ||
}]).run(); | ||
|
||
await db | ||
.insert(conflictChainExampleTable) | ||
.values([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'Anthony', | ||
email: '[email protected]', | ||
}]) | ||
.onConflictDoUpdate({ | ||
target: [conflictChainExampleTable.id, conflictChainExampleTable.name], | ||
set: { email: '[email protected]' }, | ||
}) | ||
.onConflictDoUpdate({ target: conflictChainExampleTable.id, set: { email: '[email protected]' } }) | ||
.run(); | ||
|
||
const res = await db | ||
.select({ | ||
id: conflictChainExampleTable.id, | ||
name: conflictChainExampleTable.name, | ||
email: conflictChainExampleTable.email, | ||
}) | ||
.from(conflictChainExampleTable) | ||
.orderBy(conflictChainExampleTable.id) | ||
.all(); | ||
|
||
expect(res).toEqual([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'John Second', | ||
email: '[email protected]', | ||
}]); | ||
}); | ||
|
||
test('insert with onConflict chained (.nothing -> .nothing)', async (ctx) => { | ||
const { db } = ctx.sqlite; | ||
|
||
await db.insert(conflictChainExampleTable).values([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'John Second', | ||
email: '[email protected]', | ||
}]).run(); | ||
|
||
await db | ||
.insert(conflictChainExampleTable) | ||
.values([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'Anthony', | ||
email: '[email protected]', | ||
}]) | ||
.onConflictDoNothing({ | ||
target: [conflictChainExampleTable.id, conflictChainExampleTable.name], | ||
}) | ||
.onConflictDoNothing({ target: conflictChainExampleTable.id }) | ||
.run(); | ||
|
||
const res = await db | ||
.select({ | ||
id: conflictChainExampleTable.id, | ||
name: conflictChainExampleTable.name, | ||
email: conflictChainExampleTable.email, | ||
}) | ||
.from(conflictChainExampleTable) | ||
.orderBy(conflictChainExampleTable.id) | ||
.all(); | ||
|
||
expect(res).toEqual([{ id: 1, name: 'John', email: '[email protected]' }, { | ||
id: 2, | ||
name: 'John Second', | ||
email: '[email protected]', | ||
}]); | ||
}); | ||
|
||
test('insert undefined', async (ctx) => { | ||
const { db } = ctx.sqlite; | ||
|
||
|