From 141e84559b7989a84987887fc01227e0dd0bf662 Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Wed, 25 Mar 2020 21:17:41 -0600 Subject: [PATCH] DB setup script --- db/sql/0-setup.sql | 8 +- db/sql/1-tables.sql | 155 ++++++++++++++++++++------------------- db/sql/2-views.sql | 42 +++++------ db/sql/3-functions.sql | 72 +++++++++--------- db/sql/4-permissions.sql | 2 + db/sql/setup.sh | 71 ++++++++++++++++++ 6 files changed, 213 insertions(+), 137 deletions(-) create mode 100644 db/sql/4-permissions.sql create mode 100755 db/sql/setup.sh diff --git a/db/sql/0-setup.sql b/db/sql/0-setup.sql index 91f4da5..7d542a6 100644 --- a/db/sql/0-setup.sql +++ b/db/sql/0-setup.sql @@ -1,7 +1,9 @@ -CREATE database sos; -create user sos with encrypted password 'password'; +create schema sos; grant all privileges on database sos to sos; -GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO sos; +grant all privileges on schema sos to sos; + +alter default privileges for user sos in schema sos grant all privileges on tables to sos; +alter default privileges for user sos in schema sos grant all privileges on functions to sos; create extension if not exists "uuid-ossp"; CREATE EXTENSION IF NOT EXISTS citext; diff --git a/db/sql/1-tables.sql b/db/sql/1-tables.sql index a646880..82608f1 100644 --- a/db/sql/1-tables.sql +++ b/db/sql/1-tables.sql @@ -1,10 +1,10 @@ -create type "delivery_type_enum" as enum ('hand_shipped', 'easypost', 'hand_delivered'); -create type "transaction_state_enum" as enum ('started', 'completed', 'cancelled', 'expired'); -create type "payment_type_enum" as enum ('ks_reward', 'stripe', 'paypal', 'account_credit'); -create type "stockchange_type_enum" as enum ('purchase', 'shipment', 'admin'); -create type "stock_change_dir_enum" as enum ('added', 'subtracted'); +create type sos."delivery_type_enum" as enum ('hand_shipped', 'easypost', 'hand_delivered'); +create type sos."transaction_state_enum" as enum ('started', 'completed', 'cancelled', 'expired'); +create type sos."payment_type_enum" as enum ('ks_reward', 'stripe', 'paypal', 'account_credit'); +create type sos."stockchange_type_enum" as enum ('purchase', 'shipment', 'admin'); +create type sos."stock_change_dir_enum" as enum ('added', 'subtracted'); -create table "user" ( +create table sos."user" ( user_uuid uuid primary key default uuid_generate_v4(), user_email citext unique not null, user_email_confirmed boolean not null default false, @@ -13,32 +13,33 @@ create table "user" ( user_time_email_confirmed timestamptz ); -create table "login_link" ( +create table sos."login_link" ( login_link_uuid uuid primary key default uuid_generate_v4(), - login_link_user_uuid uuid not null references "user" (user_uuid), + login_link_user_uuid uuid not null references sos."user" (user_uuid), login_link_time_created timestamptz not null default now(), login_link_timeout_length interval not null, login_link_login_hash varchar(60) not null ); -create table "cart" ( +create table sos."cart" ( cart_uuid uuid primary key default uuid_generate_v4() ); -create table "session" ( +create table sos."session" ( session_uuid uuid primary key default uuid_generate_v4(), session_time_created timestamptz not null default now(), session_time_last_active timestamptz not null default now(), + session_ended boolean not null default false, session_timeout_length interval not null, session_ip_address varchar(50) not null, session_user_agent varchar(500) not null, session_referer varchar(500) not null, - session_user_uuid uuid not null references "user" (user_uuid), - session_originating_link uuid references "login_link" (login_link_uuid), - session_cart uuid references "cart" (cart_uuid) + session_user_uuid uuid not null references sos."user" (user_uuid), + session_originating_link uuid references sos."login_link" (login_link_uuid), + session_cart uuid references sos."cart" (cart_uuid) ); -create table "item" ( +create table sos."item" ( item_uuid uuid primary key default uuid_generate_v4(), item_name text not null, item_description text not null, @@ -47,48 +48,48 @@ create table "item" ( item_published boolean not null default true ); -create table "cart_item" ( +create table sos."cart_item" ( cart_item_uuid uuid primary key default uuid_generate_v4(), - cart_item_item_uuid uuid not null references "item" (item_uuid), - cart_item_cart_uuid uuid not null references "cart" (cart_uuid), + cart_item_item_uuid uuid not null references sos."item" (item_uuid), + cart_item_cart_uuid uuid not null references sos."cart" (cart_uuid), cart_item_count integer not null default 1 check (cart_item_count > 0), cart_item_time_added timestamptz not null default now() ); -create table "image" ( +create table sos."image" ( image_uuid uuid primary key default uuid_generate_v4(), image_featured boolean not null default false, - image_item_uuid uuid not null references "item" (item_uuid), + image_item_uuid uuid not null references sos."item" (item_uuid), image_large_file bytea not null, image_thumb_file bytea not null, image_mime_type varchar not null, - image_uploader_uuid uuid not null references "user" (user_uuid), + image_uploader_uuid uuid not null references sos."user" (user_uuid), image_date_uploaded timestamptz not null default now() ); -create unique index only_one_primary_key on "image" (image_item_uuid, image_featured) +create unique index only_one_primary_key on sos."image" (image_item_uuid, image_featured) where image_featured = true; -create table "category" ( +create table sos."category" ( category_uuid uuid primary key default uuid_generate_v4(), category_name text not null, category_description text not null, category_urlslug citext unique not null ); -create table "category_item" ( +create table sos."category_item" ( category_item_uuid uuid primary key default uuid_generate_v4(), - category_item_item_uuid uuid not null references "item" (item_uuid), - category_item_category_uuid uuid not null references "category" (category_uuid) + category_item_item_uuid uuid not null references sos."item" (item_uuid), + category_item_category_uuid uuid not null references sos."category" (category_uuid) ); -create table "category_category" ( +create table sos."category_category" ( category_category_uuid uuid primary key default uuid_generate_v4(), - category_category_parent_uuid uuid not null references "category" (category_uuid), - category_category_child_uuid uuid not null references "category" (category_uuid) + category_category_parent_uuid uuid not null references sos."category" (category_uuid), + category_category_child_uuid uuid not null references sos."category" (category_uuid) ); -create table "address" ( +create table sos."address" ( address_uuid uuid primary key default uuid_generate_v4(), address_name text not null, address_company text, @@ -102,50 +103,50 @@ create table "address" ( address_easypost_id text ); -create table "delivery" ( +create table sos."delivery" ( delivery_uuid uuid primary key default uuid_generate_v4(), - delivery_type delivery_type_enum not null, + delivery_type sos.delivery_type_enum not null, unique (delivery_uuid, delivery_type) ); -create table "delivery_hand_shipped" ( +create table sos."delivery_hand_shipped" ( delivery_uuid uuid not null, - delivery_type delivery_type_enum check (delivery_type = 'hand_shipped'), - foreign key (delivery_uuid, delivery_type) references "delivery" (delivery_uuid, delivery_type), + delivery_type sos.delivery_type_enum check (delivery_type = 'hand_shipped'), + foreign key (delivery_uuid, delivery_type) references sos."delivery" (delivery_uuid, delivery_type), delivery_tracking_number text not null, delivery_date_shipped timestamptz not null ); -create table "delivery_easypost" ( +create table sos."delivery_easypost" ( delivery_uuid uuid not null, - delivery_type delivery_type_enum check (delivery_type = 'easypost'), - foreign key (delivery_uuid, delivery_type) references "delivery" (delivery_uuid, delivery_type), + delivery_type sos.delivery_type_enum check (delivery_type = 'easypost'), + foreign key (delivery_uuid, delivery_type) references sos."delivery" (delivery_uuid, delivery_type), delivery_tracking_number text not null, delivery_date_shipped timestamptz not null, delivery_easypost_id text not null ); -create table "delivery_hand_delivered" ( +create table sos."delivery_hand_delivered" ( delivery_uuid uuid not null, - delivery_type delivery_type_enum check (delivery_type = 'hand_delivered'), - foreign key (delivery_uuid, delivery_type) references "delivery" (delivery_uuid, delivery_type), + delivery_type sos.delivery_type_enum check (delivery_type = 'hand_delivered'), + foreign key (delivery_uuid, delivery_type) references sos."delivery" (delivery_uuid, delivery_type), delivery_description text not null, delivery_date_delivered timestamptz not null ); -create table "order" ( +create table sos."order" ( order_uuid uuid primary key default uuid_generate_v4(), order_number int not null, order_start_time timestamptz not null default now(), - order_user_uuid uuid references "user" (user_uuid), - order_address_uuid uuid references "address" (address_uuid), - order_delivery_uuid uuid references "delivery" (delivery_uuid) + order_user_uuid uuid references sos."user" (user_uuid), + order_address_uuid uuid references sos."address" (address_uuid), + order_delivery_uuid uuid references sos."delivery" (delivery_uuid) ); -create table "coupon" ( +create table sos."coupon" ( coupon_uuid uuid primary key default uuid_generate_v4(), coupon_code varchar(50) unique not null, coupon_valid_until timestamptz not null, @@ -157,89 +158,89 @@ create table "coupon" ( coupon_number_of_socks_free integer not null default 0 check (coupon_number_of_socks_free >= 0) ); -create table "transaction" ( +create table sos."transaction" ( transaction_uuid uuid primary key default uuid_generate_v4(), - transaction_order_uuid uuid references "order" (order_uuid), - transaction_cart_uuid uuid references "cart" (cart_uuid), - transaction_coupon_uuid uuid references "coupon" (coupon_uuid), + transaction_order_uuid uuid references sos."order" (order_uuid), + transaction_cart_uuid uuid references sos."cart" (cart_uuid), + transaction_coupon_uuid uuid references sos."coupon" (coupon_uuid), transaction_start_time timestamptz not null default now(), transaction_completion_time timestamptz, - transaction_payment_state transaction_state_enum not null default 'started', + transaction_payment_state sos.transaction_state_enum not null default 'started', transaction_item_total_price integer not null, transaction_coupon_effective_discount integer not null, transaction_shipping_price integer not null ); -create table "payment" ( +create table sos."payment" ( payment_uuid uuid primary key default uuid_generate_v4(), - payment_type payment_type_enum not null, + payment_type sos.payment_type_enum not null, payment_time timestamptz not null default now(), payment_value_cents integer not null, - payment_transaction_uuid uuid references "transaction" (transaction_uuid), + payment_transaction_uuid uuid references sos."transaction" (transaction_uuid), unique (payment_uuid, payment_type) ); -create table "payment_ks_reward" ( +create table sos."payment_ks_reward" ( payment_uuid uuid primary key default uuid_generate_v4(), - payment_type payment_type_enum check (payment_type = 'ks_reward'), - foreign key (payment_uuid, payment_type) references "payment" (payment_uuid, payment_type) + payment_type sos.payment_type_enum check (payment_type = 'ks_reward'), + foreign key (payment_uuid, payment_type) references sos."payment" (payment_uuid, payment_type) -- Kickstarter data? -- TODO: Figure out how this should work ); -create table "payment_stripe" ( +create table sos."payment_stripe" ( payment_uuid uuid primary key default uuid_generate_v4(), - payment_type payment_type_enum check (payment_type = 'stripe'), - foreign key (payment_uuid, payment_type) references "payment" (payment_uuid, payment_type), + payment_type sos.payment_type_enum check (payment_type = 'stripe'), + foreign key (payment_uuid, payment_type) references sos."payment" (payment_uuid, payment_type), stripe_transaction_id text not null, stripe_reciept_email citext not null ); -create table "shipment" ( +create table sos."shipment" ( shipment_uuid uuid primary key default uuid_generate_v4(), shipment_date timestamptz not null default now(), shipment_description text not null ); -create table "admin_withdrawal" ( +create table sos."admin_withdrawal" ( withdrawal_uuid uuid primary key default uuid_generate_v4(), withdrawal_date timestamptz not null default now(), withdrawal_description text not null ); -create table "item_stockchange" ( +create table sos."item_stockchange" ( stockchange_uuid uuid primary key default uuid_generate_v4(), - stockchange_type stockchange_type_enum not null, + stockchange_type sos.stockchange_type_enum not null, unique (stockchange_uuid, stockchange_type), - stockchange_item_uuid uuid references "item" (item_uuid), + stockchange_item_uuid uuid references sos."item" (item_uuid), stockchange_change integer not null check (stockchange_change > 0), - stockchange_direction stock_change_dir_enum not null + stockchange_direction sos.stock_change_dir_enum not null ); -create table "item_stockchange_purchase" ( +create table sos."item_stockchange_purchase" ( stockchange_uuid uuid primary key default uuid_generate_v4(), - stockchange_type stockchange_type_enum check (stockchange_type = 'purchase'), - foreign key (stockchange_uuid, stockchange_type) references "item_stockchange" (stockchange_uuid, stockchange_type), + stockchange_type sos.stockchange_type_enum check (stockchange_type = 'purchase'), + foreign key (stockchange_uuid, stockchange_type) references sos."item_stockchange" (stockchange_uuid, stockchange_type), - stockchange_transaction_uuid uuid references "transaction" (transaction_uuid) + stockchange_transaction_uuid uuid references sos."transaction" (transaction_uuid) ); -create table "item_stockchange_shipment" ( +create table sos."item_stockchange_shipment" ( stockchange_uuid uuid primary key default uuid_generate_v4(), - stockchange_type stockchange_type_enum check (stockchange_type = 'shipment'), - foreign key (stockchange_uuid, stockchange_type) references "item_stockchange" (stockchange_uuid, stockchange_type), + stockchange_type sos.stockchange_type_enum check (stockchange_type = 'shipment'), + foreign key (stockchange_uuid, stockchange_type) references sos."item_stockchange" (stockchange_uuid, stockchange_type), - stockchange_shipment_uuid uuid references "shipment" (shipment_uuid) + stockchange_shipment_uuid uuid references sos."shipment" (shipment_uuid) ); -create table "item_stockchange_admin" ( +create table sos."item_stockchange_admin" ( stockchange_uuid uuid primary key default uuid_generate_v4(), - stockchange_type stockchange_type_enum check (stockchange_type = 'admin'), - foreign key (stockchange_uuid, stockchange_type) references "item_stockchange" (stockchange_uuid, stockchange_type), + stockchange_type sos.stockchange_type_enum check (stockchange_type = 'admin'), + foreign key (stockchange_uuid, stockchange_type) references sos."item_stockchange" (stockchange_uuid, stockchange_type), - stockchange_withdrawal_uuid uuid references "admin_withdrawal" (withdrawal_uuid) + stockchange_withdrawal_uuid uuid references sos."admin_withdrawal" (withdrawal_uuid) ); diff --git a/db/sql/2-views.sql b/db/sql/2-views.sql index 713af81..c5100a0 100644 --- a/db/sql/2-views.sql +++ b/db/sql/2-views.sql @@ -1,4 +1,4 @@ -create or replace view public.v_session as +create or replace view sos.v_session as select "session".*, "session_user".user_email as session_user_email, @@ -7,24 +7,24 @@ create or replace view public.v_session as "session_user".user_time_registered as session_user_time_registered, "session_user".user_time_email_confirmed as session_user_time_email_confirmed, "login_link".* - from "session" - left join "user" "session_user" on "session".session_user_uuid = "session_user".user_uuid - left join "login_link" on "session".session_originating_link = "login_link".login_link_uuid; + from sos."session" + left join sos."user" "session_user" on "session".session_user_uuid = "session_user".user_uuid + left join sos."login_link" on "session".session_originating_link = "login_link".login_link_uuid; -create or replace view public.v_login_link as +create or replace view sos.v_login_link as select * - from "login_link" - left join "user" on "login_link".login_link_user_uuid = "user".user_uuid; + from sos."login_link" + left join sos."user" on "login_link".login_link_user_uuid = "user".user_uuid; -create or replace view public.v_cart as +create or replace view sos.v_cart as select * - from "cart" - left join "cart_item" on "cart".cart_uuid = "cart_item".cart_item_cart_uuid - left join "item" on "cart_item".cart_item_item_uuid = "item".item_uuid; + from sos."cart" + left join sos."cart_item" on "cart".cart_uuid = "cart_item".cart_item_cart_uuid + left join sos."item" on "cart_item".cart_item_item_uuid = "item".item_uuid; -create or replace view public.v_item as +create or replace view sos.v_item as select "item".*, "image".image_uuid, @@ -32,19 +32,19 @@ create or replace view public.v_item as "image".image_mime_type, "image".image_date_uploaded, "user".user_email - from "item" - left join "image" on "item".item_uuid = "image".image_item_uuid - left join "user" on "image".image_uploader_uuid = "user".user_uuid; + from sos."item" + left join sos."image" on "item".item_uuid = "image".image_item_uuid + left join sos."user" on "image".image_uploader_uuid = "user".user_uuid; -create or replace view public.v_category as +create or replace view sos.v_category as select "category".*, "child_category".category_uuid as child_category_uuid, "child_category".category_name as child_category_name, "child_category".category_urlslug as child_category_urlslug, v_item.* - from "category" - left join "category_category" on "category".category_uuid = "category_category".category_category_parent_uuid - left join "category" "child_category" on "category_category".category_category_child_uuid = "child_category".category_uuid - left join "category_item" on "category".category_uuid = "category_item".category_item_category_uuid - left join v_item on "category_item".category_item_item_uuid = item_uuid; + from sos."category" + left join sos."category_category" on "category".category_uuid = "category_category".category_category_parent_uuid + left join sos."category" "child_category" on "category_category".category_category_child_uuid = "child_category".category_uuid + left join sos."category_item" on "category".category_uuid = "category_item".category_item_category_uuid + left join sos.v_item on "category_item".category_item_item_uuid = item_uuid; diff --git a/db/sql/3-functions.sql b/db/sql/3-functions.sql index 2f900e0..bc0c4bb 100644 --- a/db/sql/3-functions.sql +++ b/db/sql/3-functions.sql @@ -1,11 +1,11 @@ -create or replace function public.register_user(_email text, _password_hash text) - returns setof public.user +create or replace function sos.register_user(_email text, _password_hash text) + returns setof sos.user language plpgsql as $function$ declare _user_uuid uuid; begin - insert into "user" ( + insert into sos."user" ( user_email, user_password_hash ) values ( @@ -13,40 +13,40 @@ begin _password_hash ) returning user_uuid into _user_uuid; - return query select * from "user" where user_uuid = _user_uuid; + return query select * from sos."user" where user_uuid = _user_uuid; end; $function$; -create or replace function public.validate_session(_session_uuid uuid) - returns setof public.v_session +create or replace function sos.validate_session(_session_uuid uuid) + returns setof sos.v_session language plpgsql as $function$ begin - return query select * from v_session + return query select * from sos.v_session where session_uuid = _session_uuid and session_time_last_active + session_timeout_length > now(); end; $function$; -create or replace function public.update_session(_session_uuid uuid) - returns setof public.v_session +create or replace function sos.update_session(_session_uuid uuid) + returns setof sos.v_session language plpgsql as $function$ begin - update "session" + update sos."session" set session_time_last_active = now() where session_uuid = _session_uuid and now() < (select session_time_last_active + session_timeout_length); - return query select * from validate_session(_session_uuid); + return query select * from sos.validate_session(_session_uuid); end; $function$; -create or replace function public.login_user_session(_user_uuid uuid, _timeout_length interval, _ip_addr varchar(50), _user_agent varchar(500), _referer varchar(500), _link uuid) - returns setof public.v_session +create or replace function sos.login_user_session(_user_uuid uuid, _timeout_length interval, _ip_addr varchar(50), _user_agent varchar(500), _referer varchar(500), _link uuid) + returns setof sos.v_session language plpgsql as $function$ declare _session_uuid uuid; begin - insert into "session" ( + insert into sos."session" ( session_user_uuid, session_timeout_length, session_ip_address, @@ -62,17 +62,17 @@ begin _link ) returning session_uuid into _session_uuid; - return query select * from public.validate_session(_session_uuid); + return query select * from sos.validate_session(_session_uuid); end; $function$; -create or replace function public.create_item(_name text, _urlslug citext, _description text, _price_cents integer, _published boolean) - returns setof public.v_item +create or replace function sos.create_item(_name text, _urlslug citext, _description text, _price_cents integer, _published boolean) + returns setof sos.v_item language plpgsql as $function$ declare _item_uuid uuid; begin - insert into "item" ( + insert into sos."item" ( item_name, item_urlslug, item_description, @@ -86,17 +86,17 @@ begin _published ) returning item_uuid into _item_uuid; - return query select * from public.v_item where item_uuid = _item_uuid; + return query select * from sos.v_item where item_uuid = _item_uuid; end; $function$; -create or replace function public.add_image_to_item(_item_uuid uuid, _large_file bytea, _thumb_file bytea, _mime_type varchar, _uploader_uuid uuid) - returns setof public.v_item +create or replace function sos.add_image_to_item(_item_uuid uuid, _large_file bytea, _thumb_file bytea, _mime_type varchar, _uploader_uuid uuid) + returns setof sos.v_item language plpgsql as $function$ declare _image_uuid uuid; begin - insert into "image" ( + insert into sos."image" ( image_item_uuid, image_large_file, image_thumb_file, @@ -110,27 +110,27 @@ begin _uploader_uuid ) returning image_uuid into _image_uuid; - return query select * from public.v_item where item_uuid = _item_uuid; + return query select * from sos.v_item where item_uuid = _item_uuid; end; $function$; -create or replace function public.set_featured_image(_item_uuid uuid, _image_uuid uuid) - returns setof public.v_item +create or replace function sos.set_featured_image(_item_uuid uuid, _image_uuid uuid) + returns setof sos.v_item language plpgsql as $function$ begin -- Un-feature all other images - update "image" set + update sos."image" set image_featured = false where image_item_uuid = _item_uuid; - update "image" set + update sos."image" set image_featured = true where image_uuid = _image_uuid; - return query select * from public.v_item where item_uuid = _item_uuid; + return query select * from sos.v_item where item_uuid = _item_uuid; end; $function$; -create or replace function public.get_image_large(_image_uuid uuid) +create or replace function sos.get_image_large(_image_uuid uuid) returns table (image_uuid uuid, image_mime_type varchar, image_file bytea) language plpgsql as $function$ @@ -139,11 +139,11 @@ begin "image".image_uuid, "image".image_mime_type, "image".image_large_file as image_file - from "image" + from sos."image" where "image".image_uuid = _image_uuid; end; $function$; -create or replace function public.get_image_thumb(_image_uuid uuid) +create or replace function sos.get_image_thumb(_image_uuid uuid) returns table (image_uuid uuid, image_mime_type varchar, image_file bytea) language plpgsql as $function$ @@ -152,18 +152,18 @@ begin "image".image_uuid, "image".image_mime_type, "image".image_thumb_file as image_file - from "image" + from sos."image" where "image".image_uuid = _image_uuid; end; $function$; -create or replace function public.create_category(_category_name text, _category_urlslug citext, _category_description text) - returns setof public.v_category +create or replace function sos.create_category(_category_name text, _category_urlslug citext, _category_description text) + returns setof sos.v_category language plpgsql as $function$ declare _category_uuid uuid; begin - insert into "category" ( + insert into sos."category" ( category_name, category_urlslug, category_description @@ -173,5 +173,5 @@ begin _category_description ) returning category_uuid into _category_uuid; - return query select * from v_category where category_uuid = _category_uuid; + return query select * from sos.v_category where category_uuid = _category_uuid; end; $function$; diff --git a/db/sql/4-permissions.sql b/db/sql/4-permissions.sql new file mode 100644 index 0000000..e78ce98 --- /dev/null +++ b/db/sql/4-permissions.sql @@ -0,0 +1,2 @@ +GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA sos TO sos; +GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA sos TO sos; \ No newline at end of file diff --git a/db/sql/setup.sh b/db/sql/setup.sh new file mode 100755 index 0000000..d422d82 --- /dev/null +++ b/db/sql/setup.sh @@ -0,0 +1,71 @@ +#!/bin/bash +prefix='\033[' +RED=$prefix'0;31m' +UNDIM=$prefix'22m' +RESET=$prefix'0m' + +REPODIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && cd ../.. && pwd )" + +source .env + +if [ -z "$DB_HOST" ]; then + echo "No DB_HOST found" + exit 1 +fi + +if [ -z "$DB_NAME" ]; then + echo "No DB_NAME found" + exit 1 +fi + +if [ -z "$DB_USER" ]; then + echo "No DB_USER found" + exit 1 +fi + +if [ -z "$DB_PASS" ]; then + echo "No DB_PASS found" + exit 1 +fi + +function handleError { + if [ $? -ne 0 ]; then + echo -e "$UNDIM$RESET" + echo -e $RED"ERROR:"$RESET $1 + exit 1 + fi +} + +function execFile { + echo " $1" + PGPASSWORD=$PG_PASS psql -v ON_ERROR_STOP=1 -h $DB_HOST -U postgres -d "$DB_NAME" -f $1 > /dev/null + handleError "Could not execute file $1" +} + +echo -e $RED"This will delete ALL DATA in the database $DB_NAME"$RESET +read -p "Continue? " + +if [[ $REPLY =~ ^[Yy]$ ]] +then + : +else + echo "Cancelling" + exit 1 +fi + +read -s -p "Enter postgres Password: " PG_PASS +echo + +PGPASSWORD=$PG_PASS psql -v ON_ERROR_STOP=1 -h $DB_HOST -U postgres -c "drop database if exists $DB_NAME" > /dev/null +handleError "Could not remove database $DB_NAME" + +PGPASSWORD=$PG_PASS psql -v ON_ERROR_STOP=1 -h $DB_HOST -U postgres -c "create database $DB_NAME" > /dev/null +handleError "Could not create database $DB_NAME" + +PGPASSWORD=$PG_PASS psql -v ON_ERROR_STOP=1 -h $DB_HOST -U postgres -c "drop user if exists $DB_USER" > /dev/null +PGPASSWORD=$PG_PASS psql -v ON_ERROR_STOP=1 -h $DB_HOST -U postgres -c "create user $DB_USER with encrypted password '$DB_PASS';" > /dev/null + + +for file in $REPODIR/db/sql/*.sql; do + execFile $file +done \ No newline at end of file