Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrop/f8476b14835485f05d1b to your computer and use it in GitHub Desktop.
Save shrop/f8476b14835485f05d1b to your computer and use it in GitHub Desktop.
Hold your hand through the process of converting an existing Drupal 7 file field to use the private file system.Update the $FIELD variable (and, if required, the path and arguments for drush) and do what it tells you to.
#!/bin/sh
#
# This script will hold your hand during the process of converting an existing Drupal 7 file field from public to private.
#
# http://twitter.com/thsutton
# http://www.linkedin.com/pub/thomas-sutton/55/391/350
# http://thomas-sutton.id.au/
set -eu
#
# CONFIGURATION
#
# Field machine name
FIELD="field_file_field"
# Path to drush
DRUSH="/usr/local/bin/drush"
#
# PROCESS
#
# Check that there *is* a private file system configured.
if $DRUSH vget file_private_path --exact >/dev/null 2>&1; then
# @todo Check that the directory exists, is writable, etc.
true
else
echo
echo "[error] Please configure a private file system directory." > /dev/stderr
echo
exit;
fi
# Drop temporary tables, if they exist.
$DRUSH sqlq "DROP TABLE IF EXISTS temp_field_data_${FIELD};"
$DRUSH sqlq "DROP TABLE IF EXISTS temp_field_revision_${FIELD};"
# Create temporary tables.
$DRUSH sqlq "CREATE TABLE temp_field_data_${FIELD} LIKE field_data_${FIELD};"
$DRUSH sqlq "CREATE TABLE temp_field_revision_${FIELD} LIKE field_revision_${FIELD};"
# Copy data into temporary tables.
$DRUSH sqlq "INSERT INTO temp_field_data_${FIELD} SELECT * FROM field_data_${FIELD};"
$DRUSH sqlq "INSERT INTO temp_field_revision_${FIELD} SELECT * FROM field_revision_${FIELD};"
# Truncate the data tables.
$DRUSH sqlq "DELETE FROM field_data_${FIELD} WHERE 1=1;"
$DRUSH sqlq "DELETE FROM field_revision_${FIELD} WHERE 1=1;"
# Download and install the module.
if $DRUSH pm-info filefield_paths 2>/dev/null | egrep '^\s+Status\s+:' | grep -qi enabled; then
echo "filefield_paths already installed"
else
$DRUSH dl filefield_paths
$DRUSH en filefield_paths
fi
# Tell the user to configure the field to use private file system.
echo ; echo ; echo
echo "GO AND UPDATE ${FIELD} TO USE PRIVATE FILE SYSTEM"
echo ; echo ; echo
echo "Change made?: [Yn]"
while read complete; do
case "$complete" in
"y"|"Y"|"yes")
echo 'Good!'
break
;;
*)
echo 'Well hurry up!'
;;
esac
done
# Copy data back to original tables.
$DRUSH sqlq "INSERT INTO field_data_${FIELD} SELECT * FROM temp_field_data_${FIELD};"
$DRUSH sqlq "INSERT INTO field_revision_${FIELD} SELECT * FROM temp_field_revision_${FIELD};"
# Drop the temporary tables.
$DRUSH sqlq "DROP TABLE temp_field_data_${FIELD};"
$DRUSH sqlq "DROP TABLE temp_field_revision_${FIELD};"
echo ; echo ; echo
echo "Now edit ${FIELD} again and tick the 'Retroactive update' checkbox in the 'FILE (FIELD) PATH SETTINGS' fieldset."
echo ; echo ; echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment