TableBlueprint.fromJson constructor

TableBlueprint.fromJson(
  1. Map<String, Object?> json
)

Implementation

factory TableBlueprint.fromJson(Map<String, Object?> json) {
  final operation = TableOperationKind.values.byName(
    json['operation'] as String,
  );
  final table = json['table'] as String;
  final blueprint = operation == TableOperationKind.create
      ? TableBlueprint.create(table)
      : TableBlueprint.alter(table);
  if (json['temporary'] == true) {
    blueprint.markTemporary();
  }

  final columnMaps = (json['columns'] as List?) ?? const [];
  for (final column in columnMaps.cast<Map<String, Object?>>()) {
    final command = ColumnCommand.fromJson(column);
    switch (command.kind) {
      case ColumnCommandKind.add:
      case ColumnCommandKind.alter:
        blueprint._columns.add(
          _ColumnEntry(
            name: command.name,
            kind: command.kind,
            definition: command.definition,
          ),
        );
      case ColumnCommandKind.drop:
        blueprint._columns.add(_ColumnEntry.drop(command.name));
    }
  }

  final renames = (json['renamedColumns'] as List?) ?? const [];
  for (final rename in renames.cast<Map<String, Object?>>()) {
    blueprint._renamedColumns.add(ColumnRename.fromJson(rename));
  }

  final indexCommands = (json['indexes'] as List?) ?? const [];
  for (final indexJson in indexCommands.cast<Map<String, Object?>>()) {
    final command = IndexCommand.fromJson(indexJson);
    switch (command.kind) {
      case IndexCommandKind.add:
        if (command.definition != null) {
          blueprint._indexes.add(_IndexEntry(command.definition!));
        }
      case IndexCommandKind.drop:
        blueprint._droppedIndexes.add(command.name);
    }
  }

  final foreignCommands = (json['foreignKeys'] as List?) ?? const [];
  for (final foreignJson in foreignCommands.cast<Map<String, Object?>>()) {
    final command = ForeignKeyCommand.fromJson(foreignJson);
    switch (command.kind) {
      case ForeignKeyCommandKind.add:
        if (command.definition != null) {
          blueprint._foreignKeys.add(ForeignKeyEntry(command.definition!));
        }
      case ForeignKeyCommandKind.drop:
        blueprint._droppedForeignKeys.add(command.name);
    }
  }

  return blueprint;
}