Custom command scripts

The directory structure

The current project structure (see code base) looks like this:

Among them, db/migrations folder is the migration class folder, config/db.php is the original db configuration of our project, and migrations. PHP and migrations-db.php are the configuration files needed for the migration component.

Write custom command scripts

Now create a new file in the root directory: migrate, with no suffixes and add executable permissions.

And with reference to the original component command scripts vendor/doctrine migrations/doctrine – migrations. PHP, first access to project the original database configuration information, replace the migrations – db. PHP database configuration file:

$db_config = include 'config/db.php';
$db_params = [
    'driver' => 'pdo_mysql',
    'host' => $db_config['host'],
    'port' => $db_config['port'],
    'dbname' => $db_config['dbname'],
    'user' => $db_config['user'],
    'password' => $db_config['password'],
];
try {
    $connection = DriverManager::getConnection($db_params);
} catch (DBALException $e) {
    echo $e->getMessage() . PHP_EOL;
    exit;
}

Then configure the component, replacing the migrations.php configuration file:

$configuration = new Configuration($connection);
$configuration->setName('Doctrine Migrations');
$configuration->setMigrationsNamespace('db\migrations');
$configuration->setMigrationsTableName('migration_versions');
$configuration->setMigrationsDirectory('db/migrations');

Finally, here is the complete command script code:

#! /usr/bin/env php <? php require_once 'vendor/autoload.php'; use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Migrations\Configuration\Configuration; use Doctrine\DBAL\Migrations\Tools\Console\ConsoleRunner; use Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper; use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\QuestionHelper; $db_config = include 'config/db.php'; $db_config = include 'config/db.php'; $db_params = [ 'driver' => 'pdo_mysql', 'host' => $db_config['host'], 'port' => $db_config['port'], 'dbname' => $db_config['dbname'], 'user' => $db_config['user'], 'password' => $db_config['password'], ]; try { $connection = DriverManager::getConnection($db_params); } catch (DBALException $e) { echo $e->getMessage() . PHP_EOL; exit; $configuration = new configuration ($connection); $configuration->setName('Doctrine Migrations'); $configuration->setMigrationsNamespace('db\migrations'); $configuration->setMigrationsTableName('migration_versions'); $configuration->setMigrationsDirectory('db/migrations'); $helper_set = new HelperSet(['question' => new ConnectionHelper(), 'db' => new ConnectionHelper($connection)), $helper_set = new HelperSet(['question' => new ConnectionHelper(), 'db' => new ConnectionHelper($connection)), new ConfigurationHelper($connection, $configuration), ]); $cli = ConsoleRunner::createApplication($helper_set); try { $cli->run(); } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; }

Now replace the vendor/bin/ trine-migrations section with./migrate.

The migrations.php and migrations-db.php configuration files can also be removed.

PHPStorm integrates migration commands

If you use PHPStorm, the command line can also be integrated into the development tool with auto-prompts, which can greatly improve productivity:

  1. PhpStorm -> Preferences -> Command Line Tool Support– > add
  2. Choose Tool Based on Symfony Console and click OK
  3. Alias type M, Path to PHP Executable select PHP Path, Path to Script select migrate file under directory
  4. Click OK to indicate that Found 9 Commands is configured successfully.
  5. PhpStorm -> Tools -> Run Command, pop up a command window, type m will appear command prompt.

conclusion

At this point, the data migration component is already flexibly integrated into our own projects, and you can also flexibly modify the custom command script files for your own projects.

However, if you use it for a long time, you will find that there are two problems with the current data migration:

  1. TinyInt type is not supported. In the related issues, the developer replies:

    “Tiny integer” is not supported by many database vendors. DBAL’s type system is an abstraction layer for SQL types including type conversion from PHP to database value and back. I am assuming that your issue refers to MySQL, where we do not have a distinct native SQL type to use for BooleanType mapping. Therefore MySQL’s TINYINT is used for that purpose as is fits best from all available native types and in DBAL we do not have an abstraction for tiny integers as such (as mentioned above).

    What you can do is implement your own tiny integer custom type and tell DBAL to use column comments (for distinction from BooleanType in MySQL). That should work. See the documentation.

    To tell DBAL to use column comments for the custom type, simply override the requiresSQLCommentHint() method to return true. So you can only add custom types.

  2. Another problem is that the enum type is not supported. As you dig deeper into the code, you find that the special format of enum types is not easy to integrate into the component, and custom types are not supported, but if you are adding a data migration component halfway through the project, using a migration component will give an error if you already have a data table structure that contains fields of type enum.

So, in the next chapter, we’ll do two things: add a custom type to TinyInt, and solve the enum error problem.

in
My code baseYou can view the detailed code for this article, and welcome STAR.