Maggy is a tool written in PHP to facilitate the making and applying of migrations for SQL databases.
In order to install Maggy:
- Clone this repository
git clone https://github.com/JoelNiemela/Maggy.git - Download an SQL server, such as
mariadb - Configure
config.inito connect to your database
In Maggy, each migration is described by a Maggy file. Maggy files consist of vanilla SQL with Maggy macros that enable maggy migrate and maggy rollback.
Maggy files end with the .sql.maggy file extension;
An example Maggy file:
--@Version 42 "Add user table"
--@Up
CREATE TABLE IF NOT EXISTS user (
user_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
--@Down
DROP TABLE user;There are three types of Macros in Maggy:
- Segment macros (
--@Segment):- Introduce new segments, such as
--@Upand--@Down. - Declare the context of a file with the
--@Versionmacro.
- Introduce new segments, such as
- Attribute macros (
--#Attribute):- Modify the behavior of SQL queries or other macros.
- Command macros (
--!Command):- Enable Maggy for a database with the
--!Maggymacro. - Shorthand for SQL queries.
- Enable Maggy for a database with the
Every Maggy file must start with the --@Version macro.
--@Version [version] [description]- Declare migration version of a file, along with a short description describing the migration.
--@Up- Initiates the
Upsegment; all subsequent lines (until the next segment macro) belong to theUpsegment.
- Initiates the
--@Down- Initiates the
Downsegment; all subsequent lines (until the next segment macro) belong to theDownsegment.
- Initiates the
--!Maggy- Enables Maggy support for the database. The first migration for a database with Maggy should use this command.