98 lines
2.7 KiB
PHP
98 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Database\Factories\MigrationScheduleFactory;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class MigrationSchedule extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'source_database_id',
|
|
'target_database_id',
|
|
'tables',
|
|
'cron_expression',
|
|
'timezone',
|
|
'is_active',
|
|
'is_incremental',
|
|
'incremental_column',
|
|
'use_life_table',
|
|
'life_table_name',
|
|
'life_id_column',
|
|
'base_id_column',
|
|
'operation_column',
|
|
'datetime_column',
|
|
'run_in_parallel',
|
|
'batch_size',
|
|
'truncate_before_migration',
|
|
'create_indexes_after',
|
|
'python_script_path',
|
|
'python_script_args',
|
|
'description',
|
|
'last_run_at',
|
|
'last_successful_migration_at',
|
|
'next_run_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'tables' => 'array',
|
|
'is_active' => 'boolean',
|
|
'is_incremental' => 'boolean',
|
|
'use_life_table' => 'boolean',
|
|
'run_in_parallel' => 'boolean',
|
|
'batch_size' => 'integer',
|
|
'truncate_before_migration' => 'boolean',
|
|
'create_indexes_after' => 'boolean',
|
|
'python_script_args' => 'array',
|
|
'last_run_at' => 'datetime',
|
|
'last_successful_migration_at' => 'datetime',
|
|
'next_run_at' => 'datetime',
|
|
];
|
|
|
|
protected static function newFactory(): MigrationScheduleFactory
|
|
{
|
|
return MigrationScheduleFactory::new();
|
|
}
|
|
|
|
public function sourceDatabase(): BelongsTo
|
|
{
|
|
return $this->belongsTo(SourceDatabase::class);
|
|
}
|
|
|
|
public function targetDatabase(): BelongsTo
|
|
{
|
|
return $this->belongsTo(TargetDatabase::class);
|
|
}
|
|
|
|
public function migrationRuns(): HasMany
|
|
{
|
|
return $this->hasMany(MigrationRun::class, 'schedule_id');
|
|
}
|
|
|
|
public function scheduledTables(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Table::class, 'migration_schedule_tables')
|
|
->withPivot('order')
|
|
->orderByPivot('order');
|
|
}
|
|
|
|
public function getNextRunAttribute(): ?string
|
|
{
|
|
if (!$this->is_active || !$this->cron_expression) {
|
|
return null;
|
|
}
|
|
|
|
$cron = new \Cron\CronExpression($this->cron_expression);
|
|
$nextRun = $cron->getNextRunDate(new \DateTime(), 0, true);
|
|
|
|
return $nextRun->setTimezone(new \DateTimeZone($this->timezone))->format('Y-m-d H:i:s');
|
|
}
|
|
}
|