Merge pull request #1219 from kuhnroyal/bugfix/insert-returning-stream-update

Fix insertReturning stream update
This commit is contained in:
Simon Binder 2021-05-25 18:45:35 +02:00 committed by GitHub
commit 246add0c73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 25 deletions

View File

@ -9,25 +9,35 @@ jobs:
moor:
name: "moor package"
runs-on: ubuntu-20.04
defaults:
run:
working-directory: moor
steps:
# setup
- uses: actions/checkout@v2
- uses: cedx/setup-dart@v2
- run: sudo apt-get install -y libsqlite3-dev
name: Install sqlite3 for tests
- name: Install sqlite3 for tests
run: |
mkdir sqlite
cd sqlite
curl https://sqlite.org/2021/sqlite-autoconf-3350500.tar.gz --output sqlite.tar.gz
tar zxvf sqlite.tar.gz
cd sqlite-autoconf-3350500
./configure
make
sudo make install
echo "/usr/local/lib" >> $GITHUB_PATH
echo "LD_LIBRARY_PATH=`pwd`/.libs" >> $GITHUB_ENV
- name: Check sqlite3 version
run: sqlite3 --version
- run: dart pub upgrade
working-directory: moor
# analysis
- run: dart format -o none --set-exit-if-changed moor/
- run: dart format -o none --set-exit-if-changed .
name: dartfmt
- run: dart analyze --fatal-infos --fatal-warnings
working-directory: moor/
# build, test and upload coverage
- run: dart run build_runner build --delete-conflicting-outputs
working-directory: moor
- run: dart test #-x background_isolate --coverage=coverage
working-directory: moor
# - uses: actions/upload-artifact@v2
# with:
# name: moor-coverage-data
@ -38,20 +48,24 @@ jobs:
moor_generator:
runs-on: ubuntu-20.04
defaults:
run:
working-directory: moor_generator
steps:
# setup
- uses: actions/checkout@v2
- uses: cedx/setup-dart@v2
- run: dart pub upgrade
working-directory: moor_generator
# analysis
- run: dart format -o none --set-exit-if-changed moor_generator/
- run: dart format -o none --set-exit-if-changed .
name: dartfmt
- run: dart analyze --fatal-infos --fatal-warnings
working-directory: moor_generator/
sqlparser:
runs-on: ubuntu-20.04
defaults:
run:
working-directory: sqlparser
steps:
# setup
- uses: actions/checkout@v2
@ -59,15 +73,12 @@ jobs:
- run: sudo apt-get install -y libsqlite3-dev
name: Install sqlite3 for tests
- run: dart pub upgrade
working-directory: sqlparser
# analysis
- run: dart format -o none --set-exit-if-changed sqlparser/
- run: dart format -o none --set-exit-if-changed .
name: dartfmt
- run: dart analyze --fatal-infos --fatal-warnings
working-directory: sqlparser/
# tests and coverage
- run: dart test #--coverage=coverage
working-directory: sqlparser
# - uses: actions/upload-artifact@v2
# with:
# name: sqlparser-coverage-data

View File

@ -87,6 +87,8 @@ class InsertStatement<T extends Table, D> {
return database.doWhenOpened((e) async {
final result = await e.runSelect(ctx.sql, ctx.boundVariables);
database
.notifyUpdates({TableUpdate.onTable(table, kind: UpdateKind.insert)});
return table.map(result.single);
});
}

View File

@ -4,6 +4,7 @@ import 'package:test/test.dart';
import 'data/tables/todos.dart';
import 'data/utils/mocks.dart';
import 'skips.dart';
void main() {
late TodoDb db;
@ -69,6 +70,31 @@ void main() {
{const TableUpdate('users', kind: UpdateKind.insert)}));
});
test('notifies stream queries on insertReturning', () async {
when(executor.runSelect(any, any)).thenAnswer((_) {
return Future.value([
{
'id': 5,
'name': 'User McUserface',
'is_awesome': true,
'profile_picture': Uint8List(0),
'creation_time': DateTime.now().millisecondsSinceEpoch,
}
]);
});
final user = await db.into(db.users).insertReturning(UsersCompanion(
name: const Value('User McUserface'),
isAwesome: const Value(true),
profilePicture: Value(Uint8List(0)),
));
verify(streamQueries.handleTableUpdates(
{const TableUpdate('users', kind: UpdateKind.insert)}));
expect(user.id, 5);
}, skip: onNoReturningSupport());
group('enforces integrity', () {
test('for regular inserts', () async {
InvalidDataException exception;

View File

@ -1,16 +1,14 @@
import 'package:moor/ffi.dart';
@TestOn('vm')
import 'package:moor/moor.dart';
import 'package:moor/ffi.dart';
import 'package:sqlite3/sqlite3.dart';
import 'package:test/test.dart';
import '../data/tables/todos.dart';
import '../skips.dart';
void main() {
late TodoDb db;
final supportsReturning = sqlite3.version.versionNumber > 3035000;
setUp(() {
db = TodoDb(VmDatabase.memory());
});
@ -40,5 +38,5 @@ void main() {
priority: CategoryPriority.low,
),
);
}, skip: supportsReturning ? null : 'RETURNING is not supported');
}, skip: onNoReturningSupport());
}

View File

@ -1,10 +1,10 @@
import 'package:moor/ffi.dart';
import 'package:moor/moor.dart' hide isNull;
import 'package:sqlite3/sqlite3.dart';
import 'package:test/test.dart';
import '../data/tables/converter.dart';
import '../data/tables/custom_tables.dart';
import '../skips.dart';
void main() {
late VmDatabase executor;
@ -70,9 +70,6 @@ void main() {
});
});
final sqliteVersion = sqlite3.version;
final hasReturning = sqliteVersion.versionNumber > 3035000;
group('returning', () {
test('for custom inserts', () async {
final result = await db.addConfig(
@ -94,5 +91,5 @@ void main() {
),
);
});
}, skip: hasReturning ? null : 'RETURNING not supported by current sqlite');
}, skip: onNoReturningSupport());
}

5
moor/test/skips.dart Normal file
View File

@ -0,0 +1,5 @@
import 'package:sqlite3/sqlite3.dart';
String? onNoReturningSupport() => sqlite3.version.versionNumber > 3035000
? null
: 'RETURNING not supported by sqlite version ${sqlite3.version.libVersion}';