From a15bc02500ad2c8056fea32369310a8cc075dc8c Mon Sep 17 00:00:00 2001
From: Jure Triglav <juretriglav@gmail.com>
Date: Mon, 27 Jul 2020 01:33:38 +0200
Subject: [PATCH] fix: use a different method for waiting on db readiness

---
 scripts/clearAndSeed.js | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/scripts/clearAndSeed.js b/scripts/clearAndSeed.js
index bdcdf51da9..bca6157e8c 100644
--- a/scripts/clearAndSeed.js
+++ b/scripts/clearAndSeed.js
@@ -30,17 +30,31 @@ const clearDb = async () => {
 }
 
 const seed = async dumpSql => {
+  let ready
+
   if (dumpSql) {
     await clearDb()
     await db.raw(dumpSql)
     logger.info('Cleared the database and restored from dump')
-    // TODO: This wait is necessary for the database to "settle".
-    await wait(2000)
-    return true
+  } else {
+    await createTables(true)
   }
 
-  await createTables(true)
+  // TODO: This wait is necessary for the database to "settle".
+  while (!ready) {
+    // eslint-disable-next-line
+    const { rows } = await db.raw(`SELECT EXISTS (
+      SELECT FROM information_schema.tables
+      WHERE  table_schema = 'public'
+      AND    table_name   = 'users'
+    );`)
+    if (rows && rows[0] && rows[0].exists) {
+      ready = true
+    }
 
+    // eslint-disable-next-line
+    await wait(1000)
+  }
   return true
 }
 
-- 
GitLab