renameColumn method
Future<void>
renameColumn(
- TableInfo<
Table, dynamic> table, - String oldName,
- GeneratedColumn<
Object> column
Changes the name of a column in a table.
After renaming a column in a Dart table or a drift file and re-running the generator, you can use renameColumn in a migration step to rename the column for existing databases.
The table argument must be set to the table enclosing the changed
column. The oldName must be set to the old name of the column in SQL.
For Dart tables, note that drift will transform camelCase column names
in Dart to snake_case column names in SQL.
Important compatibility information: renameColumn uses an
ALTER TABLE RENAME COLUMN internally. Support for that syntax was added
in sqlite version 3.25.0, released on 2018-09-15.
In MariaDB, support for that same syntax was added in MariaDB version
10.5.2, released on 2020-03-26.
Implementation
Future<void> renameColumn(
TableInfo table,
String oldName,
GeneratedColumn column,
) async {
final context = _createContext();
context.buffer
..write('ALTER TABLE ${context.identifier(table.aliasedName)} ')
..write('RENAME COLUMN ${context.identifier(oldName)} ')
..write('TO ${column.escapedNameFor(context.dialect)};');
return _issueCustomQuery(context.sql);
}