mirror of https://github.com/AMT-Cheif/drift.git
Add warnings for `customConstraint` misuse
This commit is contained in:
parent
3898114962
commit
a721e896d3
|
@ -29,6 +29,14 @@ const String _methodDefault = 'withDefault';
|
|||
const String _methodClientDefault = 'clientDefault';
|
||||
const String _methodMap = 'map';
|
||||
const String _methodGenerated = 'generatedAs';
|
||||
const Set<String> _addsSqlConstraint = {
|
||||
_methodNamed,
|
||||
_methodReferences,
|
||||
_methodAutoIncrement,
|
||||
_methodUnique,
|
||||
_methodDefault,
|
||||
_methodGenerated
|
||||
};
|
||||
|
||||
const String _errorMessage = 'This getter does not create a valid column that '
|
||||
'can be parsed by moor. Please refer to the readme from moor to see how '
|
||||
|
@ -74,6 +82,7 @@ class ColumnParser {
|
|||
DriftDartType? typeConverterRuntime;
|
||||
ColumnGeneratedAs? generatedAs;
|
||||
var nullable = false;
|
||||
var hasDefaultConstraints = false;
|
||||
|
||||
final foundFeatures = <ColumnFeature>[];
|
||||
|
||||
|
@ -85,6 +94,10 @@ class ColumnParser {
|
|||
break;
|
||||
}
|
||||
|
||||
if (_addsSqlConstraint.contains(methodName)) {
|
||||
hasDefaultConstraints = true;
|
||||
}
|
||||
|
||||
switch (methodName) {
|
||||
case _methodNamed:
|
||||
if (foundExplicitName != null) {
|
||||
|
@ -223,6 +236,19 @@ class ColumnParser {
|
|||
foundFeatures.add(const UniqueKey());
|
||||
break;
|
||||
case _methodCustomConstraint:
|
||||
if (foundCustomConstraint != null) {
|
||||
base.step.reportError(
|
||||
ErrorInDartCode(
|
||||
severity: Severity.warning,
|
||||
affectedElement: getter.declaredElement,
|
||||
affectedNode: remainingExpr.methodName,
|
||||
message:
|
||||
"You've already set custom constraints on this column, "
|
||||
'they will be overriden by this call.',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
foundCustomConstraint = base.readStringLiteral(
|
||||
remainingExpr.argumentList.arguments.first, () {
|
||||
base.step.reportError(
|
||||
|
@ -370,6 +396,19 @@ class ColumnParser {
|
|||
);
|
||||
}
|
||||
|
||||
if (hasDefaultConstraints && foundCustomConstraint != null) {
|
||||
base.step.reportError(
|
||||
ErrorInDartCode(
|
||||
severity: Severity.warning,
|
||||
affectedElement: getter.declaredElement,
|
||||
message: 'This column definition is using both drift-defined '
|
||||
'constraints (like references, autoIncrement, ...) and a '
|
||||
'customConstraint(). Only the custom constraint will be added '
|
||||
'to the column in SQL!',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final docString =
|
||||
getter.documentationComment?.tokens.map((t) => t.toString()).join('\n');
|
||||
return MoorColumn(
|
||||
|
|
|
@ -11,6 +11,7 @@ import 'package:drift_dev/writer.dart';
|
|||
import 'package:test/test.dart';
|
||||
|
||||
import '../../utils/test_backend.dart';
|
||||
import '../utils.dart';
|
||||
|
||||
void main() {
|
||||
late TestBackend backend;
|
||||
|
@ -118,6 +119,14 @@ void main() {
|
|||
@DriftDatabase(tables: [Foo, DoesNotExist])
|
||||
class Database {}
|
||||
''',
|
||||
AssetId.parse('test_lib|lib/invalid_constraints.dart'): '''
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
class InvalidConstraints extends Table {
|
||||
IntColumn get a => integer().autoIncrement().customConstraint('foo')();
|
||||
IntColumn get b => integer().customConstraint('a').customConstraint('b')();
|
||||
}
|
||||
''',
|
||||
});
|
||||
});
|
||||
tearDownAll(() {
|
||||
|
@ -366,4 +375,26 @@ void main() {
|
|||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('reports errors around suspicous customConstraint uses', () async {
|
||||
final session = MoorSession(backend);
|
||||
final uri = Uri.parse('package:test_lib/invalid_constraints.dart');
|
||||
final backendTask = backend.startTask(uri);
|
||||
final task = session.startTask(backendTask);
|
||||
await task.runTask();
|
||||
|
||||
final file = session.registerFile(uri);
|
||||
file.expectDartError(
|
||||
allOf(
|
||||
contains(
|
||||
'This column definition is using both drift-defined constraints'),
|
||||
contains('and a customConstraint()'),
|
||||
),
|
||||
'a',
|
||||
);
|
||||
file.expectDartError(
|
||||
contains("You've already set custom constraints on this column"),
|
||||
'customConstraint',
|
||||
);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue