# To create the conda environment for the first time, do:

  mamba create -n fastspecfit-webapp python numpy scipy matplotlib astropy Django
  mamba activate fastspecfit-webapp
  pip install fitsio django-filter psycopg2
  tdir=$(mktemp -d)
  pushd $tdir
  for package in desiutil desitarget; do git clone https://github.com/desihub/$package && cd $package && python setup.py install && cd ..; done
  popd

# Then, subsequently, do:

module load python
mamba activate fastspecfit-webapp
export PYTHONPATH=/global/cfs/cdirs/desi/spectro/fastspecfit/webapp/py:$PYTHONPATH
export DJANGO_SETTINGS_MODULE=fastspecfit.webapp.settings
export POSTGRES_DB_PASSWORD=[insert-the-password-here]
cd /global/cfs/cdirs/desi/spectro/fastspecfit/webapp/py/fastspecfit/webapp

# The very first time do:
    python manage.py makemigrations fastmodel

# Subsequently (note that the first two commands are only needed if the
# pre-production database gets funky):

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete
python manage.py makemigrations
python manage.py migrate

# To perform a new database load do:

python manage.py dbshell
select count(*) from fastmodel_fastmodel;
\dt # list all tables
DELETE from fastmodel_fastmodel; # delete all rows from a table

# To totally delete the table you would do:
DROP TABLE fastmodel_fastmodel;  # delete the table itself

# Then do 
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete
python manage.py sqlmigrate fastmodel 0001_initial

# And then put the printed SQL into the dbshell to create an empty table which
# can then get loaded.

# Then load the new table with:

time python load.py
chgrp -R desi *
touch wsgi.py

