Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set persistence for introspected model if passed to Migrator #1221

Merged
merged 1 commit into from
May 23, 2024
Merged
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
25 changes: 20 additions & 5 deletions src/Schema/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Migrator
public const REF_TYPE_LINK = 1;
public const REF_TYPE_PRIMARY = 2;

private Connection $_connection;
/** @var Connection|Persistence\Sql */
private object $_connection;

public Table $table;

Expand All @@ -48,12 +49,10 @@ class Migrator
*/
public function __construct(object $source)
{
if ($source instanceof Connection) {
if ($source instanceof Connection || $source instanceof Persistence\Sql) {
$this->_connection = $source;
} elseif ($source instanceof Persistence\Sql) {
$this->_connection = $source->getConnection();
} elseif ($source instanceof Model && $source->getPersistence() instanceof Persistence\Sql) { // @phpstan-ignore instanceof.alwaysTrue
$this->_connection = $source->getPersistence()->getConnection();
$this->_connection = $source->getPersistence();
} else {
throw (new Exception('Source must be SQL connection, persistence or initialized model'))
->addMoreInfo('source', $source);
Expand All @@ -65,6 +64,18 @@ public function __construct(object $source)
}

public function getConnection(): Connection
{
return $this->_connection instanceof Persistence\Sql
? $this->_connection->getConnection()
: $this->_connection;
}

protected function hasPersistence(): bool
{
return $this->_connection instanceof Persistence\Sql;
}

protected function getPersistence(): Persistence\Sql
{
return $this->_connection;
}
Expand Down Expand Up @@ -425,6 +436,10 @@ public function introspectTableToModel(string $tableName): Model
]);
}

if ($this->hasPersistence()) {
$model->setPersistence($this->getPersistence());
}

return $model;
}

Expand Down
1 change: 0 additions & 1 deletion src/Schema/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ public function getDb(?array $tableNames = null, bool $noId = false): array
$resAll = [];
foreach ($tableNames as $table) {
$model = $this->createMigrator()->introspectTableToModel($table);
$model->setPersistence($this->db);

if (!$noId) {
$model->setOrder($model->idField);
Expand Down
Loading