New databases commonly require seed data in order to be functional for an application. When you deploy a new stack with Cloud 66 (and choose to have a new database provisioned), we will provision that database for you. However, the database will not have any data in it.
To address this, you can make use of a deploy hook to execute your required scripts and seed your database during deployment. This guide will show you how to create a deploy hook and custom script to seed your database.
Simply add a bash script to your repository that contains the script for seeding your database.
This could be a custom script, a custom rake command, or the default Rails
rake db:seed command depending on your requirements/implementation.
In our example, we will use the default Rails rake db:seed command.
Create the file
/.cloud66/dbseed.sh as below:
#!/bin/bash cd $STACK_PATH bundle exec rake db:seed
Add a deploy hook to execute the above script during the first deploy (on the first server only).
Create the file
.cloud66/deploy_hooks.yml as below (replacing production with your target environment).
production: after_symlink: # Or use after_rails depending on your application - source: /.cloud66/dbseed.sh destination: /tmp/dbseed.sh target: rails execute: true run_on: single_server apply_during: build_only
The deploy hook example above will only execute during the build for a new stack. If you want to seed an existing stack you could either
- Execute the seed command manually, or
- Change the apply_during specification of the deploy hook (could be used for DB data resets during subsequent testing deploys for instance)