KBK
KBK

Reputation: 375

exsisting database synchronize with laravel migration

I have designed a database using mysql workbench and synchronize, then my mysql database exists @ phpmyadmin . then how can I use this database with laravel.. I mean I need to create migration files for existing database and continue editing..

Upvotes: 2

Views: 4255

Answers (4)

thePHPHero
thePHPHero

Reputation: 179

My situation was the following:

  • I had hundreds of migration classes in the database/migrations folder.
  • My classes had already been migrated, but at some point I imported an old version of my migration table by accident.
  • My migrations table had fewer records than my classes.

After trying everything, I decided to walk the extra mile and solve my problem like this:

I created a method in a temporary controller. Basically, it checks through all my migration classes in database/migrations folder and then checks if they exist in the migrations table. If not, insert them into the table. Called the method from the browser and now everything is synched

 public function syncMigration(){
        $migrations_folder = database_path('migrations');
        $filesInFolder = File::files($migrations_folder);
        $lastMigration = DB::table('migrations')->orderBy('batch', 'desc')->limit(1)->first();
        var_dump($lastMigration);

        $batch = $lastMigration->batch +1;


        foreach($filesInFolder as $path) {
            $file = pathinfo($path);
            $migration = DB::table('migrations')->where('migration', $file['filename'])->first();
            if(!$migration){
                DB::table('migrations')->insert(
                    [
                        'migration' => $file['filename'],
                        'batch' => $batch
                    ]
                );
            }
        }
    }

Upvotes: 0

Bader
Bader

Reputation: 845

while in developing stage you can use : https://github.com/awssat/laravel-sync-migration

by using :

php artisan migrate:sync

but I won't recommend use it for production since you can't track the migrations changes

Upvotes: 0

Kola
Kola

Reputation: 559

You could you this package Migrations Generator

  1. Include it in your project(via composer or directly downloading it into your vendor folder) and configure service providers.
  2. run: php artisan migrate:generate to generate the migrations for all the tables in you current database. To generate the migrations for specific tables, run php artisan migrate:generate table1,table2,table3,table4,table5.

Upvotes: 0

subzeta
subzeta

Reputation: 439

You should write the migration files, so your schema can be built from zero without a manual intervention. But if you can't or you don't want to do it, you can start your migrations from your desired point:

php artisan make:migration blablabla

Write the new schema and:

php artisan migrate

And so on.

Upvotes: 2

Related Questions