Enum migration values

This commit is contained in:
Tobe O 2019-04-04 16:41:27 -04:00
parent 4aed69fb78
commit d2785741dc
2 changed files with 23 additions and 5 deletions

View file

@ -4,6 +4,7 @@
* Stop assuming things have `id`, etc. * Stop assuming things have `id`, etc.
* Resolve a bug where the `indexType` of `@Column` annotations. would not be found. * Resolve a bug where the `indexType` of `@Column` annotations. would not be found.
* Add `cascade: true` to drops for hasOne/hasMany/ManyToMany migrations. * Add `cascade: true` to drops for hasOne/hasMany/ManyToMany migrations.
* Support enum default values in migrations.
# 2.0.0-dev.6 # 2.0.0-dev.6
* Fix bug where an extra field would be inserted into joins and botch the result. * Fix bug where an extra field would be inserted into joins and botch the result.

View file

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:angel_model/angel_model.dart'; import 'package:angel_model/angel_model.dart';
import 'package:angel_orm/angel_orm.dart'; import 'package:angel_orm/angel_orm.dart';
import 'package:angel_serialize_generator/angel_serialize_generator.dart'; import 'package:angel_serialize_generator/angel_serialize_generator.dart';
@ -164,12 +165,28 @@ class MigrationGenerator extends GeneratorForAnnotation<Orm> {
var defaultValue = ctx.buildContext.defaults[name]; var defaultValue = ctx.buildContext.defaults[name];
if (defaultValue != null) { if (defaultValue != null && !defaultValue.isNull) {
cascade.add(refer('defaultsTo').call([ var type = defaultValue.type;
new CodeExpression( 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)), new Code(dartObjectToString(defaultValue)),
), );
])); }
cascade.add(refer('defaultsTo').call([defaultExpr]));
} }
if (col.indexType == IndexType.primaryKey || if (col.indexType == IndexType.primaryKey ||