Migrator is a command-line tool designed to simplify the management of databases using migrations. It enables users to create, execute, and roll back migrations, ensuring a smooth and controlled evolution of the database schema.
Current version: 0.5
Guilherme Isaías wgsilva.isaias@gmail.com
Make sure your system meets the following requirements:
Go: Ensure that Go is installed on your machine. You can download and install it from the official Go website.
Database Client Tools: For the schema dump feature, you must have the native client tools for your database installed on your machine (e.g., mysqldump for MySQL, pg_dump for PostgreSQL, sqlite3 for SQLite).
Docker (optional): If you plan to build and run the migrator tool in a Docker container, make sure Docker is installed on your system. You can find instructions for installing Docker here. The provided Docker image already includes the necessary database client tools.
- Clone the repository to your local machine:
git clone https://github.com/guilhermewebdev/migrator.gitChange into the project directory:
cd migratorRun the build command using the provided Makefile:
make buildThis will compile the migrator tool. Install the migrator tool globally on your system:
make installThis will copy the compiled executable to /usr/local/bin/, making it accessible system-wide.
You can also use a pre-built Docker image for Migrator available on Docker Hub. The image is hosted at guilhermewebdev/migrator. To run Migrator using Docker, use the following command:
docker run -it guilhermewebdev/migrator:latest migrate [global options] command [command options] [arguments...]Replace [global options], command, [command options], and [arguments...] with the specific options and commands you want to execute.
# Create a new migration using Docker
docker run -it guilhermewebdev/migrator:latest migrate new migration_nameThis will execute the specified Migrator command inside a Docker container based on the provided image.
Note: Ensure that you have Docker installed on your machine. You can find instructions for installing Docker here.
migrate [global options] command [command options] [arguments...]-
init
- Create a new config file.
-
new
- Creates a new migration.
-
up
- Executes the next migration.
-
down
- Rolls back the last migration.
-
unlock
- Unlocks migrations.
-
latest
- Performs missing migrations.
-
schema, dump
- Dump the database schema to a SQL file.
-
settings
- Show settings.
-
help, h
- Shows a list of commands or help for one command.
-
--conf-file FILE, -c FILE- Load configuration from FILE (default: "migrator.yml").
-
--migrations value, -m value- Select the migrations directory (default: "./migrations").
-
--dsn value, -d value- Database connection string.
-
--driver value, -r value- Database driver (mysql, postgres, sqlserver, sqlite, sqlite3, or oracle).
-
--table value, -t value- Migrations table name (default: "migrations").
-
--auto-dump- Auto dump schema after migrations (default: false).
-
--schema-file value- Path to the schema dump file (default: "./schema.sql").
-
--help, -h- Show help.
-
--version, -v- Print the version.
# Create a new migration
migrate -c ./db/migrator.yml new <migration_name>
# Execute the next migration
migrate --driver mysql up
# Rollback the last migration
migrate down
# Unlock migrations
migrate unlock
# Perform missing migrations
migrate latest
# Dump the database schema
migrate schemaYou can apply configurations to Migrator using environment variables. The following variables are supported:
DB_DSN: Database connection string.DB_DRIVER: Database driver (mysql, postgres, sqlserver, sqlite, sqlite3, or oracle).MIGRATIONS_DIR: Select the migrations directory (default: "./migrations").MIGRATIONS_TABLE: Migrations table name (default: "migrations").AUTO_DUMP_SCHEMA: Auto dump schema after migrations (default: false).SCHEMA_FILE_PATH: Path to the schema dump file (default: "./schema.sql").
To set these variables, you can use your shell's syntax. For example, in Bash:
export DB_DSN="your_database_connection_string"
export DB_DRIVER="your_database_driver"
export MIGRATIONS_DIR="your_migrations_directory"
export MIGRATIONS_TABLE="your_migrations_table_name"By default, Migrate looks for a configuration file named "migrator.yml" for global settings. You can specify an alternative configuration file using the --conf-file option.
The migrator.yml file is used to configure settings for the Migrator command-line tool. Below is an example of the configuration file syntax along with explanations of each parameter:
# Example migrator.yml Configuration File
# Directory where migration files are stored
migrations_dir: ./migrations
# Name of the table to track migrations in the database
migrations_table_name: migrations
# If true, generates the database schema automatically after running migrations
auto_dump_schema: false
# Path where the database schema SQL file will be generated
schema_file_path: ./schema.sql
# Database connection string (DSN)
db_dsn: "postgres://user:pass@postgres:5432/test?sslmode=disable"
# Database driver (Supported drivers: mysql, postgres, sqlserver, sqlite, sqlite3, oracle)
db_driver: postgresExplanation:
-
migrations_dir: Specifies the directory where migration files are located. In the example, migrations are expected to be in the./migrationsdirectory. You can customize this path according to your project structure. -
migrations_table_name: Defines the name of the table used to track migrations in the database. The default is set to "migrations," but you can modify it based on your preferences. -
auto_dump_schema: If set totrue, the tool will automatically generate a database schema dump after each successful migration operation (up,down,latest). -
schema_file_path: The file path where the generated schema dump will be saved. -
db_dsn: Represents the Database Source Name (DSN), which contains information about the database connection. In the example, a PostgreSQL database connection string is provided. Update this with the appropriate credentials and connection details for your database. -
db_driver: Specifies the database driver to be used (e.g., mysql, postgres, sqlserver, sqlite, sqlite3, oracle). In the example, the driver is set to "postgres." Choose the appropriate driver based on your database system.
Ensure that the information in the migrator.yml file accurately reflects your database setup. You can customize these parameters to suit your project's requirements. If needed, refer to the Global Options section in the README for additional options that can be specified when running Migrate commands.
For additional information on each command and its options, use:
migrate [command] --helpThank you for using Migrator! If you have any questions or feedback, please contact Guilherme Isaías at wgsilva.isaias@gmail.com.