toPgSqlFragment method

String toPgSqlFragment()

Implementation

String toPgSqlFragment() {
  String out = '';

  if (name == 'id') {
    // The id column is special.
    assert(isNullable == false);
    assert(
      columnType == ColumnType.integer || columnType == ColumnType.bigint,
    );
    // TODO: Migrate to bigserial / bigint
    return '"id" serial PRIMARY KEY';
  }

  var nullable = isNullable ? '' : ' NOT NULL';
  String type;
  switch (columnType) {
    case ColumnType.bigint:
      type = 'bigint';
      break;
    case ColumnType.boolean:
      type = 'boolean';
      break;
    case ColumnType.bytea:
      type = 'bytea';
      break;
    case ColumnType.doublePrecision:
      type = 'double precision';
      break;
    case ColumnType.integer:
      type = 'integer';
      break;
    case ColumnType.json:
      type = 'json';
      break;
    case ColumnType.text:
      type = 'text';
      break;
    case ColumnType.timestampWithoutTimeZone:
      type = 'timestamp without time zone';
      break;
    case ColumnType.uuid:
      type = 'uuid';
      break;
    case ColumnType.unknown:
      throw (const FormatException('Unknown column type'));
  }

  out += '"$name" $type$nullable';
  return out;
}