Alter Table
interface AlterTable
Provides a set of functions to perform ALTER TABLE operations on a database table.
This interface defines the contract for modifying the structure of an existing table, such as renaming the table, renaming a column, adding a new column, or dropping an existing column. Implementations of this interface are responsible for generating and executing the appropriate SQL ALTER TABLE statements.
Example
class MigrationV12 : KQLiteMigration(1, 2) {
override fun migrate(database: KQLiteDatabase) {
// It is already in EXCLUSIVE TRANSACTION
// Fetching schema object
val schema = database.schema()
// RENAME TABLE: 'users_old' to 'users'
// Assuming the `Users` table name has been changed to 'users'
// ALTER TABLE `users_old` RENAME TO `users`;
schema
.alterTable(Users)
.renameTable("users_old", Users.tableName)
// RENAME COLUMN: 'first_name' to 'fullName'
// Assuming the `Users` table object now has a `fullName` column definition
// with updated column name as 'fullName'.
// ALTER TABLE `Users` RENAME COLUMN `first_name` TO `fullName`;
schema
.alterTable(Users)
.renameColumn("first_name", Users.fullName)
// ADD COLUMN: 'email' to the 'Users' table.
// Assuming the `Users` table object now includes an `email` column definition.
// ALTER TABLE `Users` ADD COLUMN `email` TEXT NOT NULL DEFAULT '';
schema
.alterTable(Users)
.addColumn(Users.email)
// DROP COLUMN: 'age' from the 'Users' table.
// Assuming the `age` property has been removed from the `Users` table object.
// ALTER TABLE `Users` DROP COLUMN `age`;
.alterTable(Users)
.dropColumn("age")
}
}Content copied to clipboard
Author
MOHAMMAD AZIM ANSARI