Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding load-sql.sh #93

Merged
merged 2 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions compiler/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

Data for four example application can be found here: [nba](https://www.dropbox.com/s/baqb01thxvfthk5/nba_db_psql.sql?dl=0), [usmap](https://www.dropbox.com/s/youvfap909mk1m3/usmap_db_psql.sql?dl=0), [forest](https://www.dropbox.com/s/39ji04m926lfx5i/forest_db_psql.sql?dl=0) and [flare](https://www.dropbox.com/s/ugr3cx63ul3tt0k/flare_db_psql.sql?dl=0).

To load data of `app` (can be one of `nba`, `usmap`, `forest` or `flare`) into your Postgres database, run the following:
To load data of `app` (can be one of `nba`, `usmap`, `forest` or `flare`) into our docker container, run the following under root git directory:

$ createdb app
$ psql app < app.sql
$ ./docker-scripts/load-sql.sh app.sql --dbname app

55 changes: 55 additions & 0 deletions docker-scripts/load-sql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

# check input sql file exists
SQL_FILE="$1"
shift
if [ ! -f $SQL_FILE ]; then
echo "Input file $SQL_FILE Not found"
exit
fi
if [ ! ${SQL_FILE: -4} == ".sql" ]; then
echo "Not a sql file: $SQL_FILE"
exit
fi


# default db name and table name
x="${SQL_FILE##*/}"
if [[ $x == $SQL_FILE ]]; then
DB_NAME=${SQL_FILE:0:$((${#SQL_FILE} - 4))}
# db name is the same name as the sql file
else
x=${SQL_FILE:$((${#SQL_FILE} - ${#x}))}
DB_NAME=${x:0:$((${#x} - 4))}
fi

while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--dbname)
DB_NAME="$2"
shift
shift
;;
*)
echo "Wrong argument name $key"
exit
;;
esac
done

echo 'DB_NAME='$DB_NAME
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trivial: for consistency with the code below: echo "DBNAME=${DB_NAME}"


# create table and drop table
docker exec -it kyrix_db_1 psql postgresql://postgres:kyrixftw@localhost/postgres -c "CREATE DATABASE ${DB_NAME}";

# copy file to kyrix_db_1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably worth a comment... on most host OSs you can pipe directly to docker exec... except that (I think) win32 this might be weird... dunno about late-model versions of win32 and powershell... also nice to have the .sql file around when debugging inside the VM...

docker cp $SQL_FILE kyrix_db_1:/$SQL_FILE

# import sql file content into the selected database
docker exec -it kyrix_db_1 /bin/sh -c "psql postgresql://postgres:kyrixftw@localhost/${DB_NAME} < ${SQL_FILE}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi hardcoded password... instead pull from the environment, and if needed make this the default...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script assumes local environment. It feels bad if we run setup-kyrix-var in user's local environment. I'm thinking this password thing should be in the config file (of course, need to fix #64 first), so that every script that needs it can read from it.