Yii 2 migrations for test database

10,455

Solution 1

[edit] updated the answer to reflect the current version of Yii2

Just posting the correct answer as I found it myself and I feel a bit stupid as it was under my eyes.

There's another yii commandline within the test folder directory, which means you can easily do:

$ cd tests/codeception/bin
$ chmod a+x yii
$ yii migrate/up

and it will automatically use the database you've specified in tests/codeception/config/config.php.

Enjoy

Solution 2

Just using yii_test migrate for Yii2.

Solution 3

To apply migrations for test database in Yii2 run this command in project's directory:

php tests/bin/yii migrate
Share:
10,455
Mr Peach
Author by

Mr Peach

Updated on July 21, 2022

Comments

  • Mr Peach
    Mr Peach almost 2 years

    I'm failing to see a clear way to do this, although I can see it being reiterated multiple times in the documentation, specifically:

    Use yii migrate tool to upgrade your test database to the latest version;

    found in http://www.yiiframework.com/doc-2.0/guide-test-fixture.html#summary (just to give an example)

    What happens is that no matter how I configure the _console.php and _config.php within my tests/unit folder, if I run the migration tool is picking up the main app db:

    $ cd tests/unit
    $ ../../yii migrate/up
    Yii Migration Tool (based on Yii v2.0.0-dev)
    
    No new migration found. Your system is up-to-date.
    

    And yes, the test database is correctly configured (running tests will throw an error saying there are no tables). I can obviously create the db structure myself, but this is all about getting this feature work as it should.

    I'm currently using the Basic app installed via composer.