PHP code example of grazulex / laravel-strongmigrations
1. Go to this page and download the library: Download grazulex/laravel-strongmigrations library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
grazulex / laravel-strongmigrations example snippets
Schema::table('users', function (Blueprint $table) {
$table->uuid('public_id')->default(DB::raw('gen_random_uuid()')); // reported
$table->timestamp('seen_at')->nullable()->default(new Expression('clock_timestamp()')); // reported
$table->timestamp('created_at')->useCurrent(); // not reported (stable)
});
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained(); // add_foreign_key on user_id
$table->foreignId('author_id')->constrained('users'); // add_foreign_key, referenced table "users"
$table->foreignIdFor(Team::class, 'team_id')->constrained(); // add_foreign_key on team_id
});
use Grazulex\StrongMigrations\StrongMigrations;
return new class extends Migration
{
public function up(): void
{
StrongMigrations::safetyAssured(function () {
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('legacy_field');
});
});
}
};
use Grazulex\StrongMigrations\StrongMigrations;
StrongMigrations::addCheck(function (Operation $operation, DatabaseContext $context) {
if ($operation->type === OperationType::AddColumn && $operation->column === 'metadata') {
StrongMigrations::stop('Use `data` instead of `metadata` for consistency.');
}
});
StrongMigrations::setMessage('This migration has been reviewed and approved by the DBA team.');
// config/strong-migrations.php
return [
// Enable/disable the package
'enabled' => env('STRONG_MIGRATIONS_ENABLED', true),
// 'block' or 'warn'
'mode' => env('STRONG_MIGRATIONS_MODE', 'block'),
// Override auto-detected database driver
'database_driver' => env('STRONG_MIGRATIONS_DRIVER'),
// Override auto-detected database version
'database_version' => env('STRONG_MIGRATIONS_VERSION'),
// Skip migrations up to this timestamp, inclusive (format: YYYY_MM_DD_HHMMSS).
// Applies to both `php artisan migrate` and `php artisan migrate:check`.
'start_after' => null,
// Disable specific rules by ID
'disabled_checks' => [
// 'remove_column',
// 'raw_sql',
],
// Session timeouts applied while each migration runs (null = untouched)
'timeouts' => [
'statement_timeout' => '1h', // PostgreSQL
'lock_timeout' => '10s', // PostgreSQL
'lock_wait_timeout' => 10, // MySQL / MariaDB (seconds)
],
];
bash
php artisan strong-migrations:install
$ php artisan migrate
Dangerous operation detected in migration: 2025_01_15_000001_drop_email.php
[remove_column] Removing column `email` that is used by the application
will cause errors until application servers are restarted.
Safe alternative:
Step 1 - Ignore the column in your Eloquent model:
protected static array $ignoredColumns = ['email'];
Step 2 - Deploy the code.
Step 3 - Create a migration that drops the column, wrapped in:
StrongMigrations::safetyAssured(function () { ... });