The launch of the console extension a few weeks ago made it possible to significantly expand the range of tasks solved using PHPixie and its components. And now I’m happy to introduce you to PHPixie Migrate, a database migration utility. Like other components, it can work completely independently, and in the end I will give an example of how to run it without a framework.return array( 'default' => array( 'connection' => 'mysql:dbname=phpixie', 'user' => 'phpixie', 'password' => 'phpixie', 'driver' => 'pdo' ) ); return array( 'default' => array( 'database' => 'phpixie', 'user' => 'phpixie', 'password' => 'phpixie', 'adapter' => 'mysql', // one of: mysql, pgsql, sqlite 'driver' => 'pdo' ) ); <?php return array( // 'migrations' => array( 'default' => array( // database.php 'connection' => 'default', // , /assets/migrate/ 'path' => 'migrations', // : // 'migrationTable' => '__migrate', // 'lastMigrationField' => 'lastMigration' ) ), // ( ) 'seeds' => array( 'default' => array( // database.php 'connection' => 'default', // , /assets/migrate/ 'path' => 'seeds' ) ) ); framework:database ACTION [ CONFIG ] Create or drop a database Arguments: ACTION Either 'create' or 'drop' CONFIG Migration configuration name, defaults to 'default' CREATE TABLE fairies( id int NOT NULL, name VARCHAR(255) ); -- statement CREATE TABLE flowers( id int NOT NULL, name VARCHAR(255) ); $this->execute("CREATE TABLE fairies( id int NOT NULL, name VARCHAR(255) )"); $this->message("- "); // $this->connection()->updateQuery() ->table('users') ->set(['role' => 'user']) ->execute(); // /assets/migrate/seeds/fairies.php <?php return array( array( 'id' => 1, 'name' => 'Pixie' ), array( 'id' => 2, 'name' => 'Trixie' ), ); // /assets/migrate/seeds/flowers.json [ { "id": 1, "name": "daisy" }, { "id": 2, "name": "Rose" }, ] // /assets/migrate/seeds/fairies.php <?php $this->connection()->insertQuery() ->data([ 'id' => 1, 'name' => 'Pixie' ]) ->execute(); framework:seed [ --truncate ] [ CONFIG ] Seed the database with data Options: truncate Truncate the tables before inserting the data. Arguments: CONFIG Seed configuration name, defaults to 'default' $slice = new \PHPixie\Slice(); $database = new \PHPixie\Database($slice->arrayData(array( 'default' => array( 'database' => 'phpixie', 'user' => 'phpixie', 'password' => 'phpixie', 'adapter' => 'mysql', // one of: mysql, pgsql, sqlite 'driver' => 'pdo' ) ))); $filesystem = new \PHPixie\Filesystem(); $migrate = new \PHPixie\Migrate( $filesystem->root(__DIR__.'/assets/migrate'), $database, $slice->arrayData(array( 'migrations' => array( 'default' => array( 'connection' => 'default', 'path' => 'migrations', ) ), 'seeds' => array( 'default' => array( 'connection' => 'default', 'path' => 'seeds' ) ) ))); $cli = new \PHPixie\CLI(); $console = new \PHPixie\Console($slice, $cli, $migrate->consoleCommands()); $console->runCommand(); Source: https://habr.com/ru/post/315254/
All Articles