#!/bin/bash
set -e

cd /var/app

echo "Installing DBT dependencies ..."
/var/env/bin/dbt deps

echo "Run linters ..."
yamllint .
sqlfluff lint .

echo "Copying and seeing fixtures ..."
cp -r tests/fixtures seeds
/var/env/bin/dbt seed --target test --full-refresh


run_command="/var/env/bin/dbt run --target test --full-refresh --select ${DBT_SELECT_MODELS}"
if [ -n "${DBT_SELECT_MODELS}" ]; then
  echo "Refreshing models with run command: ${run_command}"
  eval "$run_command"
else
  echo "No models modified. Skipping refresh..."
fi

echo "Running tests ..."
/var/env/bin/dbt test --target test

echo "Removing fixture files ..."
rm -r seeds/fixtures
