文件
chengduzhongchuliang/migrations/20261028_xishui_demo.php

51 行
3.2 KiB
PHP

<?php
// CLI only: php file.php /absolute/backup/directory fixture.json
if (PHP_SAPI !== 'cli') exit;
chdir(dirname(__DIR__));
define('APP_PATH', getcwd() . '/application/');
require getcwd() . '/thinkphp/base.php';
\think\App::initCommon();
$backup = $argv[1];
if (!is_dir($backup)) throw new Exception('Backup directory required');
$fixture = json_decode(file_get_contents($argv[2]), true);
if (!is_array($fixture) || count($fixture) !== 11) throw new Exception('Expected 11 Xishui warehouses');
$prefix = config('database.prefix');
if (!preg_match('/^[a-zA-Z0-9_]+$/D', $prefix)) throw new Exception('Invalid prefix');
$ids = array_column($fixture, 'gid');
$rows = \think\Db::name('granary')->where('site', 'xishui')->where('id', 'in', $ids)->select();
if (count($rows) !== 11) throw new Exception('Warehouse ownership mismatch');
$snapshot = $backup . '/xishui-before-demo.json';
if (!file_exists($snapshot)) {
file_put_contents($snapshot, json_encode(['granaries'=>$rows,
'tests'=>\think\Db::name('testing')->where('gid','in',$ids)->select(),
'params'=>\think\Db::name('testing_params')->where('gid','in',$ids)->select()], JSON_UNESCAPED_UNICODE));
chmod($snapshot, 0600);
}
$columns = array_column(\think\Db::query('SHOW COLUMNS FROM `' . $prefix . 'testing`'), 'Field');
if (!in_array('is_demo', $columns)) \think\Db::execute('ALTER TABLE `' . $prefix . 'testing` ADD `is_demo` tinyint unsigned NOT NULL DEFAULT 0 COMMENT \'1为演示化验记录,非实测\'');
$schema = RUNTIME_PATH . 'schema/' . config('database.database') . '.' . $prefix . 'testing.php';
if (is_file($schema)) { copy($schema, $backup . '/testing-schema-before.php'); unlink($schema); }
\think\Db::clear();
$created = [];
\think\Db::transaction(function () use ($fixture, &$created, $prefix) {
foreach ($fixture as $warehouse) {
foreach ($warehouse['records'] as $record) {
$month = str_replace('-', '', $record['date']);
// Any existing record for this period is preserved, real or demo.
if (\think\Db::name('testing')->where('gid', $warehouse['gid'])->where('type', $warehouse['type'])->where('test_date', $month)->count()) continue;
if (\app\common\library\QualityRule::validate($record['params']) !== '') throw new Exception('Invalid demo parameters');
$id = (new \app\common\model\TestingModel())->add([
'gid'=>$warehouse['gid'], 'type'=>$warehouse['type'], 'season'=>2,
'sort'=>0, 'create_time'=>time(), 'update_time'=>time(),
], $record['params'], $record['date']);
// This CLI process may have cached the schema before ADD COLUMN.
\think\Db::execute('UPDATE `' . $prefix . 'testing` SET is_demo = 1 WHERE id = ?', [$id]);
$created[] = $id;
}
}
});
$manifest = $backup . '/xishui-demo-created-ids.json';
$previous = is_file($manifest) ? json_decode(file_get_contents($manifest), true) : [];
file_put_contents($manifest, json_encode(array_values(array_unique(array_merge(is_array($previous) ? $previous : [], $created)))));
echo json_encode(['result'=>'XISHUI_DEMO_READY','created_tests'=>count($created),'created_ids'=>$created], JSON_UNESCAPED_UNICODE) . "\n";