Skip to content

Commit

Permalink
add: 定时自动逆向migrate表结构及配置表到本地
Browse files Browse the repository at this point in the history
  • Loading branch information
xbpk3t committed Feb 23, 2021
1 parent 38fc4ed commit 7de5107
Show file tree
Hide file tree
Showing 34 changed files with 1,658 additions and 114 deletions.
2 changes: 1 addition & 1 deletion Modules/Api/Tests/Feature/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testLogin()
{
$url = $this->host . '/api/user/login';
$response = $this->withHeaders($this->header)->post($url, [
'username' => 'd729c0e7-e726-46c1-86f5-ccfd96c9acbf',
'username' => '115fd8bb-d64e-47b2-89fa-21640927aeb2',
'password' => '102gzg9RBiLnOnwHx',
]);

Expand Down
73 changes: 67 additions & 6 deletions Modules/Common/Console/MigrationGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

namespace Modules\Common\Console;

use Exception;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Artisan;
use Orangehill\Iseed\TableNotFoundException;
use Symfony\Component\HttpFoundation\File\Exception\FileException;

class MigrationGenerateCommand extends Command
{
protected $signature = '';
protected $signature = 'migrate:genAndSeed';

protected $description = '';

protected $clearDays = '15';
protected $clearDays = '5';

protected $tableMap = [
'admin_menu',
Expand All @@ -21,27 +27,82 @@ class MigrationGenerateCommand extends Command
'admin_user',
'admin_user_permission',
'admin_user_role',
'tz_user',
];

public function handle()
{
$this->createMigrations();
if (true == $this->createMigrations()) {
$this->clearHistoryMigrations();
}

$this->seeds();

$this->clearHistoryMigrations();
}

/**
* @return bool
*/
protected function createMigrations()
{
try {
Artisan::call('migrate:generate', [
'--no-interaction' => true,
]);
$this->info('create migrations success');

return true;
} catch (Exception $exception) {
$this->error('create migrations error');
// return $exception->getMessage();
return false;
}
}

/**
* @return bool
*/
protected function seeds()
{
$tableName = 'admin_role';
try {
Artisan::call('iseed', [
'tables' => implode(',', $this->tableMap),
'--force' => true,
]);
$this->info('create table seeders success');

return true;
} catch (TableNotFoundException $exception) {
// todo 加一个crontab的log-channel
$this->error('create table seeders error');
// return $exception->getMessage();
return false;
}
}

protected function clearHistoryMigrations()
{
$dbPath = database_path('migrations');
if (!File::exists($dbPath)) {
$this->error('database文件夹不存在');
}

try {
$d = File::files($dbPath);

// 根据日期去删除文件,批量移除
$days = beforeDaysFormatList($this->clearDays, 'Y_m_d');
foreach (array_diff($days, [todayDate('Y_m_d')]) as $day) {
foreach ($d as $k) {
if (Str::startsWith($k->getRelativePathname(), $day)) {
File::delete($k->getPathname());
}
}
}
$this->info('migration files delete success');

return true;
} catch (FileException $exception) {
throw new FileException('migration files delete error');
}
}
}
2 changes: 1 addition & 1 deletion Modules/Common/Tests/Feature/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function userLogin()
// todo 需要先注册
$url = $this->host . '/api/user/login';
$response = $this->withHeaders($this->header)->post($url, [
'username' => 'd729c0e7-e726-46c1-86f5-ccfd96c9acbf',
'username' => '115fd8bb-d64e-47b2-89fa-21640927aeb2',
'password' => '102gzg9RBiLnOnwHx',
]);

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
1. *clone项目*`gcl git@github.com:x1a0xv4n/laravel-starter.git`
2. *切换分支*。切换分支到对应laravel版本,master分支默认laravel最新版本`git checkout -b origin/target-version-branch`
3. *配置文件*。开发环境下,请复制`.env.prod``.env.dev`,并填写基本配置如数据库、redis、dingo等,如有问题,请发issue。
4. *sql文件*。执行根目录下的`laravel_starter.sql`
5. *部署服务*
4. *sql文件*
1. `migrate`生成表结构
2. `db:seed`写入配置表数据
5. *部署服务*



Expand Down
3 changes: 3 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Scheduling\Schedule;
use Modules\Admin\Console\AdminInitCommand;
use Modules\Common\Console\MigrationGenerateCommand;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
Expand All @@ -15,6 +16,7 @@ class Kernel extends ConsoleKernel
*/
protected $commands = [
AdminInitCommand::class,
MigrationGenerateCommand::class,
];

/**
Expand All @@ -25,6 +27,7 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command(MigrationGenerateCommand::class)->daily();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions artisan
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ $app = require_once __DIR__.'/bootstrap/app.php';
|
*/

// .env配置多环境
$app->detectEnvironment(function () use ($app) {
//获取默认env文件配置要加载的env文件名
$envName = trim(file_get_contents($app->environmentPath() . '/.env'));
$app->loadEnvironmentFrom('.env.' . $envName);
});

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

$status = $kernel->handle(
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"mockery/mockery": "^1.4.2",
"nunomaduro/collision": "^5.0",
"nunomaduro/larastan": "^0.7.0",
"orangehill/iseed": "^3.0",
"oscarafdev/migrations-generator": "^2.0",
"phpunit/phpunit": "^9.3.3"
},
"config": {
Expand Down
Loading

0 comments on commit 7de5107

Please sign in to comment.