Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyShuii committed Feb 7, 2025
1 parent 4cc945c commit 941cf99
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
15 changes: 14 additions & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,18 @@ export type Context = {

const start = async () => {
await redisClient.connect();
await dataSource.initialize();

let connected = false;
while (!connected) {
try {
await dataSource.initialize();
console.log("Connected to the database");
connected = true;
} catch (error) {
console.log("Database connection failed, retrying...", error);
await new Promise(resolve => setTimeout(resolve, 5000));
}
}

const schema = await buildSchema({
resolvers: [
Expand Down Expand Up @@ -78,6 +89,8 @@ const start = async () => {
},
});

console.log("database connected 2");

const app = express();
const httpServer = http.createServer(app);

Expand Down
32 changes: 22 additions & 10 deletions db/init_db.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
#!/bin/bash

echo "Attente de PostgreSQL avant de procéder à l'importation..."
# Démarre PostgreSQL si ce n'est pas déjà fait
echo "Starting PostgreSQL..."
pg_ctl -D /var/lib/postgresql/data -l /var/log/postgresql.log start &

# Attend que PostgreSQL soit prêt
echo "Waiting for PostgreSQL before proceeding with the import..."
until pg_isready -U postgres; do
echo "Attente de PostgreSQL..."
echo "Waiting for PostgreSQL..."
sleep 2
done

psql -U postgres -d postgres -c "DROP DATABASE IF EXISTS \"wild-transfer\";"
psql -U postgres -d postgres -c "CREATE DATABASE \"wild-transfer\";"
# Check if the database already exists
DB_EXIST=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname='wild-transfer'")

if [ "$DB_EXIST" != "1" ]; then
echo "The database 'wild-transfer' does not exist, creating it..."
psql -U postgres -d postgres -c "CREATE DATABASE \"wild-transfer\";"

LATEST_DUMP=$(ls -t '/dumps' | head -n 1)
if [ -z "$LATEST_DUMP" ]; then
echo "Aucun fichier dump trouvé dans '/dumps'"
exit 1
# Restore the dump only if the database has just been created
LATEST_DUMP=$(ls -t '/dumps' | head -n 1)
if [ -z "$LATEST_DUMP" ]; then
echo "No dump file found in '/dumps'. The database remains empty."
else
echo "Restoring dump file $LATEST_DUMP into the 'wild-transfer' database..."
psql -U postgres -d "wild-transfer" < "/dumps/$LATEST_DUMP"
fi
else
echo "Restaurer le fichier dump $LATEST_DUMP dans la base de données 'wild-transfer'"
psql -U postgres -d "wild-transfer" < "/dumps/$LATEST_DUMP"
echo "The database 'wild-transfer' already exists. No changes made."
fi
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ services:
build: ./db
container_name: db
healthcheck:
test: ["CMD-SHELL", "pg_isready -d postgres -U postgres"]
test: ["CMD", "pg_isready", "-d", "wild-transfer", "-U", "postgres"]
interval: 1s
timeout: 2s
retries: 100
environment:
POSTGRES_PASSWORD: postgres
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- ./postgres-data/data:/var/lib/postgresql/data
- ./db/init_db.sh:/docker-entrypoint-initdb.d/init.sh
- ./db/postgres-data/data:/var/lib/postgresql/data
- ./db/dumps:/dumps
env_file:
- .env
Expand Down
2 changes: 1 addition & 1 deletion init.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE DATABASE "wild-transfer" NOT EXISTS
CREATE DATABASE "wild-transfer"

0 comments on commit 941cf99

Please sign in to comment.