Skip to content

Support for more PostgreSQL schemas? #107

@jkuchar

Description

@jkuchar

Hi! I need support for more schemas — as our app uses them. There is only one place which is affected and that is database reset, where nextras needs to know which schemas to reset.

One can easily fix it for new by overriding PostgreSQL driver. However I think it should be easier. Any ides on this? 🙂

namespace App\Libs;

use Nextras\Migrations\Drivers\PgSqlDriver;
use Nextras\Migrations\Entities\Migration;
use Nextras\Migrations\IDbal;
use Nextras\Migrations\IDriver;

/**
 * Fixes Nextras migrations usage for databases with more then one schema.
 */
final class MigrationsMultiSchemaPgSqlDriver implements IDriver
{

	/** @var PgSqlDriver */
	private $inner;

	/** @var IDbal */
	private $dbal;

	/** @var array|string[] */
	private $schemas;

	/**
	 * @param PgSqlDriver $postgreSQLDriver
	 * @param string[] $schemas list of schema names used by this application
	 */
	public function __construct(PgSqlDriver $postgreSQLDriver, IDbal $dbal, array $schemas)
	{
		$this->inner = $postgreSQLDriver;
		$this->dbal = $dbal;
		$this->schemas = $schemas;
	}


	public function setupConnection(): void
	{
		$this->inner->setupConnection();
	}


	public function emptyDatabase(): void
	{
		foreach($this->schemas as $schema) {
			$escapedSchema = $this->dbal->escapeIdentifier($schema);
			$this->dbal->exec("DROP SCHEMA IF EXISTS $escapedSchema CASCADE");
			$this->dbal->exec("CREATE SCHEMA $escapedSchema");
		}
	}

	/**
	 * @param string $path
	 * @throws \Nextras\Migrations\IOException
	 */
	public function loadFile($path): int
	{
		return $this->inner->loadFile($path);
	}

	public function beginTransaction(): void
	{
		$this->inner->beginTransaction();
	}

	public function commitTransaction(): void
	{
		$this->inner->commitTransaction();
	}

	public function rollbackTransaction(): void
	{
		$this->inner->rollbackTransaction();
	}

	/**
	 * @throws \Nextras\Migrations\LockException
	 */
	public function lock(): void
	{
		$this->inner->lock();
	}

	/**
	 * @throws \Nextras\Migrations\LockException
	 */
	public function unlock(): void
	{
		$this->inner->unlock();
	}



	// Migration table:
	// (proxy all - do not care much)

	public function createTable(): void
	{
		$this->inner->createTable();
	}

	public function dropTable(): void
	{
		$this->inner->dropTable();
	}

	public function insertMigration(Migration $migration): void
	{
		$this->inner->insertMigration($migration);
	}

	public function markMigrationAsReady(Migration $migration): void
	{
		$this->inner->markMigrationAsReady($migration);
	}

	public function getAllMigrations(): array
	{
		return $this->inner->getAllMigrations();
	}

	public function getInitTableSource(): string
	{
		return $this->inner->getInitTableSource();
	}

	public function getInitMigrationsSource(array $files): string
	{
		return $this->inner->getInitMigrationsSource($files);
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions