PHP code example of 8ctopus / nano-migrations

1. Go to this page and download the library: Download 8ctopus/nano-migrations 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/ */

    

8ctopus / nano-migrations example snippets


use Oct8pus\Migration\AbstractPDOMigration;

final class Migration extends AbstractPDOMigration
{
    protected function up1() : string
    {
        return <<<'SQL'
        CREATE TABLE user (
            email TEXT NOT NULL,
            password TEXT NOT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )
        SQL;
    }

    protected function down1() : string
    {
        return <<<'SQL'
        DROP TABLE IF EXISTS user
        SQL;
    }

    protected function up2() : string
    {
        ...
    }

    protected function down2() : string
    {
        ...
    }

    /**
     * Safety check
     *
     * @param array $methods
     *
     * @return self
     *
     * @throws MigrationException
     */
    protected function safetyCheck(array $methods) : self
    {
        $stdin = fopen('php://stdin', 'r', false);

        if ($stdin === false) {
            throw new MigrationException('fopen');
        }

        $this->logger?->info('migrations to run:');

        foreach ($methods as $method) {
            $this->logger?->info("- {$method}");
        }

        $this->logger?->warning('Confirm action (y/n): ');
        $input = trim(fgets($stdin));

        fclose($stdin);

        if ($input === 'y') {
            return $this;
        }

        throw new MigrationException('safety check abort');
    }
}