src/RocknRoot/StrayFw/Database/Postgres/Migration.php
public static function generate(array $mapping, string $mappingName, string $name) : array
    {
        $import = [];
        $up = [];
        $down = [];
        $oldSchema = Config::get(rtrim($mapping['config']['migrations']['path'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . 'schema.yml');
        $migrations = Config::get(rtrim($mapping['config']['migrations']['path'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'migrations.yml');
        $imax = count($migrations);
        for ($i = 0; $i < $imax; $i++) {
            if ($migrations[$i]['name'] == $name) {
                break;
            }
        }
        if ($i < $imax - 1) {
            $schema = Config::get(rtrim($mapping['config']['migrations']['path'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ucfirst($migrations[$i + 1]['name']) . DIRECTORY_SEPARATOR . 'schema.yml');
        } else {
            $schema = Config::get($mapping['config']['schema']);
        }

        $newKeys = array_diff_key($schema, $oldSchema);
        foreach ($newKeys as $key => $table) {
            $tableName = null;
            if (isset($table['name']) === true) {
                $tableName = $table['name'];
            } else {
                $tableName = Helper::codifyName($mappingName) . '_' . Helper::codifyName($key);
            }
            if (isset($table['type']) === false || $table['type'] == 'model') {
                $import[] = 'AddTable';
                $import[] = 'DeleteTable';
                $up[] = 'AddTable::statement($this->database, $this->nextSchema, $this->getMappingName(), \'' . $tableName . '\', \'' . $key . '\')';
                $down[] = 'DeleteTable::statement($this->database, \'' . $tableName . '\')';
                echo 'AddTable: ' . $key . PHP_EOL;
            } else {
                echo 'TODO AddEnum: ' . $key . PHP_EOL;
            }
        }

        $oldKeys = array_diff_key($oldSchema, $schema);
        foreach ($oldKeys as $key => $table) {
            $tableName = null;
            if (isset($table['name']) === true) {
                $tableName = $table['name'];
            } else {
                $tableName = Helper::codifyName($mappingName) . '_' . Helper::codifyName($key);
            }
            if (isset($table['type']) === false || $table['type'] == 'model') {
                $import[] = 'AddTable';
                $import[] = 'DeleteTable';
                $up[] = 'DeleteTable::statement($this->database, \'' . $tableName . '\')';
                $down[] = 'AddTable::statement($this->database, $this->prevSchema, $this->getMappingName(), \'' . $tableName . '\', \'' . $key . '\')';
                echo 'RemoveTable: ' . $key . PHP_EOL;
            } else {
                echo 'TODO RemoveEnum: ' . $key . PHP_EOL;
            }
        }

        $keys = array_intersect_key($oldSchema, $schema);
        foreach ($keys as $modelName => $model) {
            $tableName = null;
            if (isset($table['name']) === true) {
                $tableName = $table['name'];
            } else {
                $tableName = Helper::codifyName($mappingName) . '_' . Helper::codifyName($modelName);
            }
            if (isset($table['type']) === false || $table['type'] == 'model') {
                $newFields = array_diff_key($schema[$modelName]['fields'], $model['fields']);
                foreach ($newFields as $fieldName => $fieldDefinition) {
                    $import[] = 'AddColumn';
                    $import[] = 'DeleteColumn';
                    $up[] = 'AddColumn::statement($this->database, $this->nextSchema, $this->getMappingName(), \'' . $modelName . '\', \'' . $tableName . '\', \'' . $fieldName . '\')';
                    $down[] = 'DeleteColumn::statement($this->database, $this->nextSchema, \'' . $modelName . '\', \'' . $tableName . '\', \'' . $fieldName . '\')';
                    echo 'AddColumn: ' . $modelName . '.' . $fieldName . PHP_EOL;
                }
                $oldFields = array_diff_key($model['fields'], $schema[$modelName]['fields']);
                foreach ($oldFields as $fieldName => $fieldDefinition) {
                    $import[] = 'AddColumn';
                    $import[] = 'DeleteColumn';
                    $up[] = 'DeleteColumn::statement($this->database, $this->prevSchema, \'' . $modelName . '\', \'' . $tableName . '\', \'' . $fieldName . '\')';
                    $down[] = 'AddColumn::statement($this->database, $this->prevSchema, $this->getMappingName(), \'' . $modelName . '\', \'' . $tableName . '\', \'' . $fieldName . '\')';
                    echo 'DeleteColumn: ' . $modelName . '.' . $fieldName . PHP_EOL;
                }
                $fields = array_intersect_key($model['fields'], $schema[$modelName]['fields']);
                foreach ($fields as $fieldName => $fieldDefinition) {
                    echo 'TODO compare fields' . PHP_EOL;
                }
            } else {
                echo 'TODO Compare Enum values' . PHP_EOL;
            }
        }

        return [
            'import' => array_unique($import),
            'up' => $up,
            'down' => $down,
        ];
    }
public static function generate(array $mapping, string $mappingName, string $name) : array
    {
        $import = [];
        $up = [];
        $down = [];
        $oldSchema = Config::get(rtrim($mapping['config']['migrations']['path'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . 'schema.yml');
        $migrations = Config::get(rtrim($mapping['config']['migrations']['path'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'migrations.yml');
        $imax = count($migrations);
        for ($i = 0; $i < $imax; $i++) {
            if ($migrations[$i]['name'] == $name) {
                break;
            }
        }
        if ($i < $imax - 1) {
            $schema = Config::get(rtrim($mapping['config']['migrations']['path'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ucfirst($migrations[$i + 1]['name']) . DIRECTORY_SEPARATOR . 'schema.yml');
        } else {
            $schema = Config::get($mapping['config']['schema']);
        }

        $newKeys = array_diff_key($schema, $oldSchema);
        foreach ($newKeys as $key => $table) {
            $tableName = null;
            if (isset($table['name']) === true) {
                $tableName = $table['name'];
            } else {
                $tableName = Helper::codifyName($mappingName) . '_' . Helper::codifyName($key);
            }
            if (isset($table['type']) === false || $table['type'] == 'model') {
                $import[] = 'AddTable';
                $import[] = 'DeleteTable';
                $up[] = 'AddTable::statement($this->database, $this->nextSchema, $this->getMappingName(), \'' . $tableName . '\', \'' . $key . '\')';
                $down[] = 'DeleteTable::statement($this->database, \'' . $tableName . '\')';
                echo 'AddTable: ' . $key . PHP_EOL;
            } else {
                echo 'TODO AddEnum: ' . $key . PHP_EOL;
            }
        }

        $oldKeys = array_diff_key($oldSchema, $schema);
        foreach ($oldKeys as $key => $table) {
            $tableName = null;
            if (isset($table['name']) === true) {
                $tableName = $table['name'];
            } else {
                $tableName = Helper::codifyName($mappingName) . '_' . Helper::codifyName($key);
            }
            if (isset($table['type']) === false || $table['type'] == 'model') {
                $import[] = 'AddTable';
                $import[] = 'DeleteTable';
                $up[] = 'DeleteTable::statement($this->database, \'' . $tableName . '\')';
                $down[] = 'AddTable::statement($this->database, $this->prevSchema, $this->getMappingName(), \'' . $tableName . '\', \'' . $key . '\')';
                echo 'RemoveTable: ' . $key . PHP_EOL;
            } else {
                echo 'TODO RemoveEnum: ' . $key . PHP_EOL;
            }
        }

        $keys = array_intersect_key($oldSchema, $schema);
        foreach ($keys as $modelName => $model) {
            $tableName = null;
            if (isset($table['name']) === true) {
                $tableName = $table['name'];
            } else {
                $tableName = Helper::codifyName($mappingName) . '_' . Helper::codifyName($modelName);
            }
            if (isset($table['type']) === false || $table['type'] == 'model') {
                $newFields = array_diff_key($schema[$modelName]['fields'], $model['fields']);
                foreach ($newFields as $fieldName => $fieldDefinition) {
                    $import[] = 'AddColumn';
                    $import[] = 'DeleteColumn';
                    $up[] = 'AddColumn::statement($this->database, $this->nextSchema, $this->getMappingName(), \'' . $modelName . '\', \'' . $tableName . '\', \'' . $fieldName . '\')';
                    $down[] = 'DeleteColumn::statement($this->database, $this->nextSchema, \'' . $modelName . '\', \'' . $tableName . '\', \'' . $fieldName . '\')';
                    echo 'AddColumn: ' . $modelName . '.' . $fieldName . PHP_EOL;
                }
                $oldFields = array_diff_key($model['fields'], $schema[$modelName]['fields']);
                foreach ($oldFields as $fieldName => $fieldDefinition) {
                    $import[] = 'AddColumn';
                    $import[] = 'DeleteColumn';
                    $up[] = 'DeleteColumn::statement($this->database, $this->prevSchema, \'' . $modelName . '\', \'' . $tableName . '\', \'' . $fieldName . '\')';
                    $down[] = 'AddColumn::statement($this->database, $this->prevSchema, $this->getMappingName(), \'' . $modelName . '\', \'' . $tableName . '\', \'' . $fieldName . '\')';
                    echo 'DeleteColumn: ' . $modelName . '.' . $fieldName . PHP_EOL;
                }
                $fields = array_intersect_key($model['fields'], $schema[$modelName]['fields']);
                foreach ($fields as $fieldName => $fieldDefinition) {
                    echo 'TODO compare fields' . PHP_EOL;
                }
            } else {
                echo 'TODO Compare Enum values' . PHP_EOL;
            }
        }

        return [
            'import' => array_unique($import),
            'up' => $up,
            'down' => $down,
        ];
    }
public static function generate ( array $ mapping , string $ mappingName , string $ name ) : array { $ import = [ ] ; $ up = [ ] ; $ down = [ ] ; $ oldSchema = Config :: get ( rtrim ( $ mapping [ 'config' ] [ 'migrations' ] [ 'path' ] , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . $ name . DIRECTORY_SEPARATOR . 'schema.yml' ) ; $ migrations = Config :: get ( rtrim ( $ mapping [ 'config' ] [ 'migrations' ] [ 'path' ] , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . 'migrations.yml' ) ; $ imax = count ( $ migrations ) ; for ( $ i = 0 ; $ i < $ imax ; $ i ++ ) { if ( $ migrations [ $ i ] [ 'name' ] == $ name ) { break ; } } if ( $ i < $ imax - 1 ) { $ schema = Config :: get ( rtrim ( $ mapping [ 'config' ] [ 'migrations' ] [ 'path' ] , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . ucfirst ( $ migrations [ $ i + 1 ] [ 'name' ] ) . DIRECTORY_SEPARATOR . 'schema.yml' ) ; } else { $ schema = Config :: get ( $ mapping [ 'config' ] [ 'schema' ] ) ; } $ newKeys = array_diff_key ( $ schema , $ oldSchema ) ; foreach ( $ newKeys as $ key => $ table ) { $ tableName = null ; if ( isset ( $ table [ 'name' ] ) === true ) { $ tableName = $ table [ 'name' ] ; } else { $ tableName = Helper :: codifyName ( $ mappingName ) . '_' . Helper :: codifyName ( $ key ) ; } if ( isset ( $ table [ 'type' ] ) === false || $ table [ 'type' ] == 'model' ) { $ import [ ] = 'AddTable' ; $ import [ ] = 'DeleteTable' ; $ up [ ] = 'AddTable::statement($this->database, $this->nextSchema, $this->getMappingName(), \'' . $ tableName . '\', \'' . $ key . '\')' ; $ down [ ] = 'DeleteTable::statement($this->database, \'' . $ tableName . '\')' ; echo 'AddTable: ' . $ key . PHP_EOL ; } else { echo 'TODO AddEnum: ' . $ key . PHP_EOL ; } } $ oldKeys = array_diff_key ( $ oldSchema , $ schema ) ; foreach ( $ oldKeys as $ key => $ table ) { $ tableName = null ; if ( isset ( $ table [ 'name' ] ) === true ) { $ tableName = $ table [ 'name' ] ; } else { $ tableName = Helper :: codifyName ( $ mappingName ) . '_' . Helper :: codifyName ( $ key ) ; } if ( isset ( $ table [ 'type' ] ) === false || $ table [ 'type' ] == 'model' ) { $ import [ ] = 'AddTable' ; $ import [ ] = 'DeleteTable' ; $ up [ ] = 'DeleteTable::statement($this->database, \'' . $ tableName . '\')' ; $ down [ ] = 'AddTable::statement($this->database, $this->prevSchema, $this->getMappingName(), \'' . $ tableName . '\', \'' . $ key . '\')' ; echo 'RemoveTable: ' . $ key . PHP_EOL ; } else { echo 'TODO RemoveEnum: ' . $ key . PHP_EOL ; } } $ keys = array_intersect_key ( $ oldSchema , $ schema ) ; foreach ( $ keys as $ modelName => $ model ) { $ tableName = null ; if ( isset ( $ table [ 'name' ] ) === true ) { $ tableName = $ table [ 'name' ] ; } else { $ tableName = Helper :: codifyName ( $ mappingName ) . '_' . Helper :: codifyName ( $ modelName ) ; } if ( isset ( $ table [ 'type' ] ) === false || $ table [ 'type' ] == 'model' ) { $ newFields = array_diff_key ( $ schema [ $ modelName ] [ 'fields' ] , $ model [ 'fields' ] ) ; foreach ( $ newFields as $ fieldName => $ fieldDefinition ) { $ import [ ] = 'AddColumn' ; $ import [ ] = 'DeleteColumn' ; $ up [ ] = 'AddColumn::statement($this->database, $this->nextSchema, $this->getMappingName(), \'' . $ modelName . '\', \'' . $ tableName . '\', \'' . $ fieldName . '\')' ; $ down [ ] = 'DeleteColumn::statement($this->database, $this->nextSchema, \'' . $ modelName . '\', \'' . $ tableName . '\', \'' . $ fieldName . '\')' ; echo 'AddColumn: ' . $ modelName . '.' . $ fieldName . PHP_EOL ; } $ oldFields = array_diff_key ( $ model [ 'fields' ] , $ schema [ $ modelName ] [ 'fields' ] ) ; foreach ( $ oldFields as $ fieldName => $ fieldDefinition ) { $ import [ ] = 'AddColumn' ; $ import [ ] = 'DeleteColumn' ; $ up [ ] = 'DeleteColumn::statement($this->database, $this->prevSchema, \'' . $ modelName . '\', \'' . $ tableName . '\', \'' . $ fieldName . '\')' ; $ down [ ] = 'AddColumn::statement($this->database, $this->prevSchema, $this->getMappingName(), \'' . $ modelName . '\', \'' . $ tableName . '\', \'' . $ fieldName . '\')' ; echo 'DeleteColumn: ' . $ modelName . '.' . $ fieldName . PHP_EOL ; } $ fields = array_intersect_key ( $ model [ 'fields' ] , $ schema [ $ modelName ] [ 'fields' ] ) ; foreach ( $ fields as $ fieldName => $ fieldDefinition ) { echo 'TODO compare fields' . PHP_EOL ; } } else { echo 'TODO Compare Enum values' . PHP_EOL ; } } return [ 'import' => array_unique ( $ import ) , 'up' => $ up , 'down' => $ down , ] ; }
Generate code for migration.

@param array  $mapping     mapping definition
@param string $mappingName mapping name
@param string $name        migration name
@return array import, up and down code
Generate code for migration .