From da0bd38023820ad154abead6553715f7f59bbd30 Mon Sep 17 00:00:00 2001 From: Randy Dryburgh Date: Sat, 25 Aug 2018 12:40:48 -0500 Subject: [PATCH 1/9] New postgres column/schema implementations --- src/Meta/Postgres/Column.php | 198 +++++++++++++++++++ src/Meta/Postgres/Schema.php | 356 +++++++++++++++++++++++++++++++++++ 2 files changed, 554 insertions(+) create mode 100644 src/Meta/Postgres/Column.php create mode 100644 src/Meta/Postgres/Schema.php diff --git a/src/Meta/Postgres/Column.php b/src/Meta/Postgres/Column.php new file mode 100644 index 00000000..0b7842b0 --- /dev/null +++ b/src/Meta/Postgres/Column.php @@ -0,0 +1,198 @@ + ['varchar', 'text', 'string', 'char', 'enum', 'tinytext', 'mediumtext', 'longtext', 'json'], + 'date' => ['datetime', 'year', 'date', 'time', 'timestamp'], + 'int' => ['int', 'integer', 'tinyint', 'smallint', 'mediumint', 'bigint', 'bigserial', 'serial', 'smallserial', 'tinyserial', 'serial4', 'serial8'], + 'float' => ['float', 'decimal', 'numeric', 'dec', 'fixed', 'double', 'real', 'double precision'], + 'boolean' => ['boolean', 'bool', 'bit'], + 'binary' => ['blob', 'longblob', 'jsonb'], + ]; + + /** + * PostgresColumn constructor. + * + * @param array $metadata + */ + public function __construct($metadata = []) + { + $this->metadata = $metadata; + } + + /** + * @return \Illuminate\Support\Fluent + */ + public function normalize() + { + $attributes = new Fluent(); + + foreach ($this->metas as $meta) { + $this->{'parse'.ucfirst($meta)}($attributes); + } + + return $attributes; + } + + + /** + * @param \Illuminate\Support\Fluent $attributes + */ + protected function parseType(Fluent $attributes) + { + $dataType = $this->get('data_type', 'string'); + $attributes['type'] = $dataType; + + foreach (static::$mappings as $phpType => $database) { + if (in_array($dataType, $database)) { + $attributes['type'] = $phpType; + } + } + + $this->parsePrecision($dataType, $attributes); + } + + /** + * @param string $databaseType + * @param \Illuminate\Support\Fluent $attributes + * @todo handle non numeric precisions + */ + protected function parsePrecision($databaseType, Fluent $attributes) + { + $precision = $this->get('numeric_precision', 'string'); + $precision = explode(',', str_replace("'", '', $precision)); + + // Check whether it's an enum + if ($databaseType == 'enum') { + //$attributes['enum'] = $precision; //todo + + return; + } + + $size = (int) $precision; + + // Check whether it's a boolean + if ($size == 1 && in_array($databaseType, $this->mappings['boolean'])) { + // Make sure this column type is a boolean + $attributes['type'] = 'bool'; + + if ($databaseType == 'bit') { + $attributes['mappings'] = ["\x00" => false, "\x01" => true]; + } + + return; + } + + $attributes['size'] = $size; + + if ($scale = next($precision)) { + $attributes['scale'] = (int) $scale; + } + } + + /** + * @param \Illuminate\Support\Fluent $attributes + */ + protected function parseName(Fluent $attributes) + { + $attributes['name'] = $this->get('column_name'); + } + + /** + * @param \Illuminate\Support\Fluent $attributes + * @todo + */ + protected function parseAutoincrement(Fluent $attributes) + { + $attributes['autoincrement'] = preg_match('/serial/i', + $this->get('data_type', '')) || $this->defaultIsNextVal($attributes); + } + + /** + * @param \Illuminate\Support\Fluent $attributes + */ + protected function parseNullable(Fluent $attributes) + { + $attributes['nullable'] = $this->same('is_nullable', 'YES'); + } + + /** + * @param \Illuminate\Support\Fluent $attributes + */ + protected function parseDefault(Fluent $attributes) + { + $value = null; + if ($this->defaultIsNextVal($attributes)) { + $attributes['autoincrement'] = true; + } else { + $value = $this->get('column_default', $this->get('generation_expression', null)); + } + $attributes['default'] = $value; + } + + /** + * @param \Illuminate\Support\Fluent $attributes + * @todo + */ + protected function parseComment(Fluent $attributes) + { + $attributes['comment'] = $this->get('Comment'); + } + + /** + * @param string $key + * @param mixed $default + * + * @return mixed + */ + protected function get($key, $default = null) + { + return Arr::get($this->metadata, $key, $default); + } + + /** + * @param string $key + * @param string $value + * + * @return bool + */ + protected function same($key, $value) + { + return strcasecmp($this->get($key, ''), $value) === 0; + } + + /** + * @param \Illuminate\Support\Fluent $attributes + * + * @return bool + */ + private function defaultIsNextVal(Fluent $attributes) { + $value = $this->get('column_default', $this->get('generation_expression', null)); + return preg_match('/nextval\(/i', $value); + } + +} diff --git a/src/Meta/Postgres/Schema.php b/src/Meta/Postgres/Schema.php new file mode 100644 index 00000000..6904c94d --- /dev/null +++ b/src/Meta/Postgres/Schema.php @@ -0,0 +1,356 @@ +schema = $schema; + $this->connection = $connection; + + $this->load(); + } + + /** + * @return \Doctrine\DBAL\Schema\AbstractSchemaManager + * @todo: Use Doctrine instead of raw database queries + */ + public function manager() + { + return $this->connection->getDoctrineSchemaManager(); + } + + /** + * Loads schema's tables' information from the database. + */ + protected function load() + { + // Note that "schema" refers to the database name, + // not a pgsql schema. + $this->connection->raw('\c '.$this->wrap($this->schema)); + $tables = $this->fetchTables($this->schema); + foreach ($tables as $table) { + $blueprint = new Blueprint($this->connection->getName(), $this->schema, $table); + $this->fillColumns($blueprint); + $this->fillConstraints($blueprint); + $this->tables[$table] = $blueprint; + } + $this->loaded = true; + } + + /** + * @param string $schema + * + * @return array + */ + protected function fetchTables() + { + $rows = $this->arraify($this->connection->select( + 'SELECT * FROM pg_tables where schemaname=\'public\'' + )); + $names = array_column($rows, 'tablename'); + + return Arr::flatten($names); + } + + /** + * @param \Reliese\Meta\Blueprint $blueprint + */ + protected function fillColumns(Blueprint $blueprint) + { + $rows = $this->arraify($this->connection->select( + 'SELECT * FROM information_schema.columns '. + 'WHERE table_schema=\'public\''. + 'AND table_name='.$this->wrap($blueprint->table()) + )); + foreach ($rows as $column) { + $blueprint->withColumn( + $this->parseColumn($column) + ); + } + } + + /** + * @param array $metadata + * + * @return \Illuminate\Support\Fluent + */ + protected function parseColumn($metadata) + { + return (new Column($metadata))->normalize(); + } + + /** + * @param \Reliese\Meta\Blueprint $blueprint + */ + protected function fillConstraints(Blueprint $blueprint) + { + $sql = ' + SELECT child.attname, p.contype, p.conname, + parent_class.relname as parent_table, + parent.attname as parent_attname + FROM pg_attribute child + JOIN pg_class child_class ON child_class.oid = child.attrelid + LEFT JOIN pg_constraint p ON p.conrelid = child_class.oid + AND child.attnum = ANY (p.conkey) + LEFT JOIN pg_attribute parent on parent.attnum = ANY (p.confkey) + AND parent.attrelid = p.confrelid + LEFT JOIN pg_class parent_class on parent_class.oid = p.confrelid + WHERE child_class.relkind = \'r\'::char + AND child_class.relname = \''.$blueprint->table().'\' + AND child.attnum > 0 + AND contype IS NOT NULL + ORDER BY child.attnum + ;'; + $relations = $this->arraify($this->connection->select($sql)); + + $this->fillPrimaryKey($relations, $blueprint); + $this->fillRelations($relations, $blueprint); + + $sql = 'SELECT * FROM pg_indexes WHERE tablename = \''.$blueprint->table().'\';'; + $indexes = $this->arraify($this->connection->select($sql)); + $this->fillIndexes($indexes, $blueprint); + } + + /** + * Quick little hack since it is no longer possible to set PDO's fetch mode + * to PDO::FETCH_ASSOC. + * + * @param $data + * @return mixed + */ + protected function arraify($data) + { + return json_decode(json_encode($data), true); + } + + /** + * @param array $relations + * @param \Reliese\Meta\Blueprint $blueprint + * @todo: Support named primary keys + */ + protected function fillPrimaryKey($relations, Blueprint $blueprint) + { + $pk = []; + foreach ($relations as $row) { + if ($row['contype'] === 'p') { + $pk[] = $row['attname']; + } + } + + $key = [ + 'name' => 'primary', + 'index' => '', + 'columns' => $pk, + ]; + + $blueprint->withPrimaryKey(new Fluent($key)); + } + + /** + * @param array $indexes + * @param \Reliese\Meta\Blueprint $blueprintx + */ + protected function fillIndexes($indexes, Blueprint $blueprint) + { + foreach ($indexes as $row) { + $pattern = '/\s*(UNIQUE)?\s*(KEY|INDEX)\s+(\w+)\s+\(([^\)]+)\)/mi'; + if (preg_match($pattern, $row['indexdef'], $setup) == false) { + continue; + } + + $index = [ + 'name' => strcasecmp($setup[1], 'unique') === 0 ? 'unique' : 'index', + 'columns' => $this->columnize($setup[4]), + 'index' => $setup[3], + ]; + $blueprint->withIndex(new Fluent($index)); + } + } + + /** + * @param array $relations + * @param \Reliese\Meta\Blueprint $blueprint + * @todo: Support named foreign keys + */ + protected function fillRelations($relations, Blueprint $blueprint) + { + $fk = []; + foreach ($relations as $row) { + $relName = $row['conname']; + if ($row['contype'] === 'f') { + if (!array_key_exists($relName, $fk)) { + $fk[$relName] = [ + 'columns' => [], + 'ref' => [] + ]; + } + $fk[$relName]['columns'][] = $row['attname']; + $fk[$relName]['ref'][] = $row['parent_attname']; + $fk[$relName]['table'] = $row['parent_table']; + } + + } + + foreach ($fk as $row) { + $relation = [ + 'name' => 'foreign', + 'index' => '', + 'columns' => $row['columns'], + 'references' => $row['ref'], + 'on' => array($this->schema, $row['table']) + ]; + + + $blueprint->withRelation(new Fluent($relation)); + } + } + + /** + * @param string $columns + * + * @return array + */ + protected function columnize($columns) + { + return array_map('trim', explode(',', $columns)); + } + + /** + * Wrap within backticks. + * + * @param string $table + * + * @return string + */ + protected function wrap($table) + { + $pieces = explode('.', str_replace('\'', '', $table)); + + return implode('.', array_map(function ($piece) { + return "'$piece'"; + }, $pieces)); + } + + /** + * @param \Illuminate\Database\Connection $connection + * + * @return array + */ + public static function schemas(Connection $connection) + { + $schemas = $connection->getDoctrineSchemaManager()->listDatabases(); + + return array_diff($schemas, [ + 'information_schema', + 'sys', + 'mysql', + 'performance_schema', + ]); + } + + /** + * @return string + */ + public function schema() + { + return $this->schema; + } + + /** + * @param string $table + * + * @return bool + */ + public function has($table) + { + return array_key_exists($table, $this->tables); + } + + /** + * @return \Reliese\Meta\Blueprint[] + */ + public function tables() + { + return $this->tables; + } + + /** + * @param string $table + * + * @return \Reliese\Meta\Blueprint + */ + public function table($table) + { + if (! $this->has($table)) { + throw new \InvalidArgumentException("Table [$table] does not belong to schema [{$this->schema}]"); + } + + return $this->tables[$table]; + } + + /** + * @return \Illuminate\Database\MySqlConnection + */ + public function connection() + { + return $this->connection; + } + + /** + * @param \Reliese\Meta\Blueprint $table + * + * @return array + */ + public function referencing(Blueprint $table) + { + $references = []; + + foreach ($this->tables as $blueprint) { + foreach ($blueprint->references($table) as $reference) { + $references[] = [ + 'blueprint' => $blueprint, + 'reference' => $reference, + ]; + } + } + + return $references; + } +} From f36a23a668b8d8d9b1e17b42da032443e1c9353c Mon Sep 17 00:00:00 2001 From: Randy Dryburgh Date: Sat, 25 Aug 2018 12:41:26 -0500 Subject: [PATCH 2/9] Add postgres references; Remove call to boot which was causing recursive generation of ALL schemas. --- src/Meta/SchemaManager.php | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/Meta/SchemaManager.php b/src/Meta/SchemaManager.php index 8852936a..23cfff65 100644 --- a/src/Meta/SchemaManager.php +++ b/src/Meta/SchemaManager.php @@ -11,10 +11,10 @@ use RuntimeException; use IteratorAggregate; use Illuminate\Database\MySqlConnection; -use Illuminate\Database\SQLiteConnection; +use Illuminate\Database\PostgresConnection; use Illuminate\Database\ConnectionInterface; use Reliese\Meta\MySql\Schema as MySqlSchema; -use Reliese\Meta\Sqlite\Schema as SqliteSchema; +use Reliese\Meta\Postgres\Schema as PostgresSchema; class SchemaManager implements IteratorAggregate { @@ -23,7 +23,7 @@ class SchemaManager implements IteratorAggregate */ protected static $lookup = [ MySqlConnection::class => MySqlSchema::class, - SQLiteConnection::class => SqliteSchema::class, + PostgresConnection::class => PostgresSchema::class, ]; /** @@ -44,23 +44,10 @@ class SchemaManager implements IteratorAggregate public function __construct(ConnectionInterface $connection) { $this->connection = $connection; - $this->boot(); - } - /** - * Load all schemas from this connection. - */ - public function boot() - { if (! $this->hasMapping()) { throw new RuntimeException("There is no Schema Mapper registered for [{$this->type()}] connection."); } - - $schemas = forward_static_call([$this->getMapper(), 'schemas'], $this->connection); - - foreach ($schemas as $schema) { - $this->make($schema); - } } /** From eb111c25a9ed997508d8cb248e567b2eb9148d8e Mon Sep 17 00:00:00 2001 From: Randy Dryburgh Date: Sat, 25 Aug 2018 13:18:35 -0500 Subject: [PATCH 3/9] Added SQLite back --- src/Meta/SchemaManager.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Meta/SchemaManager.php b/src/Meta/SchemaManager.php index 23cfff65..72a0708e 100644 --- a/src/Meta/SchemaManager.php +++ b/src/Meta/SchemaManager.php @@ -11,10 +11,13 @@ use RuntimeException; use IteratorAggregate; use Illuminate\Database\MySqlConnection; +use Illuminate\Database\SQLiteConnection; use Illuminate\Database\PostgresConnection; use Illuminate\Database\ConnectionInterface; use Reliese\Meta\MySql\Schema as MySqlSchema; use Reliese\Meta\Postgres\Schema as PostgresSchema; +use Reliese\Meta\Sqlite\Schema as SqliteSchema; + class SchemaManager implements IteratorAggregate { @@ -23,6 +26,7 @@ class SchemaManager implements IteratorAggregate */ protected static $lookup = [ MySqlConnection::class => MySqlSchema::class, + SQLiteConnection::class => SqliteSchema::class, PostgresConnection::class => PostgresSchema::class, ]; From 05b54b89a0f9d3a487a24aae61245ab31d1076b2 Mon Sep 17 00:00:00 2001 From: Randy Dryburgh Date: Sat, 25 Aug 2018 13:27:04 -0500 Subject: [PATCH 4/9] Fixed reference to self:: --- src/Meta/Postgres/Column.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Meta/Postgres/Column.php b/src/Meta/Postgres/Column.php index 0b7842b0..43ed8cf8 100644 --- a/src/Meta/Postgres/Column.php +++ b/src/Meta/Postgres/Column.php @@ -96,7 +96,7 @@ protected function parsePrecision($databaseType, Fluent $attributes) $size = (int) $precision; // Check whether it's a boolean - if ($size == 1 && in_array($databaseType, $this->mappings['boolean'])) { + if ($size == 1 && in_array($databaseType, self::$mappings['boolean'])) { // Make sure this column type is a boolean $attributes['type'] = 'bool'; From 02c8ccc05005759ff9deb8ec9d9293519746ee02 Mon Sep 17 00:00:00 2001 From: Randy Dryburgh Date: Sat, 25 Aug 2018 13:34:57 -0500 Subject: [PATCH 5/9] Updated file comments --- src/Meta/Postgres/Column.php | 4 ++++ src/Meta/Postgres/Schema.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Meta/Postgres/Column.php b/src/Meta/Postgres/Column.php index 43ed8cf8..32978789 100644 --- a/src/Meta/Postgres/Column.php +++ b/src/Meta/Postgres/Column.php @@ -7,6 +7,10 @@ use Illuminate\Support\Fluent; use Symfony\Component\Debug\Exception\FatalThrowableError; +/** + * Created by rwdim from cristians MySql original. + * Date: 25/08/18 04:20 PM. + */ class Column implements \Reliese\Meta\Column { /** diff --git a/src/Meta/Postgres/Schema.php b/src/Meta/Postgres/Schema.php index 6904c94d..0c6ee79a 100644 --- a/src/Meta/Postgres/Schema.php +++ b/src/Meta/Postgres/Schema.php @@ -8,8 +8,8 @@ use Illuminate\Database\Connection; /** - * Created by Cristian. - * Date: 18/09/16 06:50 PM. + * Created by rwdim from cristians MySql original. + * Date: 25/08/18 04:13 PM. */ class Schema implements \Reliese\Meta\Schema { From 418a0b8e15a1e3b374de34511d1d423dfcb37422 Mon Sep 17 00:00:00 2001 From: Randy Dryburgh Date: Sat, 25 Aug 2018 13:51:50 -0500 Subject: [PATCH 6/9] Fixed... grumbl.. spacing.. --- src/Meta/Postgres/Column.php | 24 ++++++++++++------------ src/Meta/Postgres/Schema.php | 14 +++++++------- src/Meta/SchemaManager.php | 5 ++--- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/Meta/Postgres/Column.php b/src/Meta/Postgres/Column.php index 32978789..b3541593 100644 --- a/src/Meta/Postgres/Column.php +++ b/src/Meta/Postgres/Column.php @@ -3,9 +3,7 @@ namespace Reliese\Meta\Postgres; use Illuminate\Support\Arr; -use Illuminate\Support\Str; use Illuminate\Support\Fluent; -use Symfony\Component\Debug\Exception\FatalThrowableError; /** * Created by rwdim from cristians MySql original. @@ -56,7 +54,7 @@ public function normalize() $attributes = new Fluent(); foreach ($this->metas as $meta) { - $this->{'parse'.ucfirst($meta)}($attributes); + $this->{'parse'.ucfirst($meta)}($attributes); } return $attributes; @@ -97,10 +95,10 @@ protected function parsePrecision($databaseType, Fluent $attributes) return; } - $size = (int) $precision; + $size = (int) $precision; // Check whether it's a boolean - if ($size == 1 && in_array($databaseType, self::$mappings['boolean'])) { + if ($size == 1 && in_array($databaseType, self::$mappings['boolean'])) { // Make sure this column type is a boolean $attributes['type'] = 'bool'; @@ -132,8 +130,8 @@ protected function parseName(Fluent $attributes) */ protected function parseAutoincrement(Fluent $attributes) { - $attributes['autoincrement'] = preg_match('/serial/i', - $this->get('data_type', '')) || $this->defaultIsNextVal($attributes); + $attributes['autoincrement'] = preg_match('/serial/i', + $this->get('data_type', '')) || $this->defaultIsNextVal($attributes); } /** @@ -151,9 +149,9 @@ protected function parseDefault(Fluent $attributes) { $value = null; if ($this->defaultIsNextVal($attributes)) { - $attributes['autoincrement'] = true; + $attributes['autoincrement'] = true; } else { - $value = $this->get('column_default', $this->get('generation_expression', null)); + $value = $this->get('column_default', $this->get('generation_expression', null)); } $attributes['default'] = $value; } @@ -194,9 +192,11 @@ protected function same($key, $value) * * @return bool */ - private function defaultIsNextVal(Fluent $attributes) { - $value = $this->get('column_default', $this->get('generation_expression', null)); - return preg_match('/nextval\(/i', $value); + private function defaultIsNextVal(Fluent $attributes) + { + $value = $this->get('column_default', $this->get('generation_expression', null)); + + return preg_match('/nextval\(/i', $value); } } diff --git a/src/Meta/Postgres/Schema.php b/src/Meta/Postgres/Schema.php index 0c6ee79a..f673a3c3 100644 --- a/src/Meta/Postgres/Schema.php +++ b/src/Meta/Postgres/Schema.php @@ -66,10 +66,10 @@ protected function load() $this->connection->raw('\c '.$this->wrap($this->schema)); $tables = $this->fetchTables($this->schema); foreach ($tables as $table) { - $blueprint = new Blueprint($this->connection->getName(), $this->schema, $table); - $this->fillColumns($blueprint); - $this->fillConstraints($blueprint); - $this->tables[$table] = $blueprint; + $blueprint = new Blueprint($this->connection->getName(), $this->schema, $table); + $this->fillColumns($blueprint); + $this->fillConstraints($blueprint); + $this->tables[$table] = $blueprint; } $this->loaded = true; } @@ -193,7 +193,7 @@ protected function fillIndexes($indexes, Blueprint $blueprint) $pattern = '/\s*(UNIQUE)?\s*(KEY|INDEX)\s+(\w+)\s+\(([^\)]+)\)/mi'; if (preg_match($pattern, $row['indexdef'], $setup) == false) { continue; - } + } $index = [ 'name' => strcasecmp($setup[1], 'unique') === 0 ? 'unique' : 'index', @@ -215,7 +215,7 @@ protected function fillRelations($relations, Blueprint $blueprint) foreach ($relations as $row) { $relName = $row['conname']; if ($row['contype'] === 'f') { - if (!array_key_exists($relName, $fk)) { + if (! array_key_exists($relName, $fk)) { $fk[$relName] = [ 'columns' => [], 'ref' => [] @@ -234,7 +234,7 @@ protected function fillRelations($relations, Blueprint $blueprint) 'index' => '', 'columns' => $row['columns'], 'references' => $row['ref'], - 'on' => array($this->schema, $row['table']) + 'on' => [$this->schema, $row['table']], ]; diff --git a/src/Meta/SchemaManager.php b/src/Meta/SchemaManager.php index 72a0708e..42f34aca 100644 --- a/src/Meta/SchemaManager.php +++ b/src/Meta/SchemaManager.php @@ -15,9 +15,8 @@ use Illuminate\Database\PostgresConnection; use Illuminate\Database\ConnectionInterface; use Reliese\Meta\MySql\Schema as MySqlSchema; -use Reliese\Meta\Postgres\Schema as PostgresSchema; use Reliese\Meta\Sqlite\Schema as SqliteSchema; - +use Reliese\Meta\Postgres\Schema as PostgresSchema; class SchemaManager implements IteratorAggregate { @@ -26,7 +25,7 @@ class SchemaManager implements IteratorAggregate */ protected static $lookup = [ MySqlConnection::class => MySqlSchema::class, - SQLiteConnection::class => SqliteSchema::class, + SQLiteConnection::class => SqliteSchema::class, PostgresConnection::class => PostgresSchema::class, ]; From 45136d8e8227542661fbf51f3bd94c84926583c0 Mon Sep 17 00:00:00 2001 From: Randy Dryburgh Date: Sat, 25 Aug 2018 13:56:25 -0500 Subject: [PATCH 7/9] Added comma that isn't needed. --- src/Meta/Postgres/Column.php | 2 -- src/Meta/Postgres/Schema.php | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Meta/Postgres/Column.php b/src/Meta/Postgres/Column.php index b3541593..c80b0bef 100644 --- a/src/Meta/Postgres/Column.php +++ b/src/Meta/Postgres/Column.php @@ -60,7 +60,6 @@ public function normalize() return $attributes; } - /** * @param \Illuminate\Support\Fluent $attributes */ @@ -198,5 +197,4 @@ private function defaultIsNextVal(Fluent $attributes) return preg_match('/nextval\(/i', $value); } - } diff --git a/src/Meta/Postgres/Schema.php b/src/Meta/Postgres/Schema.php index f673a3c3..269d8039 100644 --- a/src/Meta/Postgres/Schema.php +++ b/src/Meta/Postgres/Schema.php @@ -218,14 +218,13 @@ protected function fillRelations($relations, Blueprint $blueprint) if (! array_key_exists($relName, $fk)) { $fk[$relName] = [ 'columns' => [], - 'ref' => [] + 'ref' => [], ]; } $fk[$relName]['columns'][] = $row['attname']; $fk[$relName]['ref'][] = $row['parent_attname']; $fk[$relName]['table'] = $row['parent_table']; } - } foreach ($fk as $row) { @@ -237,7 +236,6 @@ protected function fillRelations($relations, Blueprint $blueprint) 'on' => [$this->schema, $row['table']], ]; - $blueprint->withRelation(new Fluent($relation)); } } From eab9a3fa5bf18fbcdf9b095164b8d4a71b655ea0 Mon Sep 17 00:00:00 2001 From: rwdim Date: Sun, 2 Sep 2018 19:38:40 -0500 Subject: [PATCH 8/9] Replaced removed lines... testing showed I had a configuration issue. Original code seems to work fine. --- src/Meta/SchemaManager.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Meta/SchemaManager.php b/src/Meta/SchemaManager.php index 42f34aca..3bfe9dfe 100644 --- a/src/Meta/SchemaManager.php +++ b/src/Meta/SchemaManager.php @@ -48,9 +48,23 @@ public function __construct(ConnectionInterface $connection) { $this->connection = $connection; + $this->boot(); + } + + /** + * Load all schemas from this connection. + */ + public function boot() + { if (! $this->hasMapping()) { throw new RuntimeException("There is no Schema Mapper registered for [{$this->type()}] connection."); } + + $schemas = forward_static_call([$this->getMapper(), 'schemas'], $this->connection); + + foreach ($schemas as $schema) { + $this->make($schema); + } } /** From 35fae994d98e2ff7ae3b8c56439abff4e4574d33 Mon Sep 17 00:00:00 2001 From: Randy Dryburgh Date: Thu, 6 Sep 2018 12:31:44 -0500 Subject: [PATCH 9/9] Changed the default database names to those most often found in PostgreSQL --- src/Meta/Postgres/Schema.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Meta/Postgres/Schema.php b/src/Meta/Postgres/Schema.php index 269d8039..abe2c455 100644 --- a/src/Meta/Postgres/Schema.php +++ b/src/Meta/Postgres/Schema.php @@ -276,10 +276,9 @@ public static function schemas(Connection $connection) $schemas = $connection->getDoctrineSchemaManager()->listDatabases(); return array_diff($schemas, [ - 'information_schema', - 'sys', - 'mysql', - 'performance_schema', + 'postgres', + 'template0', + 'template1', ]); }