src/Resources/DbSchemaResource.php
protected function buildTableField($tableName, $field, $oldTable = false, $oldField = null)
    {
        $name = array_get($field, 'name');
        $type = strtolower((string)array_get($field, 'type'));
        $commands = [];
        $indexes = [];
        $references = [];
        $schema = $this->parent->getSchema();
        switch ($type) {
            case DbSimpleTypes::TYPE_ID:
                $pkExtras = $schema->getPrimaryKeyCommands($tableName, $name);
                $commands = array_merge($commands, $pkExtras);
                break;
        }

        if (((DbSimpleTypes::TYPE_REF == $type) || array_get($field, 'is_foreign_key'))) {
            // special case for references because the table referenced may not be created yet
            if (empty($refTable = array_get($field, 'ref_table'))) {
                throw new \Exception("Invalid schema detected - no table element for reference type of $name.");
            }

            if ((false === strpos($refTable, '.')) && !empty($namingSchema = $this->parent->getNamingSchema())) {
                $refTable = $namingSchema . '.' . $refTable;
            }
            $refColumns = array_get($field, 'ref_field', array_get($field, 'ref_fields'));
            $refOnDelete = array_get($field, 'ref_on_delete');
            $refOnUpdate = array_get($field, 'ref_on_update');

            if ($schema->allowsSeparateForeignConstraint()) {
                if (!isset($oldField) || !$oldField->isForeignKey) {
                    // will get to it later, $refTable may not be there
                    $keyName = $schema->makeConstraintName('fk', $tableName, $name);
                    $references[] = [
                        'name'      => $keyName,
                        'table'     => $tableName,
                        'column'    => $name,
                        'ref_table' => $refTable,
                        'ref_field' => $refColumns,
                        'delete'    => $refOnDelete,
                        'update'    => $refOnUpdate,
                    ];
                }
            }
        }

        // regardless of type
        if (array_get($field, 'is_unique')) {
            if ($schema->requiresCreateIndex(true, !$oldTable)) {
                // will get to it later, create after table built
                $keyName = $schema->makeConstraintName('undx', $tableName, $name);
                $indexes[] = [
                    'name'   => $keyName,
                    'table'  => $tableName,
                    'column' => $name,
                    'unique' => true,
                    'drop'   => isset($oldField),
                ];
            }
        } elseif (array_get($field, 'is_index')) {
            if ($schema->requiresCreateIndex($oldTable, !$oldTable)) {
                // will get to it later, create after table built
                $keyName = $schema->makeConstraintName('ndx', $tableName, $name);
                $indexes[] = [
                    'name'   => $keyName,
                    'table'  => $tableName,
                    'column' => $name,
                    'drop'   => isset($oldField),
                ];
            }
        }

        return ['field' => $field, 'commands' => $commands, 'references' => $references, 'indexes' => $indexes];
    }
protected function buildTableField($tableName, $field, $oldTable = false, $oldField = null)
    {
        $name = array_get($field, 'name');
        $type = strtolower((string)array_get($field, 'type'));
        $commands = [];
        $indexes = [];
        $references = [];
        $schema = $this->parent->getSchema();
        switch ($type) {
            case DbSimpleTypes::TYPE_ID:
                $pkExtras = $schema->getPrimaryKeyCommands($tableName, $name);
                $commands = array_merge($commands, $pkExtras);
                break;
        }

        if (((DbSimpleTypes::TYPE_REF == $type) || array_get($field, 'is_foreign_key'))) {
            // special case for references because the table referenced may not be created yet
            if (empty($refTable = array_get($field, 'ref_table'))) {
                throw new \Exception("Invalid schema detected - no table element for reference type of $name.");
            }

            if ((false === strpos($refTable, '.')) && !empty($namingSchema = $this->parent->getNamingSchema())) {
                $refTable = $namingSchema . '.' . $refTable;
            }
            $refColumns = array_get($field, 'ref_field', array_get($field, 'ref_fields'));
            $refOnDelete = array_get($field, 'ref_on_delete');
            $refOnUpdate = array_get($field, 'ref_on_update');

            if ($schema->allowsSeparateForeignConstraint()) {
                if (!isset($oldField) || !$oldField->isForeignKey) {
                    // will get to it later, $refTable may not be there
                    $keyName = $schema->makeConstraintName('fk', $tableName, $name);
                    $references[] = [
                        'name'      => $keyName,
                        'table'     => $tableName,
                        'column'    => $name,
                        'ref_table' => $refTable,
                        'ref_field' => $refColumns,
                        'delete'    => $refOnDelete,
                        'update'    => $refOnUpdate,
                    ];
                }
            }
        }

        // regardless of type
        if (array_get($field, 'is_unique')) {
            if ($schema->requiresCreateIndex(true, !$oldTable)) {
                // will get to it later, create after table built
                $keyName = $schema->makeConstraintName('undx', $tableName, $name);
                $indexes[] = [
                    'name'   => $keyName,
                    'table'  => $tableName,
                    'column' => $name,
                    'unique' => true,
                    'drop'   => isset($oldField),
                ];
            }
        } elseif (array_get($field, 'is_index')) {
            if ($schema->requiresCreateIndex($oldTable, !$oldTable)) {
                // will get to it later, create after table built
                $keyName = $schema->makeConstraintName('ndx', $tableName, $name);
                $indexes[] = [
                    'name'   => $keyName,
                    'table'  => $tableName,
                    'column' => $name,
                    'drop'   => isset($oldField),
                ];
            }
        }

        return ['field' => $field, 'commands' => $commands, 'references' => $references, 'indexes' => $indexes];
    }
@param string       $tableName
@param array        $field
@param bool         $oldTable
@param ColumnSchema $oldField

@return array
@throws \Exception