diff --git a/README.md b/README.md index e20fa93ecc07d3252f3dca2487aad8e969bd77ca..d7b3b066d85517254b7308375f8957ced5548c60 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,15 @@ This repository defines the VOSpace database structure. It consists in a PostgreSQL database with the [ltree](https://www.postgresql.org/docs/12/ltree.html) extension. SQL files are named starting with a number because they are put in a Docker image where they have to be executed in order. + +## Dump and restore + +To restore the file catalog from a dump it is necessary to drop the generated columns and regenerate them after the import. This can be done using the restore_db.sh script located in the dump_and_restore folder of this repository. + +To obtain a data only dump of the database use the following command: + + pg_dump -h <hostname> -U postgres --data-only --format=tar vospace_testdb > mydump.tar + +Then you can use the restore script in the following way: + + ./restore_db.sh -U postgres -h 127.0.0.1 -p 5432 -d vospace_testdb mydump.tar \ No newline at end of file diff --git a/dump_and_restore/path-indexes.sql b/dump_and_restore/path-indexes.sql new file mode 100644 index 0000000000000000000000000000000000000000..b5f7e90686fc465a65bd0c0f2765ceedaba94222 --- /dev/null +++ b/dump_and_restore/path-indexes.sql @@ -0,0 +1,6 @@ +ALTER TABLE node ADD COLUMN path ltree GENERATED ALWAYS AS (path(parent_path, node_id)) STORED; +CREATE INDEX file_path_gist_idx ON node USING GIST(path); +CREATE UNIQUE INDEX file_path_idx ON node USING btree(path); + +ALTER TABLE node ADD COLUMN relative_path ltree GENERATED ALWAYS AS (path(parent_relative_path, node_id)) STORED; +CREATE INDEX file_rel_path_gist_idx ON node USING GIST(relative_path); diff --git a/dump_and_restore/pre-restore.sql b/dump_and_restore/pre-restore.sql new file mode 100644 index 0000000000000000000000000000000000000000..149e3997e79dc7e5aa2f6496cbf0598781d037cb --- /dev/null +++ b/dump_and_restore/pre-restore.sql @@ -0,0 +1,6 @@ +DELETE FROM node; +ALTER SEQUENCE node_node_id_seq RESTART WITH 1; + +-- Generated columns need to be dropped and generated again. This causes also path support views to be dropped +ALTER TABLE node DROP COLUMN path CASCADE; +ALTER TABLE node DROP COLUMN relative_path CASCADE; diff --git a/dump_and_restore/restore_db.sh b/dump_and_restore/restore_db.sh new file mode 100755 index 0000000000000000000000000000000000000000..0f4e92dd8174e960743d431595db72a776a9ece7 --- /dev/null +++ b/dump_and_restore/restore_db.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +user="postgres" +host="localhost" +port="5432" +database="postgres" + +while [[ "$#" -gt 0 ]]; do + case $1 in + -U) user="$2"; shift ;; + -h) host="$2"; shift ;; + -p) port="$2"; shift ;; + -d) database="$2"; shift ;; + *) dump_file="$1" ;; + esac + shift +done + +if [ -z "$dump_file" ]; then + echo "Specify dump file as last argument" + exit 1 +fi + +psql -U $user -h $host -p $port $database < pre-restore.sql +pg_restore -U $user -h $host -p $port -d $database $dump_file +psql -U $user -h $host -p $port $database < path-indexes.sql +psql -U $user -h $host -p $port $database < ../02-views.sql