From 61f2cde6904b936bbeb1244ff9043691b04af41a Mon Sep 17 00:00:00 2001
From: Fabio Manganiello <fabio@manganiello.tech>
Date: Thu, 6 Mar 2025 23:51:12 +0100
Subject: [PATCH] Sync db tables before initializing the constraints.

---
 src/db/Db.ts | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/src/db/Db.ts b/src/db/Db.ts
index eabdd83..55f3aeb 100644
--- a/src/db/Db.ts
+++ b/src/db/Db.ts
@@ -112,20 +112,15 @@ class Db {
   public async sync() {
     console.log('Syncing databases');
 
-    const gpsData = this.GPSData();
-    const role = this.Role();
-    const user = this.User();
-    const userRole = this.UserRole();
-    const userSession = this.UserSession();
+    for (const modelName of ['GPSData', 'Role', 'User', 'UserRole', 'UserSession']) {
+      const model = (this as any)[modelName]();
+      process.stdout.write(`  [⌛] Syncing ${model.name}`);
+      await model.sync();
+      await this.appDb.sync();
+      process.stdout.write(`\r  [✅] Synced ${model.name} \n`);
+    }
+
     this.initConstraints();
-
-    await gpsData.sync();
-    await role.sync();
-    await user.sync();
-    await userRole.sync();
-    await userSession.sync();
-
-    await this.appDb.sync();
     console.log('Database sync completed');
   }