Skip to content

Instantly share code, notes, and snippets.

@davidread
Last active March 27, 2020 11:39
Show Gist options
  • Save davidread/3ed0d962b05cb87678b389f34186fa3f to your computer and use it in GitHub Desktop.
Save davidread/3ed0d962b05cb87678b389f34186fa3f to your computer and use it in GitHub Desktop.
#! /bin/bash
# Resets a CKAN install e.g. after you switch branches, particularly downgrading.
# Destructive! Designed for developers, not production installs.
# Run this script from the ckan repo directory.
# Make sure you have $CKAN_INI set.
set -ex
CKAN_MAJOR_VERSION=$(grep '__version__' ckan/__init__.py | awk '{print $3}' |sed "s/'\([0-9]\+\)\.[0-9]\+\..*/\1/")
CKAN_MINOR_VERSION=$(grep '__version__' ckan/__init__.py | awk '{print $3}' |sed "s/'[0-9]\+\.\([0-9]\+\)\..*/\1/")
CKAN_VERSION=$CKAN_MAJOR_VERSION.$CKAN_MINOR_VERSION
# e.g. 2.9
PYTHON_MAJOR_VERSION=$(python -c 'import platform; print(platform.python_version()[0])')
# i.e. 2 or 3
# wipe all the databases
# If there are active connections, do this first: sudo service postgresql restart
sudo -u postgres dropdb ckan_default
sudo -u postgres createdb -O ckan_default ckan_default -E utf-8
sudo -u postgres dropdb ckan_test
sudo -u postgres createdb -O ckan_default ckan_test -E utf-8
sudo -u postgres dropdb datastore_default
sudo -u postgres createdb -O ckan_default datastore_default -E utf-8
sudo -u postgres dropdb datastore_test
sudo -u postgres createdb -O ckan_default datastore_test -E utf-8
# install paster commands, if we've gone back from CKAN 2.9 to 2.8
pip install -e .
# pyutilib doesn't reinstall properly when switching between CKAN 2.9 and 2.8
pip uninstall -y pyutilib.component.core pyutilib
# install requirements
if [[ $PYTHON_MAJOR_VERSION = 3 ]]; then
# Python 3 is always requirements.txt
pip install -r requirements.txt -r dev-requirements.txt
elif [[ $CKAN_MAJOR_VERSION -gt 2 || $CKAN_MINOR_VERSION -gt 8 ]]; then
# CKAN 2.9 and later with Python 2 needs -py2.txt
pip install -r requirements-py2.txt -r dev-requirements.txt
else
# must be CKAN 2.8 and earlier with Python 2
pip install -r requirements.txt -r dev-requirements.txt
fi
# delete pyc files - migration scripts cause problems on downgrade
find . -name "*.pyc" -delete
# If you are using CKAN version before 2.8.x you may need full_text_function.sql
#sudo -u postgres psql datastore_default -f ../ckanext-xloader/full_text_function.sql
#sudo -u postgres psql datastore_test -f ../ckanext-xloader/full_text_function.sql
if [[ $CKAN_MAJOR_VERSION -gt 2 || $CKAN_MINOR_VERSION -gt 8 ]]; then
# for CKAN 2.9 and later
# initialize the db
ckan -c $CKAN_INI db init
ckan -c $CKAN_INI datastore set-permissions 2>/dev/null | sudo -u postgres psql --set ON_ERROR_STOP=1
ckan -c test-core.ini datastore set-permissions 2>/dev/null | sudo -u postgres psql --set ON_ERROR_STOP=1
## Automated tests should run fine now. For testing in the browser, carry on ##
# Create a test user
ckan -c $CKAN_INI user add admin email=david.read@hackneyworkshop.com password=password
ckan -c $CKAN_INI sysadmin add admin
# Clear the search index
ckan -c $CKAN_INI search-index rebuild
else
# initialize the db
paster --plugin=ckan db init -c $CKAN_INI
paster --plugin=ckan datastore set-permissions -c $CKAN_INI| sudo -u postgres psql --set ON_ERROR_STOP=1
paster --plugin=ckan datastore set-permissions -c test-core.ini| sudo -u postgres psql --set ON_ERROR_STOP=1
## Automated tests should run fine now. For testing in the browser, carry on ##
# Create a test user
paster --plugin=ckan user add admin email=david.read@hackneyworkshop.com password=password -c $CKAN_INI
paster --plugin=ckan sysadmin add admin -c $CKAN_INI
# Clear the search index
paster --plugin=ckan search-index rebuild -c $CKAN_INI
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment