rails: Create a state directory where stateful files can live

This is necessary for files like the state file that tells the
migration script whether or not the schema needs to be loaded.

Fixes a bug where the migration script would try to load the schema on
a second deployment (since touching a file in the db directory fails
because it's in the nix store).
This commit is contained in:
Peter Jones 2018-12-20 17:28:31 -07:00
parent b9061e43a4
commit b5c6563d5b
No known key found for this signature in database
GPG key ID: 9DAFAA8D01941E49
2 changed files with 15 additions and 7 deletions

View file

@ -9,6 +9,7 @@ set -u
################################################################################
option_env=${RAILS_ENV:-production}
option_root=$(pwd)
option_statedir=$(pwd)/state
################################################################################
usage () {
@ -18,11 +19,12 @@ Usage: db-migrate.sh [options]
-e NAME Set RAILS_ENV to NAME
-h This message
-r DIR The root directory of the Rails app
-s DIR Directory where state files can be stored
EOF
}
################################################################################
while getopts "he:r:" o; do
while getopts "he:r:s:" o; do
case "${o}" in
e) option_env=$OPTARG
;;
@ -34,6 +36,9 @@ while getopts "he:r:" o; do
r) option_root=$OPTARG
;;
s) option_statedir=$OPTARG
;;
*) exit 1
;;
esac
@ -47,9 +52,9 @@ export RAILS_ENV=$option_env
################################################################################
# If this is a new database, load the schema file:
if [ ! -e config/database-loaded.flag ]; then
if [ ! -e "$option_statedir/database-loaded.flag" ]; then
rake db:schema:load
touch config/database-loaded.flag
touch "$option_statedir/database-loaded.flag"
fi
################################################################################

View file

@ -66,7 +66,7 @@ let
DATABASE_PORT = toString app.database.port;
DATABASE_NAME = app.database.name;
DATABASE_USER = app.database.user;
DATABASE_PASSWORD_FILE = "${app.home}/config/database.password";
DATABASE_PASSWORD_FILE = "${app.home}/state/database.password";
} // app.environment;
wantedBy = [ "multi-user.target" ];
@ -78,11 +78,12 @@ let
preStart = ''
# Prepare the config directory:
rm -rf ${app.home}/config
mkdir -p ${app.home}/{config,log,tmp,db}
mkdir -p ${app.home}/{config,log,tmp,db,state}
cp -rf ${app.package}/share/${app.name}/config.dist/* ${app.home}/config/
cp ${app.package}/share/${app.name}/db/schema.rb.dist ${app.home}/db/schema.rb
cp ${./database.yml} ${app.home}/config/database.yml
cp ${app.database.passwordFile} ${app.home}/config/database.password
cp ${app.database.passwordFile} ${app.home}/state/database.password
mkdir -p ${app.home}/home
ln -nfs ${app.package}/share/${app.name} ${app.home}/home/${app.name}
@ -95,7 +96,9 @@ let
'' + optionalString app.database.migrate ''
# Migrate the database (use sudo so environment variables go through):
${pkgs.sudo}/bin/sudo -u rails-${app.name} -EH \
${scripts}/bin/db-migrate.sh -r ${app.package}/share/${app.name}
${scripts}/bin/db-migrate.sh \
-r ${app.package}/share/${app.name} \
-s ${app.home}/state
'';
serviceConfig = {