#!/usr/bin/env bash
set -e

SQL_DIR="/docker-entrypoint-initdb.d/sql"

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f "$SQL_DIR/common/update_updated_at_column.sql"

ordered=(
  users/users.sql
  users/user_follows.sql
  users/user_preferences.sql
  users/user_verifications.sql
  game/games.sql
  player/players.sql
  player/player_services.sql
  shop/shop.sql
  shop/shop_players.sql
  shop/shop_invitations.sql
  order/00-orders.sql
  order/order_state_log.sql
  wallet/00-wallets.sql
  wallet/wallet_transactions.sql
  community/posts.sql
  community/comments.sql
  community/post_likes.sql
  community/comment_likes.sql
  dispute/disputes.sql
  dispute/dispute_timeline.sql
  review/reviews.sql
  search/favorites.sql
)

for f in "${ordered[@]}"; do
  [ -f "$SQL_DIR/$f" ] || continue
  psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f "$SQL_DIR/$f"
done
