@@ -68,45 +68,62 @@ func (ep *EmbeddedPostgres) Start() error {
6868 return err
6969 }
7070
71- cacheLocation , exists := ep .cacheLocator ()
72- if ! exists {
73- if err := ep .remoteFetchStrategy (); err != nil {
74- return err
75- }
71+ cacheLocation , cacheExists := ep .cacheLocator ()
72+
73+ if ep .config .runtimePath == "" {
74+ ep .config .runtimePath = filepath .Join (filepath .Dir (cacheLocation ), "extracted" )
75+ }
76+
77+ if ep .config .dataPath == "" {
78+ ep .config .dataPath = filepath .Join (ep .config .runtimePath , "data" )
7679 }
7780
78- binaryExtractLocation := userRuntimePathOrDefault (ep .config .runtimePath , cacheLocation )
79- if err := os .RemoveAll (binaryExtractLocation ); err != nil {
80- return fmt .Errorf ("unable to clean up runtime directory %s with error: %s" , binaryExtractLocation , err )
81+ if err := os .RemoveAll (ep .config .runtimePath ); err != nil {
82+ return fmt .Errorf ("unable to clean up runtime directory %s with error: %s" , ep .config .runtimePath , err )
8183 }
8284
83- if err := archiver .NewTarXz ().Unarchive (cacheLocation , binaryExtractLocation ); err != nil {
84- return fmt .Errorf ("unable to extract postgres archive %s to %s" , cacheLocation , binaryExtractLocation )
85+ if ep .config .binariesPath == "" {
86+ ep .config .binariesPath = ep .config .runtimePath
87+ }
88+
89+ _ , binDirErr := os .Stat (filepath .Join (ep .config .binariesPath , "bin" ))
90+ if os .IsNotExist (binDirErr ) {
91+ if ! cacheExists {
92+ if err := ep .remoteFetchStrategy (); err != nil {
93+ return err
94+ }
95+ }
96+
97+ if err := archiver .NewTarXz ().Unarchive (cacheLocation , ep .config .binariesPath ); err != nil {
98+ return fmt .Errorf ("unable to extract postgres archive %s to %s" , cacheLocation , ep .config .binariesPath )
99+ }
85100 }
86101
87- dataLocation := userDataPathOrDefault (ep .config .dataPath , binaryExtractLocation )
102+ if err := os .MkdirAll (ep .config .runtimePath , 0755 ); err != nil {
103+ return fmt .Errorf ("unable to create runtime directory %s with error: %s" , ep .config .runtimePath , err )
104+ }
88105
89- reuseData := ep .config .dataPath != "" && dataDirIsValid ( dataLocation , ep .config .version )
106+ reuseData := dataDirIsValid ( ep .config .dataPath , ep .config .version )
90107
91108 if ! reuseData {
92- if err := os .RemoveAll (dataLocation ); err != nil {
93- return fmt .Errorf ("unable to clean up data directory %s with error: %s" , dataLocation , err )
109+ if err := os .RemoveAll (ep . config . dataPath ); err != nil {
110+ return fmt .Errorf ("unable to clean up data directory %s with error: %s" , ep . config . dataPath , err )
94111 }
95112
96- if err := ep .initDatabase (binaryExtractLocation , dataLocation , ep .config .username , ep .config .password , ep .config .locale , ep .config .logger ); err != nil {
113+ if err := ep .initDatabase (ep . config . binariesPath , ep . config . runtimePath , ep . config . dataPath , ep .config .username , ep .config .password , ep .config .locale , ep .config .logger ); err != nil {
97114 return err
98115 }
99116 }
100117
101- if err := startPostgres (binaryExtractLocation , ep .config ); err != nil {
118+ if err := startPostgres (ep .config ); err != nil {
102119 return err
103120 }
104121
105122 ep .started = true
106123
107124 if ! reuseData {
108125 if err := ep .createDatabase (ep .config .port , ep .config .username , ep .config .password , ep .config .database ); err != nil {
109- if stopErr := stopPostgres (binaryExtractLocation , ep .config ); stopErr != nil {
126+ if stopErr := stopPostgres (ep .config ); stopErr != nil {
110127 return fmt .Errorf ("unable to stop database casused by error %s" , err )
111128 }
112129
@@ -115,7 +132,7 @@ func (ep *EmbeddedPostgres) Start() error {
115132 }
116133
117134 if err := healthCheckDatabaseOrTimeout (ep .config ); err != nil {
118- if stopErr := stopPostgres (binaryExtractLocation , ep .config ); stopErr != nil {
135+ if stopErr := stopPostgres (ep .config ); stopErr != nil {
119136 return fmt .Errorf ("unable to stop database casused by error %s" , err )
120137 }
121138
@@ -127,13 +144,11 @@ func (ep *EmbeddedPostgres) Start() error {
127144
128145// Stop will try to stop the Postgres process gracefully returning an error when there were any problems.
129146func (ep * EmbeddedPostgres ) Stop () error {
130- cacheLocation , exists := ep .cacheLocator ()
131- if ! exists || ! ep .started {
147+ if ! ep .started {
132148 return errors .New ("server has not been started" )
133149 }
134150
135- binaryExtractLocation := userRuntimePathOrDefault (ep .config .runtimePath , cacheLocation )
136- if err := stopPostgres (binaryExtractLocation , ep .config ); err != nil {
151+ if err := stopPostgres (ep .config ); err != nil {
137152 return err
138153 }
139154
@@ -142,10 +157,10 @@ func (ep *EmbeddedPostgres) Stop() error {
142157 return nil
143158}
144159
145- func startPostgres (binaryExtractLocation string , config Config ) error {
146- postgresBinary := filepath .Join (binaryExtractLocation , "bin/pg_ctl" )
160+ func startPostgres (config Config ) error {
161+ postgresBinary := filepath .Join (config . binariesPath , "bin/pg_ctl" )
147162 postgresProcess := exec .Command (postgresBinary , "start" , "-w" ,
148- "-D" , userDataPathOrDefault ( config .dataPath , binaryExtractLocation ) ,
163+ "-D" , config .dataPath ,
149164 "-o" , fmt .Sprintf (`"-p %d"` , config .port ))
150165 postgresProcess .Stderr = config .logger
151166 postgresProcess .Stdout = config .logger
@@ -157,10 +172,10 @@ func startPostgres(binaryExtractLocation string, config Config) error {
157172 return nil
158173}
159174
160- func stopPostgres (binaryExtractLocation string , config Config ) error {
161- postgresBinary := filepath .Join (binaryExtractLocation , "bin/pg_ctl" )
175+ func stopPostgres (config Config ) error {
176+ postgresBinary := filepath .Join (config . binariesPath , "bin/pg_ctl" )
162177 postgresProcess := exec .Command (postgresBinary , "stop" , "-w" ,
163- "-D" , userDataPathOrDefault ( config .dataPath , binaryExtractLocation ) )
178+ "-D" , config .dataPath )
164179 postgresProcess .Stderr = config .logger
165180 postgresProcess .Stdout = config .logger
166181
@@ -180,22 +195,6 @@ func ensurePortAvailable(port uint32) error {
180195 return nil
181196}
182197
183- func userRuntimePathOrDefault (userLocation , cacheLocation string ) string {
184- if userLocation != "" {
185- return userLocation
186- }
187-
188- return filepath .Join (filepath .Dir (cacheLocation ), "extracted" )
189- }
190-
191- func userDataPathOrDefault (userLocation , runtimeLocation string ) string {
192- if userLocation != "" {
193- return userLocation
194- }
195-
196- return filepath .Join (runtimeLocation , "data" )
197- }
198-
199198func dataDirIsValid (dataDir string , version PostgresVersion ) bool {
200199 pgVersion := filepath .Join (dataDir , "PG_VERSION" )
201200
0 commit comments