Skip to content

Commit

Permalink
✨ Reduce downtime of make refresh by swapping the database
Browse files Browse the repository at this point in the history
  • Loading branch information
Marigold committed Jun 26, 2024
1 parent a7117f4 commit b92a452
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions devTools/docker/refresh-grapher-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,37 @@ _mysql() {
}

import_db() {
cat $1 | gunzip | sed s/.\*DEFINER\=\`.\*// | _mysql $GRAPHER_DB_NAME
cat $1 | gunzip | sed s/.\*DEFINER\=\`.\*// | _mysql $2
}

fillGrapherDb() {
echo "==> Refreshing grapher database"
_mysql --database="" -e "DROP DATABASE IF EXISTS $GRAPHER_DB_NAME;CREATE DATABASE $GRAPHER_DB_NAME;ALTER DATABASE $GRAPHER_DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_as_cs;"

if [ -f "${DATA_FOLDER}/owid_metadata.sql.gz" ]; then
echo "Importing live Grapher metadata database (owid_metadata)"
import_db $DATA_FOLDER/owid_metadata.sql.gz
else
echo "owid_metata.sql.gz missing in ${DATA_FOLDER}. Refresh aborted."
return 1
fi
TEMP_DB_NAME="${GRAPHER_DB_NAME}_temp"

# Create a temporary database
# _mysql --database="" -e "DROP DATABASE IF EXISTS $TEMP_DB_NAME; CREATE DATABASE $TEMP_DB_NAME; ALTER DATABASE $TEMP_DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_as_cs;"

# if [ -f "${DATA_FOLDER}/owid_metadata.sql.gz" ]; then
# echo "Importing live Grapher metadata database into temporary database (owid_metadata)"
# import_db $DATA_FOLDER/owid_metadata.sql.gz $TEMP_DB_NAME
# else
# echo "owid_metadata.sql.gz missing in ${DATA_FOLDER}. Refresh aborted."
# return 1
# fi

# echo "==> Drop owid database and recreate it"
# _mysql --database="" -e "DROP DATABASE IF EXISTS $GRAPHER_DB_NAME;CREATE DATABASE $GRAPHER_DB_NAME;ALTER DATABASE $GRAPHER_DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_as_cs;"

echo "==> Fill new DB with tables"
tables=$(_mysql --batch --skip-column-names --database="" -e "SHOW TABLES IN $TEMP_DB_NAME")
for table in $tables; do
# TODO: doesn't work with views!!! we'd have to recreate them
_mysql --database="" -e "RENAME TABLE $TEMP_DB_NAME.$table TO $GRAPHER_DB_NAME.$table;"
done

# Drop the original database and rename the temporary database to the original name
# _mysql --database="" -e "DROP DATABASE IF EXISTS $GRAPHER_DB_NAME; RENAME DATABASE $TEMP_DB_NAME TO $GRAPHER_DB_NAME;"

echo "==> ✅ Grapher DB refresh complete"
}
Expand Down

0 comments on commit b92a452

Please sign in to comment.