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 ## 0.19.1
- Make the result of `group_concat` nullable. - Make the result of `group_concat` nullable.

View File

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

View File

@ -1,6 +1,6 @@
name: sqlparser name: sqlparser
description: Parses sqlite statements and performs static analysis on them 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://github.com/simolus3/moor/tree/develop/sqlparser
#homepage: https://moor.simonbinder.eu/ #homepage: https://moor.simonbinder.eu/
issue_tracker: https://github.com/simolus3/moor/issues issue_tracker: https://github.com/simolus3/moor/issues

View File

@ -6,6 +6,8 @@ import 'utils.dart';
void main() { void main() {
final oldEngine = SqlEngine(EngineOptions(version: SqliteVersion.v3_35)); final oldEngine = SqlEngine(EngineOptions(version: SqliteVersion.v3_35));
final engine = SqlEngine(EngineOptions(version: SqliteVersion.v3_37)); final engine = SqlEngine(EngineOptions(version: SqliteVersion.v3_37));
final engineInDriftMode = SqlEngine(
EngineOptions(version: SqliteVersion.v3_37, useMoorExtensions: true));
group('using STRICT', () { group('using STRICT', () {
test('with an old sqlite3 version', () { 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', () { test('using WITHOUT ROWID and then not declaring a primary key', () {