Added default to Column
This commit is contained in:
parent
224d2be026
commit
6c51e7a231
5 changed files with 43 additions and 15 deletions
|
@ -12,6 +12,7 @@ class MigrationColumn extends Column {
|
|||
@override
|
||||
IndexType get indexType => _index;
|
||||
|
||||
@override
|
||||
dynamic get defaultValue => _defaultValue;
|
||||
|
||||
List<MigrationColumnReference> get externalReferences =>
|
||||
|
@ -21,8 +22,12 @@ class MigrationColumn extends Column {
|
|||
{bool isNullable = true,
|
||||
int length = 255,
|
||||
IndexType indexType = IndexType.standardIndex,
|
||||
defaultValue})
|
||||
: super(type: type, length: length) {
|
||||
dynamic defaultValue})
|
||||
: super(
|
||||
type: type,
|
||||
length: length,
|
||||
isNullable: isNullable,
|
||||
defaultValue: defaultValue) {
|
||||
_nullable = isNullable;
|
||||
_index = indexType;
|
||||
_defaultValue = defaultValue;
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
## 6.0.2
|
||||
|
||||
* Fixed issue #68: Support for non-nullable type
|
||||
* Added `defaultValue` to Column
|
||||
* Upgraded to `lints` 2.x.x
|
||||
|
||||
## 6.0.1
|
||||
|
|
|
@ -18,7 +18,7 @@ class Column {
|
|||
final bool isNullable;
|
||||
|
||||
/// Specifies this column name.
|
||||
final String name;
|
||||
final String? name;
|
||||
|
||||
/// Specifies the length of a `VARCHAR`.
|
||||
final int length;
|
||||
|
@ -29,8 +29,8 @@ class Column {
|
|||
/// Specifies the scale of a `NUMERIC` or `DECIMAL`.
|
||||
final int scale;
|
||||
|
||||
/// Specifies the timezone of a temporal field.
|
||||
final String timezone;
|
||||
/// Specifies the timezone for temporal field.
|
||||
final String? timezone;
|
||||
|
||||
/// Explicitly defines a SQL type for this column.
|
||||
final ColumnType type;
|
||||
|
@ -41,16 +41,20 @@ class Column {
|
|||
/// A custom SQL expression to execute, instead of a named column.
|
||||
final String? expression;
|
||||
|
||||
/// Specifies the default values.
|
||||
final dynamic defaultValue;
|
||||
|
||||
const Column(
|
||||
{this.isNullable = true,
|
||||
this.length = 255,
|
||||
this.precision = 17,
|
||||
this.scale = 3,
|
||||
this.name = "",
|
||||
this.timezone = "",
|
||||
this.name,
|
||||
this.timezone,
|
||||
this.type = ColumnType.varChar,
|
||||
this.indexType = IndexType.none,
|
||||
this.expression});
|
||||
this.expression,
|
||||
this.defaultValue});
|
||||
|
||||
/// Returns `true` if [expression] is not `null`.
|
||||
bool get hasExpression => expression != null;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import 'dart:async';
|
||||
import 'package:analyzer/dart/constant/value.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:angel3_model/angel3_model.dart';
|
||||
|
@ -198,7 +199,20 @@ class MigrationGenerator extends GeneratorForAnnotation<Orm> {
|
|||
|
||||
var defaultValue = ctx.buildContext.defaults[name];
|
||||
|
||||
if (defaultValue != null && !defaultValue.isNull) {
|
||||
// Handle 'defaultValue' on Column annotation
|
||||
if (col.defaultValue != null) {
|
||||
var defaultCode =
|
||||
dartObjectToString(col.defaultValue as DartObject);
|
||||
|
||||
if (defaultCode != null) {
|
||||
Expression defaultExpr = CodeExpression(
|
||||
Code(defaultCode),
|
||||
);
|
||||
cascade.add(refer('defaultsTo').call([defaultExpr]));
|
||||
}
|
||||
|
||||
// Handle 'defaultValue' on SerializableField annotation
|
||||
} else if (defaultValue != null && !defaultValue.isNull) {
|
||||
var type = defaultValue.type;
|
||||
Expression? defaultExpr;
|
||||
|
||||
|
@ -219,9 +233,12 @@ class MigrationGenerator extends GeneratorForAnnotation<Orm> {
|
|||
// Definitely an analyzer issue.
|
||||
}
|
||||
} else {
|
||||
defaultExpr = CodeExpression(
|
||||
Code(dartObjectToString(defaultValue)!),
|
||||
);
|
||||
var defaultCode = dartObjectToString(defaultValue);
|
||||
if (defaultCode != null) {
|
||||
defaultExpr = CodeExpression(
|
||||
Code(defaultCode),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (defaultExpr != null) {
|
||||
|
|
|
@ -105,12 +105,10 @@ Future<OrmBuildContext?> buildOrmContext(
|
|||
// Check for column annotation...
|
||||
var element = _findElement(field);
|
||||
var columnAnnotation = columnTypeChecker.firstAnnotationOf(element);
|
||||
if (columnAnnotation != null) {
|
||||
print('[ORM_BUILD_CONTEXT] ${element.name} => $columnAnnotation');
|
||||
}
|
||||
|
||||
Column? column;
|
||||
if (columnAnnotation != null) {
|
||||
// print('[ORM_BUILD_CONTEXT] ${element.name} => $columnAnnotation');
|
||||
column = reviveColumn(ConstantReader(columnAnnotation));
|
||||
}
|
||||
|
||||
|
@ -132,6 +130,7 @@ Future<OrmBuildContext?> buildOrmContext(
|
|||
length: column.length,
|
||||
indexType: column.indexType,
|
||||
type: inferColumnType(field.type),
|
||||
defaultValue: column.defaultValue,
|
||||
);
|
||||
|
||||
// Try to find a relationship
|
||||
|
@ -321,6 +320,7 @@ Future<OrmBuildContext?> buildOrmContext(
|
|||
length: column.length,
|
||||
type: column.type,
|
||||
indexType: column.indexType,
|
||||
defaultValue: column.defaultValue,
|
||||
expression:
|
||||
ConstantReader(columnAnnotation).peek('expression')?.stringValue,
|
||||
);
|
||||
|
@ -396,6 +396,7 @@ Column reviveColumn(ConstantReader cr) {
|
|||
return Column(
|
||||
isNullable: cr.peek('isNullable')?.boolValue ?? false,
|
||||
length: cr.peek('length')?.intValue ?? 255,
|
||||
defaultValue: cr.peek('defaultValue')?.objectValue,
|
||||
type: columnType,
|
||||
indexType: indexType,
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue