Migrate multiple databases with Symfony migrations, without specifying db name

929 views Asked by At

I have four different databases in my Symfony project. Locally those databases have their individual names so in the migrations one can tell them apart. I have prefixed each table with db_name.table in each migration so that the correct database is migrated.

Now I want to set this up on a remote where the database names are auto generated (using flynn.io), but the database names are fixed in all the migrations... Is there a way to read the db names from environment variables or some similar solution?

1

There are 1 answers

0
Muhammad Taqi On

You can use DoctrineMigrationsBundle

public function up(Schema $schema)
{
    // ... empty function/ no migrations commands
}


public function postUp(Schema $schema)
{

    // get datebase name here, and then pass this database names to another function
   $converter = $this->container->get('db_names_from_paratmers.yml file');
   $this->initDatabase('db1');
}

public function initDatabase($dbname)
{
  $this->addSql('CREATE TABLE '.$dbname.'office (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
}

See, If this somehow satisfied your need, you just need to find DB Names and embeded in paramters.yml file.