iqomp / migrate
数据库迁移,同步迁移配置到数据库表
Requires
- hyperf/command: ^2.1
- iqomp/model: ^2.0
Requires (Dev)
Suggests
- iqomp/migrate-mysql: Database mysql migration driver
This package is auto-updated.
Last update: 2024-09-27 15:22:50 UTC
README
数据库迁移,同步迁移配置到数据库表。这种迁移方法会将迁移配置文件与数据库中的内容同步。这种迁移方式是将数据库迁移存储在文件配置中,并与当前数据库状态同步。迁移日志不在每次迁移时存储在文件或数据库表中,您需要检查存储库中的迁移日志。
迁移是可扩展的,这意味着在执行之前,一个迁移可以与其他迁移合并。例如,模块post
已经为表post
定义了结构,另一个模块(例如post-publish
)允许在post-publish
迁移配置中为表post
添加列。
安装
composer require iqomp/migrate
命令行
此模块创建了一个新的Hyperf命令,可以用于测试、创建数据库、同步表和配置,以及打印出供开发者手动执行的可执行sql/script。
# Create database defined on config/autoload/databases.php # if the database is not yet there. php bin/hyperf.php iqomp:migrate db # Start migrating for migrate config to database table. php bin/hyperf.php iqomp:migrate start # Compare migration config and database without executing the migration php bin/hyperf.php iqomp:migrate test # Compare migration config and database without executing the migration # and print it to STD_OUT for manual execution by developer. php bin/hyperf.php iqomp:migrate to > ./migrate.sql
迁移配置
更新您的composer.json
文件,包括以下内容
{ "extra": { "iqomp": { "migrate": "iqomp/migrate/config.php" } } }
然后在您的主要模块目录下创建一个名为iqomp/migrate/config.php
的新文件,填写以下内容
<?php return [ '/ModelClassName/' => [ 'fields' => [ '/field-name/' => [ 'comment' => '/comment/', 'type' => '/type/', 'attrs' => [ // list of column attrs ], 'index' => '/index/' ] ], 'indexes' => [ '/index-name/' => [ 'type' => '/index-type/', 'fields' => [ '/field/' => [ /* option */ ] // list of columns ], ] ], 'data' => [ '/search-field/' => [ '/search-value/' => [ '/field/' => '/value/' ] ] ] ], 'Company\\Model\\User' => [ 'fields' => [ 'id' => [ 'type' => 'int', 'attrs' => [ 'unsigned' => true, 'primary_key' => true, 'auto_increment' => true ], 'index' => 100 ], 'name' => [ 'type' => 'varchar', 'attrs' => [ 'length' => 5, 'null' => false, 'unique' => true ], 'index' => 200 ], 'status' => [ 'comment' => '0: Deleted, 1: Active', 'type' => 'tinyint', 'attrs' => [ 'null' => false, 'default' => 1 ], 'index' => 3000 ] ], 'indexes' => [ 'by_name_status' => [ 'fields' => [ 'name' => [], 'status' => [] ] ] ], 'data' => [ 'name' => [ 'admin' => [ 'name' => 'admin', 'status' => 1 ] ] ] ] ];
所有迁移属性的解释如下
fields
一个表字段的数组列表,格式为name->meta
对,其中name
是表列名,meta
是列元数据列表。列应至少有一个属性,即type
。以下是迄今为止已知的元数据列表
comment::string
列注释,实际上并非所有数据库引擎都接受此属性。
type::string
列数据类型,目前支持的数据类型如下
- 文本
CHAR
。需要attrs: {length}
ENUM
。需要attrs: {options:[]}
LONGTEXT
SET
。需要attrs: {options:[]}
TEXT
TINYTEXT
VARCHAR
。需要attrs: {length}
- 数字
BIGINT
BOOLEAN
DECIMAL
DOUBLE
。需要attrs: {length}
FLOAT
INTEGER
MEDIUMINT
SMALLINT
TINYINT
- 日期
DATE
DATETIME
TIMESTAMP
TIME
YEAR
attrs::array
列的附加属性列表。目前支持以下属性
length::string
列的长度。对于DOUBLE
,它接受逗号和十进制值。options::array
列的选项列表。主要用于ENUM
和SET
。null::boolean
设置为false
以确保列不接受null
值default::mixed
列的默认值。update::mixed
update
操作的默认值。此属性主要用于列updated_at
,值为CURRENT_TIMESTAMP
。unsigned::boolean
将数字列设置为UNSIGNED
,不接受负值。unique::boolean
将列设置为UNIQUE
,不接受重复值。primary_key::boolean
将列设置为主键。auto_increment:boolean
将列设置为自动递增。
indexes
列索引列表。它是name-meta
对的数组,其中name
是索引名称,meta
是索引元数据和字段列表。此属性接受以下属性
type::string
索引类型,接受的值是UNIQUE
、FULLTEXT
、SPATIAL
、BTREE
和HASH
。如果未设置,则使用BTREE
。fields::array
使用作为索引列的数组列表,格式为column-options
对。其中column
是列名,options
是索引列的额外选项列表。例如,类型为文本的列可能具有用于索引长度的options
长度。
数据
在迁移过程中,根据表列插入数据列表。该属性是一个 column->datalist
对,其中 column
是列名,datalist
是具有 search-value->row
对的行列表,其中 search-value
是用于搜索查询的值,以确定该值是否应插入到表中,而 row
是新数据的 column-value
对。
请注意,在迁移中没有这样的 update
和 remove
。
创建迁移器
这部分解释了如何为某些数据库类型创建新的迁移处理器。
创建一个实现接口 Iqomp\Migrate\MigratorInterface
的新类。该类应具有以下方法
public __construct(\Hyperf\Command\Command $cli, array $config)
构造类,此方法接受命令行 cli 数据库连接配置参数。
public createDb(): bool
根据提供的配置在 __construct
中创建新数据库。
public dbExists(): bool
根据提供的连接检查数据库是否存在。
public lastError(): ?string
返回最后发生的错误。
public syncTable(string $model, string $table, array $config): void
将迁移配置同步到数据库表。
public syncTableTo(string $model, string $table, array $config): void
将迁移配置同步到数据库,并创建用于迁移的脚本/sql,而不是将其执行到数据库中。此操作意味着需要由开发人员手动执行。
public testTable(string $model, string $table, array $config): void
将迁移配置与数据库表进行比较,并打印比较结果,而不执行迁移。
有关迁移示例,请参阅 iqomp/migrate-mysql。
创建迁移器类后,通过在您的模块中创建类 ConfigProvider
并在类调用方法中返回以下数据来注册处理器
// ... public function __invoke() { return [ 'model' => [ 'migrators' => [ '/db-type/' => 'ClassHandler', 'mysql' => 'Vendor\\Module\\Migrator' ] ] ]; } // ...
然后更新您的 composer.json
文件以包含以下内容
{ "extra": { "hyperf": { "config": "Vendor\\Module\\ConfigProvider" } } }