Package: mat
MAT - Migration Assistant ToolManage your database versions with pure SQLMAT allows you to write your app migrations, rollbacks and data seeding scripts in pure sql, no matter which languages or frameworks your application is build onSupported databasesCurrently following databases are supported:MySQLCompatibility for following databases is in developmentSQLitePostgresSQL ServerInstallPython PIPYou can use pip:pipinstallmatMAT depends on python 3, in some systems you would want to use 'pip3' to install programs under python3 environmentUsage through dockerYou don't know pip/python? No problem, you can use 'mat' through docker.I have created a wrapper script that will invokematinside docker whenever you need it, just download it inside your migrations directory and use it the same way you would do withmatcommandGet scriptwgethttps://raw.githubusercontent.com/DiganmeGiovanni/Mat/master/docker/matw
chmod+xmatw# Grant exec permissions to wrapperThen you can use the wrapper the same way asmatcommand./matwstatus
./matwmigrate-s2Check 'Getting started' section to understand what above commands meansBehind scenes the script will:Create a docker container ready to run 'mat'Mount current path '$PWD' as a volume in /mat container pathInvoke 'mat' command inside container by forwarding argumentsHow it works?You write your migration scripts in multiple sql files (One per migration)You write rollback migration scripts (One per migration)You setup database connection and scripts location in a configuration fileRunmatto apply/unapply/list your migrationsGetting startedPrepare migrations directoryCreate a directory to hold all your database migrationsDirectory location is up to you, but usually you'll want to place it
inside your main project or as an independent repository in cases where
multiple applications will access same databaseYour directory should have a structure like this:|-migrations/|-|-migrations.yml|-|-up/|-|--v1_create_tables.sql|-|--v2_create_credentials_table.sql|-|-down/|-|--v1_undo_create_tables.sql|-|--v2_destroy_credentials_table.sqlWhere:Theup/directory will hold all your migrations scriptsThedown/directory will hold all your rollback scriptsThemigrations.ymlwill be used for setup to specify how to access databaseYou can use whatever name you want for your migration directories as long as
you specify the right paths in the config fileCreate your migrationsPlace your first migration insideup/directory in a sql file, ensure to name the file like:V<version_number>_<snake_case_name>.sqlFollow naming convention is essential in order to run migrations
in appropriate order and shows right name to youLet's say you have following file namedv1_create_tables.sql(which will be parsed as: Version:V1, name:Create tables)CREATETABLEuser(idINTEGERPRIMARYKEYAUTO_INCREMENT,first_nameVARCHAR(255)NOTNULL,last_nameVARCHAR(255)NOTNULL,emailVARCHAR(500)NOTNULL);CREATETABLEcar(idINTEGERPRIMARYKEYAUTO_INCREMENT,nameVARCHAR(255)NOTNULL,user_idINTEGERNOTNULL,CONSTRAINTfk_userFOREIGNKEY(user_id)REFERENCESuser(id))Create your rollbacksPlace your rollback insidedown/directory in a sql file, following same name conventions as per migrations:V<version_number>_snake_case_name>.sqlVersion number must match exactly with version in migration file, however you can use
whatever you want for the name partLet's say you have:v1_undo_create_tables.sql:DROPTABLEcar;DROPTABLEuser;Setup database connection and settingsPlace the database connection settings intomigrations.ymlfile:datasource:host:192.168.100.43username:developerpassword:d3v3lopm3ntdatabase:carsmigrations_path:up:up/down:down/And now let 'mat' do it's magic ;)Using matcdinto migrations directory and run one of following commandsmatstatus# Will list all your migrationsmatmigrate# Will apply your migrationsmatmigrate-s2# Will apply only '2' non applied migrationsmatrollback# Will run you rollback scriptsmatrollback-s2# Will run rollback scripts for latest '2' applied migrations