From cf95ed6f056ea28cdb4444408336b882bd6c7db8 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 29 Jul 2022 18:54:41 +0200 Subject: [PATCH] Re-generate migration tests in app example --- drift_dev/drift_dev.build.yaml | 2 +- drift_dev/lib/src/analyzer/options.dart | 76 ++++++++++--------- drift_dev/lib/src/analyzer/options.g.dart | 34 ++++++++- .../test/generated_migrations/schema_v1.dart | 26 +++---- .../test/generated_migrations/schema_v2.dart | 30 ++++---- 5 files changed, 101 insertions(+), 67 deletions(-) diff --git a/drift_dev/drift_dev.build.yaml b/drift_dev/drift_dev.build.yaml index f329e8dd..58cf7643 100644 --- a/drift_dev/drift_dev.build.yaml +++ b/drift_dev/drift_dev.build.yaml @@ -10,4 +10,4 @@ targets: any_map: true disallow_unrecognized_keys: true field_rename: snake - create_to_json: false + explicit_to_json: true diff --git a/drift_dev/lib/src/analyzer/options.dart b/drift_dev/lib/src/analyzer/options.dart index 54f9f6e0..3e3f9777 100644 --- a/drift_dev/lib/src/analyzer/options.dart +++ b/drift_dev/lib/src/analyzer/options.dart @@ -7,13 +7,7 @@ part 'options.g.dart'; /// Controllable options to define the behavior of the analyzer and the /// generator. -@JsonSerializable( - checked: true, - anyMap: true, - disallowUnrecognizedKeys: true, - fieldRename: FieldRename.snake, - createToJson: true, -) +@JsonSerializable() class DriftOptions { static const _defaultSqliteVersion = SqliteVersion.v3(34); @@ -203,6 +197,8 @@ class DialectOptions { const DialectOptions(this.dialect, this.options); factory DialectOptions.fromJson(Map json) => _$DialectOptionsFromJson(json); + + Map toJson() => _$DialectOptionsToJson(this); } @JsonSerializable() @@ -210,7 +206,7 @@ class SqliteAnalysisOptions { @JsonKey(name: 'modules') final List modules; - @JsonKey(fromJson: _parseSqliteVersion) + @_SqliteVersionConverter() final SqliteVersion? version; const SqliteAnalysisOptions({this.modules = const [], this.version}); @@ -218,40 +214,50 @@ class SqliteAnalysisOptions { factory SqliteAnalysisOptions.fromJson(Map json) { return _$SqliteAnalysisOptionsFromJson(json); } + + Map toJson() => _$SqliteAnalysisOptionsToJson(this); } -final _versionRegex = RegExp(r'(\d+)\.(\d+)'); +class _SqliteVersionConverter extends JsonConverter { + static final _versionRegex = RegExp(r'(\d+)\.(\d+)'); -SqliteVersion? _parseSqliteVersion(String? name) { - if (name == null) return null; + const _SqliteVersionConverter(); - final match = _versionRegex.firstMatch(name); - if (match == null) { - throw ArgumentError.value(name, 'name', - 'Not a valid sqlite version: Expected format major.minor (e.g. 3.34)'); + @override + SqliteVersion fromJson(String json) { + final match = _versionRegex.firstMatch(json); + if (match == null) { + throw ArgumentError.value(json, 'json', + 'Not a valid sqlite version: Expected format major.minor (e.g. 3.34)'); + } + + final major = int.parse(match.group(1)!); + final minor = int.parse(match.group(2)!); + + final version = SqliteVersion(major, minor, 0); + if (version < SqliteVersion.minimum) { + throw ArgumentError.value( + json, + 'json', + 'Version is not supported for analysis (minimum is ' + '${SqliteVersion.minimum}).', + ); + } else if (version > SqliteVersion.current) { + throw ArgumentError.value( + json, + 'json', + 'Version is not supported for analysis (current maximum is ' + '${SqliteVersion.current}).', + ); + } + + return version; } - final major = int.parse(match.group(1)!); - final minor = int.parse(match.group(2)!); - - final version = SqliteVersion(major, minor, 0); - if (version < SqliteVersion.minimum) { - throw ArgumentError.value( - name, - 'name', - 'Version is not supported for analysis (minimum is ' - '${SqliteVersion.minimum}).', - ); - } else if (version > SqliteVersion.current) { - throw ArgumentError.value( - name, - 'name', - 'Version is not supported for analysis (current maximum is ' - '${SqliteVersion.current}).', - ); + @override + String toJson(SqliteVersion object) { + return '${object.major}.${object.minor}'; } - - return version; } /// Set of sqlite modules that require special knowledge from the generator. diff --git a/drift_dev/lib/src/analyzer/options.g.dart b/drift_dev/lib/src/analyzer/options.g.dart index 5b6fe796..e6cb89b6 100644 --- a/drift_dev/lib/src/analyzer/options.g.dart +++ b/drift_dev/lib/src/analyzer/options.g.dart @@ -130,8 +130,8 @@ Map _$DriftOptionsToJson(DriftOptions instance) => 'generate_connect_constructor': instance.generateConnectConstructor, 'sqlite_modules': instance.modules.map((e) => _$SqlModuleEnumMap[e]!).toList(), - 'sqlite': instance.sqliteAnalysisOptions, - 'sql': instance.dialect, + 'sqlite': instance.sqliteAnalysisOptions?.toJson(), + 'sql': instance.dialect?.toJson(), 'eagerly_load_dart_ast': instance.eagerlyLoadDartAst, 'data_class_to_companions': instance.dataClassToCompanions, 'mutable_classes': instance.generateMutableClasses, @@ -172,6 +172,12 @@ DialectOptions _$DialectOptionsFromJson(Map json) => $checkedCreate( }, ); +Map _$DialectOptionsToJson(DialectOptions instance) => + { + 'dialect': _$SqlDialectEnumMap[instance.dialect]!, + 'options': instance.options?.toJson(), + }; + const _$SqlDialectEnumMap = { SqlDialect.sqlite: 'sqlite', SqlDialect.mysql: 'mysql', @@ -196,8 +202,30 @@ SqliteAnalysisOptions _$SqliteAnalysisOptionsFromJson(Map json) => .toList() ?? const []), version: $checkedConvert( - 'version', (v) => _parseSqliteVersion(v as String?)), + 'version', + (v) => _$JsonConverterFromJson( + v, const _SqliteVersionConverter().fromJson)), ); return val; }, ); + +Map _$SqliteAnalysisOptionsToJson( + SqliteAnalysisOptions instance) => + { + 'modules': instance.modules.map((e) => _$SqlModuleEnumMap[e]!).toList(), + 'version': _$JsonConverterToJson( + instance.version, const _SqliteVersionConverter().toJson), + }; + +Value? _$JsonConverterFromJson( + Object? json, + Value? Function(Json json) fromJson, +) => + json == null ? null : fromJson(json as Json); + +Json? _$JsonConverterToJson( + Value? value, + Json? Function(Value value) toJson, +) => + value == null ? null : toJson(value); diff --git a/examples/app/test/generated_migrations/schema_v1.dart b/examples/app/test/generated_migrations/schema_v1.dart index 1df9e323..7869557e 100644 --- a/examples/app/test/generated_migrations/schema_v1.dart +++ b/examples/app/test/generated_migrations/schema_v1.dart @@ -7,17 +7,17 @@ class Categories extends Table with TableInfo { final GeneratedDatabase attachedDatabase; final String? _alias; Categories(this.attachedDatabase, [this._alias]); - late final GeneratedColumn id = GeneratedColumn( + late final GeneratedColumn id = GeneratedColumn( 'id', aliasedName, false, - type: const IntType(), + type: DriftSqlType.int, requiredDuringInsert: false, defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); - late final GeneratedColumn name = GeneratedColumn( + late final GeneratedColumn name = GeneratedColumn( 'name', aliasedName, false, - type: const StringType(), requiredDuringInsert: true); - late final GeneratedColumn color = GeneratedColumn( + type: DriftSqlType.string, requiredDuringInsert: true); + late final GeneratedColumn color = GeneratedColumn( 'color', aliasedName, false, - type: const IntType(), requiredDuringInsert: true); + type: DriftSqlType.int, requiredDuringInsert: true); @override List get $columns => [id, name, color]; @override @@ -45,17 +45,17 @@ class TodoEntries extends Table with TableInfo { final GeneratedDatabase attachedDatabase; final String? _alias; TodoEntries(this.attachedDatabase, [this._alias]); - late final GeneratedColumn id = GeneratedColumn( + late final GeneratedColumn id = GeneratedColumn( 'id', aliasedName, false, - type: const IntType(), + type: DriftSqlType.int, requiredDuringInsert: false, defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); - late final GeneratedColumn description = GeneratedColumn( + late final GeneratedColumn description = GeneratedColumn( 'description', aliasedName, false, - type: const StringType(), requiredDuringInsert: true); - late final GeneratedColumn category = GeneratedColumn( + type: DriftSqlType.string, requiredDuringInsert: true); + late final GeneratedColumn category = GeneratedColumn( 'category', aliasedName, true, - type: const IntType(), requiredDuringInsert: false); + type: DriftSqlType.int, requiredDuringInsert: false); @override List get $columns => [id, description, category]; @override @@ -109,7 +109,7 @@ class text_entriesTable extends Table with TableInfo, VirtualTableInfo { } class DatabaseAtV1 extends GeneratedDatabase { - DatabaseAtV1(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e); + DatabaseAtV1(QueryExecutor e) : super(e); DatabaseAtV1.connect(DatabaseConnection c) : super.connect(c); late final Categories categories = Categories(this); late final TodoEntries todoEntries = TodoEntries(this); diff --git a/examples/app/test/generated_migrations/schema_v2.dart b/examples/app/test/generated_migrations/schema_v2.dart index 7ecd59cb..52ce3e47 100644 --- a/examples/app/test/generated_migrations/schema_v2.dart +++ b/examples/app/test/generated_migrations/schema_v2.dart @@ -7,17 +7,17 @@ class Categories extends Table with TableInfo { final GeneratedDatabase attachedDatabase; final String? _alias; Categories(this.attachedDatabase, [this._alias]); - late final GeneratedColumn id = GeneratedColumn( + late final GeneratedColumn id = GeneratedColumn( 'id', aliasedName, false, - type: const IntType(), + type: DriftSqlType.int, requiredDuringInsert: false, defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); - late final GeneratedColumn name = GeneratedColumn( + late final GeneratedColumn name = GeneratedColumn( 'name', aliasedName, false, - type: const StringType(), requiredDuringInsert: true); - late final GeneratedColumn color = GeneratedColumn( + type: DriftSqlType.string, requiredDuringInsert: true); + late final GeneratedColumn color = GeneratedColumn( 'color', aliasedName, false, - type: const IntType(), requiredDuringInsert: true); + type: DriftSqlType.int, requiredDuringInsert: true); @override List get $columns => [id, name, color]; @override @@ -45,20 +45,20 @@ class TodoEntries extends Table with TableInfo { final GeneratedDatabase attachedDatabase; final String? _alias; TodoEntries(this.attachedDatabase, [this._alias]); - late final GeneratedColumn id = GeneratedColumn( + late final GeneratedColumn id = GeneratedColumn( 'id', aliasedName, false, - type: const IntType(), + type: DriftSqlType.int, requiredDuringInsert: false, defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); - late final GeneratedColumn description = GeneratedColumn( + late final GeneratedColumn description = GeneratedColumn( 'description', aliasedName, false, - type: const StringType(), requiredDuringInsert: true); - late final GeneratedColumn category = GeneratedColumn( + type: DriftSqlType.string, requiredDuringInsert: true); + late final GeneratedColumn category = GeneratedColumn( 'category', aliasedName, true, - type: const IntType(), requiredDuringInsert: false); - late final GeneratedColumn dueDate = GeneratedColumn( + type: DriftSqlType.int, requiredDuringInsert: false); + late final GeneratedColumn dueDate = GeneratedColumn( 'due_date', aliasedName, true, - type: const IntType(), requiredDuringInsert: false); + type: DriftSqlType.dateTime, requiredDuringInsert: false); @override List get $columns => [id, description, category, dueDate]; @override @@ -112,7 +112,7 @@ class text_entriesTable extends Table with TableInfo, VirtualTableInfo { } class DatabaseAtV2 extends GeneratedDatabase { - DatabaseAtV2(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e); + DatabaseAtV2(QueryExecutor e) : super(e); DatabaseAtV2.connect(DatabaseConnection c) : super.connect(c); late final Categories categories = Categories(this); late final TodoEntries todoEntries = TodoEntries(this);