2
votes

For a new Laravel project, I need to use an existing MySQL database (176 tables). I don't want to create a Laravel migration for each existing table, so I've made an export of the database structure to a sql file.

In a migration I want to execute the SQL file, like so:

public function up()
{
   DB::unprepared(file_get_contents('/path/to/file.sql'));
}

unprepared returns true but it seems the import will not be (fully) executed. No error, no effect (sometimes, there are 1 or 2 tables created, for example after dropping and recreating the database before executing the sql file).

When I execute this file with mysql source /path/to/file.sql, the import works fine (some errors by version difference will be reported, but the execution continues).

My question: for testing purposes, I want to creating the 176 old/existing tables from an SQL file during the migration process. I need to alter some tables during the migration process.

I don't want to create a migration for each table.

1

1 Answers

0
votes

You can do reverse migration for all your tables by following these steps:-

1)composer require --dev "xethron/migrations-generator"

2)in bootstrap/app -$app->register(\Way\Generators\GeneratorsServiceProvider::class); $app->register(\Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider::class);

3)In bootstrap/app.php add

class Application extends Laravel\Lumen\Application
{
    /**
     * Get the path to the application configuration files.
     *
     * @param string $path Optionally, a path to append to the config path
     * @return string
     */
    public function configPath($path = '')
    {
        return $this->basePath.DIRECTORY_SEPARATOR.'config'.($path ? DIRECTORY_SEPARATOR.$path : $path);
    }
}

if (!function_exists('config_path')) {
    /**
     * Get the configuration path.
     *
     * @param  string $path
     * @return string
     */
    function config_path($path = '')
    {
        return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
    }
}

if (!function_exists('app_path')) {
    /**
     * Get the path to the application folder.
     *
     * @param  string $path
     * @return string
     */
    function app_path($path = '')
    {
        return app('path') . ($path ? DIRECTORY_SEPARATOR . $path : $path);
    }
}

class_alias('Illuminate\Support\Facades\Config', 'Config');



$app = new Application(
    realpath(__DIR__.'/../')
);

4) write php artisan migrate:generate in terminal

5) change

$app = new Application(
    realpath(__DIR__.'/../')
); 

to

$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);

in bootstrap/app.php