@@ -68,45 +68,62 @@ func (ep *EmbeddedPostgres) Start() error {
68
68
return err
69
69
}
70
70
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" )
76
79
}
77
80
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 )
81
83
}
82
84
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
+ }
85
100
}
86
101
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
+ }
88
105
89
- reuseData := ep .config .dataPath != "" && dataDirIsValid ( dataLocation , ep .config .version )
106
+ reuseData := dataDirIsValid ( ep .config .dataPath , ep .config .version )
90
107
91
108
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 )
94
111
}
95
112
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 {
97
114
return err
98
115
}
99
116
}
100
117
101
- if err := startPostgres (binaryExtractLocation , ep .config ); err != nil {
118
+ if err := startPostgres (ep .config ); err != nil {
102
119
return err
103
120
}
104
121
105
122
ep .started = true
106
123
107
124
if ! reuseData {
108
125
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 {
110
127
return fmt .Errorf ("unable to stop database casused by error %s" , err )
111
128
}
112
129
@@ -115,7 +132,7 @@ func (ep *EmbeddedPostgres) Start() error {
115
132
}
116
133
117
134
if err := healthCheckDatabaseOrTimeout (ep .config ); err != nil {
118
- if stopErr := stopPostgres (binaryExtractLocation , ep .config ); stopErr != nil {
135
+ if stopErr := stopPostgres (ep .config ); stopErr != nil {
119
136
return fmt .Errorf ("unable to stop database casused by error %s" , err )
120
137
}
121
138
@@ -127,13 +144,11 @@ func (ep *EmbeddedPostgres) Start() error {
127
144
128
145
// Stop will try to stop the Postgres process gracefully returning an error when there were any problems.
129
146
func (ep * EmbeddedPostgres ) Stop () error {
130
- cacheLocation , exists := ep .cacheLocator ()
131
- if ! exists || ! ep .started {
147
+ if ! ep .started {
132
148
return errors .New ("server has not been started" )
133
149
}
134
150
135
- binaryExtractLocation := userRuntimePathOrDefault (ep .config .runtimePath , cacheLocation )
136
- if err := stopPostgres (binaryExtractLocation , ep .config ); err != nil {
151
+ if err := stopPostgres (ep .config ); err != nil {
137
152
return err
138
153
}
139
154
@@ -142,10 +157,10 @@ func (ep *EmbeddedPostgres) Stop() error {
142
157
return nil
143
158
}
144
159
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" )
147
162
postgresProcess := exec .Command (postgresBinary , "start" , "-w" ,
148
- "-D" , userDataPathOrDefault ( config .dataPath , binaryExtractLocation ) ,
163
+ "-D" , config .dataPath ,
149
164
"-o" , fmt .Sprintf (`"-p %d"` , config .port ))
150
165
postgresProcess .Stderr = config .logger
151
166
postgresProcess .Stdout = config .logger
@@ -157,10 +172,10 @@ func startPostgres(binaryExtractLocation string, config Config) error {
157
172
return nil
158
173
}
159
174
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" )
162
177
postgresProcess := exec .Command (postgresBinary , "stop" , "-w" ,
163
- "-D" , userDataPathOrDefault ( config .dataPath , binaryExtractLocation ) )
178
+ "-D" , config .dataPath )
164
179
postgresProcess .Stderr = config .logger
165
180
postgresProcess .Stdout = config .logger
166
181
@@ -180,22 +195,6 @@ func ensurePortAvailable(port uint32) error {
180
195
return nil
181
196
}
182
197
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
-
199
198
func dataDirIsValid (dataDir string , version PostgresVersion ) bool {
200
199
pgVersion := filepath .Join (dataDir , "PG_VERSION" )
201
200
0 commit comments