Advanced Topics
Schema Management
Schema Management in Tauri SQLite ORM.
Manual Schema Management
For complex database migrations like renaming a column or for use in custom migration scripts, the ORM provides a suite of low-level helper methods. These give you direct, granular control over your database schema.
These methods should be used with caution, as they directly manipulate the database structure and can lead to data loss if used improperly. They are excellent tools for tasks that the automatic migrator does not handle.
Table Helpers
| Method | Description | Example |
|---|---|---|
db.doesTableExist(tableName) | Checks if a table with the given name exists. Returns a Promise<boolean>. | const exists = await db.doesTableExist('logs'); |
db.dropTable(tableName) | Drops a table from the database. | await db.dropTable('old_settings'); |
db.renameTable(from, to) | Renames an existing table. | await db.renameTable('users', 'app_users'); |
Column Helpers
| Method | Description | Example |
|---|---|---|
db.doesColumnExist(tbl, col) | Checks if a column exists on a given table. Returns Promise<boolean>. | const exists = await db.doesColumnExist('users', 'last_login'); |
db.dropColumn(tableName, colName) | Drops a column from a table. Requires a modern SQLite version. | await db.dropColumn('posts', 'legacy_views'); |
db.renameColumn(tbl, from, to) | Renames a column on an existing table. | await db.renameColumn('users', 'fullName', 'full_name'); |
Example: A Custom Migration Script
You can combine these helpers to create sophisticated, versioned migration scripts that handle changes the automatic system cannot.
// my-migration-script.ts
async function runMigrationV2() {
console.log('Running migration to version 2...');
// V2 changes the 'posts.title' column to 'headline'
const columnExists = await db.doesColumnExist('posts', 'title');
if (columnExists) {
console.log("Renaming 'posts.title' to 'headline'...");
await db.renameColumn('posts', 'title', 'headline');
console.log('Rename complete.');
} else {
console.log("Column 'posts.title' not found, skipping rename.");
}
}
runMigrationV2();