Skip to content

Commit

Permalink
Fix defining indices on expressions (#2643)
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Sep 27, 2023
1 parent d0a3fee commit 2edd75a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 17 deletions.
1 change: 1 addition & 0 deletions drift_dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.12.1-dev

- Fix invalid types listed in `views` crashing the generator.
- Fix indices in drift files crashing the generator when they index expressions.

## 2.12.0

Expand Down
12 changes: 1 addition & 11 deletions drift_dev/lib/src/analysis/resolver/drift/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,18 @@ class DriftIndexResolver extends DriftElementResolver<DiscoveredDriftIndex> {

final onTable = stmt.on.resolved;
DriftTable? target;
List<DriftColumn> indexedColumns = [];

if (onTable is Table) {
target = references
.whereType<DriftTable>()
.firstWhere((e) => e.schemaName == onTable.name);

for (final indexedColumn in stmt.columns) {
final name = (indexedColumn.expression as Reference).columnName;
final tableColumn = target.columnBySqlName[name];

if (tableColumn != null) {
indexedColumns.add(tableColumn);
}
}
}

return DriftIndex(
discovered.ownId,
DriftDeclaration.driftFile(stmt, file.ownUri),
table: target,
indexedColumns: indexedColumns,
indexedColumns: [],
unique: stmt.unique,
createStmt: source.substring(stmt.firstPosition, stmt.lastPosition),
);
Expand Down
4 changes: 4 additions & 0 deletions drift_dev/lib/src/analysis/results/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class DriftIndex extends DriftSchemaElement {
DriftTable? table;

/// Columns of [table] that have been indexed.
///
/// This list is empty for indices created in SQL because it can't represent
/// all expressions being indexed. It is useful for Dart-defined indices to
/// implement [createStatementForDartDefinition].
List<DriftColumn> indexedColumns;

/// Whethet the index has been declared to be unique.
Expand Down
5 changes: 3 additions & 2 deletions drift_dev/test/backends/build/build_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ class MyDatabase {}
'a|lib/a.drift': '''
import 'b.drift';
CREATE INDEX b_idx /* comment should be stripped */ ON b (foo);
CREATE INDEX b_idx /* comment should be stripped */ ON b (foo, upper(foo));
''',
'a|lib/b.drift': 'CREATE TABLE b (foo TEXT);',
});

checkOutputs({
'a|lib/main.drift.dart': decodedMatches(contains(
"late final Index bIdx = Index('b_idx', 'CREATE INDEX b_idx ON b (foo)')")),
'late final Index bIdx =\n'
" Index('b_idx', 'CREATE INDEX b_idx ON b (foo, upper(foo))')")),
}, result.dartOutputs, result.writer);
});

Expand Down
2 changes: 1 addition & 1 deletion drift_dev/test/cli/schema/dump_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void main() {
"name": "idx",
"sql": "CREATE INDEX idx ON users (name);",
"unique": false,
"columns": ["name"]
"columns": []
}
}
]
Expand Down
6 changes: 3 additions & 3 deletions drift_dev/test/services/schema/writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ CREATE TRIGGER delete_empty_groups AFTER DELETE ON group_members BEGIN
WHERE NOT EXISTS (SELECT * FROM group_members WHERE "group" = "groups".id);
END;
CREATE INDEX groups_name ON "groups"(name);
CREATE INDEX groups_name ON "groups"(name, upper(name));
CREATE VIEW my_view WITH MyViewRow AS SELECT id FROM "groups";
Expand Down Expand Up @@ -368,9 +368,9 @@ const expected = r'''
"data": {
"on": 0,
"name": "groups_name",
"sql": "CREATE INDEX groups_name ON \"groups\"(name);",
"sql": "CREATE INDEX groups_name ON \"groups\"(name, upper(name));",
"unique": false,
"columns": ["name"]
"columns": []
}
},
{
Expand Down

0 comments on commit 2edd75a

Please sign in to comment.