Add Migrator.renameTable (#956)

This commit is contained in:
Simon Binder 2020-12-04 21:15:43 +01:00
parent 74fc2cea3d
commit cf8215ecfe
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 44 additions and 0 deletions

View File

@ -7,6 +7,7 @@
- Added extensions for `isNull` and `isNotNull`
- Support creating a `VmDatabase` from a raw sqlite3 `Database` via `VmDatabase.opened`
- New `named_parameters` build option to generate named parameters for named variables in moor files
- Added `Migrator.renameTable` to rename tables
## 3.4.0

View File

@ -367,6 +367,19 @@ class Migrator {
return _issueCustomQuery(context.sql);
}
/// Changes the [table] name from [oldName] to the current
/// [TableInfo.actualTableName].
///
/// After renaming a table in moor or Dart and re-running the generator, you
/// can use [renameTable] in a migration step to rename the table in existing
/// databases.
Future<void> renameTable(TableInfo table, String oldName) async {
final context = _createContext();
context.buffer.write('ALTER TABLE ${escapeIfNeeded(oldName)} '
'RENAME TO ${escapeIfNeeded(table.actualTableName)};');
return _issueCustomQuery(context.sql);
}
/// Executes the custom query.
@Deprecated('Use customStatement in the database class')
Future<void> issueCustomQuery(String sql, [List<dynamic>? args]) {

View File

@ -104,6 +104,36 @@ void main() {
expect(item.category, isNull);
});
test('rename tables', () async {
// Create todos table with old name
final executor = VmDatabase.memory(setup: (db) {
db.execute('''
CREATE TABLE todos_old_name (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
target_date INTEGER NOT NULL,
category INTEGER NULL
);
''');
db.execute('INSERT INTO todos_old_name (title, content, target_date) '
"VALUES ('title', 'content', 0)");
});
final db = TodoDb(executor);
db.migration = MigrationStrategy(
onCreate: (m) async {
await m.renameTable(db.todosTable, 'todos_old_name');
},
);
// basic check to ensure we can query the table by its new name and that
// we have all the necessary data.
final entry = await db.select(db.todosTable).getSingle();
expect(entry.title, 'title');
});
test('add columns with default value', () async {
final executor = VmDatabase.memory(setup: (db) {
// Create todos table without content column