Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": ">=1.4",
"phpunit/phpunit": "^9"
"phpunit/phpunit": "^9",
"orchestra/testbench": "^6.2"
},
"autoload": {
"psr-4": {
"Reliese\\": "src/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
"psr-4": {
"Reliese\\Tests\\": "tests"
}
},
"config": {
"preferred-install": "dist"
Expand Down
13 changes: 12 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
Expand Down
2 changes: 1 addition & 1 deletion src/Coders/Model/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected function models()
*/
public function on($connection = null)
{
$this->schemas = new SchemaManager($this->db->connection($connection));
$this->schemas = new SchemaManager($this->db->connection($connection), $connection);

return $this;
}
Expand Down
28 changes: 24 additions & 4 deletions src/Meta/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Blueprint
*/
protected $schema;

/**
* @var string
*/
protected $schemaname;

/**
* @var string
*/
Expand Down Expand Up @@ -63,11 +68,12 @@ class Blueprint
* @param string $schema
* @param string $table
*/
public function __construct($connection, $schema, $table, $isView = false)
public function __construct($connection, $schema, $table, $isView = false, $schemaname = '')
{
$this->connection = $connection;
$this->schema = $schema;
$this->table = $table;
$this->schema = $schema; // DATABASE
$this->schemaname = $schemaname; // (real) SCHEMA
$this->table = $table; // TABLE
$this->isView = $isView;
}

Expand All @@ -79,6 +85,14 @@ public function schema()
return $this->schema;
}

/**
* @return string
*/
public function schemaname()
{
return $this->schemaname;
}

/**
* @return string
*/
Expand All @@ -92,7 +106,13 @@ public function table()
*/
public function qualifiedTable()
{
return $this->schema().'.'.$this->table();
$schemaname = $this->schemaname();
$table = $this->table();
if ('' !== $schemaname) {
return sprintf('%s.%s', $schemaname, $table);
}

return sprintf('%s.%s', $this->schema(), $table);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Meta/MySql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Schema implements \Reliese\Meta\Schema
* @param string $schema
* @param \Illuminate\Database\MySqlConnection $connection
*/
public function __construct($schema, $connection)
public function __construct($schema, $connection, $connectionName = '')
{
$this->schema = $schema;
$this->connection = $connection;
Expand Down Expand Up @@ -268,7 +268,7 @@ protected function resolveForeignTable($table, Blueprint $blueprint)
*
* @return array
*/
public static function schemas(Connection $connection)
public static function schemas(Connection $connection, $connectionName)
{
$schemas = $connection->select('SELECT schema_name FROM information_schema.schemata');
$schemas = array_column($schemas, 'schema_name');
Expand Down
68 changes: 42 additions & 26 deletions src/Meta/Postgres/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Reliese\Meta\Postgres;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Reliese\Meta\Blueprint;
use Illuminate\Support\Fluent;
Expand Down Expand Up @@ -35,22 +34,28 @@ class Schema implements \Reliese\Meta\Schema
protected $tables = [];

/**
* @var mixed|null
* @var array
*/
protected $schema_database = null;
protected $schemanames = [];

/**
* Mapper constructor.
*
* @param string $schema
* @param \Illuminate\Database\PostgresConnection $connection
*/
public function __construct($schema, $connection)
public function __construct($schema, $connection, $connectionName = 'pgsql')
{
$this->schema_database = Config::get("database.connections.pgsql.schema");
if (!$this->schema_database){
$this->schema_database = 'public';
$this->schemanames = Config::get(sprintf('database.connections.%s.search_path', $connectionName));
if (!$this->schemanames) {
$this->schemanames = [
Config::get(sprintf('database.connections.%s.schema', $connectionName)),
];
}
if (!$this->schemanames) {
$this->schemanames = ['public'];
}

$this->schema = $schema;
$this->connection = $connection;

Expand All @@ -74,9 +79,10 @@ 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);
$tableDatas = $this->fetchTables();
foreach ($tableDatas as $tableData) {
$table = $tableData['tablename'];
$blueprint = new Blueprint($this->connection->getName(), $this->schema, $table, false, $tableData['schemaname']);
$this->fillColumns($blueprint);
$this->fillConstraints($blueprint);
$this->tables[$table] = $blueprint;
Expand All @@ -92,11 +98,18 @@ protected function load()
protected function fetchTables()
{
$rows = $this->arraify($this->connection->select(
"SELECT * FROM pg_tables where schemaname='$this->schema_database'"
'SELECT * FROM pg_tables ' .
"WHERE schemaname IN ('" . implode("', '", $this->schemanames) . "')"
));
$names = array_column($rows, 'tablename');
$names = [];
foreach ($rows as $row) {
$names[] = [
'tablename' => $row['tablename'],
'schemaname' => $row['schemaname'],
];
}

return Arr::flatten($names);
return $names;
}

/**
Expand All @@ -105,9 +118,9 @@ protected function fetchTables()
protected function fillColumns(Blueprint $blueprint)
{
$rows = $this->arraify($this->connection->select(
'SELECT * FROM information_schema.columns '.
"WHERE table_schema='$this->schema_database'".
'AND table_name='.$this->wrap($blueprint->table())
'SELECT * FROM information_schema.columns ' .
"WHERE table_schema IN ('" . implode("', '", $this->schemanames) . "')" .
'AND table_name = '.$this->wrap($blueprint->table())
));
foreach ($rows as $column) {
$blueprint->withColumn(
Expand Down Expand Up @@ -281,16 +294,11 @@ protected function wrap($table)
*
* @return array
*/
public static function schemas(Connection $connection)
public static function schemas(Connection $connection, string $connectionName = 'pgsql')
{
$schemas = $connection->select('SELECT datname FROM pg_database');
$schemas = array_column($schemas, 'datname');

return array_diff($schemas, [
'postgres',
'template0',
'template1',
]);
return [
Config::get(sprintf('database.connections.%s.database', $connectionName))
];
}

/**
Expand All @@ -301,6 +309,14 @@ public function schema()
return $this->schema;
}

/**
* @return array
*/
public function schemanames()
{
return $this->schemanames;
}

/**
* @param string $table
*
Expand Down Expand Up @@ -334,7 +350,7 @@ public function table($table)
}

/**
* @return \Illuminate\Database\MySqlConnection
* @return \Illuminate\Database\PostgresConnection
*/
public function connection()
{
Expand Down
17 changes: 14 additions & 3 deletions src/Meta/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class SchemaManager implements IteratorAggregate
*/
private $connection;

/**
* @var string
*/
private $connectionName;

/**
* @var \Reliese\Meta\Schema[]
*/
Expand All @@ -48,9 +53,10 @@ class SchemaManager implements IteratorAggregate
*
* @param \Illuminate\Database\ConnectionInterface $connection
*/
public function __construct(ConnectionInterface $connection)
public function __construct(ConnectionInterface $connection, string $connectionName = '')
{
$this->connection = $connection;
$this->connectionName = $connectionName;

$this->boot();
}
Expand All @@ -64,7 +70,12 @@ public function boot()
throw new RuntimeException("There is no Schema Mapper registered for [{$this->type()}] connection.");
}

$schemas = forward_static_call([$this->getMapper(), 'schemas'], $this->connection);
/**
* @see \Reliese\Meta\Postgres\Schema::schemas()
* @see \Reliese\Meta\MySql\Schema::schemas()
* @see \Reliese\Meta\Sqlite\Schema::schemas()
*/
$schemas = forward_static_call([$this->getMapper(), 'schemas'], $this->connection, $this->connectionName);

foreach ($schemas as $schema) {
$this->make($schema);
Expand Down Expand Up @@ -94,7 +105,7 @@ protected function makeMapper($schema)
{
$mapper = $this->getMapper();

return new $mapper($schema, $this->connection);
return new $mapper($schema, $this->connection, $this->connectionName);
}

/**
Expand Down
24 changes: 16 additions & 8 deletions tests/Coders/Console/Model/ModelTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?php

namespace Reliese\Tests\Coders\Console\Model;

use Illuminate\Database\DatabaseManager;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Fluent;
use Mockery;
use Reliese\Coders\Model\Config;
use Reliese\Coders\Model\Factory;
use Reliese\Coders\Model\Model;
use Reliese\Coders\Model\Relations\BelongsTo;
use Reliese\Meta\Blueprint;
use Reliese\Support\Classify;
use Reliese\Tests\TestCase;

class ModelTest extends TestCase
{
Expand Down Expand Up @@ -56,10 +64,10 @@ public function testPhpTypeHint($castType, $nullable, $expect)
$model = new Model(
new Blueprint('test', 'test', 'test'),
new Factory(
\Mockery::mock(\Illuminate\Database\DatabaseManager::class),
\Mockery::mock(Illuminate\Filesystem\Filesystem::class),
\Mockery::mock(\Reliese\Support\Classify::class),
new \Reliese\Coders\Model\Config()
Mockery::mock(DatabaseManager::class),
Mockery::mock(Filesystem::class),
Mockery::mock(Classify::class),
new Config()
)
);

Expand Down Expand Up @@ -93,10 +101,10 @@ public function testBelongsToNullableRelationships($nullable, $expectedTypehint)
$model = new Model(
$baseBlueprint,
new Factory(
\Mockery::mock(\Illuminate\Database\DatabaseManager::class),
\Mockery::mock(Illuminate\Filesystem\Filesystem::class),
\Mockery::mock(\Reliese\Support\Classify::class),
new \Reliese\Coders\Model\Config()
Mockery::mock(DatabaseManager::class),
Mockery::mock(Filesystem::class),
Mockery::mock(Classify::class),
new Config()
)
);

Expand Down
5 changes: 4 additions & 1 deletion tests/Coders/Model/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

use PHPUnit\Framework\TestCase;
namespace Reliese\Tests\Coders\Model;

use Mockery;
use Reliese\Coders\Model\Config;
use Reliese\Meta\Blueprint;
use Reliese\Tests\TestCase;

class ConfigTest extends TestCase
{
Expand Down
5 changes: 4 additions & 1 deletion tests/Coders/Model/Relations/BelongsToTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

namespace Reliese\Tests\Coders\Model\Relations;

use Illuminate\Support\Fluent;
use PHPUnit\Framework\TestCase;
use Mockery;
use Reliese\Coders\Model\Model;
use Reliese\Coders\Model\Relations\BelongsTo;
use Reliese\Tests\TestCase;

class BelongsToTest extends TestCase
{
Expand Down
Loading