knight-maria-migration
v0.0.1
Published
A database schema migration lib for MariaDB
Downloads
3
Readme
Knight Maria Migration by Coderitter
A database schema migration lib for MariaDB.
Related packages
There is an SQL package knight-sql which helps with building SQL strings. It can be combined with knight-criteria and knight-sql-criteria-filler. If you are looking for a more sophisticated version for database access you can also use knight-orm.
Another helpful MariaDB tool is knight-maria-transaction.
Install
npm install knight-maria-migration
Overview
The migration
import { MariaMigration } from 'knight-maria-migration'
import { Pool } from 'mariadb'
class DbMigration extends MariaMigration {
constructor(pool: Pool) {
super(pool)
}
async migrate() {
await this.version1()
}
async version1() {
if (await this.getVersion() >= 1) {
return
}
await this.pool.query(
'CREATE TABLE IF NOT EXISTS User (' +
'id SERIAL PRIMARY KEY, ' +
'email VARCHAR(255), ' +
'firstName VARCHAR(255), ' +
'lastName VARCHAR(255)' +
');')
await this.increaseVersion()
}
}
let migration = new DbMigration
migration.migrate()
The available migration methods
// check if the version table exists
let exists = await migration.versionTableExists()
// create the version table but only if it not exists
await migration.createVersionTable()
// get the version from the version table
let version = await migration.getVersion()
// set the version explicitly (mistakes are your own)
await migration.setVersion(4)
// increase the version to the next version number
let increasedVersion = await migration.increaseVersion()
// get all tables of the database
let tables = await migration.getTables()
// delete all tables from the database
let clearedTables = await migration.clearDatabase()
// clear the database and recreate the schema
await migration.resetDatabase()