ambareesha7

ambareesha7

Need some clarity on shell-script for Docker setup on Phoeinx project

I found below shell-script(entrypoint.sh) from some GitHub Phoenix repo and I saw the same code in multiple Phoenix projects

#!/bin/bash

# Wait until postgres is ready

while ! pg_isready -q -h $PGHOST -p $PGPORT -U $PGUSER

do

echo "$(date) - waiting for database to start"

sleep 2

done


# Create migrate and seed database if it does't exist.

if [[ -z `psql -Atqc "\\list $PGDATABASE"` ]]; then

echo "Database $PGDATABASE does not exist. Creating..."

createdb -E UTF8 $PGDATABASE -l en_US.UTF-8 -T template0

mix ecto.migrate

mix run priv/repo/seeds.exs

echo "Database $PGDATABASE created."

fi

exec mix phx.server

the 1st block of code is clear it checks for Postgres readiness
but 2nd part I could not understand the if condition line
if [[ -z `psql -Atqc "\\list $PGDATABASE"` ]]; and I searched postgres document some online resources but I couldn’t find any reasonable explanation
and in my project I think that if fi block of code not excuted, then I added 3 lines of code to make it work

echo "$(date) - PostgreSQL is ready"
mix ecto.create
mix ecto.migrate

#!/bin/bash
# Docker entry point script.
# Wait until postgres is ready
while ! pg_isready -q -h $PGHOST -p $PGPORT -U $PGUSER
do
  echo "$(date) - waiting for database to start"
  sleep 2
done

# I added
echo "$(date) - PostgreSQL is ready"
mix ecto.create
mix ecto.migrate

# Create migrate and seed database if it does't exist.
if [[ -z `psql -Atqc "\\list $PGDATABASE"` ]]; then
  echo "Database $PGDATABASE does not exist. Creating..."
  createdb -E UTF8 $PGDATABASE -l en_US.UTF-8 -T template0
  mix ecto.migrate
  mix run priv/repo/seeds.exs
  echo "Database $PGDATABASE created."
fi
exec mix phx.server

What is the explanation for this if [[ -z `psql -Atqc "\\list $PGDATABASE"` ]]; then ?

thank you.

Most Liked

moogle19

moogle19

Like described in the comment above the if:
# Create migrate and seed database if it does't exist.

psql -Atqc "\\list $PGDATABASE" returns infos about the specified database (name, owner, …) and [[ -z ... ]] checks for an empty string.
Which means if the psql command doesn’t return infos, the commands inside the if block are executed.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

We're in Beta

About us Mission Statement