From 4eb44f9623c94ddc90cc287975e29f935dd4e35c Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 27 Sep 2023 22:06:49 +0200 Subject: [PATCH] Fix indices not being created for imported tables --- drift_dev/CHANGELOG.md | 4 +++ drift_dev/lib/src/analysis/driver/driver.dart | 31 ++++++++++++------- .../src/analysis/resolver/file_analysis.dart | 5 +-- drift_dev/lib/src/analysis/serializer.dart | 1 + .../lib/src/backends/build/drift_builder.dart | 8 ++++- drift_dev/test/writer/table_writer_test.dart | 25 ++++++++++++--- 6 files changed, 55 insertions(+), 19 deletions(-) diff --git a/drift_dev/CHANGELOG.md b/drift_dev/CHANGELOG.md index fb5eb5e9..3e750710 100644 --- a/drift_dev/CHANGELOG.md +++ b/drift_dev/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.12.2-dev + +- Fix indices not being created for Dart tables from different tables. + ## 2.12.1 - Fix invalid types listed in `views` crashing the generator. diff --git a/drift_dev/lib/src/analysis/driver/driver.dart b/drift_dev/lib/src/analysis/driver/driver.dart index 57225c2b..0e9c15e9 100644 --- a/drift_dev/lib/src/analysis/driver/driver.dart +++ b/drift_dev/lib/src/analysis/driver/driver.dart @@ -12,6 +12,7 @@ import '../resolver/drift/sqlparser/mapping.dart'; import '../resolver/file_analysis.dart'; import '../resolver/queries/custom_known_functions.dart'; import '../resolver/resolver.dart'; +import '../results/results.dart'; import '../serializer.dart'; import 'cache.dart'; import 'error.dart'; @@ -198,23 +199,31 @@ class DriftAnalysisDriver { /// was discovered or because discovered elements have been imported from /// cache. Future _analyzeLocalElements(FileState state) async { - assert(state.discovery != null || state.cachedDiscovery != null); - for (final discovered in state.definedElements) { - if (!state.elementIsAnalyzed(discovered.ownId)) { - final resolver = DriftResolver(this); + await resolveElement(state, discovered.ownId); + } + } - try { - await resolver.resolveEntrypoint(discovered.ownId); - } catch (e, s) { - if (e is! CouldNotResolveElementException) { - backend.log.warning('Could not analyze ${discovered.ownId}', e, s); + Future resolveElement( + FileState state, DriftElementId id) async { + assert(state.discovery != null || state.cachedDiscovery != null); + assert(id.libraryUri == state.ownUri); - if (_isTesting) rethrow; - } + if (!state.elementIsAnalyzed(id)) { + final resolver = DriftResolver(this); + + try { + return await resolver.resolveEntrypoint(id); + } catch (e, s) { + if (e is! CouldNotResolveElementException) { + backend.log.warning('Could not analyze $id', e, s); + + if (_isTesting) rethrow; } } } + + return null; } /// Resolves elements in a file under the given [uri] by doing all the diff --git a/drift_dev/lib/src/analysis/resolver/file_analysis.dart b/drift_dev/lib/src/analysis/resolver/file_analysis.dart index dc418fed..2802c065 100644 --- a/drift_dev/lib/src/analysis/resolver/file_analysis.dart +++ b/drift_dev/lib/src/analysis/resolver/file_analysis.dart @@ -53,8 +53,9 @@ class FileAnalyzer { final fileState = driver.cache.knownFiles[table.id.libraryUri]!; for (final attachedIndex in table.attachedIndices) { - final index = - fileState.analysis[fileState.id(attachedIndex)]?.result; + final index = await driver.resolveElement( + fileState, fileState.id(attachedIndex)); + if (index is DriftIndex) { availableByDefault.add(index); } diff --git a/drift_dev/lib/src/analysis/serializer.dart b/drift_dev/lib/src/analysis/serializer.dart index 5c3d9d8c..46fd2745 100644 --- a/drift_dev/lib/src/analysis/serializer.dart +++ b/drift_dev/lib/src/analysis/serializer.dart @@ -428,6 +428,7 @@ class ElementDeserializer { state ..result = result ..isUpToDate = true; + return result; } catch (e, s) { if (e is CouldNotDeserializeException) rethrow; diff --git a/drift_dev/lib/src/backends/build/drift_builder.dart b/drift_dev/lib/src/backends/build/drift_builder.dart index 3e7012dd..37a6b925 100644 --- a/drift_dev/lib/src/backends/build/drift_builder.dart +++ b/drift_dev/lib/src/backends/build/drift_builder.dart @@ -346,7 +346,13 @@ class _DriftBuildRun { // In the monolithic build mode, we also need to analyze all reachable // imports - it is needed to fully resolve triggers and indices, and we // should also warn about issues in those files. - for (final file in driver.cache.crawlMulti(resolved.knownImports)) { + final importRoots = { + ...resolved.knownImports, + for (final element in resolved.availableElements) + if (driver.cache.knownFiles.containsKey(element.id.libraryUri)) + driver.cache.knownFiles[element.id.libraryUri]!, + }; + for (final file in driver.cache.crawlMulti(importRoots)) { await _analyze(file.ownUri); } diff --git a/drift_dev/test/writer/table_writer_test.dart b/drift_dev/test/writer/table_writer_test.dart index bba4a44c..853da3f7 100644 --- a/drift_dev/test/writer/table_writer_test.dart +++ b/drift_dev/test/writer/table_writer_test.dart @@ -40,13 +40,18 @@ class Tags extends Table { 'a|lib/a.dart': ''' import 'package:drift/drift.dart'; +import 'table.dart'; + +@DriftDatabase(tables: [Tags]) +class MyDatabase {} +''', + 'a|lib/table.dart': ''' +import 'package:drift/drift.dart'; + @TableIndex(name: 'tag_id', columns: {#id}) class Tags extends Table { IntColumn get id => integer().autoIncrement()(); } - -@DriftDatabase(tables: [Tags]) -class MyDatabase {} ''', }, ); @@ -64,7 +69,15 @@ class MyDatabase {} test('generates index attached to table in modular build', () async { final result = await emulateDriftBuild( inputs: { - 'a|lib/a.dart': ''' + 'a|lib/database.dart': ''' +import 'package:drift/drift.dart'; + +import 'table.dart'; + +@DriftDatabase(tables: [Tags]) +class MyDatabase {} +''', + 'a|lib/table.dart': ''' import 'package:drift/drift.dart'; @TableIndex(name: 'tag_id', columns: {#id}) @@ -77,7 +90,9 @@ class Tags extends Table { ); checkOutputs({ - 'a|lib/a.drift.dart': decodedMatches( + 'a|lib/database.drift.dart': + decodedMatches(contains('get allSchemaEntities => [tags, i1.tagId]')), + 'a|lib/table.drift.dart': decodedMatches( contains( "i0.Index get tagId => i0.Index('tag_id', 'CREATE INDEX tag_id ON tags (id)')"), ),