2021-05-17 15:34:19 +00:00
|
|
|
import 'package:angel3_orm/angel3_orm.dart';
|
2019-02-14 17:15:34 +00:00
|
|
|
|
|
|
|
class MigrationColumn extends Column {
|
|
|
|
final List<MigrationColumnReference> _references = [];
|
2021-05-02 07:36:33 +00:00
|
|
|
late bool _nullable;
|
2021-07-19 04:32:45 +00:00
|
|
|
late IndexType _index;
|
2019-02-14 17:15:34 +00:00
|
|
|
dynamic _defaultValue;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isNullable => _nullable;
|
|
|
|
|
|
|
|
@override
|
2021-07-19 04:32:45 +00:00
|
|
|
IndexType get indexType => _index;
|
2019-02-14 17:15:34 +00:00
|
|
|
|
2021-05-17 15:34:19 +00:00
|
|
|
dynamic get defaultValue => _defaultValue;
|
2019-02-14 17:15:34 +00:00
|
|
|
|
|
|
|
List<MigrationColumnReference> get externalReferences =>
|
2021-05-17 15:34:19 +00:00
|
|
|
List<MigrationColumnReference>.unmodifiable(_references);
|
2019-02-14 17:15:34 +00:00
|
|
|
|
2021-07-19 04:32:45 +00:00
|
|
|
MigrationColumn(ColumnType type,
|
|
|
|
{bool isNullable = true,
|
2022-01-02 07:21:17 +00:00
|
|
|
int length = 255,
|
2021-07-19 04:32:45 +00:00
|
|
|
IndexType indexType = IndexType.standardIndex,
|
|
|
|
defaultValue})
|
2019-02-14 17:15:34 +00:00
|
|
|
: super(type: type, length: length) {
|
|
|
|
_nullable = isNullable;
|
|
|
|
_index = indexType;
|
|
|
|
_defaultValue = defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
factory MigrationColumn.from(Column column) => column is MigrationColumn
|
|
|
|
? column
|
2021-05-17 15:34:19 +00:00
|
|
|
: MigrationColumn(column.type,
|
2019-02-14 17:15:34 +00:00
|
|
|
isNullable: column.isNullable,
|
|
|
|
length: column.length,
|
|
|
|
indexType: column.indexType);
|
|
|
|
|
|
|
|
MigrationColumn notNull() => this.._nullable = false;
|
|
|
|
|
|
|
|
MigrationColumn defaultsTo(value) => this.._defaultValue = value;
|
|
|
|
|
|
|
|
MigrationColumn unique() => this.._index = IndexType.unique;
|
|
|
|
|
|
|
|
MigrationColumn primaryKey() => this.._index = IndexType.primaryKey;
|
|
|
|
|
|
|
|
MigrationColumnReference references(String foreignTable, String foreignKey) {
|
2021-05-17 15:34:19 +00:00
|
|
|
var ref = MigrationColumnReference._(foreignTable, foreignKey);
|
2019-02-14 17:15:34 +00:00
|
|
|
_references.add(ref);
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MigrationColumnReference {
|
|
|
|
final String foreignTable, foreignKey;
|
2021-05-02 07:36:33 +00:00
|
|
|
String? _behavior;
|
2019-02-14 17:15:34 +00:00
|
|
|
|
|
|
|
MigrationColumnReference._(this.foreignTable, this.foreignKey);
|
|
|
|
|
2021-05-02 07:36:33 +00:00
|
|
|
String? get behavior => _behavior;
|
2019-02-14 17:15:34 +00:00
|
|
|
|
|
|
|
StateError _locked() =>
|
2021-05-17 15:34:19 +00:00
|
|
|
StateError('Cannot override existing "$_behavior" behavior.');
|
2019-02-14 17:15:34 +00:00
|
|
|
|
|
|
|
void onDeleteCascade() {
|
|
|
|
if (_behavior != null) throw _locked();
|
|
|
|
_behavior = 'ON DELETE CASCADE';
|
|
|
|
}
|
|
|
|
|
|
|
|
void onUpdateCascade() {
|
|
|
|
if (_behavior != null) throw _locked();
|
|
|
|
_behavior = 'ON UPDATE CASCADE';
|
|
|
|
}
|
|
|
|
|
|
|
|
void onNoAction() {
|
|
|
|
if (_behavior != null) throw _locked();
|
|
|
|
_behavior = 'ON UPDATE NO ACTION';
|
|
|
|
}
|
|
|
|
|
|
|
|
void onUpdateSetDefault() {
|
|
|
|
if (_behavior != null) throw _locked();
|
|
|
|
_behavior = 'ON UPDATE SET DEFAULT';
|
|
|
|
}
|
|
|
|
|
|
|
|
void onUpdateSetNull() {
|
|
|
|
if (_behavior != null) throw _locked();
|
|
|
|
_behavior = 'ON UPDATE SET NULL';
|
|
|
|
}
|
|
|
|
}
|