Run Laravel Database Migration after every build on Google App Engine

I am running Laravel 5.5 on GAE with a MySQL instance running database. I want to be able to run database migrations after every build. I have added command "php artisan migrate" on post-install-cmd in composer.json but it does not seem to work.

"post-install-cmd": [
        "chmod -R 755 bootstrap\/cache",
        "php artisan migrate",
        "php artisan cache:clear"
    ]

The build log shows command running but looks like down there there is command cancelled. What could be the issue? How can I run migration after every build? Any help will be appreciated.

Here is screenshot of the build logs

SQL error:

ERROR: SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = My_DB_NAME and table_name = migrations) SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = My_DB_NAME and table_name = migrations) at /app/vendor/laravel/framework/src/Illuminate/Database/Connec‌​tion.php:664, PDOException(code: 2002): SQLSTATE[HY000] [2002] No such file or directory at

Upvotes: 2

Views: 2688

Answers (3)

bezz
bezz

Reputation: 83

Was not able to add such a long log file as a comment. So trying here.

<pre>
[2020-05-03 08:51:50] local.ERROR: SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = aimforthesky and table_name = migrations and table_type = 'BASE TABLE') {"exception":"[object] (Illuminate\\Database\\QueryException(code: 2002): SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = aimforthesky and table_name = migrations and table_type = 'BASE TABLE') at /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connection.php:671)
[stacktrace]
#0 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connection.php(631): Illuminate\\Database\\Connection->runQueryCallback('select * from i...', Array, Object(Closure))
#1 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connection.php(339): Illuminate\\Database\\Connection->run('select * from i...', Array, Object(Closure))
#2 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Schema/MySqlBuilder.php(18): Illuminate\\Database\\Connection->select('select * from i...', Array)
#3 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php(169): Illuminate\\Database\\Schema\\MySqlBuilder->hasTable('migrations')
#4 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(608): Illuminate\\Database\\Migrations\\DatabaseMigrationRepository->repositoryExists()
#5 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php(93): Illuminate\\Database\\Migrations\\Migrator->repositoryExists()
#6 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php(64): Illuminate\\Database\\Console\\Migrations\\MigrateCommand->prepareDatabase()
#7 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(541): Illuminate\\Database\\Console\\Migrations\\MigrateCommand->Illuminate\\Database\\Console\\Migrations\\{closure}()
#8 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php(81): Illuminate\\Database\\Migrations\\Migrator->usingConnection(NULL, Object(Closure))
#9 [internal function]: Illuminate\\Database\\Console\\Migrations\\MigrateCommand->handle()
#10 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(33): call_user_func_array(Array, Array)
#11 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Container/Util.php(36): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#12 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(91): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#13 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#14 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Container/Container.php(592): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#15 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Console/Command.php(134): Illuminate\\Container\\Container->call(Array)
#16 /home/nitish/code/aimforthesky/vendor/symfony/console/Command/Command.php(255): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#17 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#18 /home/nitish/code/aimforthesky/vendor/symfony/console/Application.php(912): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#19 /home/nitish/code/aimforthesky/vendor/symfony/console/Application.php(264): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Database\\Console\\Migrations\\MigrateCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#20 /home/nitish/code/aimforthesky/vendor/symfony/console/Application.php(140): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#21 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Console/Application.php(93): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#22 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#23 /home/nitish/code/aimforthesky/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#24 {main}
[previous exception] [object] (PDOException(code: 2002): SQLSTATE[HY000] [2002] No such file or directory at /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70)
[stacktrace]
#0 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(70): PDO->__construct('mysql:unix_sock...', 'root', '', Array)
#1 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(46): Illuminate\\Database\\Connectors\\Connector->createPdoConnection('mysql:unix_sock...', 'root', '', Array)
#2 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php(24): Illuminate\\Database\\Connectors\\Connector->createConnection('mysql:unix_sock...', Array, Array)
#3 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(182): Illuminate\\Database\\Connectors\\MySqlConnector->connect(Array)
#4 [internal function]: Illuminate\\Database\\Connectors\\ConnectionFactory->Illuminate\\Database\\Connectors\\{closure}()
#5 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connection.php(926): call_user_func(Object(Closure))
#6 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connection.php(961): Illuminate\\Database\\Connection->getPdo()
#7 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connection.php(405): Illuminate\\Database\\Connection->getReadPdo()
#8 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connection.php(331): Illuminate\\Database\\Connection->getPdoForSelect(true)
#9 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connection.php(664): Illuminate\\Database\\Connection->Illuminate\\Database\\{closure}('select * from i...', Array)
#10 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connection.php(631): Illuminate\\Database\\Connection->runQueryCallback('select * from i...', Array, Object(Closure))
#11 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Connection.php(339): Illuminate\\Database\\Connection->run('select * from i...', Array, Object(Closure))
#12 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Schema/MySqlBuilder.php(18): Illuminate\\Database\\Connection->select('select * from i...', Array)
#13 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php(169): Illuminate\\Database\\Schema\\MySqlBuilder->hasTable('migrations')
#14 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(608): Illuminate\\Database\\Migrations\\DatabaseMigrationRepository->repositoryExists()
#15 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php(93): Illuminate\\Database\\Migrations\\Migrator->repositoryExists()
#16 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php(64): Illuminate\\Database\\Console\\Migrations\\MigrateCommand->prepareDatabase()
#17 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(541): Illuminate\\Database\\Console\\Migrations\\MigrateCommand->Illuminate\\Database\\Console\\Migrations\\{closure}()
#18 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php(81): Illuminate\\Database\\Migrations\\Migrator->usingConnection(NULL, Object(Closure))
#19 [internal function]: Illuminate\\Database\\Console\\Migrations\\MigrateCommand->handle()
#20 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(33): call_user_func_array(Array, Array)
#21 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Container/Util.php(36): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#22 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(91): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#23 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#24 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Container/Container.php(592): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#25 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Console/Command.php(134): Illuminate\\Container\\Container->call(Array)
#26 /home/nitish/code/aimforthesky/vendor/symfony/console/Command/Command.php(255): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#27 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))#28 /home/nitish/code/aimforthesky/vendor/symfony/console/Application.php(912): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#29 /home/nitish/code/aimforthesky/vendor/symfony/console/Application.php(264): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Database\\Console\\Migrations\\MigrateCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#30 /home/nitish/code/aimforthesky/vendor/symfony/console/Application.php(140): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#31 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Console/Application.php(93): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#32 /home/nitish/code/aimforthesky/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#33 /home/nitish/code/aimforthesky/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#34 {main}
"}
</pre>

Upvotes: 0

lchachurski
lchachurski

Reputation: 1800

Short answer:

You need to set up cloud SQL connection to production DB and your laravel app at your build server. Then you run migrations after the code is deployed to google.


How to install gcloud SDK?

https://github.com/GoogleCloudPlatform/continuous-deployment-bitbucket (check bitbucket-pipelines.yml)

How to setup google cloud SQL proxy?

https://cloud.google.com/sql/docs/mysql/connect-admin-proxy

How to setup laravel app to connect to cloud SQL proxy?

Add DB_HOST=127.0.0.1 & DB_PORT=3306 to .env file, or whatever port you choose.

Run migration and enjoy!


Long answer:

When you SSH into GAE instance, you can't see your code just yet. It's hidden beneath another layer of Docker containers. In my case, there are 5 containers running: gaeapp, cloudsql, api, nginx_proxy and fluentd_logger. As you can see, cloudsql is one of the containers, which means that both your app container and cloudsql has to be built in order to connect to each other.

What most likely happens is that container with your code is provisioned before cloundsql one. As an effect, any database related command, or if you have database related code executed during the laravel startup, results in SQLSTATE[HY000] [2002] No such file or directory because /cloudsql/ directory/socket is not ready yet.

Therefore the most straightforward way is to run migrations from anywhere else but not from GAE. It might be also possible to update docker config of GAE, but I'm not that familiar with it.


Hope this helps!

Upvotes: 3

user320487
user320487

Reputation:

You need to add the --no-interaction and --force flags to the migrate command. Since you're in production, it is prompting for Y or N to run the command.

php artisan migrate  --no-interaction --force

Upvotes: 0

Related Questions