58 lines
1.8 KiB
PHP
Raw Normal View History

2020-04-28 22:02:03 +08:00
<?php
namespace catcher\generate\factory;
use catcher\CatchAdmin;
use catcher\exceptions\FailedException;
2020-07-27 07:58:11 +08:00
use catcher\Utils;
2020-04-28 22:02:03 +08:00
use JaguarJack\MigrateGenerator\MigrateGenerator;
2020-05-01 00:59:46 +08:00
use think\facade\Db;
use think\helper\Str;
2020-04-28 22:02:03 +08:00
class Migration extends Factory
{
public function done($params)
{
[$module, $tableName] = $params;
// TODO: Implement done() method.
$migrationPath = CatchAdmin::directory() . $module . DIRECTORY_SEPARATOR.
2020-05-20 10:06:42 +08:00
'database' . DIRECTORY_SEPARATOR . 'migrations' .DIRECTORY_SEPARATOR;
2020-04-28 22:02:03 +08:00
CatchAdmin::makeDirectory($migrationPath);
$migrateGenerator = (new MigrateGenerator('thinkphp'));
$tables = $migrateGenerator->getDatabase()->getAllTables($tableName);
2020-05-01 00:59:46 +08:00
$version = date('YmdHis');
$file = $migrationPath . $version . '_'. $tableName . '.php';
2020-04-28 22:02:03 +08:00
foreach ($tables as $table) {
if ($table->getName() == $tableName) {
2020-07-27 07:58:11 +08:00
$content = $migrateGenerator->getMigrationContent($table);
$noPrefix = str_replace(Utils::tablePrefix(), '', $tableName);
$_content = str_replace($tableName, $noPrefix, $content, $count);
file_put_contents($file, $count == 1 ? $_content : $content);
2020-04-28 22:02:03 +08:00
if (!file_exists($file)) {
throw new FailedException('migration generate failed');
}
2020-05-01 00:59:46 +08:00
$model = new class extends \think\Model {
protected $name = 'migrations';
};
$model->insert([
'version' => $version,
2020-05-01 01:00:55 +08:00
'migration_name' => ucfirst(Str::camel($tableName)),
2020-05-01 00:59:46 +08:00
'start_time' => date('Y-m-d H:i:s'),
'end_time' => date('Y-m-d H:i:s')
]);
2020-04-28 22:02:03 +08:00
break;
}
}
2020-07-11 10:59:57 +08:00
return $file;
2020-04-28 22:02:03 +08:00
}
}