mirror of https://github.com/AMT-Cheif/drift.git
Fix indices not being created for imported tables
This commit is contained in:
parent
b5bc646187
commit
4eb44f9623
|
@ -1,3 +1,7 @@
|
||||||
|
## 2.12.2-dev
|
||||||
|
|
||||||
|
- Fix indices not being created for Dart tables from different tables.
|
||||||
|
|
||||||
## 2.12.1
|
## 2.12.1
|
||||||
|
|
||||||
- Fix invalid types listed in `views` crashing the generator.
|
- Fix invalid types listed in `views` crashing the generator.
|
||||||
|
|
|
@ -12,6 +12,7 @@ import '../resolver/drift/sqlparser/mapping.dart';
|
||||||
import '../resolver/file_analysis.dart';
|
import '../resolver/file_analysis.dart';
|
||||||
import '../resolver/queries/custom_known_functions.dart';
|
import '../resolver/queries/custom_known_functions.dart';
|
||||||
import '../resolver/resolver.dart';
|
import '../resolver/resolver.dart';
|
||||||
|
import '../results/results.dart';
|
||||||
import '../serializer.dart';
|
import '../serializer.dart';
|
||||||
import 'cache.dart';
|
import 'cache.dart';
|
||||||
import 'error.dart';
|
import 'error.dart';
|
||||||
|
@ -198,23 +199,31 @@ class DriftAnalysisDriver {
|
||||||
/// was discovered or because discovered elements have been imported from
|
/// was discovered or because discovered elements have been imported from
|
||||||
/// cache.
|
/// cache.
|
||||||
Future<void> _analyzeLocalElements(FileState state) async {
|
Future<void> _analyzeLocalElements(FileState state) async {
|
||||||
assert(state.discovery != null || state.cachedDiscovery != null);
|
|
||||||
|
|
||||||
for (final discovered in state.definedElements) {
|
for (final discovered in state.definedElements) {
|
||||||
if (!state.elementIsAnalyzed(discovered.ownId)) {
|
await resolveElement(state, discovered.ownId);
|
||||||
final resolver = DriftResolver(this);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
Future<DriftElement?> resolveElement(
|
||||||
await resolver.resolveEntrypoint(discovered.ownId);
|
FileState state, DriftElementId id) async {
|
||||||
} catch (e, s) {
|
assert(state.discovery != null || state.cachedDiscovery != null);
|
||||||
if (e is! CouldNotResolveElementException) {
|
assert(id.libraryUri == state.ownUri);
|
||||||
backend.log.warning('Could not analyze ${discovered.ownId}', e, s);
|
|
||||||
|
|
||||||
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
|
/// Resolves elements in a file under the given [uri] by doing all the
|
||||||
|
|
|
@ -53,8 +53,9 @@ class FileAnalyzer {
|
||||||
final fileState = driver.cache.knownFiles[table.id.libraryUri]!;
|
final fileState = driver.cache.knownFiles[table.id.libraryUri]!;
|
||||||
|
|
||||||
for (final attachedIndex in table.attachedIndices) {
|
for (final attachedIndex in table.attachedIndices) {
|
||||||
final index =
|
final index = await driver.resolveElement(
|
||||||
fileState.analysis[fileState.id(attachedIndex)]?.result;
|
fileState, fileState.id(attachedIndex));
|
||||||
|
|
||||||
if (index is DriftIndex) {
|
if (index is DriftIndex) {
|
||||||
availableByDefault.add(index);
|
availableByDefault.add(index);
|
||||||
}
|
}
|
||||||
|
|
|
@ -428,6 +428,7 @@ class ElementDeserializer {
|
||||||
state
|
state
|
||||||
..result = result
|
..result = result
|
||||||
..isUpToDate = true;
|
..isUpToDate = true;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
if (e is CouldNotDeserializeException) rethrow;
|
if (e is CouldNotDeserializeException) rethrow;
|
||||||
|
|
|
@ -346,7 +346,13 @@ class _DriftBuildRun {
|
||||||
// In the monolithic build mode, we also need to analyze all reachable
|
// In the monolithic build mode, we also need to analyze all reachable
|
||||||
// imports - it is needed to fully resolve triggers and indices, and we
|
// imports - it is needed to fully resolve triggers and indices, and we
|
||||||
// should also warn about issues in those files.
|
// 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);
|
await _analyze(file.ownUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,13 +40,18 @@ class Tags extends Table {
|
||||||
'a|lib/a.dart': '''
|
'a|lib/a.dart': '''
|
||||||
import 'package:drift/drift.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})
|
@TableIndex(name: 'tag_id', columns: {#id})
|
||||||
class Tags extends Table {
|
class Tags extends Table {
|
||||||
IntColumn get id => integer().autoIncrement()();
|
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 {
|
test('generates index attached to table in modular build', () async {
|
||||||
final result = await emulateDriftBuild(
|
final result = await emulateDriftBuild(
|
||||||
inputs: {
|
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';
|
import 'package:drift/drift.dart';
|
||||||
|
|
||||||
@TableIndex(name: 'tag_id', columns: {#id})
|
@TableIndex(name: 'tag_id', columns: {#id})
|
||||||
|
@ -77,7 +90,9 @@ class Tags extends Table {
|
||||||
);
|
);
|
||||||
|
|
||||||
checkOutputs({
|
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(
|
contains(
|
||||||
"i0.Index get tagId => i0.Index('tag_id', 'CREATE INDEX tag_id ON tags (id)')"),
|
"i0.Index get tagId => i0.Index('tag_id', 'CREATE INDEX tag_id ON tags (id)')"),
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in New Issue