Skip to content

Commit a12c615

Browse files
suristerwilliamhbaker
authored andcommitted
materialize-cratedb: Remove temp tables
1 parent 2a5da58 commit a12c615

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

materialize-cratedb/driver.go

+25
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ func newTransactor(
277277
if d.load.conn, err = pgx.Connect(ctx, cfg.ToURI()); err != nil {
278278
return nil, nil, fmt.Errorf("load pgx.Connect: %w", err)
279279
}
280+
280281
if d.store.conn, err = pgx.Connect(ctx, cfg.ToURI()); err != nil {
281282
return nil, nil, fmt.Errorf("store pgx.Connect: %w", err)
282283
}
@@ -290,6 +291,11 @@ func newTransactor(
290291
}
291292

292293
for _, binding := range bindings {
294+
// Make sure that the binding does not exist before creating it.
295+
if err = d.removeBinding(ctx, binding); err != nil {
296+
return nil, nil, fmt.Errorf("remove binding: %w", err)
297+
}
298+
293299
if err = d.addBinding(ctx, binding, is); err != nil {
294300
return nil, nil, fmt.Errorf("addBinding of %s: %w", binding.Path, err)
295301
}
@@ -318,6 +324,18 @@ type binding struct {
318324
loadQuerySQL string
319325
}
320326

327+
func (t *transactor) removeBinding(ctx context.Context, target sql.Table) error {
328+
var b = &binding{target: target}
329+
var w strings.Builder
330+
331+
if err := tplDropLoadTable.Execute(&w, &b.target); err != nil {
332+
return fmt.Errorf("executing dropLoadTable template: %w", err)
333+
} else if _, err := t.load.conn.Exec(ctx, w.String()); err != nil {
334+
return fmt.Errorf("Exec(%s): %w", w.String(), err)
335+
}
336+
return nil
337+
}
338+
321339
func (t *transactor) addBinding(ctx context.Context, target sql.Table, is *boilerplate.InfoSchema) error {
322340
var b = &binding{target: target}
323341

@@ -519,6 +537,13 @@ func (d *transactor) Store(it *m.StoreIterator) (_ m.StartCommitFunc, err error)
519537
}
520538

521539
func (d *transactor) Destroy() {
540+
for _, b := range d.bindings {
541+
err := d.removeBinding(context.Background(), b.target)
542+
if err != nil {
543+
return
544+
}
545+
}
546+
522547
d.load.conn.Close(context.Background())
523548
d.store.conn.Close(context.Background())
524549
}

materialize-cratedb/sqlgen.go

+6
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ CREATE TABLE {{ template "temp_name" . }} (
170170
);
171171
{{ end }}
172172
173+
-- Templated deletion of a temporary load table:
174+
{{ define "dropLoadTable" }}
175+
DROP TABLE IF EXISTS {{ template "temp_name" . }};
176+
{{ end }}
177+
173178
-- Templated insertion into the temporary load table:
174179
175180
{{ define "loadInsert" }}
@@ -296,6 +301,7 @@ UPDATE {{ Identifier $.TablePath }}
296301
AND fence = {{ $.Fence }};
297302
{{ end }}
298303
`)
304+
tplDropLoadTable = tplAll.Lookup("dropLoadTable")
299305
tplCreateLoadTable = tplAll.Lookup("createLoadTable")
300306
tplCreateTargetTable = tplAll.Lookup("createTargetTable")
301307
tplAlterTableColumns = tplAll.Lookup("alterTableColumns")

0 commit comments

Comments
 (0)