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: moor:
name: "moor package" name: "moor package"
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
defaults:
run:
working-directory: moor
steps: steps:
# setup # setup
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: cedx/setup-dart@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 - run: dart pub upgrade
working-directory: moor
# analysis # analysis
- run: dart format -o none --set-exit-if-changed moor/ - run: dart format -o none --set-exit-if-changed .
name: dartfmt name: dartfmt
- run: dart analyze --fatal-infos --fatal-warnings - run: dart analyze --fatal-infos --fatal-warnings
working-directory: moor/
# build, test and upload coverage # build, test and upload coverage
- run: dart run build_runner build --delete-conflicting-outputs - run: dart run build_runner build --delete-conflicting-outputs
working-directory: moor
- run: dart test #-x background_isolate --coverage=coverage - run: dart test #-x background_isolate --coverage=coverage
working-directory: moor
# - uses: actions/upload-artifact@v2 # - uses: actions/upload-artifact@v2
# with: # with:
# name: moor-coverage-data # name: moor-coverage-data
@ -38,20 +48,24 @@ jobs:
moor_generator: moor_generator:
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
defaults:
run:
working-directory: moor_generator
steps: steps:
# setup # setup
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: cedx/setup-dart@v2 - uses: cedx/setup-dart@v2
- run: dart pub upgrade - run: dart pub upgrade
working-directory: moor_generator
# analysis # analysis
- run: dart format -o none --set-exit-if-changed moor_generator/ - run: dart format -o none --set-exit-if-changed .
name: dartfmt name: dartfmt
- run: dart analyze --fatal-infos --fatal-warnings - run: dart analyze --fatal-infos --fatal-warnings
working-directory: moor_generator/
sqlparser: sqlparser:
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
defaults:
run:
working-directory: sqlparser
steps: steps:
# setup # setup
- uses: actions/checkout@v2 - uses: actions/checkout@v2
@ -59,15 +73,12 @@ jobs:
- run: sudo apt-get install -y libsqlite3-dev - run: sudo apt-get install -y libsqlite3-dev
name: Install sqlite3 for tests name: Install sqlite3 for tests
- run: dart pub upgrade - run: dart pub upgrade
working-directory: sqlparser
# analysis # analysis
- run: dart format -o none --set-exit-if-changed sqlparser/ - run: dart format -o none --set-exit-if-changed .
name: dartfmt name: dartfmt
- run: dart analyze --fatal-infos --fatal-warnings - run: dart analyze --fatal-infos --fatal-warnings
working-directory: sqlparser/
# tests and coverage # tests and coverage
- run: dart test #--coverage=coverage - run: dart test #--coverage=coverage
working-directory: sqlparser
# - uses: actions/upload-artifact@v2 # - uses: actions/upload-artifact@v2
# with: # with:
# name: sqlparser-coverage-data # name: sqlparser-coverage-data

View File

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

View File

@ -4,6 +4,7 @@ import 'package:test/test.dart';
import 'data/tables/todos.dart'; import 'data/tables/todos.dart';
import 'data/utils/mocks.dart'; import 'data/utils/mocks.dart';
import 'skips.dart';
void main() { void main() {
late TodoDb db; late TodoDb db;
@ -69,6 +70,31 @@ void main() {
{const TableUpdate('users', kind: UpdateKind.insert)})); {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', () { group('enforces integrity', () {
test('for regular inserts', () async { test('for regular inserts', () async {
InvalidDataException exception; InvalidDataException exception;

View File

@ -1,16 +1,14 @@
import 'package:moor/ffi.dart';
@TestOn('vm') @TestOn('vm')
import 'package:moor/moor.dart'; import 'package:moor/moor.dart';
import 'package:moor/ffi.dart';
import 'package:sqlite3/sqlite3.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import '../data/tables/todos.dart'; import '../data/tables/todos.dart';
import '../skips.dart';
void main() { void main() {
late TodoDb db; late TodoDb db;
final supportsReturning = sqlite3.version.versionNumber > 3035000;
setUp(() { setUp(() {
db = TodoDb(VmDatabase.memory()); db = TodoDb(VmDatabase.memory());
}); });
@ -40,5 +38,5 @@ void main() {
priority: CategoryPriority.low, 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/ffi.dart';
import 'package:moor/moor.dart' hide isNull; import 'package:moor/moor.dart' hide isNull;
import 'package:sqlite3/sqlite3.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import '../data/tables/converter.dart'; import '../data/tables/converter.dart';
import '../data/tables/custom_tables.dart'; import '../data/tables/custom_tables.dart';
import '../skips.dart';
void main() { void main() {
late VmDatabase executor; late VmDatabase executor;
@ -70,9 +70,6 @@ void main() {
}); });
}); });
final sqliteVersion = sqlite3.version;
final hasReturning = sqliteVersion.versionNumber > 3035000;
group('returning', () { group('returning', () {
test('for custom inserts', () async { test('for custom inserts', () async {
final result = await db.addConfig( 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}';