renameTable method

Future<void> renameTable(
  1. TableInfo<Table, dynamic> table,
  2. String oldName
)

Changes the table name from oldName to the current TableInfo.actualTableName.

After renaming a table in drift or Dart and re-running the generator, you can use renameTable in a migration step to rename the table in existing databases.

Implementation

Future<void> renameTable(TableInfo table, String oldName) async {
  final context = _createContext();
  context.buffer.write(switch (context.dialect) {
    SqlDialect.mariadb => 'RENAME TABLE ${context.identifier(oldName)} '
        'TO ${context.identifier(table.actualTableName)};',
    _ => 'ALTER TABLE ${context.identifier(oldName)} '
        'RENAME TO ${context.identifier(table.actualTableName)};',
  });

  return _issueCustomQuery(context.sql);
}