More lenient `strict` column type handling

This commit is contained in:
Simon Binder 2022-01-24 17:47:53 +01:00
parent 7d56497c18
commit aa38ab42f2
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 28 additions and 14 deletions

View File

@ -1,3 +1,7 @@
## 0.19.2
- Improve handling of drift-specific column types in `STRICT` tables.
## 0.19.1
- Make the result of `group_concat` nullable.

View File

@ -135,6 +135,11 @@ class SchemaFromCreateTable {
return const ResolvedType(type: BasicType.blob);
}
return _handleColumnTypeWithoutDefault(typeName) ??
const ResolvedType(type: BasicType.real);
}
ResolvedType? _handleColumnTypeWithoutDefault(String typeName) {
final upper = typeName.toUpperCase();
if (upper.contains('INT')) {
return const ResolvedType(type: BasicType.int);
@ -161,24 +166,20 @@ class SchemaFromCreateTable {
return const ResolvedType(type: BasicType.int);
}
}
return const ResolvedType(type: BasicType.real);
}
bool isValidTypeNameForStrictTable(String typeName) {
if (moorExtensions) {
// Anything accepted by drift_dev goes, the generated table will have
// a correct column type for that either way (it's transformed).
return _handleColumnTypeWithoutDefault(typeName) != null;
} else {
// See https://www.sqlite.org/draft/stricttables.html
const allowed = {'INT', 'INTEGER', 'REAL', 'TEXT', 'BLOB', 'ANY'};
const alsoAllowedInMoor = {'BOOL', 'DATE'};
final upper = typeName.toUpperCase();
if (allowed.contains(upper) ||
(moorExtensions &&
(alsoAllowedInMoor.contains(upper) || upper.contains('ENUM')))) {
return true;
return allowed.contains(upper);
}
return false;
}
/// Looks up the correct column affinity for a declared type name with the

View File

@ -1,6 +1,6 @@
name: sqlparser
description: Parses sqlite statements and performs static analysis on them
version: 0.19.1
version: 0.19.2-dev
homepage: https://github.com/simolus3/moor/tree/develop/sqlparser
#homepage: https://moor.simonbinder.eu/
issue_tracker: https://github.com/simolus3/moor/issues

View File

@ -6,6 +6,8 @@ import 'utils.dart';
void main() {
final oldEngine = SqlEngine(EngineOptions(version: SqliteVersion.v3_35));
final engine = SqlEngine(EngineOptions(version: SqliteVersion.v3_37));
final engineInDriftMode = SqlEngine(
EngineOptions(version: SqliteVersion.v3_37, useMoorExtensions: true));
group('using STRICT', () {
test('with an old sqlite3 version', () {
@ -34,6 +36,13 @@ void main() {
);
},
);
test('with a type that is only valid in drift mode', () {
// https://github.com/simolus3/moor/discussions/1651
engineInDriftMode
.analyze('CREATE TABLE a (c IS_DATETIME) STRICT;')
.expectNoError();
});
});
test('using WITHOUT ROWID and then not declaring a primary key', () {