src/Resources/DbSchemaResource.php
public function updateSchema($tables, $allow_merge = false, $allow_delete = false, $rollback = false)
    {
        if (!is_array($tables) || empty($tables)) {
            throw new \Exception('There are no table sets in the request.');
        }

        if (!isset($tables[0])) {
            // single record possibly passed in without wrapper array
            $tables = [$tables];
        }

        $created = [];
        $references = [];
        $indexes = [];
        $out = [];
        $tableExtras = [];
        $fieldExtras = [];
        $fieldDrops = [];
        $relatedExtras = [];
        $relatedDrops = [];
        $virtualRelations = [];
        $virtualRelationDrops = [];
        $count = 0;
        $singleTable = (1 == count($tables));
        $schema = $this->parent->getSchema();

        foreach ($tables as $table) {
            try {
                if (empty($tableName = array_get($table, 'name'))) {
                    throw new \Exception('Table name missing from schema.');
                }

                //	Does it already exist
                if ($tableSchema = $this->parent->getTableSchema($tableName)) {
                    if (!$allow_merge) {
                        throw new \Exception("A table with name '$tableName' already exist in the database.");
                    }

                    \Log::debug('Schema update: ' . $tableName);

                    $results = [];
                    if (!empty($fields = array_get($table, 'field'))) {
                        $results = $this->buildTableFields($tableSchema, $fields, true, $allow_delete);
                    }
                    if (!empty($related = array_get($table, 'related'))) {
                        $related = $this->buildTableRelated($tableSchema, $related, true, $allow_delete);
                        $results = array_merge($results, $related);
                    }

                    $schema->updateTable($tableSchema, array_merge($table, $results));
                } else {
                    \Log::debug('Creating table: ' . $tableName);

                    $results = [];
                    if (!empty($fields = array_get($table, 'field'))) {
                        $results = $this->createTableFields($tableName, $fields);
                    }
                    if (!empty($related = array_get($table, 'related'))) {
                        $temp = $this->createTableRelated($tableName, $related);
                        $results = array_merge($results, $temp);
                    }

                    $schema->createTable($table, $results);

                    if (!$singleTable && $rollback) {
                        $created[] = $tableName;
                    }
                }

                if (!empty($results['commands'])) {
                    foreach ($results['commands'] as $extraCommand) {
                        try {
                            $this->parent->getConnection()->statement($extraCommand);
                        } catch (\Exception $ex) {
                            // oh well, we tried.
                        }
                    }
                }

                // add table extras
                $extras = array_only($table, ['label', 'plural', 'alias', 'description', 'name_field']);
                if (!empty($extras)) {
                    $extras['table'] = $tableName;
                    $tableExtras[] = $extras;
                }

                $fieldExtras = array_merge($fieldExtras, (array)array_get($results, 'extras'));
                $fieldDrops = array_merge($fieldDrops, (array)array_get($results, 'drop_extras'));
                $references = array_merge($references, (array)array_get($results, 'references'));
                $indexes = array_merge($indexes, (array)array_get($results, 'indexes'));
                $relatedExtras = array_merge($relatedExtras, (array)array_get($results, 'related_extras'));
                $relatedDrops = array_merge($relatedDrops, (array)array_get($results, 'drop_related_extras'));
                $virtualRelations = array_merge($virtualRelations, (array)array_get($results, 'virtual_relations'));
                $virtualRelationDrops = array_merge($virtualRelationDrops,
                    (array)array_get($results, 'drop_virtual_relations'));

                $out[$count] = ['name' => $tableName];
            } catch (\Exception $ex) {
                if ($rollback || $singleTable) {
                    //  Delete any created tables
                    throw $ex;
                }

                $out[$count] = [
                    'error' => [
                        'message' => $ex->getMessage(),
                        'code'    => $ex->getCode()
                    ]
                ];
            }

            $count++;
        }

        if (!empty($references)) {
            $schema->createFieldReferences($references);
        }
        if (!empty($indexes)) {
            $schema->createFieldIndexes($indexes);
        }
        if (!empty($tableExtras)) {
            $this->setSchemaTableExtras($tableExtras);
        }
        if (!empty($fieldExtras)) {
            $this->setSchemaFieldExtras($fieldExtras);
        }
        if (!empty($fieldDrops)) {
            foreach ($fieldDrops as $table => $dropped) {
                $this->removeSchemaExtrasForFields($table, $dropped);
            }
        }
        if (!empty($relatedExtras)) {
            $this->setSchemaRelatedExtras($relatedExtras);
        }
        if (!empty($relatedDrops)) {
            foreach ($relatedDrops as $table => $dropped) {
                $this->removeSchemaExtrasForRelated($table, $dropped);
            }
        }
        if (!empty($virtualRelations)) {
            $this->setSchemaVirtualRelationships($virtualRelations);
        }
        if (!empty($virtualRelationDrops)) {
            foreach ($virtualRelationDrops as $table => $dropped) {
                $this->removeSchemaVirtualRelationships($table, $dropped);
            }
        }

        return $out;
    }
public function updateSchema($tables, $allow_merge = false, $allow_delete = false, $rollback = false)
    {
        if (!is_array($tables) || empty($tables)) {
            throw new \Exception('There are no table sets in the request.');
        }

        if (!isset($tables[0])) {
            // single record possibly passed in without wrapper array
            $tables = [$tables];
        }

        $created = [];
        $references = [];
        $indexes = [];
        $out = [];
        $tableExtras = [];
        $fieldExtras = [];
        $fieldDrops = [];
        $relatedExtras = [];
        $relatedDrops = [];
        $virtualRelations = [];
        $virtualRelationDrops = [];
        $count = 0;
        $singleTable = (1 == count($tables));
        $schema = $this->parent->getSchema();

        foreach ($tables as $table) {
            try {
                if (empty($tableName = array_get($table, 'name'))) {
                    throw new \Exception('Table name missing from schema.');
                }

                //	Does it already exist
                if ($tableSchema = $this->parent->getTableSchema($tableName)) {
                    if (!$allow_merge) {
                        throw new \Exception("A table with name '$tableName' already exist in the database.");
                    }

                    \Log::debug('Schema update: ' . $tableName);

                    $results = [];
                    if (!empty($fields = array_get($table, 'field'))) {
                        $results = $this->buildTableFields($tableSchema, $fields, true, $allow_delete);
                    }
                    if (!empty($related = array_get($table, 'related'))) {
                        $related = $this->buildTableRelated($tableSchema, $related, true, $allow_delete);
                        $results = array_merge($results, $related);
                    }

                    $schema->updateTable($tableSchema, array_merge($table, $results));
                } else {
                    \Log::debug('Creating table: ' . $tableName);

                    $results = [];
                    if (!empty($fields = array_get($table, 'field'))) {
                        $results = $this->createTableFields($tableName, $fields);
                    }
                    if (!empty($related = array_get($table, 'related'))) {
                        $temp = $this->createTableRelated($tableName, $related);
                        $results = array_merge($results, $temp);
                    }

                    $schema->createTable($table, $results);

                    if (!$singleTable && $rollback) {
                        $created[] = $tableName;
                    }
                }

                if (!empty($results['commands'])) {
                    foreach ($results['commands'] as $extraCommand) {
                        try {
                            $this->parent->getConnection()->statement($extraCommand);
                        } catch (\Exception $ex) {
                            // oh well, we tried.
                        }
                    }
                }

                // add table extras
                $extras = array_only($table, ['label', 'plural', 'alias', 'description', 'name_field']);
                if (!empty($extras)) {
                    $extras['table'] = $tableName;
                    $tableExtras[] = $extras;
                }

                $fieldExtras = array_merge($fieldExtras, (array)array_get($results, 'extras'));
                $fieldDrops = array_merge($fieldDrops, (array)array_get($results, 'drop_extras'));
                $references = array_merge($references, (array)array_get($results, 'references'));
                $indexes = array_merge($indexes, (array)array_get($results, 'indexes'));
                $relatedExtras = array_merge($relatedExtras, (array)array_get($results, 'related_extras'));
                $relatedDrops = array_merge($relatedDrops, (array)array_get($results, 'drop_related_extras'));
                $virtualRelations = array_merge($virtualRelations, (array)array_get($results, 'virtual_relations'));
                $virtualRelationDrops = array_merge($virtualRelationDrops,
                    (array)array_get($results, 'drop_virtual_relations'));

                $out[$count] = ['name' => $tableName];
            } catch (\Exception $ex) {
                if ($rollback || $singleTable) {
                    //  Delete any created tables
                    throw $ex;
                }

                $out[$count] = [
                    'error' => [
                        'message' => $ex->getMessage(),
                        'code'    => $ex->getCode()
                    ]
                ];
            }

            $count++;
        }

        if (!empty($references)) {
            $schema->createFieldReferences($references);
        }
        if (!empty($indexes)) {
            $schema->createFieldIndexes($indexes);
        }
        if (!empty($tableExtras)) {
            $this->setSchemaTableExtras($tableExtras);
        }
        if (!empty($fieldExtras)) {
            $this->setSchemaFieldExtras($fieldExtras);
        }
        if (!empty($fieldDrops)) {
            foreach ($fieldDrops as $table => $dropped) {
                $this->removeSchemaExtrasForFields($table, $dropped);
            }
        }
        if (!empty($relatedExtras)) {
            $this->setSchemaRelatedExtras($relatedExtras);
        }
        if (!empty($relatedDrops)) {
            foreach ($relatedDrops as $table => $dropped) {
                $this->removeSchemaExtrasForRelated($table, $dropped);
            }
        }
        if (!empty($virtualRelations)) {
            $this->setSchemaVirtualRelationships($virtualRelations);
        }
        if (!empty($virtualRelationDrops)) {
            foreach ($virtualRelationDrops as $table => $dropped) {
                $this->removeSchemaVirtualRelationships($table, $dropped);
            }
        }

        return $out;
    }
@param      $tables
@param bool $allow_merge
@param bool $allow_delete
@param bool $rollback

@return array
@throws \Exception