Skip to content

Commit

Permalink
Add migration adding vcl_rcv config to a table
Browse files Browse the repository at this point in the history
  • Loading branch information
eest committed Dec 6, 2024
1 parent a8a683f commit 95b009a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/server/testdata/migrations/00001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ CREATE TABLE service_origins (
tls boolean DEFAULT true NOT NULL,
UNIQUE(service_version_id, host)
);

CREATE TABLE service_vcl_rcv (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
ts timestamptz NOT NULL DEFAULT now(),
service_version_id uuid NOT NULL REFERENCES service_versions(id),
content text NOT NULL CONSTRAINT non_empty CHECK(length(content)>0),
UNIQUE(service_version_id, content)
);
-- +goose down
DROP TABLE service_domains;
DROP TABLE service_origins;
Expand Down
61 changes: 61 additions & 0 deletions pkg/server/testdata/migrations/00007_add_vcl_rcv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package migrations

import (
"context"
"database/sql"
"io/ioutil"

"github.com/jackc/pgx/v5/pgtype"
"github.com/pressly/goose/v3"
)

type vclRcv struct {
id string
file string
serviceVersionID string
}

func init() {
goose.AddMigrationContext(upAddVclRcv, downAddVclRcv)
}

func upAddVclRcv(ctx context.Context, tx *sql.Tx) error {
// This code is executed when the migration is applied.

vclRcvs := []vclRcv{
{
id: "00000000-0000-0000-0000-000000000028",
serviceVersionID: "00000000-0000-0000-0000-000000000015",
file: "testdata/vcl/vcl_rcv/content1.vcl",
},
}

for _, vclRcv := range vclRcvs {
var vclID, serviceVersionID pgtype.UUID
err := vclID.Scan(vclRcv.id)
if err != nil {
return err
}

err = serviceVersionID.Scan(vclRcv.serviceVersionID)
if err != nil {
return err
}

contentBytes, err := ioutil.ReadFile(vclRcv.file)
if err != nil {
return err
}

_, err = tx.Exec("INSERT INTO service_vcl_rcv (id, service_version_id, content) VALUES($1, $2, $3)", vclID, serviceVersionID, contentBytes)
if err != nil {
return err
}
}
return nil
}

func downAddVclRcv(ctx context.Context, tx *sql.Tx) error {
// This code is executed when the migration is rolled back.
return nil
}
7 changes: 7 additions & 0 deletions pkg/server/testdata/vcl/vcl_rcv/content1.vcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The usage of the proxy module is possible because haproxy is configured
# to set PROXY SSL headers for us.
if (proxy.is_ssl()) {
std.syslog(180, "vcl_rcv: this is https");
} else {
std.syslog(180, "vcl_rcv: this is http");
}

0 comments on commit 95b009a

Please sign in to comment.