@@ -16,13 +16,16 @@ import (
16
16
"github.com/docker/docker/client"
17
17
"github.com/docker/docker/pkg/stdcopy"
18
18
19
+ "gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools"
19
20
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/cont"
20
21
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
21
22
"gitlab.com/postgres-ai/database-lab/v3/pkg/models"
22
23
"gitlab.com/postgres-ai/database-lab/v3/pkg/util"
23
24
"gitlab.com/postgres-ai/database-lab/v3/pkg/util/networks"
24
25
)
25
26
27
+ const schemaDiffImage = "supabase/pgadmin-schema-diff"
28
+
26
29
// Diff defines a schema generator.
27
30
type Diff struct {
28
31
cli * client.Client
@@ -34,6 +37,7 @@ func NewDiff(cli *client.Client) *Diff {
34
37
}
35
38
36
39
func connStr (clone * models.Clone ) string {
40
+ // TODO: fix params
37
41
return fmt .Sprintf ("postgres://%s:%s@%s:%s/%s" ,
38
42
clone .DB .Username ,
39
43
"test" , // clone.DB.Password,
@@ -50,18 +54,13 @@ func (d *Diff) GenerateDiff(actual, origin *models.Clone, instanceID string) (st
50
54
51
55
ctx := context .Background ()
52
56
53
- if _ , err := d .watchCloneStatus (ctx , origin , origin .Status .Code ); err != nil {
54
- return "" , fmt .Errorf ("failed to watch the clone status: %w" , err )
55
- }
56
-
57
- reader , err := d .cli .ImagePull (ctx , "supabase/pgadmin-schema-diff" , types.ImagePullOptions {})
58
- if err != nil {
59
- return "" , fmt .Errorf ("failed to pull image: %w" , err )
57
+ if origin .Status .Code != models .StatusOK {
58
+ if _ , err := d .watchCloneStatus (ctx , origin , origin .Status .Code ); err != nil {
59
+ return "" , fmt .Errorf ("failed to watch the clone status: %w" , err )
60
+ }
60
61
}
61
62
62
- defer func () { _ = reader .Close () }()
63
-
64
- if _ , err := io .Copy (os .Stdout , reader ); err != nil && err != io .EOF {
63
+ if err := tools .PullImage (ctx , d .cli , schemaDiffImage ); err != nil {
65
64
return "" , fmt .Errorf ("failed to pull image: %w" , err )
66
65
}
67
66
@@ -70,9 +69,8 @@ func (d *Diff) GenerateDiff(actual, origin *models.Clone, instanceID string) (st
70
69
Labels : map [string ]string {
71
70
cont .DBLabControlLabel : cont .DBLabSchemaDiff ,
72
71
cont .DBLabInstanceIDLabel : instanceID ,
73
- // cont.DBLabEngineNameLabel: d.engineProps.ContainerName,
74
72
},
75
- Image : "supabase/pgadmin-schema-diff" ,
73
+ Image : schemaDiffImage ,
76
74
Cmd : []string {
77
75
connStr (actual ),
78
76
connStr (origin ),
@@ -96,6 +94,15 @@ func (d *Diff) GenerateDiff(actual, origin *models.Clone, instanceID string) (st
96
94
return "" , fmt .Errorf ("failed to create diff container: %w" , err )
97
95
}
98
96
97
+ defer func () {
98
+ if err := d .cli .ContainerRemove (ctx , diffCont .ID , types.ContainerRemoveOptions {
99
+ RemoveVolumes : true ,
100
+ Force : true ,
101
+ }); err != nil {
102
+ log .Err ("failed to remove the diff container:" , diffCont .ID , err )
103
+ }
104
+ }()
105
+
99
106
statusCh , errCh := d .cli .ContainerWait (ctx , diffCont .ID , container .WaitConditionNotRunning )
100
107
select {
101
108
case err := <- errCh :
@@ -117,12 +124,12 @@ func (d *Diff) GenerateDiff(actual, origin *models.Clone, instanceID string) (st
117
124
return "" , fmt .Errorf ("failed to copy container output: %w" , err )
118
125
}
119
126
120
- stringsB , err := filterOutput (buf )
127
+ filteredOutput , err := filterOutput (buf )
121
128
if err != nil {
122
129
return "" , fmt .Errorf ("failed to filter output: %w" , err )
123
130
}
124
131
125
- return stringsB .String (), nil
132
+ return filteredOutput .String (), nil
126
133
}
127
134
128
135
// watchCloneStatus checks the clone status for changing.
@@ -157,13 +164,13 @@ func (d *Diff) watchCloneStatus(ctx context.Context, clone *models.Clone, initia
157
164
}
158
165
159
166
func filterOutput (b * bytes.Buffer ) (* strings.Builder , error ) {
160
- strB := & strings.Builder {}
167
+ filteredBuilder := & strings.Builder {}
161
168
162
169
for {
163
170
line , err := b .ReadBytes ('\n' )
164
171
if err != nil {
165
172
if err == io .EOF {
166
- return strB , nil
173
+ return filteredBuilder , nil
167
174
}
168
175
169
176
return nil , err
@@ -173,6 +180,6 @@ func filterOutput(b *bytes.Buffer) (*strings.Builder, error) {
173
180
continue
174
181
}
175
182
176
- strB .Write (line )
183
+ filteredBuilder .Write (line )
177
184
}
178
185
}
0 commit comments