asgard / migration
Requires
- php: >=5.5.9
- asgard/common: ~0.3.0
- asgard/container: ~0.3.0
- asgard/db: ~0.3.0
Requires (Dev)
- asgard/file: ~0.3.0
- doctrine/dbal: ^2.0
This package is not auto-updated.
Last update: 2024-09-14 15:16:33 UTC
README
#迁移
迁移包允许您管理和执行迁移。
##安装 如果您正在处理一个Asgard项目,不需要安装此库,因为它已经是标准库的一部分。
composer require asgard/migration 0.*
##概述
所有迁移都必须扩展 \Asgard\Migration\Migration 或 \Asgard\Migration\DBMigration(它扩展 \Asgard\Migration\Migration)。
\Asgard\Migration\DBMigration 对于数据库迁移非常有用,因为它将在迁移执行之前自动开始一个事务,并且在迁移成功后提交。
迁移必须实现 up() 或/和 down() 方法。
例如
<?php
class News extends \Asgard\Migration\DBMigration {
public function up() {
$this->schema->create('news', function($table) {
$table->addColumn('id', 'integer', [
'length' => 11,
'autoincrement' => true,
]);
$table->addColumn('text', 'string', [
'length' => '255',
]);
$table->addColumn('meta_title', 'text', [
'length' => '65535',
]);
$table->setPrimaryKey(['id']);
});
}
public function down() {
$this->schema->drop('news');
}
}
所有迁移文件必须位于同一文件夹中。在Asgard项目中,迁移文件夹位于 migrations/。
此文件夹包含一个json文件
- migrations.json 包含所有活动迁移
迁移状态存储在 _migrations 表中。
示例
migrations.json
{
"Data": {
"added": 1401943722.9835
}
}
数据迁移必须在名为 Data.php 的文件中进行,位于 migrations/Data.php。
##MigrationManager
###在Asgard框架中的使用
$migrationManager = $container['migrationManager'];
容器通常可以通过方法参数或通过 容器 对象访问。您也可以使用 ContainerAware,但不太推荐。
###在Asgard框架之外的使用
$migrationManager = new \Asgard\Migration\MigrationManager('/path/to/migrations/', $container /*optional*/);
###方法
添加迁移
$migrationManager->add('/path/to/Migration.php');
将文件复制到迁移目录,并将其添加到 migrations.json 中。
检查迁移是否已存在
$migrationManager->has('Migrationname');
删除迁移
$migrationManager->remove('Migrationname');
执行迁移
$migrationManager->migrate('Migrationname');
迁移文件
$migrationManager->migrateFile('/path/to/Migration.php');
迁移所有迁移
$migrationManager->migrateAll();
重置迁移(回滚并重新迁移所有迁移)
$migrationManager->reset();
取消迁移特定迁移
$migrationManager->unmigrate('Migrationname');
回滚最后一个迁移
$migrationManager->rollback();
回滚到特定迁移
$migrationManager->rollbackUntil('Migrationname');
创建新迁移
$up = "$this->schema->create('news', function($table) {
$table->addColumn('id', 'integer', [
'length' => 11,
'autoincrement' => true,
]);
$table->addColumn('text', 'string', [
'length' => '255',
]);
$table->addColumn('meta_title', 'text', [
'length' => '65535',
]);
$table->setPrimaryKey(['id']);
});";
$down = "$this->schema->drop('news');";
$migrationManager->create($up, $down, 'Migrationname', $class='\Asgard\Migration\Migration');
##追踪器
追踪器有助于跟踪迁移的状态。
###在Asgard框架中的使用
$tracker = $container['migrationManager']->getTracker();
###在Asgard框架之外的使用
$tracker = $migrationManager->getTracker();
容器通常可以通过方法参数或通过 容器 对象访问。您也可以使用 ContainerAware,但不太推荐。
###方法
获取所有迁移
$tracker->getList();
获取所有未执行迁移
$tracker->getDownList();
获取所有已执行迁移
$tracker->getUpList();
检查迁移是否存在
$tracker->has('migrationName');
获取下一个要执行的迁移
$tracker->getNext();
获取最后一个已执行的迁移
$tracker->getLast();
获取特定迁移之前的所有迁移
$tracker->getUntil('migrationName');
以相反顺序获取特定迁移之前的所有已迁移迁移
$tracker->getRevereMigratedUntil('migrationName');
将迁移添加到 migrations.json(迁移文件必须已经在迁移文件夹中)
$tracker->add('migrationName');
从 migrations.json 中删除迁移(不删除文件)
$tracker->remove('migrationName');
将迁移标记为未迁移
$tracker->unmigrate('migrationName');
将迁移标记为已迁移
$tracker->migrate('migrationName');
检查迁移是否处于 up 状态
$tracker->isUp('migrationName');
##命令
###AddCommand
将新迁移添加到列表中
用法
php console migrations:add [src]
src: 迁移文件
###ListCommand
显示迁移列表
用法
php console migrations:list
###MigrateCommand
运行迁移
用法
php console migrate
###MigrateOneCommand
运行迁移
用法
php console migrations:migrate [migration]
migration: 迁移名称
###RefreshCommand
重置并重新运行所有迁移
用法
php console migrations:refresh
###RemoveCommand
删除迁移
用法
php console migrations:remove [migration]
migration: 迁移名称
###RollbackCommand
回滚最后一个数据库迁移
用法
php console migrations:rollback
###UnmigrateCommand
取消迁移
用法
php console migrations:unmigrate [migration]
migration: 迁移名称
### 贡献
请将所有问题和拉取请求提交到 asgardphp/asgard 仓库。
许可证
Asgard 框架是开源软件,采用 MIT 许可协议 许可。