mirror of
https://github.com/immich-app/drift.git
synced 2026-09-30 13:22:57 +08:00
Warn about unparsed customConstraints
This commit is contained in:
@@ -418,15 +418,12 @@ class DartTableResolver extends LocalElementResolver<DiscoveredDartTable> {
|
||||
|
||||
if (expression is ListLiteral) {
|
||||
for (final entry in expression.elements) {
|
||||
if (entry is StringLiteral) {
|
||||
final value = entry.stringValue;
|
||||
if (value != null) {
|
||||
foundConstraints.add(value);
|
||||
foundConstraintSources.add(entry);
|
||||
}
|
||||
if (entry case StringLiteral(:final stringValue?)) {
|
||||
foundConstraints.add(stringValue);
|
||||
foundConstraintSources.add(entry);
|
||||
} else {
|
||||
reportError(DriftAnalysisError.inDartAst(
|
||||
element, entry, 'This must be a string literal.'));
|
||||
reportError(DriftAnalysisError.inDartAst(element, entry,
|
||||
'Drift can only verify custom constraints set as constant string literals.'));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -491,4 +491,36 @@ class Preferences extends Table {
|
||||
expect(file.allErrors,
|
||||
[isDriftError(contains('is only meaningful for `STRICT` tables'))]);
|
||||
});
|
||||
|
||||
test('warns about non-constant custom constraints', () async {
|
||||
final backend = await TestBackend.inTest({
|
||||
'a|lib/main.dart': r'''
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
@DataClassName('MyTable')
|
||||
class MyTables extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
TextColumn get name => text().nullable()();
|
||||
}
|
||||
|
||||
@DataClassName('MyOtherThing')
|
||||
class MyOtherThings extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
IntColumn get otherId => integer()();
|
||||
IntColumn get blahId => integer()();
|
||||
TextColumn get name => text().nullable()();
|
||||
|
||||
@override
|
||||
List<String> get customConstraints => ["UNIQUE(${otherId.name}, ${blahId.name})"];
|
||||
}
|
||||
|
||||
''',
|
||||
});
|
||||
|
||||
final file = await backend.analyze('package:a/main.dart');
|
||||
expect(file.allErrors, [
|
||||
isDriftError(contains(
|
||||
'can only verify custom constraints set as constant string literals.'))
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user