alirezasalehizadeh / quick-migration
一个用于在PHP项目中快速实现迁移的包。
v1.8.0
2023-10-21 08:25 UTC
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^10.1.x-dev
README
使用Quick Migration快速运行迁移!
要求
PHP >= 8.1
可用数据库
- MySql
- PostgreSql
入门
安装
通过Composer
composer require alirezasalehizadeh/quick-migration
迁移类模板
创建一个类似于xMigration
的类,该类必须扩展自\Alirezasalehizadeh\QuickMigration\Migration
类
use Alirezasalehizadeh\QuickMigration\Migration; use Alirezasalehizadeh\QuickMigration\Structure\Structure; use Alirezasalehizadeh\QuickMigration\Structure\StructureBuilder; class xMigration extends Migration { protected $database = "database name"; protected $translator = "set database translator name from available translators array (default MySql)"; public function set(): array { return Structure::create('table_name', function (StructureBuilder $structure) { // Write your structure here... }); } }
运行迁移
接下来,创建一个xMigration
类的对象,并运行migrate
方法
// index.php $connection = PDO connection object; (new xMigration($connection))->migrate();
php index.php
删除表
// index.php $connection = PDO connection object; (new xMigration($connection))->drop('table name'); // OR (new xMigration($connection))->dropIfExists('table name');
php index.php
用法
结构方法
$structure->id(string $name); $structure->uuid(string $name, int $length); $structure->ulid(string $name, int $length); $structure->string(string $name, int $length); $structure->number(string $name); $structure->text(string $name); $structure->timestamp(string $name); $structure->timestamps(); $structure->json(string $name); $structure->enum(string $name, array $enums); $structure->array(string $name, array $values); $structure->foreign(string $column)->reference(string $column)->on(string $table);
*注意:请参阅结构测试文件中的示例
列属性
$structure->number('test') ->primary() // Set this as primary key ->nullable() // Set this nullable or not ->unique() // Set this unique ->default(1) // Set default value ->autoIncrement() // Set this auto increment ->index() // Index this column ->unsigned() // Set unsigned attribute ->after('column') // Set this column after specific column ->check('test >= 0') // Check a expression ->comment('this is test column') // Set a comment
自定义列
有时你可能需要一个不在Type
枚举中存在的特定类型的列,你必须手动创建它。QuickMigration
为你提供了快速创建特定类型列的简单方法!
要创建一个列,只需将方法名
设置为列类型
,并在第一个参数
中写入列名
即可,如下所示
// TINYTEXT type not defined in `Type` enum $structure = new StructureBuilder('table name'); $structure->tinyText('foo'); // ...
命令
migrate(); dropIfExists(string $table); drop(string $table); createIndex(string $name, string $table, array $columns); // It is used to index several columns together dropIndex(string $name, string $table); alterTable(); dropCheck(string $table, string $name);
*注意:请参阅命令测试文件中的示例
获取SQL
你可以通过将迁移类对象作为字符串来调用,以获取sql
$obj = new xMigration($connection); $obj->dropIfExists('bar'); $obj->migrate(); echo $obj; /** DROP TABLE IF EXISTS `foo`.`bar` CREATE TABLE `foo`.`bar` (`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, ...) **/
将SQL保存为文件
你可以使用export
方法,将你的sql保存到文件中
$obj = new xMigration($connection); $obj->dropIfExists('bar'); $obj->migrate(); $obj->export(string $fileName); // Create a file named fileName.sql
自定义外键
创建外键的快速方法是,方法名
必须是foreign + {外键列名}
$structure = new StructureBuilder('table name'); $structure->foreign('bar_id')->reference('id')->on('bar'); // OR $structure->foreignBarId()->reference('id')->on('bar'); // ...
修改表
现在,要修改你的表,可以在Structure
上使用change
方法
use Alirezasalehizadeh\QuickMigration\Migration; use Alirezasalehizadeh\QuickMigration\Structure\Structure; use Alirezasalehizadeh\QuickMigration\Structure\TableAlter; class xMigration extends Migration { protected $database = "database name"; protected $translator = "set database translator name from available translators array (default MySql)"; public function set(): array { return Structure::change('table_name', function (TableAlter $alter) { // Write your commands for modify table... }); } }
运行命令
// index.php $connection = PDO connection object; (new xMigration($connection))->alterTable();
php index.php
*注意:请参阅表修改测试文件中的示例
贡献
发送拉取请求或打开问题以进行贡献。
许可
MIT.