Fix indices not being created for imported tables

This commit is contained in:
Simon Binder 2023-09-27 22:06:49 +02:00
parent b5bc646187
commit 4eb44f9623
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 55 additions and 19 deletions

View File

@ -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.

View File

@ -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<void> _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<DriftElement?> 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

View File

@ -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);
}

View File

@ -428,6 +428,7 @@ class ElementDeserializer {
state
..result = result
..isUpToDate = true;
return result;
} catch (e, s) {
if (e is CouldNotDeserializeException) rethrow;

View File

@ -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);
}

View File

@ -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)')"),
),