mysql2-migrations
v3.4.1
Published
Create and manager migrations with mysql2 in module type apps
Maintainers
Readme
Mysql2 Migrations :: Type module projects
- Create and manager migrations with mysql2 from repositories with configuration "TYPE MODULE"
📫 Disclaimer
- This package must be used with MODULE TYPE IMPORT AND FROM
- Set config on package.json "type": "module"
🧠 Configuration
step 1
- Install dependencies
- Should install 'mysql2' dependency in your projects first
npm i mysql2 npm i mysql2-migrationsstep 2
- Execute script to add files configuration in environment (reminder: add credentials in Migration instance on finalize)
npx mysql2-migrations initstep 3 (Optional configure environment yourself)
- 1.Create a folder in root app with name "mysql2-migrations"
- 2.Create a "migrations_config.js" file in "mysql2-migrations" folder with next configuration(add your db credentials here)
import Migration from 'mysql2-migrations' const db_query = new Migration() db_query.database = "test" db_query.user = "root" db_query.password = "password" db_query.host = "127.0.0.1" db_query.port = 3306 db_query.name_table_migrations = "table_migrations_app" // two characters minimum db_query.show_query = true db_query.skip_migration_error = true // use for skip some migration file with some error and continue with the rest of migration files, but this can cause problems if the error is in the query and not in the migration file, example: when use migrations files without "down" query db_query.show_depuration = true // show depuration on finalize migration, recommended db_query.start()3.Create a subfolder "migrations" into "mysql2-migrations" folder:
- root_app/
- mysql2-migrations/
- migrations_config.js
- migrations/
- mysql2-migrations/
- root_app/
4.Add scripts commands to package.json configuration:
"scripts": { "db_create": "node mysql2-migrations/migrations_config.js create", "db_refresh": "node mysql2-migrations/migrations_config.js refresh", "db_migrate_all": "node mysql2-migrations/migrations_config.js migrate", "db_migrate": "node mysql2-migrations/migrations_config.js up", "db_rollback": "node mysql2-migrations/migrations_config.js down", "db_status": "node mysql2-migrations/migrations_config.js status" }
👋 Description script commnads
db_create
Create file to migrate:
npm run db_create create_users_tablenpm run db_create alter_sales_tableEdit migration file with query
db_migrate
Migrate last file pending:
npm run db_migrateMigrate file with index: (check index with "npm run db_status")
npm run db_migrate <index>db_migrate_all
Migrate all files pending:
npm run db_migrate_allIf already exists:
db_rollback
Undo latest migration:
npm run db_rollbackUndo migration with index: (check index with "npm run db_status")
npm run db_rollback <index>db_status
Check migrations integrity, check indexes of pending and executed migrations:
npm run db_statusdb_refresh
Undo and redo all migrations (CAUTION DATA LOSS, It is not recommended to do it ):
npm run db_refreshtoo You can also UP or DOWN direct migrations
DIRECT MIGRATIONS WILL NOT BE SAVED IN THE "table_migrations_app" TABLE
Don't mixed direct migrations with stored migrations in database
Use this function only for execute some querys that are not necessary to be stored in the database and that you are sure that will not cause problems in the future.
This file name is example(image), dont use timestamp, you can to use unformatted names in files: example: "direct_create_users_table.js"
Example type up:
node mysql2-migrations/migrations_config.js run direct_create_users_table.js upExample type down:
node mysql2-migrations/migrations_config.js run direct_create_users_table.js down
🔥 Others
- Help
npx mysql2-migrations help