Skip to content

Commit cac9d45

Browse files
committed
Implement basic import-database-dump.sh script
1 parent 1410ed7 commit cac9d45

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

script/import-database-dump.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/sh
2+
3+
# Downloads the database dump tarball from crates.io and imports it
4+
# into the `cargo_registry` database. If the database already exists it
5+
# is recreated from scratch!
6+
7+
set -o errexit
8+
set -o nounset
9+
set -o pipefail
10+
11+
readonly TARBALL_PATH="tmp/db-dump.tar.gz"
12+
readonly DUMP_PATH="tmp/db-dump"
13+
14+
if [ -f "$TARBALL_PATH" ]; then
15+
echo "Skipping https://static.crates.io/db-dump.tar.gz download since it exists already "
16+
else
17+
echo "Downloading https://static.crates.io/db-dump.tar.gz to the 'tmp' folder"
18+
curl https://static.crates.io/db-dump.tar.gz --output $TARBALL_PATH
19+
fi
20+
21+
if [ -d "$DUMP_PATH" ]; then
22+
echo "Skipping db-dump.tar.gz extraction since '$DUMP_PATH' exists already"
23+
else
24+
echo "Extracting db-dump.tar.gz to '$DUMP_PATH'"
25+
mkdir -p $DUMP_PATH
26+
tar -xf $TARBALL_PATH --strip 1 -C $DUMP_PATH
27+
fi
28+
29+
cd $DUMP_PATH
30+
echo "Creating 'cargo_registry' database"
31+
psql --command="DROP DATABASE IF EXISTS cargo_registry"
32+
psql --command="CREATE DATABASE cargo_registry"
33+
34+
echo "Importing database schema"
35+
psql -a cargo_registry < schema.sql
36+
37+
echo "Importing data"
38+
psql -a cargo_registry < import.sql

0 commit comments

Comments
 (0)