This package allows you easily generate migration files for your AdonisJS app from an existing Database, including indexes and foreign keys!
- MySQL
- SQLite
- PostgreSQL
You can install the package via composer:
npm install @shagital/adonisjs-migrations-generator
Or with yarn
yarn add @shagital/adonisjs-migrations-generator
Open start/app.js
and add @shagital/adonisjs-migrations-generator/src/Commands/MigrationsGeneratorCommand
to the commands array
- Note that you can replace the
adonis
withnode ace
if adonis is not installed globally on your system.
Will generate migration files for all tables in the DB set as default, and save files in database/migrations
directory
adonis migration:generate
adonis migration:generate --include=table1,table2
adonis migration:generate --exclude=table1,table2
NOTE: Connection type must have been specified in
config/databases
adonis migration:generate --connection=mysql2
NOTE: The package will attempt to create the specified directory if it doesn't exist
adonis migration:generate --path=/var/www/html/backups
This option is suitable for a fresh application install
NOTE: The directory and all it's content will be deleted before the directory is recreated! ENSURE THE DIRECTORY DOESN'T CONTAIN FILES YOU PREFER TO KEEP!!!
adonis migration:generate --force
[MySQL] Because we're generating migrations are being generated from an existing DB, when you run the migration, you might run into foreign key constraint errors because tables might not be created before been referenced in another table. This option adds a command to disable foreign key check so your migrations can run smoothly
adonis migration:generate --disable-fkc
'use strict';
/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema');
/** @type {import('@adonisjs/lucid/src/Database')} */
const Database = use('Database');
class UsersSchema extends Schema {
static get connection() {
return 'mysql'
}
up() {
return Promise.all([
Database.raw("SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS;"),
Database.raw("SET FOREIGN_KEY_CHECKS=0;")
]).then(() => {
this.create('users', (table) => {
table.increments('id').unsigned();
table.string('username', 80).unique();
table.string('first_name', 80).nullable();
table.string('last_name', 80).nullable();
table.string('email', 100).unique();
table.string('password', 255);
table.timestamp('deleted_at').nullable();
table.timestamp('created_at').defaultTo(Database.fn.now());
table.timestamp('updated_at').defaultTo(Database.fn.now());
table.index(['username'], 'users_username_unique_22');
table.index(['email'], 'users_email_unique_76');
});
});
}
down() {
return Promise.all([
Database.raw("SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS;"),
Database.raw("SET FOREIGN_KEY_CHECKS=0;")
]).then(() => {
this.dropIfExists('users');
});
}
}
module.exports = UsersSchema
Please see CHANGELOG for more information what has changed recently.
If you have a feature you'd like to add, kindly send a Pull Request (PR)
If you discover any security related issues, please email zacchaeus@shagital.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.