Skip to content

Instantly share code, notes, and snippets.

@milosjanda
Last active May 10, 2017 08:16
Show Gist options
  • Save milosjanda/a17b7408b4a3bf83e2c48bef7b25b5cc to your computer and use it in GitHub Desktop.
Save milosjanda/a17b7408b4a3bf83e2c48bef7b25b5cc to your computer and use it in GitHub Desktop.
Scaffold for Nextras migration - https://nextras.org/migrations/docs/3.0/
#!/usr/bin/env bash
# 1. install `composer require nextras/migrations`
# 2. install favorite DBAL (Dibi | Nextras\Dbal | Doctrine\Dbal | Nette\Database), if still not exists in project
# 3. in application root run this script (which create basic directory sctructure and template for run.php script)
# 4. modify file ${DIRECTORY}/run.php with respect your adapter + read DB config setting from yout project
# 5. to file ${DIRECTORY}/structures/${DATE}-initDatabase.sql insert actual create scritp of your DB without data
# 6. eventually to file ${DIRECTORY}/basic-data/${DATE}-initData.sql add basic data (for exmple any enumeration)
# 7. run `/usr/bin/php -f ${DIRECTORY}/run.php structures production-data --init-sql`
# 8. output SQL script run on your database
# 9. before every depoloye run `php migration/run.php structures production-data`
# set direstory where migration will be created
DIRECTORY="./migration"
# create directory structure
mkdir ${DIRECTORY}
mkdir ${DIRECTORY}/structures
touch ${DIRECTORY}/structures/.gitkeep
mkdir ${DIRECTORY}/basic-data
touch ${DIRECTORY}/basic-data/.gitkeep
mkdir ${DIRECTORY}/dummy-data
touch ${DIRECTORY}/dummy-data/.gitkeep
# create init DB file
DATE=`date +%Y-%m-%d-%H%M%S`
touch ${DIRECTORY}/structures/${DATE}-initDatabase.sql
# create template for run.php script
cat << EOF > ${DIRECTORY}/run.php
<?php
use Nextras\Migrations\Bridges;
use Nextras\Migrations\Controllers;
use Nextras\Migrations\Drivers;
use Nextras\Migrations\Extensions;
require __DIR__ . '/../vendor/autoload.php';
// disable argument --reset on production environment
// if ( production enviroment ) {
// fprintf(STDERR, "Argument --reset is not allowed on production environment\n");
// exit(1);
// }
// !!! Load configuration from project config file
// Nextras\Dbal example
// migration 3.0.4
// Attention: For proper functionality Nextras DBal with mysql is needed: https://nextras.org/dbal/docs/2.1/timezones-mysql-support
//\$dbConf = [
// 'driver' => 'mysqli',
// 'host' => '',
// 'database' => '',
// 'user' => '',
// 'password' => '',
//];
//\$conn = new Nextras\Dbal\Connection(\$dbConf);
//\$dbal = new Bridges\NextrasDbal\NextrasAdapter(\$conn);
// Dibi example
// migration 3.1.0-rc3 - kvuli dibi adapteru
\$dbConf = [
'driver' => 'mysqli',
'host' => '',
'database' => '',
'user' => '',
'password' => '',
];
\$conn = new \Dibi\Connection(\$dbConf);
\$dbal = new Bridges\Dibi\Dibi3Adapter(\$conn);
// Nette\Database example
// migration 3.0.4
//\$dbConf = [
// 'dsn' => 'mysql:host=;dbname=',
// 'user' => '',
// 'password' => '',
//];
//\$conn = new Nette\Database\Connection(\$dbConf['dsn'], \$dbConf['user'], \$dbConf['password']);
//\$dbal = new Bridges\NetteDatabase\NetteAdapter(\$conn);
\$driver = new Drivers\MySqlDriver(\$dbal);
//\$controller = new Controllers\HttpController(\$driver);
\$controller = new Controllers\ConsoleController(\$driver);
\$baseDir = __DIR__;
\$controller->addGroup('structures', "\$baseDir/structures");
\$controller->addGroup('basic-data', "\$baseDir/basic-data", ['structures']);
\$controller->addGroup('dummy-data', "\$baseDir/dummy-data", ['basic-data']);
\$controller->addExtension('sql', new Extensions\SqlHandler(\$driver));
\$controller->addExtension('php', new Extensions\PhpHandler(['connection' => \$conn]));
\$controller->run();
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment