@@ -475,7 +475,10 @@ let getPsqlExec = {
475
475
` ${binary} --version` ,
476
476
(~error , ~stdout as _ , ~stderr as _ ) => {
477
477
switch error {
478
- | Value (_ ) => resolve (Error ("Failed to find psql binary" ))
478
+ | Value (_ ) =>
479
+ resolve (
480
+ Error (` Please check if "psql" binary is installed or docker-compose is running for the local indexer.` ),
481
+ )
479
482
| Null => resolve (Ok (binary ))
480
483
}
481
484
},
@@ -496,20 +499,20 @@ let getPsqlExec = {
496
499
}
497
500
}
498
501
}
499
- let psqlExecMissingErrorMessage = ` Please check if "psql" binary is installed or docker-compose is running for the local indexer.`
500
502
501
503
let make = (
502
504
~sql : Postgres .sql ,
503
505
~pgHost ,
504
506
~pgSchema ,
507
+ ~pgPort ,
505
508
~pgUser ,
506
509
~pgDatabase ,
507
510
~pgPassword ,
508
511
~onInitialize = ?,
509
512
~onNewTables = ?,
510
513
): Persistence .storage => {
511
514
let psqlExecOptions : NodeJs .ChildProcess .execOptions = {
512
- env : Js .Dict .fromArray ([("PGPASSWORD" , pgPassword )]),
515
+ env : Js .Dict .fromArray ([("PGPASSWORD" , pgPassword ), ( "PATH" , % raw ( ` process . env . PATH ` ) ) ]),
513
516
}
514
517
515
518
let cacheDirPath = NodeJs .Path .resolve ([
@@ -709,73 +712,79 @@ let make = (
709
712
}
710
713
711
714
let dumpEffectCache = async () => {
712
- let cacheTableInfo : array <schemaCacheTableInfo > =
713
- (await sql
714
- -> Postgres .unsafe (makeSchemaCacheTableInfoQuery (~pgSchema )))
715
- -> Js .Array2 .filter (i => i .count > 0 )
716
-
717
- if cacheTableInfo -> Utils .Array .notEmpty {
718
- // Create .envio/cache directory if it doesn't exist
719
- try {
720
- await NodeJs .Fs .Promises .access (cacheDirPath )
721
- } catch {
722
- | _ =>
723
- // Create directory if it doesn't exist
724
- await NodeJs .Fs .Promises .mkdir (~path = cacheDirPath , ~options = {recursive : true })
725
- }
715
+ try {
716
+ let cacheTableInfo : array <schemaCacheTableInfo > =
717
+ (await sql
718
+ -> Postgres .unsafe (makeSchemaCacheTableInfoQuery (~pgSchema )))
719
+ -> Js .Array2 .filter (i => i .count > 0 )
720
+
721
+ if cacheTableInfo -> Utils .Array .notEmpty {
722
+ // Create .envio/cache directory if it doesn't exist
723
+ try {
724
+ await NodeJs .Fs .Promises .access (cacheDirPath )
725
+ } catch {
726
+ | _ =>
727
+ // Create directory if it doesn't exist
728
+ await NodeJs .Fs .Promises .mkdir (~path = cacheDirPath , ~options = {recursive : true })
729
+ }
726
730
727
- // Command for testing. Run from generated
728
- // docker-compose exec -T -u postgres envio-postgres psql -d envio-dev -c 'COPY "public"."envio_effect_getTokenMetadata" TO STDOUT (FORMAT text, HEADER);' > ../.envio/cache/getTokenMetadata.tsv
731
+ // Command for testing. Run from generated
732
+ // docker-compose exec -T -u postgres envio-postgres psql -d envio-dev -c 'COPY "public"."envio_effect_getTokenMetadata" TO STDOUT (FORMAT text, HEADER);' > ../.envio/cache/getTokenMetadata.tsv
729
733
730
- switch await getPsqlExec (~pgUser , ~pgHost ) {
731
- | Ok (psqlExec ) => {
732
- Logging .info (
733
- ` Dumping cache: ${cacheTableInfo
734
- -> Js.Array2.map(({tableName, count}) =>
735
- tableName ++ " (" ++ count->Belt.Int.toString ++ " rows)"
736
- )
737
- ->Js.Array2.joinWith(", ")}` ,
738
- )
734
+ switch await getPsqlExec (~pgUser , ~pgHost ) {
735
+ | Ok (psqlExec ) => {
736
+ Logging .info (
737
+ ` Dumping cache: ${cacheTableInfo
738
+ -> Js.Array2.map(({tableName, count}) =>
739
+ tableName ++ " (" ++ count->Belt.Int.toString ++ " rows)"
740
+ )
741
+ ->Js.Array2.joinWith(", ")}` ,
742
+ )
739
743
740
- let promises = cacheTableInfo -> Js .Array2 .map (async ({tableName }) => {
741
- let cacheName = tableName -> Js .String2 .sliceToEnd (~from = cacheTablePrefixLength )
742
- let outputFile =
743
- NodeJs .Path .join (cacheDirPath , cacheName ++ ".tsv" )-> NodeJs .Path .toString
744
-
745
- let command = ` ${psqlExec} -h ${pgHost} -U ${pgUser} -d ${pgDatabase} -c 'COPY "${pgSchema}"."${tableName}" TO STDOUT WITH (FORMAT text, HEADER);' > ${outputFile}`
746
-
747
- Promise .make ((resolve , reject ) => {
748
- NodeJs .ChildProcess .execWithOptions (
749
- command ,
750
- psqlExecOptions ,
751
- (~error , ~stdout , ~stderr as _ ) => {
752
- switch error {
753
- | Value (error ) => reject (error )
754
- | Null => resolve (stdout )
755
- }
756
- },
757
- )
744
+ let promises = cacheTableInfo -> Js .Array2 .map (async ({tableName }) => {
745
+ let cacheName = tableName -> Js .String2 .sliceToEnd (~from = cacheTablePrefixLength )
746
+ let outputFile =
747
+ NodeJs .Path .join (cacheDirPath , cacheName ++ ".tsv" )-> NodeJs .Path .toString
748
+
749
+ let command = ` ${psqlExec} -h ${pgHost} -p ${pgPort-> Js.Int.toString} -U ${pgUser} -d ${pgDatabase} -c 'COPY "${pgSchema}"."${tableName}" TO STDOUT WITH (FORMAT text, HEADER);' > ${outputFile}`
750
+
751
+ Promise .make ((resolve , reject ) => {
752
+ NodeJs .ChildProcess .execWithOptions (
753
+ command ,
754
+ psqlExecOptions ,
755
+ (~error , ~stdout , ~stderr as _ ) => {
756
+ switch error {
757
+ | Value (error ) => reject (error )
758
+ | Null => resolve (stdout )
759
+ }
760
+ },
761
+ )
762
+ })
758
763
})
759
- })
760
764
761
- let _ = await promises -> Promise .all
762
- Logging .info (` Successfully dumped cache to ${cacheDirPath-> NodeJs.Path.toString}` )
765
+ let _ = await promises -> Promise .all
766
+ Logging .info (` Successfully dumped cache to ${cacheDirPath-> NodeJs.Path.toString}` )
767
+ }
768
+ | Error (message ) => Logging .error (` Failed to dump cache. ${message}` )
763
769
}
764
- | Error (_ ) => Logging .error (` Failed to dump cache. ${psqlExecMissingErrorMessage}` )
765
770
}
771
+ } catch {
772
+ | exn => Logging .errorWithExn (exn -> Internal .prettifyExn , ` Failed to dump cache.` )
766
773
}
767
774
}
768
775
769
776
let restoreEffectCache = async (~withUpload ) => {
770
777
if withUpload {
771
778
// Try to restore cache tables from binary files
772
- let (entries , psqlExecResult ) = await Promise .all2 ((
773
- NodeJs .Fs .Promises .readdir (cacheDirPath ),
774
- getPsqlExec (~pgUser , ~pgHost ),
775
- ))
779
+ let nothingToUploadErrorMessage = "Nothing to upload."
776
780
777
- switch psqlExecResult {
778
- | Ok (psqlExec ) => {
781
+ switch await Promise .all2 ((
782
+ NodeJs .Fs .Promises .readdir (cacheDirPath )
783
+ -> Promise .thenResolve (e => Ok (e ))
784
+ -> Promise .catch (_ => Promise .resolve (Error (nothingToUploadErrorMessage ))),
785
+ getPsqlExec (~pgUser , ~pgHost ),
786
+ )) {
787
+ | (Ok (entries ), Ok (psqlExec )) => {
779
788
let cacheFiles = entries -> Js .Array2 .filter (entry => {
780
789
entry -> Js .String2 .endsWith (".tsv" )
781
790
})
@@ -799,7 +808,7 @@ let make = (
799
808
-> Promise .then (() => {
800
809
let inputFile = NodeJs .Path .join (cacheDirPath , entry )-> NodeJs .Path .toString
801
810
802
- let command = ` ${psqlExec} -h ${pgHost} -U ${pgUser} -d ${pgDatabase} -c 'COPY "${pgSchema}"."${tableName}" FROM STDIN WITH (FORMAT text, HEADER);' < ${inputFile}`
811
+ let command = ` ${psqlExec} -h ${pgHost} -p ${pgPort -> Js.Int.toString} - U ${pgUser} -d ${pgDatabase} -c 'COPY "${pgSchema}"."${tableName}" FROM STDIN WITH (FORMAT text, HEADER);' < ${inputFile}`
803
812
804
813
Promise .make (
805
814
(resolve , reject ) => {
@@ -819,17 +828,20 @@ let make = (
819
828
})
820
829
-> Promise .all
821
830
}
822
- | Error (_ ) =>
823
- Logging .error (
824
- ` Failed to restore cache, continuing without it. ${psqlExecMissingErrorMessage}` ,
825
- )
831
+ | (Error (message ), _ )
832
+ | (_ , Error (message )) =>
833
+ if message === nothingToUploadErrorMessage {
834
+ Logging .info ("No cache found to upload." )
835
+ } else {
836
+ Logging .error (` Failed to upload cache, continuing without it. ${message}` )
837
+ }
826
838
}
827
839
}
828
840
829
841
let cacheTableInfo : array <schemaCacheTableInfo > =
830
842
await sql -> Postgres .unsafe (makeSchemaCacheTableInfoQuery (~pgSchema ))
831
843
832
- if withUpload {
844
+ if withUpload && cacheTableInfo -> Utils . Array . notEmpty {
833
845
switch onNewTables {
834
846
| Some (onNewTables ) =>
835
847
await onNewTables (
0 commit comments