Matthew Appleby
Matthew Appleby

Reputation: 53

How to migrate Laravel database on Docker onto Google Cloud

My Laravel app is running on docker using Linux commands on a Windows Machine. This means instead of using 'php artisan' commands, I use 'sail artisan' commands. I am able to migrate locally onto the SQL server on docker (sail artisan migrate) and am able to deploy the app onto Google Cloud (gcloud app deploy). The final bit of my puzzle piece is migrating the database onto the Google Cloud SQL server. When I was first setting the app up, I had problems with this so I exported the SQL server, uploaded it to Google Cloud and then deployed it manually which was fine for a one-off but I now have things I would like to change about the database structure without losing all the data. I also figured it was about time I learnt to do it properly anyway.

I have attempted to use the instructions on Google's community tutorial, however, this guide presupposes the project does not yet exist whereas I am working with a pre-existing application and a pre-existing database. I tried to jump into the tutorial halfway through but I couldn't get the Cloud SQL proxy to work. After a bit more research, I found this article which I got partway through, however, once I got to the part needing TCP or Unix sockets, neither set of commands will run without error on my Ubuntu terminal.

If anyone knows of any useful articles or has had this problem themself, I would greatly appreciate your help.

Additional Info:

Upvotes: 1

Views: 611

Answers (1)

Sam Dean
Sam Dean

Reputation: 404

I'm not aware of anyway to run sail/artisan command line commands on GCP.

However in ./vendor/facade/ignition/src/Solutions/RunMigrationsSolution.php there is the following function

public function run(array $parameters = [])
{
    Artisan::call('migrate');
}

which can be used to programmatically run migrations.

So in ./routes/web.php make a route like

Route::get('/run_migrate', 
    [DataController::class, 'runMigrate']
)->middleware(['auth', 'verified'])->name('run_migrate');

and in ./app/Http/Controllers/DataController.php add the function

public function runMigrate() {
    Artisan::call('migrate');
}

Now after signing in as an authorised user (or if you remove the auth middleware you won't need to sign in) you can go to https://your-app-url.com/run_migrate and it will run any outstanding migrations.

To be able to do this the schema table has to exist so you will have to import your local database to start.

Upvotes: 3

Related Questions