0

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!

QuietSeditionist
  • 703
  • 4
  • 8
  • 18

2 Answers2

1

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/

Michelle
  • 31
  • 1
  • Hi @Michelle, and thanks for your input. Unfortunately, that link is pretty limited. Couple follow up questions - how do I use environment variables in the test script? I need to take the following steps: 1) Create new user that is required by my script, 2) Create a new schema, 3) give permissions to user created in step 1, 4) run the custom script to create the schema and default data – QuietSeditionist Jul 31 '19 at 16:10
1

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.

QuietSeditionist
  • 703
  • 4
  • 8
  • 18