diff --git a/drift/example/main.g.dart b/drift/example/main.g.dart index 966f06b68..adb70e1bf 100644 --- a/drift/example/main.g.dart +++ b/drift/example/main.g.dart @@ -691,10 +691,17 @@ abstract class _$Database extends GeneratedDatabase { $TodoCategoryItemCountView(this); late final $TodoItemWithCategoryNameViewView customViewName = $TodoItemWithCategoryNameViewView(this); + late final Index itemTitle = + Index('item_title', 'CREATE INDEX item_title ON todo_items (title)'); @override Iterable> get allTables => allSchemaEntities.whereType>(); @override - List get allSchemaEntities => - [todoCategories, todoItems, todoCategoryItemCount, customViewName]; + List get allSchemaEntities => [ + todoCategories, + todoItems, + todoCategoryItemCount, + customViewName, + itemTitle + ]; } diff --git a/drift_dev/lib/src/analysis/driver/driver.dart b/drift_dev/lib/src/analysis/driver/driver.dart index c5e7b2487..610ce00a6 100644 --- a/drift_dev/lib/src/analysis/driver/driver.dart +++ b/drift_dev/lib/src/analysis/driver/driver.dart @@ -230,7 +230,9 @@ class DriftAnalysisDriver { assert(state.discovery != null || state.cachedDiscovery != null); assert(id.libraryUri == state.ownUri); - if (!state.elementIsAnalyzed(id)) { + if (state.elementIsAnalyzed(id)) { + return state.analysis[id]?.result; + } else { final resolver = DriftResolver(this); try { @@ -241,10 +243,10 @@ class DriftAnalysisDriver { if (_isTesting) rethrow; } + + return null; } } - - return null; } /// Resolves elements in a file under the given [uri] by doing all the diff --git a/drift_dev/test/writer/table_writer_test.dart b/drift_dev/test/writer/table_writer_test.dart index 853da3f72..fb5ef4d4f 100644 --- a/drift_dev/test/writer/table_writer_test.dart +++ b/drift_dev/test/writer/table_writer_test.dart @@ -66,6 +66,34 @@ class Tags extends Table { }, result.dartOutputs, result.writer); }); + test('generates index attached to table in same file', () async { + // Regression test for https://github.com/simolus3/drift/discussions/2766 + final result = await emulateDriftBuild( + inputs: { + 'a|lib/a.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 {} +''', + }, + ); + + checkOutputs({ + 'a|lib/a.drift.dart': decodedMatches(allOf( + contains( + "Index tagId = Index('tag_id', 'CREATE INDEX tag_id ON tags (id)')", + ), + contains('allSchemaEntities => [tags, tagId]'), + )), + }, result.dartOutputs, result.writer); + }); + test('generates index attached to table in modular build', () async { final result = await emulateDriftBuild( inputs: {