changeColumnType method

  1. @override
void changeColumnType(
  1. String name,
  2. ColumnType type, {
  3. int length = Column.defaultLength,
  4. int? precision,
  5. int? scale,
  6. bool? isNullable,
  7. Object? defaultValue,
  8. bool? autoIncrement,
  9. String? using,
})
inherited

Changes the type of the column name to type, with its length, precision and scale, which are as for a Column: without a precision or scale, a numeric is unbounded on PostgreSQL, so that its values are kept, and numeric(17, 3) on MySQL and MariaDB.

The column has the constraints given after the change: it is nullable or not as isNullable, has the default defaultValue, or none if it is noDefault, and on MySQL and MariaDB is AUTO_INCREMENT or not as autoIncrement. Those not given (null) are kept as they are; a default that the new type cannot have, such as 'n/a' of an int, is removed with noDefault. MySQL and MariaDB define the whole column again, so the migration runner reads them from the database first; they keep its NOT NULL, DEFAULT, AUTO_INCREMENT, ON UPDATE, UNSIGNED, generation, INVISIBLE and COMMENT, its CHARACTER SET and COLLATE for a type of text, and on MariaDB its CHECK, where the new type can have them.

On PostgreSQL, using is the expression that converts the values when there is no implicit cast, such as price::numeric from text.

Throws an ArgumentError if type has a precision and precision is not positive, or has a scale and scale is negative or greater than the precision.

Implementation

@override
void changeColumnType(
  String name,
  ColumnType type, {
  int length = Column.defaultLength,
  int? precision,
  int? scale,
  bool? isNullable,
  Object? defaultValue,
  bool? autoIncrement,
  String? using,
}) {
  // MODIFY converts the values itself, so `using` is not needed.
  var modification = ColumnModification(
    name,
    type,
    length: length,
    precision: precision,
    scale: scale,
    isNullable: isNullable,
    defaultValue: defaultValue,
    autoIncrement: autoIncrement,
  );
  // Checks the precision and scale against the database's limits when the
  // change is declared, before any of the schema is run.
  modification.compile(compileColumn);
  _modify(modification);
}