How to run custom mysql script for Laravel app in test pipeline in CodeShip Basic?

361 Views Asked by At

Relative newbie to PhpUnit and testing in general. We do not use migrations in our project, but have a couple of scripts that I need to run in order to set up the database for testing. How can I run mysql scripts from the project in the test pipeline? I also need to create a new database with a specific name before running those scripts.

Thanks!

2

There are 2 best solutions below

0
QuietSeditionist On BEST ANSWER

OK, after some digging I found out how to do all this. Below is the script I used for testing with a new mysql schema created with specific user from a sql script. Hope this helps someone in the future.

mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE USER 'myuser'@'%' IDENTIFIED BY 'testpassword';"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE SCHEMA myschemaname;"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'testpassword';"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD myschemaname < ./app/Ship/Migrations/1of2-schema.sql
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD myschemaname < ./app/Ship/Migrations/2of2-seed.sql
php artisan passport:install
./vendor/bin/phpunit

$MYSQL_USER and $MYSQL_PASSWORD are replaced by Codeship - these are the env variables for the user and password for the mysql that exists in the build container.

the -e switch on the mysql call runs the script given and exits. Had to do it this way since I couldn't interact with the mysql client.

1
Michelle On

The commands that you use on your local machine are the same commands you can run in CodeShip Basic. CodeShip Basic is just a build machine with Ubuntu Bionic and it will run through the setup, test, and deploy commands as if you were entering each line into your CLI. :)

We also have some documentation about mysql: https://documentation.codeship.com/basic/databases/mysql/