diff --git a/angel_orm_generator/CHANGELOG.md b/angel_orm_generator/CHANGELOG.md index b102cc56..1b1602eb 100644 --- a/angel_orm_generator/CHANGELOG.md +++ b/angel_orm_generator/CHANGELOG.md @@ -4,6 +4,7 @@ * Stop assuming things have `id`, etc. * Resolve a bug where the `indexType` of `@Column` annotations. would not be found. * Add `cascade: true` to drops for hasOne/hasMany/ManyToMany migrations. +* Support enum default values in migrations. # 2.0.0-dev.6 * Fix bug where an extra field would be inserted into joins and botch the result. diff --git a/angel_orm_generator/lib/src/migration_generator.dart b/angel_orm_generator/lib/src/migration_generator.dart index abd1c027..4355353a 100644 --- a/angel_orm_generator/lib/src/migration_generator.dart +++ b/angel_orm_generator/lib/src/migration_generator.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/type.dart'; import 'package:angel_model/angel_model.dart'; import 'package:angel_orm/angel_orm.dart'; import 'package:angel_serialize_generator/angel_serialize_generator.dart'; @@ -164,12 +165,28 @@ class MigrationGenerator extends GeneratorForAnnotation { var defaultValue = ctx.buildContext.defaults[name]; - if (defaultValue != null) { - cascade.add(refer('defaultsTo').call([ - new CodeExpression( + if (defaultValue != null && !defaultValue.isNull) { + var type = defaultValue.type; + Expression defaultExpr; + + if (const TypeChecker.fromRuntime(RawSql) + .isAssignableFromType(defaultValue.type)) { + var value = + ConstantReader(defaultValue).read('value').stringValue; + defaultExpr = + refer('RawSql').constInstance([literalString(value)]); + } else if (type is InterfaceType && type.element.isEnum) { + // Default to enum index. + var index = + ConstantReader(defaultValue).read('index').intValue; + defaultExpr = literalNum(index); + } else { + defaultExpr = new CodeExpression( new Code(dartObjectToString(defaultValue)), - ), - ])); + ); + } + + cascade.add(refer('defaultsTo').call([defaultExpr])); } if (col.indexType == IndexType.primaryKey ||