diff --git a/sqlparser/CHANGELOG.md b/sqlparser/CHANGELOG.md index 3c477bbe..a1598fe7 100644 --- a/sqlparser/CHANGELOG.md +++ b/sqlparser/CHANGELOG.md @@ -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. diff --git a/sqlparser/lib/src/analysis/schema/from_create_table.dart b/sqlparser/lib/src/analysis/schema/from_create_table.dart index ed76a3ed..76e7d435 100644 --- a/sqlparser/lib/src/analysis/schema/from_create_table.dart +++ b/sqlparser/lib/src/analysis/schema/from_create_table.dart @@ -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) { - // See https://www.sqlite.org/draft/stricttables.html - const allowed = {'INT', 'INTEGER', 'REAL', 'TEXT', 'BLOB', 'ANY'}; - const alsoAllowedInMoor = {'BOOL', 'DATE'}; + 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'}; + final upper = typeName.toUpperCase(); - 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 diff --git a/sqlparser/pubspec.yaml b/sqlparser/pubspec.yaml index f020afef..542f7622 100644 --- a/sqlparser/pubspec.yaml +++ b/sqlparser/pubspec.yaml @@ -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 diff --git a/sqlparser/test/analysis/errors/create_table_errors_test.dart b/sqlparser/test/analysis/errors/create_table_errors_test.dart index 9c509842..1185a1e6 100644 --- a/sqlparser/test/analysis/errors/create_table_errors_test.dart +++ b/sqlparser/test/analysis/errors/create_table_errors_test.dart @@ -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', () {