Scenario analysis

In team development, every developer has to record all changes to the database manually, which needs to be manually sorted out when going online, and the operation and maintenance cost is extremely high. Synchronization of data structures between multiple developers is also a big problem. Doctrine Migrations component adds database changes to the code and carries out version management with the code, which solves the above problems well.

Doctrine Migrations is a data migration component based on Doctrine DBAL components. Integration with Laravel, Symfony and other mainstream frameworks. There are two ways of migration, version management and diff.

Versioning: Versioning database changes by writing them to code. The Laravel framework is a versioning mode that is supported by the default command line of the migration component.

Diff: Compare the existing database structure to the database structure in the code, and execute different SQL to ensure consistency. ORM support is generally required, and the Symfony framework uses the Doctrine2ORM tool plus Doctrine DBAL to perform data migration in a diff manner.

Instead of discussing the use of data migration components in existing frameworks, this series of articles focuses on how to use migration components in isolation and how to integrate them into your own projects and personalize them.

The installation

Composer installation

Composer require doctrine/migrations ~ 1.8.0 comes with

This series uses the current 1.8.1 release

configuration

It can not be used directly after installation, but also requires component configuration and database configuration:

  1. Create the file migrations.php in the root directory and configure the components:

    Return ['name' => 'Doctrine MigrationsNamespace '=> 'db Migrations', // migrated class namespace 'table_name' => 'migration_versions', // migrated component table name' migrations_directory' => 'db/migrations', // migrated class folder];

    See the official documentation for configuration parameters for details.

  2. Mysql > create a migrations-db.php file in the root directory.

    return [
        'driver' => 'pdo_mysql',
        'host' => 'localhost',
        'port' => 3306,
        'user' => 'root',
        'password' => '1236',
        'dbname' => 'migrations',
    ];

At this point, the configuration required for the component is complete.

use

  1. Run the command under vendor directory to generate the version migration class file.

    ./vendor/bin/doctrine-migrations migrations:generate

    During the migration under the directory/db/migrations generate Version20180608155932. PHP class file, the file is used to write to migrate SQL class, the Numbers behind the Version is the current Version number.

  2. Overwrite the UP method to perform the migration SQL:

    public function up(Schema $schema): void
    {
        $table = $schema->createTable('test1');
        $table->addColumn('id', 'integer')->setUnsigned(true)->setAutoincrement(true);
        $table->addColumn('name', 'string')->setDefault('')->setLength(20);
        $table->setPrimaryKey(['id']);
    }

    This script is to generate a table from test1.

  3. Override the down method for versioning fallback and undo the migration of the up method:

    public function down(Schema $schema): void { if ($schema->hasTable('test1')) { $schema->dropTable('test1'); }}
  4. Execute the migration command:

    ./vendor/bin/doctrine-migrations migrations:migrate

This successfully creates a version of the migration data. The migration script can use $schema to manipulate database entities, or it can use the $this-> addSQL () method to write directly to the associated SQL.

Version management

Since the migration uses versioning, multiple versions can be switched back and forth. Here is the related version switch command:

./vendor/bin/doctrine-migrations migrations:migrate 20180608161758

This command reverts to version 20180608161758. It can also be used as a version alias:

  • First: Go back to the original version
  • Prev: Going back to the previous version
  • Next: Migrate to the next version
  • Latest: Migrate to the latest version (same as without a version number)

Common commands

Migrate migrate migrate migrate migrate migrate migrate migrate migrate migrate migrate migrate migrate migrate migrate migrate migrate PHP migration. PHP migrations:migrate --dry-run # Migrations :migrate -- migrate PHP migrations:migrate --write-sql=file.sql # PHP migrations:migrate --write-sql=file.sql # For more information about migrations:migrate --write-sql=file.sql #

conclusion

At this point, you have learned the basic use of the migrations component, but there are a few issues that need to be resolved:

  1. Database configuration files are usually included in the project. How to make the configuration files in the component common project?
  2. Now that the configuration files needed to migrate the components are only in the root directory, how to make the configuration files more properly archived?
  3. The command line is long and hard to remember. How to use the command line easily?

In the next chapter, we will address these issues and make the components more personalized to our own projects.

in
My code baseSee the detailed code for this article.