简体   繁体   中英

Laravel specify a migration folder when I run the database migrations via phphunit utilizing the laravel's `RefreshDatabase` trait

I am migrating my application from Codeigniter into laravel, also we are in the process of making itegration and unit tests as well.

The database is consisted of 2 databases:

  • old That is the one used from the original codeigniter.
  • new That is used for other features not related in the codeingiter project.

Therefore I want to make a migration script used for the old database, but in order to avoid breakdowns I want to specify a specific folder for the migration scripts for each database.

Therefore I found this tool: https://github.com/Xethron/migrations-generator and via this help output:

Description:
  Generate a migration from an existing table structure.

Usage:
  migrate:generate [options] [--] [<tables>]

Arguments:
  tables                              A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments

Options:
  -c, --connection[=CONNECTION]       The database connection to use. [default: "etable_api"]
  -t, --tables[=TABLES]               A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments
  -i, --ignore[=IGNORE]               A list of Tables you wish to ignore, separated by a comma: users,posts,comments
  -p, --path[=PATH]                   Where should the file be created?
      --defaultIndexNames             Don't use db index names for migrations
      --defaultFKNames                Don't use db foreign key names for migrations
  -h, --help                          Display this help message
  -q, --quiet                         Do not output any message
  -V, --version                       Display this application version
      --ansi                          Force ANSI output
      --no-ansi                       Disable ANSI output
  -n, --no-interaction                Do not ask any interactive question
      --env[=ENV]                     The environment the command should run under
  -tp, --templatePath[=TEMPLATEPATH]  The location of the template for this generator
  -v|vv|vvv, --verbose                Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

I can use the following command sequence in order to make a dedicated folder for the migration script:

mkdir -p ./database/migration/old
php artisan migrate:generate -c old -p ./database/migration/old

And via artisan I can run the migrations via:

php artisan migrate -c old -p ./database/migration/old

Therefore I can use the laravel provided solution:

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
      // Do some fancy stuff here
    }
}

But how I can specify the specified folder for the migration script of the database that I want to participate in test when using the Illuminate\\Foundation\\Testing\\RefreshDatabase ?

I see this question is somewhat old but if anyone else is looking for the answer to this problem this is what worked for me. You can accomplish what you're trying to do with a few tweaks to your application.

The two problems I found when trying to run tests against an application with a migrations sub folder is that when the application is built and when the database is refreshed by the RefreshDatabase trait the migrations in the sub folder do not get executed.

To get it to work I had to:

  1. Modify my CreatesApplication trait to run the migrations in the sub folder after the application is created.
  2. Create my own RefreshDatabase trait that would piggy-back on the included RefreshDatabase trait.

RefreshDatabase trait

  • Create a new file under tests/Traits directory (I had to create this) called RecursiveRefreshDatabase.php This file contains the following code:
<?php

namespace Tests\Traits;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\RefreshDatabase;

trait RecursiveRefreshDatabase {
    use RefreshDatabase;

    /**
     * Refresh the in-memory database.
     *
     * @return void
     */
    protected function refreshInMemoryDatabase()
    {
        $this->artisan('migrate');
        // 'database/migrations/sub-folder’ would probably be ‘database/migrations/old’ in the case of the OP
        $this->artisan('migrate', ['--path' => 'database/migrations/sub-folder’]);

        $this->app[Kernel::class]->setArtisan(null);
    }
}
  • In your tests replace
use Illuminate\Foundation\Testing\RefreshDatabase;

with

use Tests\Traits\RecursiveRefreshDatabase as RefreshDatabase;
  • Note: I am overriding the refreshInMemoryDatabase method in this example but you may need to override a different one if you are not using an in memory database for testing.

Modify CreatesApplication.php

  • Modify the file tests/CreatesApplication.php to call your sub folder migrations the createApplication() as seen below
public function createApplication()
{
    $app = require __DIR__ . ‘/../../bootstrap/app.php’;

    $app->make(Kernel::class)->bootstrap();

    $this->afterApplicationCreated(function () {
        // 'database/migrations/sub-folder’ would probably be ‘database/migrations/old’ in the case of the OP
        $this->artisan(‘migrate’, [‘—path’ => ‘database/migrations/sub-folder’]);
    });

    return $app;
}



These changes worked for me and got my tests working again. Let me know if it works for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM