File tree Expand file tree Collapse file tree 3 files changed +32
-3
lines changed Expand file tree Collapse file tree 3 files changed +32
-3
lines changed Original file line number Diff line number Diff line change 1
1
services :
2
2
database :
3
+ container_name : " postgres-dev"
3
4
image : " postgres:16.0-alpine3.18"
4
5
env_file :
5
6
- ../.env.development
Original file line number Diff line number Diff line change
1
+ require ( "dotenv" ) . config ( { path : ".env.development" } ) ;
2
+
3
+ if ( ! process . env . POSTGRES_HOST ) {
4
+ throw new Error ( "Environment variable POSTGRES_HOST is not defined" ) ;
5
+ }
6
+
7
+ const { exec } = require ( "node:child_process" ) ;
8
+
9
+ function checkPostgres ( ) {
10
+ exec (
11
+ `docker exec postgres-dev pg_isready --host ${ process . env . POSTGRES_HOST } ` ,
12
+ handleReturn ,
13
+ ) ;
14
+
15
+ async function handleReturn ( error , stdout ) {
16
+ if ( stdout . search ( "accepting connections" ) === - 1 ) {
17
+ process . stdout . write ( "🟡" ) ;
18
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
19
+ return checkPostgres ( ) ;
20
+ }
21
+
22
+ console . log ( "🟢 PostgreSQL is ready!" ) ;
23
+ }
24
+ }
25
+
26
+ console . log ( "🔴 Waiting for PostgreSQL to become available..." ) ;
27
+ checkPostgres ( ) ;
Original file line number Diff line number Diff line change 4
4
"description" : " Clone of tabs news project" ,
5
5
"main" : " index.js" ,
6
6
"scripts" : {
7
- "dev" : " npm run services:up && next dev" ,
7
+ "dev" : " npm run services:up && npm run wait-for-postgres && npm run migration:up && next dev" ,
8
8
"services:up" : " docker compose -f infra/compose.yaml up -d" ,
9
9
"services:down" : " docker compose -f infra/compose.yaml down" ,
10
10
"services:stop" : " docker compose -f infra/compose.yaml stop" ,
13
13
"test" : " jest --runInBand" ,
14
14
"test:watch" : " jest --watchAll --runInBand" ,
15
15
"migration:create" : " node-pg-migrate --migrations-dir infra/migrations create" ,
16
- "migration:up" : " node-pg-migrate --migrations-dir infra/migrations --envPath .env.development up"
16
+ "migration:up" : " node-pg-migrate --migrations-dir infra/migrations --envPath .env.development up" ,
17
+ "wait-for-postgres" : " node infra/scripts/wait-for-postgres.js"
17
18
},
18
19
"author" : " " ,
19
20
"license" : " MIT" ,
29
30
"jest" : " ^29.6.2" ,
30
31
"prettier" : " ^3.3.3"
31
32
}
32
- }
33
+ }
You can’t perform that action at this time.
0 commit comments