2

when I run below command after creating new migration on postgres driver, I am getting errors migration table already exists.

./yii migrate

But the first migration that created "migration" table already completed succesfully.

Error Info:
Array
(
    [0] => 42P07
    [1] => 7
    [2] => ERROR:  relation "migration" already exists
)

It seems Yii2 is trying to run all migration from start everytime since in new migration I am creating some other table. it still complains about migration table missing.

saurssaurav
  • 715
  • 5
  • 16
Shrikant
  • 384
  • 2
  • 10

1 Answers1

1

I found a solution for Yii2 apps with web ui:

In web/index.php just add:

// Check if migration is required
$migrateHint = __DIR__.'/../runtime/do_migration';
if (file_exists($migrateHint)) {
    exec('php '.__DIR__.'/../yii migrate/up --interactive=0',$output,$exitCode);
    if ($exitCode || !@unlink($migrateHint)) {
        echo "migration failed. exitCode=$exitCode. Try again.\n";
        exit(1);
    }
}

The reason for using exec is: the Yii2-App is not yet initialized and cannot be used.

Don't forget to add __DIR__.'/../runtime/migration_ready as file to your git repo (contents do not matter). So every time you checkout a new version the migration is started.

WeSee
  • 3,158
  • 2
  • 30
  • 58