优化数据库迁移生成,支持decimal。

This commit is contained in:
2022-09-06 10:37:26 +08:00
parent b1b98c1e3a
commit 5d949d2b3d
3 changed files with 53 additions and 32 deletions

View File

@@ -135,7 +135,17 @@ class Migrate extends Command
$column_item['type'] = $type_info[0]; $column_item['type'] = $type_info[0];
$length = substr($type_info[1], 0, strpos($type_info[1], ')')); $length = substr($type_info[1], 0, strpos($type_info[1], ')'));
if (strpos($length, ',') !== false) {
$length_info = explode(',', $length);
$column_item['options']['precision'] = $length_info[0];
$column_item['options']['scale'] = $length_info[1];
} else {
$column_item['options']['limit'] = $length; $column_item['options']['limit'] = $length;
}
if (strpos($type, 'unsigned') !== false) { if (strpos($type, 'unsigned') !== false) {
// 无符号 // 无符号
@@ -183,6 +193,17 @@ class Migrate extends Command
if (isset($type_map[$column_item_set['type']])) { if (isset($type_map[$column_item_set['type']])) {
$column_item_set['type'] = $type_map[$column_item_set['type']]; $column_item_set['type'] = $type_map[$column_item_set['type']];
} }
foreach ($column_item_set['options'] as $key => $option) {
if(is_array($option)){
$column_item_set['options'][$key] = "[".implode(',',$option)."]";
}else{
$column_item_set['options'][$key] = "'{$option}'";
}
}
} }

View File

@@ -28,7 +28,7 @@ class {$class_name} extends Migrator
{ {
$table = $this->table('{$table}') $table = $this->table('{$table}')
->setComment('{$table_info.TABLE_COMMENT}') ->setComment('{$table_info.TABLE_COMMENT}')
{volist name="table_columns" id="column"}->addColumn('{$column.field}', '{$column.type}', [{volist name="column.options" id="option"}'{$key}' => '{$option}', {/volist}]) {volist name="table_columns" id="column"}->addColumn('{$column.field}', '{$column.type}', [{volist name="column.options" id="option"}'{$key}' => {$option|raw}, {/volist}])
{/volist} {/volist}
{volist name="table_keys_uni" id="vo"}->addIndex('{$vo}',['unique'=>true]) {volist name="table_keys_uni" id="vo"}->addIndex('{$vo}',['unique'=>true])
{/volist} {/volist}

View File

@@ -56,7 +56,7 @@ class TestGoods extends Migrator
->addColumn('store_city', 'string', ['limit' => '100', 'null' => '0', 'default' => '山东省/临沂市', 'comment' => '仓库 {city} (level:city)', ]) ->addColumn('store_city', 'string', ['limit' => '100', 'null' => '0', 'default' => '山东省/临沂市', 'comment' => '仓库 {city} (level:city)', ])
->addColumn('tag_input', 'string', ['limit' => '100', 'null' => '0', 'comment' => '商品标签 (输入) {tag}', ]) ->addColumn('tag_input', 'string', ['limit' => '100', 'null' => '0', 'comment' => '商品标签 (输入) {tag}', ])
->addColumn('uid', 'string', ['limit' => '100', 'null' => '0', 'comment' => '唯一id', ]) ->addColumn('uid', 'string', ['limit' => '100', 'null' => '0', 'comment' => '唯一id', ])
->addColumn('price', 'decimal', ['limit' => '10,2', 'null' => '1', 'comment' => '价格',]) ->addColumn('price', 'decimal', ['precision' => '10', 'scale' => '2', 'null' => '1', 'comment' => '价格', ])
->addColumn('detail', 'text', ['null' => '1', 'comment' => '详情', ]) ->addColumn('detail', 'text', ['null' => '1', 'comment' => '详情', ])
->addIndex('uid',['unique'=>true]) ->addIndex('uid',['unique'=>true])
->addIndex('detail',['type'=>'fulltext']) ->addIndex('detail',['type'=>'fulltext'])