Hello. I've made it so that you use migrations and illuminate/database as standalone. Yes you do use phinx. You do not need the service container to achieve this.All you need is illuminate/database and phinx
This is my phinx.php file located in my root directory. The bootstrap.php is a file that holds my global constants and configs. The important thing here is the 'migration_base_class' => '\Database\Migration',
<?php
/**
* Created by Deyan Plachkov.
* Email: [email protected]
* Date: 6/25/2019
* Time: 3:27 PM
*/
# Load composer
require 'vendor/autoload.php';
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'bootstrap.php');
return [
'paths' => [
'migrations' => '%%PHINX_CONFIG_DIR%%/database/migrations',
'seeds' => '%%PHINX_CONFIG_DIR%%/database/seeds',
],
'templates' => [
'file' => 'phinx-template.php.dist'
],
'migration_base_class' => '\Database\Migration',
'environments' => [
'default_migration_table' => 'phinxlog',
'default_database' => 'default',
'default' => [
'adapter' => 'mysql',
'host' => DATABASE_HOST,
'name' => DATABASE_DATABASE,
'user' => DATABASE_USERNAME,
'pass' => DATABASE_PASSWORD,
'port' => 3306
]
]
];
Next up is the migration class itself - keep in mind that i've placed it in a namespace Database which you have to add to composer ( "Database\": "database/", )
Keep in mind that the 2 rows
$platform = $this->capsule->getConnection()->getDoctrineSchemaManager()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
Are here trying to deal with the ENUM problems. You can read more about the enum problems in the official documentation.
<?php namespace Database;
require_once(__DIR__ . '/../config/bootstrap.php');
use Illuminate\Database\Capsule\Manager as Capsule;
use Phinx\Migration\AbstractMigration;
use Doctrine\DBAL\Types\{StringType, Type};
class Migration extends AbstractMigration {
/** @var \Illuminate\Database\Capsule\Manager $capsule */
public $capsule;
/** @var \Illuminate\Database\Schema\Builder $capsule */
public $schema;
public static $CAPSULE;
public function init() {
if (self::$CAPSULE) {
$this->capsule = self::$CAPSULE;
$this->schema = $this->capsule->schema();
} else {
$this->capsule = new Capsule;
$this->capsule->addConnection([
'driver' => DATABASE_DRIVER,
'host' => DATABASE_HOST,
'database' => DATABASE_DATABASE,
'username' => DATABASE_USERNAME,
'password' => DATABASE_PASSWORD,
'charset' => DATABASE_CHARSET,
'collation' => DATABASE_COLLATION,
'prefix' => DATABASE_PREFIX,
'engine' => 'InnoDB'
]);
$platform = $this->capsule->getConnection()->getDoctrineSchemaManager()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
$this->capsule->bootEloquent();
$this->capsule->setAsGlobal();
$this->schema = $this->capsule->schema();
self::$CAPSULE = $this->capsule;
}
}
}
Here is an example migration I have
<?php
use \Database\Migration;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
class EditBagsAddLinkedBag extends Migration
{
public function up()
{
Capsule::schema()->table('bags', function (Blueprint $table) {
$table->integer('actual')->nullable()->after('id');
});
}
public function down()
{
Capsule::schema()->table('bags', function (Blueprint $table) {
$table->dropColumn('actual');
});
}
}
Here are some scripts in the composer.json which you can use
"scripts": {
"create": "phinx create",
"migrate": "phinx migrate",
"migrations": "phinx status",
"rollback": "phinx rollback",
}
Keep in mind that the difference here is that those migrations are saved in "phinxlog" instead of the "migrations" table. Maybe this behaviour can be altered, but its fine by me