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

Support for more PostgreSQL schemas? #107

Open
jkuchar opened this issue Apr 2, 2020 · 0 comments
Open

Support for more PostgreSQL schemas? #107

jkuchar opened this issue Apr 2, 2020 · 0 comments
Milestone

Comments

@jkuchar
Copy link

jkuchar commented Apr 2, 2020

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);
	}
}
@JanTvrdik JanTvrdik added this to the v4 milestone Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants