Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/semisedlak/migratte

Migratte is simple SQL migrations management standalone CLI app, framework-agnostic.
https://github.com/semisedlak/migratte

database migrations php

Last synced: 2 months ago
JSON representation

Migratte is simple SQL migrations management standalone CLI app, framework-agnostic.

Awesome Lists containing this project

README

        

Migratte
========

- [How it works](#how-it-works)
- [Migration anatomy](#migration-anatomy)
- [Configuration](#configuration)
- [Executable file](#executable-file)
- [Commands](#commands)
- [`migratte:generate`](#migrattegenerate)
- [`migratte:commit`](#migrattecommit)
- [`migratte:rollback`](#migratterollback)
- [`migratte:status`](#migrattestatus)
- [`migratte:info`](#migratteinfo)
- [Are you using Nette Framework?](#are-you-using-nette-framework)
- [Changelog](#changelog)
- [0.4.0](#040)

Migratte is simple SQL migrations management standalone CLI app, framework-agnostic. It is inspired by [Phinx](https://phinx.org/), but it is much simpler, and it doesn't require CakePHP framework to be installed.

You can install it using composer:

```shell
$ composer require semisedlak/migratte
```

## How it works
It is controlled by ([Symfony Console](https://github.com/symfony/console)) CLI commands for:

1. [generate](#migrattegenerate) new migration
2. [commit](#migrattecommit) migration
3. [rollback](#migratterollback) migration
4. showing migrations [status](#migrattestatus)
5. showing configuration [info](#migratteinfo)

## Migration anatomy
Migrations are simple [PHP files](#migration-anatomy) keeping plain SQL queries. If plain SQL so why PHP files then? Answer is simple. For keeping ["up"](#example-migration) (for [commit](#migrattecommit)) and ["down"](#example-migration) (for [rollback](#migratterollback)) SQL queries with additional metadata all together.

> 💡 Migratte doesn't provide (yet) rich migrations management. It is just a simple tool for executing your SQL queries and it is up to you to write them. You can use any SQL query you want.
>
> You can still use [Phinx](https://phinx.org/) with [CakePHP Migrations](https://book.cakephp.org/3.0/en/migrations.html) for migrations management. I recommend [Adminer](https://www.adminer.org/) for database management.

Specific migration class extends from basic migration class. It contains timestamp in file name and class name witch should not be modified.

Migration file that doesn't contain `down()` method is "breakpoint", that means rollback cannot be performed. BUT! There is an option `--force` to perform rollback with this kind of migration.

This tool creates it's "memory" in same database as migrations target. It uses [dibi database layer](https://dibiphp.com/) for connection and queries executions.

You can use it with:
* MySQL database
* PostgreSQL database
* SQLite database

## Configuration
You can override this default configuration options:

```php
'timezone' => 'UTC',
'migrationsDir' => "$workingDir/database/migrations",
'migrationsTable' => [
'name' => 'migrations',
'primaryKey' => 'id',
'fileName' => 'filename',
'groupNo' => 'group',
'committedAt' => 'committed_at',
],
'connection' => [
'driver' => 'sqlite', // mysqli or postgre
'database' => "$workingDir/database/migratte.s3db",
],
```

providing options array as first argument of `Application::boot()` method from executable file. You can read how to set up connection in [dibi documentation](https://dibiphp.com/).

## Executable file
Create a file `bin/migrations` in the root dir of your project with following content:

```php
#!/usr/bin/env php
run();
```

Don't forget to change permission using `chmod +x bin/migrations`

## Commands
When you run `bin/migrations` in CLI you will see "help" overview with possible commands.

```
root@12345:/var/www/html# bin/migrations
Migratte 0.5.0

Usage:
command [options] [arguments]

Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
help Display help for a command
list List commands
migratte
migratte:commit Commit (run) migrations
migratte:generate Generate new migration file
migratte:info Show migrations configuration info
migratte:rollback Rollback migrations
migratte:status Show migrations status
```

> 💡 Hint: You can use `--help` (or `-h`) option for each command to see more details.

### `migratte:generate`
Command generates new migration file which then can be modified. You can specify migration name as first argument. Write it as normal sentence, it will be converted to desired form automatically.

```shell
$ bin/migrations migratte:generate "Create users table"
```

This will generate file `database/migrations/20190101_120000-create-users-table.php` with following content:

```php
⚠️ Warning! Don't modify migration class name. Don't modify file name after it was committed. You can modify `up()` and `down()` methods to contain your SQL queries.

If you want to change migration name you can change it in `getName()` method. It is used only for displaying purposes.

Then copy your SQL queries to `up()` and `down()` methods. If `down()` method returns NULL or FALSE it is considered as "breakpoint" migration (it cannot be rollbacked because it doesn't provide "down" operation).

#### Example migration

```php
public static function up(): string
{
return <<