Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- run_as_user() {
- mastodon_dir="/usr/local/www/mastodon"
- _cmd="$@"
- _user="${mastodon_web_user}"
- [ -z "${_user}" ] && _user="${mastodon_stream_user}"
- [ -z "${_user}" ] && _user="${mastodon_workers_user}"
- if [ -n "${_user}" ]; then
- _cmd="su -m ${_user} -c 'cd "${mastodon_dir}" && sh -c \"$_cmd\"'"
- fi
- eval "${_cmd}"
- }
- query() {
- psql_env="_=1"
- _db_name=$1
- shift
- _sql="$*"
- rc=0
- if [ -n "${mastodon_db_password}" ]; then
- passwd_directory=$(mktemp -d) || exit 1
- passwd_file="${passwd_directory}/.pgpass"
- echo "\*:\*:\*:\*:${mastodon_db_password}" > "${passwd_file}"
- chmod 600 "${passwd_file}"
- psql_env="HOME=${passwd_directory}"
- fi
- if [ -n "${mastodon_db_url}" ]; then
- _psql_cmd="env ${psql_env} psql -qtA \"${mastodon_db_url}\" dbname=\"${_db_name}\""
- elif [ -n "${mastodon_db_name}" ]; then
- _psql_cmd="env ${psql_env} psql -qtA \
- ${mastodon_db_user:+-U ${mastodon_db_user} } \
- ${mastodon_db_host:+-h ${mastodon_db_host} } \
- ${mastodon_db_port:+-p ${mastodon_db_port} } \
- \"${_db_name}\""
- else
- rc=1
- fi
- if [ "$rc" = 0 ]; then
- echo "${_sql}" | run_as_user "${_psql_cmd}" || rc=1
- fi
- if [ -n "${passwd_directory}" -a -d "${passwd_directory}" ]; then
- rm -rf "${passwd_directory}"
- fi
- return $rc
- }
- db_quick_test() {
- [ -n "${mastodon_db_url}" ] && return 0 # Our connection test would always fail
- out=$(query template1 'select 1') && test "${out}" -eq 1
- }
- db_schema_version() {
- query "${mastodon_db_name}" 'select max(version) from schema_migrations'
- }
- check_db_schema() {
- required_schema_version="$1"
- shift
- if db_quick_test; then
- schema_version=$(db_schema_version)
- if [ -n "${schema_version}" ]; then
- if [ "${schema_version}" -eq "${required_schema_version}" ]; then
- return 0
- elif [ "${schema_version}" -lt 20170604144747 ] ; then
- return 4
- else
- return 3
- fi
- else
- return 2
- fi
- else
- return 1
- fi
- }
- run_rails() {
- if [ -n "${mastodon_db_url}" ]; then
- _rails_cmd="env \
- DATABASE_URL=\"${mastodon_db_url}\" \
- RAILS_ENV=production \
- rails $1"
- else
- _rails_cmd="env \
- ${mastodon_db_host+DB_HOST=\"${mastodon_db_host}\"} \
- ${mastodon_db_name+DB_NAME=\"${mastodon_db_name}\"} \
- ${mastodon_db_port+DB_PORT=\"${mastodon_db_port}\"} \
- ${mastodon_db_user+DB_USER=\"${mastodon_db_user}\"} \
- ${mastodon_db_password+DB_PASS=\"${mastodon_db_password}\"} \
- RAILS_ENV=production \
- rails $1"
- fi
- run_as_user "${_rails_cmd}"
- }
- do_schema() {
- check_db_schema 20170913000752
- rc=$?
- case $rc in
- 0) ;;
- 1) echo "Cannot connect to Mastodon database"
- return 1
- ;;
- 2) run_rails db:setup ;;
- 3) run_rails db:migrate ;;
- 4) run_rails mastodon:maintenance:prepare_for_foreign_keys ;;
- esac
- }
- check_mastodon() {
- do_schema
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement