This Zsh script provides functionality to manage database migration files by automatically renumbering them and handling up/down migration files. It supports adding new migrations and reverting existing ones while ensuring the correct sequence of file prefixes.
-
Add New Migration Files:
- Inserts new migration files (
.up.sqland.down.sql) into the specified directory. - Automatically renumbers existing migration files to create space for the new migration.
- Inserts new migration files (
-
Revert Migration Files:
- Removes both
.up.sqland.down.sqlfiles for a specified migration. - Renumbers files following the removed migration to maintain sequential order.
- Removes both
./migration_editor.zsh <folder_path> <new_file_name> [--revert]<folder_path>: The directory containing migration files.<new_file_name>: The name of the new migration file, starting with a 6-digit prefix.[--revert]: Optional flag to remove a migration file and renumber subsequent files.
Migration files must follow this naming pattern:
000001_description.up.sql(up migration file)000001_description.down.sql(down migration file)
To add a new migration file 000002_add_transactions.sql:
./migration_editor.zsh db/migrations 000002_add_transactions.sqlBefore:
000001_create_users.sql
000002_add_column.sql
000003_update_schema.sql
After:
000001_create_users.sql
000002_add_transactions.up.sql
000002_add_transactions.down.sql
000003_add_column.sql
000004_update_schema.sql
To remove the migration 000002_add_transactions.sql:
./migration_editor.zsh db/migrations 000002_add_transactions.sql --revertBefore:
000001_create_users.sql
000002_add_transactions.up.sql
000002_add_transactions.down.sql
000003_add_column.sql
000004_update_schema.sql
After:
000001_create_users.sql
000002_add_column.sql
000003_update_schema.sql
-
If the folder does not exist, the script will exit with an error:
Error: Folder <folder_path> does not exist! -
If the new file name does not start with a 6-digit prefix, the script will exit with an error:
Error: The new file name must start with a 6-digit prefix!
- The script assumes all migration files in the directory are correctly formatted with a 6-digit prefix.
- It sorts files alphanumerically to determine sequence order.
This script is open-source and available under the MIT License.