dimer47/laravel-env-migrations

Versioned migration system for Laravel's .env file, similar to database migrations.

Maintainers

Package info

github.com/dimer47/laravel-env-migrations

pkg:composer/dimer47/laravel-env-migrations

Statistics

Installs: 24

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.1 2026-03-17 23:27 UTC

This package is auto-updated.

Last update: 2026-03-17 23:28:37 UTC


README

Latest Version on Packagist Total Downloads PHP Version Require Laravel License

Versioned migration system for Laravel's .env file, similar to database migrations. πŸ—‚οΈ

πŸ‡«πŸ‡· Lire en franΓ§ais

πŸŽ‰ Features

  • πŸ”„ Versioned migrations β€” manage .env changes just like database migrations
  • πŸ“¦ Database tracking β€” tracking table with batches and JSON history
  • βͺ Rollback β€” revert the last batch of changes
  • πŸ“Š Status β€” view the state of all migrations
  • 🧩 Laravel ready β€” auto-discovery, publishable config, Artisan commands
  • πŸ›‘οΈ Production safe β€” mandatory confirmation in production (unless --force)
  • πŸ”§ 11 methods β€” set, get, remove, rename, copy, group, block, etc.

πŸ“ Installation

Via Composer (packagist)

composer require dimer47/laravel-env-migrations

πŸ§ͺ Via a local repository (development)

Add to the host project's composer.json:

{
    "repositories": [
        {
            "type": "path",
            "url": "packages/laravel-env-migrations"
        }
    ],
    "require": {
        "dimer47/laravel-env-migrations": "*"
    }
}

Then:

composer update dimer47/laravel-env-migrations

πŸ”§ Configuration

Publish the package files:

# Publish the config
php artisan vendor:publish --tag=env-migrations-config

# Publish the tracking table migration
php artisan vendor:publish --tag=env-migrations-migrations

# Run the migration
php artisan migrate

βš™οΈ Configuration options (config/env-migrations.php)

Option Description Default
table Name of the tracking table env_migrations
path Directory for migration files database/env-migrations

πŸš€ Quick Start

βž• Create a migration

php artisan env:migrate:make add_redis_configuration

This creates a file in database/env-migrations/ with a ready-to-use template.

✏️ Write a migration

<?php

use Dimer47\EnvMigrations\EnvMigration;

return new class extends EnvMigration
{
    public function getDescription(): string
    {
        return 'Configure Redis for cache and queues';
    }

    public function up(): void
    {
        // Set a variable
        $this->set('CACHE_DRIVER', 'redis');

        // Set only if missing
        $this->setIfMissing('REDIS_HOST', '127.0.0.1');

        // Rename a variable (keeps its position)
        $this->rename('OLD_REDIS_PORT', 'REDIS_PORT');

        // Copy a variable
        $this->copy('REDIS_HOST', 'REDIS_CACHE_HOST');

        // Remove a variable
        $this->remove('DEPRECATED_CACHE_KEY');

        // Group variables together
        $this->ensureGroup([
            'REDIS_HOST' => '127.0.0.1',
            'REDIS_PASSWORD' => 'null',
            'REDIS_PORT' => '6379',
        ], '# Redis Configuration');

        // Insert a group after a reference variable
        $this->ensureGroupAfter('CACHE_DRIVER', [
            'CACHE_PREFIX' => 'myapp',
            'CACHE_TTL' => '3600',
        ]);

        // Append a multi-line block
        $this->appendBlock(<<<'ENV'
# Queue Configuration
QUEUE_CONNECTION=redis
QUEUE_RETRY_AFTER=90
ENV, 'QUEUE_CONNECTION');
    }

    public function down(): void
    {
        $this->set('CACHE_DRIVER', 'file');
        $this->remove('REDIS_CACHE_HOST');
        $this->rename('REDIS_PORT', 'OLD_REDIS_PORT');
        $this->removeBlock('# Queue Configuration', 'QUEUE_RETRY_AFTER');
    }
};

▢️ Run migrations

php artisan env:migrate
php artisan env:migrate --force  # Skip confirmation in production

πŸ“Š View status

php artisan env:migrate:status

βͺ Rollback last batch

php artisan env:migrate:rollback
php artisan env:migrate:rollback --force  # Skip confirmation in production

πŸ“š Available methods

Method Description
set($key, $value) Set or update a variable
get($key) Read a variable's value
remove($key) Remove a variable
rename($oldKey, $newKey) Rename a variable (keeps its position)
copy($source, $target) Copy a variable to a new key
exists($key) Check if a variable exists
setIfMissing($key, $value) Set only if not already defined
ensureGroup($vars, $comment) Group variables together
ensureGroupAfter($afterKey, $vars, $comment) Insert a group after a reference variable
appendBlock($block, $identifier) Append a multi-line block (duplicate-safe)
removeBlock($start, $end) Remove a block by markers

πŸ–₯️ Artisan Commands

Command Description
env:migrate ▢️ Run pending migrations
env:migrate:rollback βͺ Rollback the last batch
env:migrate:status πŸ“Š Display migrations status
env:migrate:make {name} βž• Create a new migration

πŸ—„οΈ Tracking table schema

Column Type Description
id BIGINT Auto-incremented primary key
migration VARCHAR(255) Unique migration name
batch INT Execution batch number
changes JSON Detailed change history
executed_at TIMESTAMP Execution date

πŸ“„ License

MIT β€” See LICENSE