# !/bin/sh # # repair-dspace.sh # # This script will repair the dspace database removing all pre-7.3 # Triggers, it effectively replaces the older schema for the database # with the current 1.4.2 schema. (it does not preserve oid's and removes # some stale data, columns and tables that are no longer used.) # # It is dependent on the database_schema.sql for 1.4.2 with any # inserts of data removed, this should have been included in the # same directory in the tarball. # # simple error exit function to terminate script function error_exit { echo "$1" 1>&2 exit 1 } DBNAME = "dspace" DBUSER = "dspace" # these tables are no longer used at this time. psql -U $DBUSER -d $DBNAME -c 'DROP TABLE "action";' || error_exit "Failed to drop Action table! Aborting" ; psql -U $DBUSER -d $DBNAME -c 'DROP TABLE "statistics";' || error_exit "Failed to drop Statistics table! Aborting"; psql -U $DBUSER -d $DBNAME -c 'DROP SEQUENCE statistics_seq;' || error_exit "Failed to drop statistics_seq! Aborting"; psql -U $DBUSER -d $DBNAME -c 'ALTER TABLE item DROP COLUMN withdrawal_date;' || error_exit "Failed to drop withdrawal_date! Aborting"; # This table contains dulicate keys which require dropping, the content of this table is temporary # Many of the existing records are stale. The table will be restored by the new schema psql -U $DBUSER -d $DBNAME -c 'DROP TABLE registrationdata;' || error_exit "Failed to drop registrationdata! Aborting"; psql -U $DBUSER -d $DBNAME -c 'DROP SEQUENCE registrationdata_seq;' || error_exit "Failed to drop registrationdata_seq! Aborting"; # Dump the database using the following settings # -a : data only # -D : use inserts with colmn names to correct for column ordering # -F c : Compressed tar # the rest is self explanitory pg_dump -a -D -F c -U $DBUSER $DBNAME -f dump.sql.custom || error_exit "Failed to dump data! Aborting"; # drop and initialize the new database dropdb -U $DBUSER $DBNAME || error_exit "Failed to drop old db! Aborting"; createdb -E UTF8 -U $DBUSER $DBNAME || error_exit "Failed to create new db! Aborting"; psql -U $DBUSER -d $DBNAME < database_schema.sql || error_exit "Failed to initialize new schema! Aborting"; # Enable dspace to be superuser to disable triggers on inserts psql -U postgres -d $DBNAME -c 'ALTER ROLE $DBUSER WITH SUPERUSER' || error_exit "Failed ALTER ROLE $DBUSER WITH SUPERUSER! Aborting"; # restore the data to the new schema pg_restore -v --disable-triggers -U $DBUSER -d $DBNAME -e -a dump.sql.custom || error_exit "Failed to drop pg_restore! Aborting"; # Disable dspace as superuser psql -U postgres -d $DBNAME -c 'ALTER ROLE $DBUSER WITH NOSUPERUSER' || error_exit "Failed ALTER ROLE $DBUSER WITH NOSUPERUSER! Aborting";