src/RocknRoot/StrayFw/Database/Postgres/Schema.php
public function build()
    {
        $mapping = Mapping::get($this->mapping);
        $definition = $this->getDefinition();
        $database = GlobalDatabase::get($mapping['config']['database']);

        foreach ($definition as $modelName => $modelDefinition) {
            if (isset($modelDefinition['links']) === false) {
                continue;
            }
            $tableName = null;
            if (isset($modelDefinition['name']) === true) {
                $tableName = $modelDefinition['name'];
            } else {
                $tableName = Helper::codifyName($this->mapping) . '_' . Helper::codifyName($modelName);
            }
            $query = $database->getMasterLink()->query('SELECT COUNT(*) as count FROM pg_class WHERE relname = \'' . $tableName . '\'');
            $result = $query->fetch(\PDO::FETCH_ASSOC);
            if ($result['count'] != 0) {
                foreach ($modelDefinition['links'] as $keyName => $keyDefinition) {
                    $statement = Mutation\DeleteForeignKey::statement($database, $tableName, $keyName);
                    if ($statement->execute() == false) {
                        throw new DatabaseError('db/build : ' . print_r($statement->errorInfo(), true));
                    }
                }
            }
        }

        foreach ($definition as $modelName => $modelDefinition) {
            if (isset($modelDefinition['type']) === false || $modelDefinition['type'] === 'model') {
                $tableName = null;
                if (isset($modelDefinition['name']) === true) {
                    $tableName = $modelDefinition['name'];
                } else {
                    $tableName = Helper::codifyName($this->mapping) . '_' . Helper::codifyName($modelName);
                }
                $statement = Mutation\DeleteTable::statement($database, $tableName);
                if ($statement->execute() == false) {
                    throw new DatabaseError('db/build : ' . print_r($statement->errorInfo(), true));
                }
            }
        }
        foreach ($definition as $modelName => $modelDefinition) {
            if (isset($modelDefinition['type']) === true && $modelDefinition['type'] === 'enum') {
                $modelRealName = null;
                if (isset($modelDefinition['name']) === true) {
                    $modelRealName = $modelDefinition['name'];
                } else {
                    $modelRealName = Helper::codifyName($this->mapping) . '_' . Helper::codifyName($modelName);
                }
                $statement = Mutation\DeleteEnum::statement($database, $modelRealName);
                if ($statement->execute() == false) {
                    throw new DatabaseError('db/build : ' . print_r($statement->errorInfo(), true));
                }
            }
        }

        foreach ($definition as $modelName => $modelDefinition) {
            if (isset($modelDefinition['type']) === true && $modelDefinition['type'] === 'enum') {
                $this->buildEnum($modelName, $modelDefinition);
            }
        }
        foreach ($definition as $modelName => $modelDefinition) {
            if (isset($modelDefinition['type']) === false || $modelDefinition['type'] === 'model') {
                $this->buildModel($modelName, $modelDefinition);
            }
        }

        foreach ($definition as $modelName => $modelDefinition) {
            if ((isset($modelDefinition['type']) === false || $modelDefinition['type'] === 'model') && isset($modelDefinition['links']) === true) {
                foreach ($modelDefinition['links'] as $foreignName => $foreignDefinition) {
                    $tableName = null;
                    if (isset($modelDefinition['name']) === true) {
                        $tableName = $modelDefinition['name'];
                    } else {
                        $tableName = Helper::codifyName($this->mapping) . '_' . Helper::codifyName($modelName);
                    }
                    $foreignTableName = null;
                    if (isset($definition[$foreignDefinition['model']]['name']) === true) {
                        $foreignTableName = $definition[$foreignDefinition['model']]['name'];
                    } else {
                        $foreignTableName = Helper::codifyName($this->mapping) . '_' . Helper::codifyName($foreignDefinition['model']);
                    }
                    $statement = Mutation\AddForeignKey::statement($database, $definition, $modelName, $tableName, $foreignName, $foreignTableName);
                    if ($statement->execute() == false) {
                        throw new DatabaseError('db/build : ' . print_r($statement->errorInfo(), true));
                    }
                }
            }
        }
        Migration::ensureTable($mapping);
    }
public function build()
    {
        $mapping = Mapping::get($this->mapping);
        $definition = $this->getDefinition();
        $database = GlobalDatabase::get($mapping['config']['database']);

        foreach ($definition as $modelName => $modelDefinition) {
            if (isset($modelDefinition['links']) === false) {
                continue;
            }
            $tableName = null;
            if (isset($modelDefinition['name']) === true) {
                $tableName = $modelDefinition['name'];
            } else {
                $tableName = Helper::codifyName($this->mapping) . '_' . Helper::codifyName($modelName);
            }
            $query = $database->getMasterLink()->query('SELECT COUNT(*) as count FROM pg_class WHERE relname = \'' . $tableName . '\'');
            $result = $query->fetch(\PDO::FETCH_ASSOC);
            if ($result['count'] != 0) {
                foreach ($modelDefinition['links'] as $keyName => $keyDefinition) {
                    $statement = Mutation\DeleteForeignKey::statement($database, $tableName, $keyName);
                    if ($statement->execute() == false) {
                        throw new DatabaseError('db/build : ' . print_r($statement->errorInfo(), true));
                    }
                }
            }
        }

        foreach ($definition as $modelName => $modelDefinition) {
            if (isset($modelDefinition['type']) === false || $modelDefinition['type'] === 'model') {
                $tableName = null;
                if (isset($modelDefinition['name']) === true) {
                    $tableName = $modelDefinition['name'];
                } else {
                    $tableName = Helper::codifyName($this->mapping) . '_' . Helper::codifyName($modelName);
                }
                $statement = Mutation\DeleteTable::statement($database, $tableName);
                if ($statement->execute() == false) {
                    throw new DatabaseError('db/build : ' . print_r($statement->errorInfo(), true));
                }
            }
        }
        foreach ($definition as $modelName => $modelDefinition) {
            if (isset($modelDefinition['type']) === true && $modelDefinition['type'] === 'enum') {
                $modelRealName = null;
                if (isset($modelDefinition['name']) === true) {
                    $modelRealName = $modelDefinition['name'];
                } else {
                    $modelRealName = Helper::codifyName($this->mapping) . '_' . Helper::codifyName($modelName);
                }
                $statement = Mutation\DeleteEnum::statement($database, $modelRealName);
                if ($statement->execute() == false) {
                    throw new DatabaseError('db/build : ' . print_r($statement->errorInfo(), true));
                }
            }
        }

        foreach ($definition as $modelName => $modelDefinition) {
            if (isset($modelDefinition['type']) === true && $modelDefinition['type'] === 'enum') {
                $this->buildEnum($modelName, $modelDefinition);
            }
        }
        foreach ($definition as $modelName => $modelDefinition) {
            if (isset($modelDefinition['type']) === false || $modelDefinition['type'] === 'model') {
                $this->buildModel($modelName, $modelDefinition);
            }
        }

        foreach ($definition as $modelName => $modelDefinition) {
            if ((isset($modelDefinition['type']) === false || $modelDefinition['type'] === 'model') && isset($modelDefinition['links']) === true) {
                foreach ($modelDefinition['links'] as $foreignName => $foreignDefinition) {
                    $tableName = null;
                    if (isset($modelDefinition['name']) === true) {
                        $tableName = $modelDefinition['name'];
                    } else {
                        $tableName = Helper::codifyName($this->mapping) . '_' . Helper::codifyName($modelName);
                    }
                    $foreignTableName = null;
                    if (isset($definition[$foreignDefinition['model']]['name']) === true) {
                        $foreignTableName = $definition[$foreignDefinition['model']]['name'];
                    } else {
                        $foreignTableName = Helper::codifyName($this->mapping) . '_' . Helper::codifyName($foreignDefinition['model']);
                    }
                    $statement = Mutation\AddForeignKey::statement($database, $definition, $modelName, $tableName, $foreignName, $foreignTableName);
                    if ($statement->execute() == false) {
                        throw new DatabaseError('db/build : ' . print_r($statement->errorInfo(), true));
                    }
                }
            }
        }
        Migration::ensureTable($mapping);
    }
Build data structures.

@throws DatabaseError           if a SQL query fails
@throws InvalidSchemaDefinition if a model has no field
@throws InvalidSchemaDefinition if an enum-typed field has no values defined