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

Errors trying to use loadMigrationsFrom and custom migrations at the same time #176

Closed
scottlaurent opened this issue May 31, 2017 · 9 comments

Comments

@scottlaurent
Copy link

My app broke after trying to move to 3.4. -- I have zero clue how to load the laravel migrations my migrations at the same time. The samples provided do not give a working example of this.

I am loading a users migration, and several other tables:

This doesn't work, even though I have the exact same user migration that laravel provides

public function setUp()
    {
        parent::setUp(); 
        $this->loadMigrationsFrom(realpath(__DIR__.'/../migrations'));
        $this->artisan('migrate', ['--database' => 'testbench']);
    }

So I add the loadLaravelMigration, and this doesn't work. Gives the error: Cannot declare class CreateUsersTable... -- So I know it's loading the classes, but not executing them.

public function setUp()
    {
        parent::setUp(); 
        $this->loadLaravelMigrations(['--database' => 'testbench']);
        $this->loadMigrationsFrom(realpath(__DIR__.'/../migrations'));
        $this->artisan('migrate', ['--database' => 'testbench']);
    }

** So AFTER I remove my own users migration to prevent the conflict it loads the testbench migrations, but not mine. Or maybe it is loading mine, but not executing them.

public function setUp()
    {
        parent::setUp(); 
        $this->loadLaravelMigrations(['--database' => 'testbench']);
        $this->loadMigrationsFrom(realpath(__DIR__.'/../migrations'));
        $this->artisan('migrate', ['--database' => 'testbench']);
    }
@crynobone
Copy link
Member

We already have actual test case testing this, so could you try to replicate the scenario via the available tests?

https://github.com/orchestral/testbench/blob/3.4/tests/Databases/MigrateWithRealpathTest.php#L29

@crynobone
Copy link
Member

Oh I see what you mean now, all migration class need to be uniques, you can have CreateUsersTable migration class in your custom migration path, since it would conflict with the default Laravel migrations class.

@scottlaurent
Copy link
Author

I think I narrowed down my issue. Even if I removed my own user migration, it fixes the name issue, however a second loadMIgrations type of command (below) resets the db so that only the final one represents the migration (ie: the first one, users, is wiped out).

$this->loadLaravelMigrations(['--database' => 'testbench']);

// this next line will override the first one and clear out previous migrations
$this->loadMigrationsFrom(realpath(__DIR__.'/migrations'));

So I just fixed it eliminating the loadLaravelMigrations and putting my own user one back in.

@crynobone
Copy link
Member

$this->loadMigrationsFrom(realpath(__DIR__.'/migrations')); this would run on the default database connection while $this->loadLaravelMigrations(['--database' => 'testbench']); would be executed on testbench connection.

@scottlaurent
Copy link
Author

testbench is what we are using for the default

	protected function getEnvironmentSetUp($app)
	{
	    // Setup default database to use sqlite :memory:
	    $app['config']->set('database.default', 'testbench');
	    $app['config']->set('database.connections.testbench', [
	        'driver'   => 'sqlite',
	        'database' => ':memory:',
	        'prefix'   => '',
	    ]);
	    
	    Eloquent::unguard();
	}

crynobone added a commit that referenced this issue May 31, 2017
…hat it's can rollback perfectly. #176

Signed-off-by: crynobone <crynobone@gmail.com>
@crynobone
Copy link
Member

I can't replicate this issue https://travis-ci.org/orchestral/testbench/builds/237741392

crynobone added a commit that referenced this issue May 31, 2017
Signed-off-by: crynobone <crynobone@gmail.com>
@scottlaurent
Copy link
Author

so after tinkering around more:

This works

$this->artisan('migrate', ['--database'=>'testbench','--path'=>'migrations']);
$this->artisan('migrate', ['--database'=>'testbench','--realpath'=>realpath(__DIR__.'/migrations')]);

This does not

$this->loadLaravelMigrations(['--database' => 'testbench']);
$this->artisan('migrate', ['--database'=>'testbench','--realpath'=>realpath(__DIR__.'/migrations')]);

This does not

 $this->artisan('migrate', ['--database'=>'testbench','--path'=>'migrations']);
$this->app[ConsoleKernel::class]->setArtisan(null);
$this->artisan('migrate', ['--database'=>'testbench','--realpath'=>realpath(__DIR__.'/migrations')]);

This issue is caused by the artisan null setting:
$this->app[ConsoleKernel::class]->setArtisan(null);

@crynobone
Copy link
Member

crynobone commented May 31, 2017

I don't see how this is an issue, you should be using the existing methods.

I'm closing this is I already made a test to show that loading Laravel migrations with custom migration works fine on Testbench, you can see the code https://github.com/orchestral/testbench/blob/3.4/tests/Databases/MigrateWithRealpathAndLaravelTest.php

If you still think there an issue, commit/send a Pull Request with new failing test so we can move forward.

@scottlaurent
Copy link
Author

okay. i can put one up later today and post a fix.

This is the issue: running these two commands back will not work because they include
$this->app[ConsoleKernel::class]->setArtisan(null); inside of both functions, thus rendering the previous broken.

$this->loadLaravelMigrations(['--database' => 'testbench']);
$this->loadMigrationsFrom(realpath(DIR.'/migrations'));

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