ucraft's avatar

Move database folder to different location

Hello

I am trying to move DatabaseSeeder class and the whole database folder to another location, which is a folder xxx in root

So i will have

xxx/database/factories

xxx/database/migrations

xxx/database/seeds

I moved the files, also changed few lines in composer like

"autoload-dev": {
        "classmap": [
            "tests/",
            "xxx/database/"
        ]
    },

I also override the application before init-ing it in bootstrap/app.php like

 public function path()
    {
        return $this->basePath.DIRECTORY_SEPARATOR.'xxx';
    }

    public function databasePath()
    {
        return $this->path().'/database';
    }

I made the migrations, factories and the seed, everything was running smooth. Today i decided to update the composer, and after that when trying to run the seeder, I am getting

Fatal error: Cannot redeclare class DatabaseSeeder in /Users/***/Documents/MAMP/lumen/xxx/database/seeds/DatabaseSeeder.php on line 36

after digging little bit, found out that the second include of this class is somewhere deep in the factory, because

public function run()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');

        // Truncate all tables
        XXX\Page::truncate();

        // Generate fake data for all models needed
        factory(XXX\Page::class, 10)->create();

    }

when I remove the factory helper call line, everything runs smooth again, so I am guessing something changed there in core, seems some include_once is changed to include or smth like this. Anyone has any idea on this ?

0 likes
2 replies
ucraft's avatar

After some digging and community help, we found the issue.

Otwell updated some path related code in Application

https://github.com/laravel/lumen-framework/commit/d196ab911cf7c5e3b5725d36388ee85b50613105

And we were overriding databasePath() function, which was

public function databasePath()
    {
        return $this->path().'/database';
    }

but after the commit we had to change our override to be

public function databasePath($path = '')
    {
        return $this->path().DIRECTORY_SEPARATOR.'database'.($path ? DIRECTORY_SEPARATOR.$path : $path);
    }

and Because of this eloquent factory was trying to load all php files in database folder instead of only factories folder, and as the seeder file had a class it was trying to include it again

Please or to participate in this conversation.