elefant / app-migrate
Elefant CMS 的数据库迁移
Requires
This package is auto-updated.
Last update: 2024-08-25 07:36:09 UTC
README
此应用程序为 Elefant CMS 提供数据库迁移框架,包括用于自动执行迁移的简单 API 以及一系列命令行选项,用于查看和应用/撤销更改。
状态:测试版
安装
将 migrate
应用程序文件夹复制到 apps/migrate
,然后运行以下命令以导入数据库模式
./elefant import-db apps/migrate/conf/install_mysql.sql
注意:用您网站的相应数据库驱动程序替换数据库驱动程序。
用法
从命令行
# update to the latest revision ./elefant migrate/up myapp\\MyModel # update to the specified revision ./elefant migrate/up myapp\\MyModel 20130922012345 # revert all revisions ./elefant migrate/down myapp\\MyModel # revert to the specified revision ./elefant migrate/down myapp\\MyModel 20130922012345 # list all available migrations ./elefant migrate/list # list all versions of a migration ./elefant migrate/versions myapp\\MyModel # check which version is current ./elefant migrate/current myapp\\MyModel # check if the current version is the latest ./elefant migrate/is-latest myapp\\MyModel # generate a new migration class ./elefant migrate/generate myapp\\MyModel
定义新表
以下是一个基本的迁移示例,它在 migrate/up
上创建一个表,并在 migrate/down
上删除它。
<?php namespace myapp; class MyModel_20130922012345 extends \Migration { public $table = '#prefix#mymodel'; public function up () { return $this->table () ->column ('id', 'integer', array ('primary' => true)) ->column ('title', 'string', array ('limit' => 72)) ->column ('created', 'datetime', array ('null' => false)) ->column ('body', 'text', array ('null' => false)) ->index (array ('created')) ->create (); } public function down () { return $this->drop (); } } ?>
此迁移定义了一个具有四个列的表,这将对以下 CREATE TABLE
语句进行翻译
CREATE TABLE #prefix#mymodel ( id integer primary key, title char(72), created datetime not null, body text not null );
需要注意以下几点
- 迁移是使用
MODEL_DATETIME
形式命名的类,它扩展了基类\Migration
。 - 迁移位于应用程序的
migrations
文件夹中,并且必须与类名称匹配命名。 - 迁移可以通过
$table
属性设置表名,或者明确地传递给像table()
和drop()
这样的方法。 #prefix#
在数据库名称中将替换为 Elefant 全球配置中[Database]
部分的prefix
设置的值。- 迁移定义了两个方法:
up()
和down()
,分别用于应用或撤销迁移的更改。每个方法应返回 true 或 false,具体取决于它们是否成功。
迁移版本
我们建议使用 YYYYMMDDHHIISS
形式的日期来保持迁移的顺序。但是,应用程序不强制执行特定的命名方案,因此您可以使用任何字母和数字的组合来命名它们,只需注意它们将按字母数字顺序排序,因此您应该相应地命名它们以正确应用它们。
可用方法
这些方法是从 \Migration
继承的,供您使用。执行 SQL 语句的方法将返回 true 或 false,并且 $this->error
将包含任何错误消息。
add_column($name, $type = 'char', $options = array ())
通过修改现有表添加列。请注意,SQLite 将始终将列添加到表的末尾,即使传递了 'after' => 'colname'
选项。
column($name, $type = 'char', $options = array ())
向 CREATE TABLE
定义中添加列。返回 $this
以便您可以链接多个 column()
调用,然后执行 create()
以执行查询。
列类型值可以是您选择的数据库支持的任何值。 string
也是 char
的别名。
选项包括
auto-increment
- 列应自动增加comment
- 要添加到列的注释default
- 列的默认值limit
- 列的字符限制null
- 列可以是或不可以为 nullprimary
- 列是主键signed
- 列是已签名的(可能包含负值)unique
- 列值必须唯一unsigned
- 列值是无符号的(可能只包含正值)
create()
根据之前的table()
和column()
调用执行一个CREATE TABLE
语句。
driver()
返回数据库连接的PDO驱动程序名称。
drop($table = null)
执行一个DROP TABLE
语句。
drop_column($name)
从表中删除一个列。注意:SQLite不支持此操作。
index($fields)
在表定义中添加一个索引,使用指定的字段名列表。
rename_column($old, $new, $type = 'char', $options = array ())
重命名现有列。注意:SQLite不支持此操作。
run($sql, $params = array ())
执行一个直接的SQL语句。
table($table = null)
初始化一个新的CREATE TABLE
链。返回$this
以便您可以对其调用多个column()
,然后通过create()
执行查询。