diff --git a/CHANGELOG.md b/CHANGELOG.md index a567ca68f6..c606af3377 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Recursion in `openDatabase` when using `SentrySqfliteDatabaseFactory` ([#3231](https://github.com/getsentry/sentry-dart/pull/3231)) + ## 9.7.0-beta.2 ### Features diff --git a/packages/sqflite/lib/src/sentry_sqflite_database_factory.dart b/packages/sqflite/lib/src/sentry_sqflite_database_factory.dart index 2cc0a7e7c2..f1bb787546 100644 --- a/packages/sqflite/lib/src/sentry_sqflite_database_factory.dart +++ b/packages/sqflite/lib/src/sentry_sqflite_database_factory.dart @@ -1,6 +1,6 @@ import 'package:meta/meta.dart'; import 'package:sentry/sentry.dart'; -import 'package:sqflite/sqflite.dart'; +import 'package:sqflite/sqflite.dart' as sqflite; // ignore: implementation_imports import 'package:sqflite_common/src/factory_mixin.dart'; // ignore: implementation_imports @@ -33,31 +33,31 @@ class SentrySqfliteDatabaseFactory with SqfliteDatabaseFactoryMixin { /// final database = await openDatabase('path/to/db'); /// ``` SentrySqfliteDatabaseFactory({ - DatabaseFactory? databaseFactory, + sqflite.DatabaseFactory? databaseFactory, @internal Hub? hub, - }) : _databaseFactory = databaseFactory, + }) : _databaseFactory = databaseFactory ?? sqflite.databaseFactory, _hub = hub ?? HubAdapter(); final Hub _hub; - final DatabaseFactory? _databaseFactory; + final sqflite.DatabaseFactory _databaseFactory; @override Future invokeMethod(String method, [Object? arguments]) => impl.invokeMethod(method, arguments); @override - Future openDatabase( + Future openDatabase( String path, { - OpenDatabaseOptions? options, + sqflite.OpenDatabaseOptions? options, }) async { - final databaseFactory = _databaseFactory ?? this; + final databaseFactory = _databaseFactory; // ignore: invalid_use_of_internal_member if (!_hub.options.isTracingEnabled()) { return databaseFactory.openDatabase(path, options: options); } - return Future(() async { + return Future(() async { final currentSpan = _hub.getSpan(); final description = 'Open DB: $path'; final span = currentSpan?.startChild( diff --git a/packages/sqflite/test/sentry_sqflite_database_factory_dart_test.dart b/packages/sqflite/test/sentry_sqflite_database_factory_dart_test.dart index d25ae1144d..dc3c4dc11c 100644 --- a/packages/sqflite/test/sentry_sqflite_database_factory_dart_test.dart +++ b/packages/sqflite/test/sentry_sqflite_database_factory_dart_test.dart @@ -88,6 +88,32 @@ void main() { }); }); + group('openDatabase without delegate', () { + late Fixture fixture; + + setUp(() { + fixture = Fixture(); + + when(fixture.hub.options).thenReturn(fixture.options); + when(fixture.hub.scope).thenReturn(fixture.scope); + when(fixture.hub.getSpan()).thenReturn(fixture.tracer); + + // using ffi for testing on vm + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + }); + + test('does not recurse when calling instance openDatabase', () async { + final wrapper = SentrySqfliteDatabaseFactory(hub: fixture.hub); + + final db = await wrapper.openDatabase(inMemoryDatabasePath); + + expect(db is SentryDatabase, true); + + await db.close(); + }); + }); + tearDown(() { databaseFactory = sqfliteDatabaseFactoryDefault; });