Fix type argument in `Batch.insert` (#1781)

This commit is contained in:
Simon Binder 2022-03-30 18:47:52 +02:00
parent 8e3259c971
commit 221ca2382f
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 36 additions and 1 deletions

View File

@ -48,7 +48,7 @@ class Batch {
{InsertMode? mode, UpsertClause<T, D>? onConflict}) {
_addUpdate(table, UpdateKind.insert);
final actualMode = mode ?? InsertMode.insert;
final context = InsertStatement<Table, D>(_user, table)
final context = InsertStatement<T, D>(_user, table)
.createContext(row, actualMode, onConflict: onConflict);
_addContext(context);
}

View File

@ -121,6 +121,41 @@ void main() {
)));
});
test('insert with where clause and excluded table', () async {
// https://github.com/simolus3/drift/issues/1781
final entries = [
CategoriesCompanion.insert(description: 'first'),
CategoriesCompanion.insert(description: 'second'),
];
await db.batch((batch) {
batch.insertAll<Categories, Category>(
db.categories,
entries,
onConflict: DoUpdate.withExcluded(
(old, excluded) => CategoriesCompanion.custom(
description: old.description.dartCast(),
priority: excluded.priority.dartCast(),
),
where: (old, excluded) =>
old.id.dartCast<int>().isBiggerOrEqual(excluded.id.dartCast()),
),
);
});
verify(executor.transactions.runBatched(BatchedStatements(
[
('INSERT INTO categories ("desc") VALUES (?) ON CONFLICT(id) '
'DO UPDATE SET "desc" = categories."desc", '
'priority = excluded.priority WHERE categories.id >= excluded.id')
],
[
ArgumentsForBatchedStatement(0, ['first']),
ArgumentsForBatchedStatement(0, ['second']),
],
)));
});
test('can re-use an outer transaction', () async {
await db.transaction(() async {
await db.batch((b) {});