Skip to content

Commit

Permalink
Add wait4x / logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyRae committed Jul 6, 2024
1 parent 9d6c8b2 commit cf41a39
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,44 @@ VOCAB_DATA_DIR="$VOCAB_DATA_DIR"
SCHEMA_NAME="$SCHEMA_NAME"

# SQL files
sql_files=(pk.sql constraint.sql index.sql)
sql_files=(primary-keys.sql constraints.sql indices.sql)
vocab_tables=(DRUG_STRENGTH CONCEPT CONCEPT_RELATIONSHIP CONCEPT_ANCESTOR CONCEPT_SYNONYM VOCABULARY RELATIONSHIP CONCEPT_CLASS DOMAIN)

# Directory paths
script_dir="/scripts"
temp_dir="/tmp"

echo "Waiting for the Database.."
wait4x tcp://${DB_HOST}:${DB_PORT} --timeout 60
echo "Database is up - continuing.."

# Check if the schema already exists
schema_exists=$(PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -tAc "SELECT 1 FROM information_schema.schemata WHERE schema_name = '${SCHEMA_NAME}'")
if [ "$schema_exists" ]; then
echo "Schema '${SCHEMA_NAME}' already exists. Skipping CDM creation."
exit 0 # Exit gracefully
fi

# Create schema
echo "Creating schema.."
PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "CREATE SCHEMA IF NOT EXISTS ${SCHEMA_NAME};"

# Create tables
echo "Creating tables.."
temp_ddl="${temp_dir}/temp_ddl.sql"
sed "s/@cdmDatabaseSchema/${SCHEMA_NAME}/g" "${script_dir}/ddl.sql" > "$temp_ddl"
PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$temp_ddl"
rm "$temp_ddl"

# Load vocabs
echo "Loading data.."
for table in "${vocab_tables[@]}"; do
echo 'loading: ' $table
echo 'Loading: ' $table
table_lower=$(echo "$table" | tr '[:upper:]' '[:lower:]')
PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" \
-c "\COPY ${SCHEMA_NAME}.${table_lower} FROM '${VOCAB_DATA_DIR}/${table}.csv' WITH (FORMAT csv, DELIMITER E'\t', NULL '""', QUOTE E'\b', HEADER, ENCODING 'UTF8')"
done

# Create pk, constraints, indexes
for sql_file in "${sql_files[@]}"; do
echo "Creating $sql_file.."
input_file="${script_dir}/${sql_file}"
temp_file="${temp_dir}/temp_${sql_file}"

Expand All @@ -51,3 +56,5 @@ for sql_file in "${sql_files[@]}"; do
PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$temp_file"
rm "$temp_file"
done

echo "OMOP CDM creation finished."

0 comments on commit cf41a39

Please sign in to comment.