Skip to content

Commit c65c7c6

Browse files
committed
simpler
1 parent adba0ff commit c65c7c6

File tree

2 files changed

+35
-44
lines changed

2 files changed

+35
-44
lines changed

dbos/migrations/1_initial_dbos_schema.sql.tmpl renamed to dbos/migrations/1_initial_dbos_schema.sql

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
33

44
-- Create the schema if it doesn't exist
5-
CREATE SCHEMA IF NOT EXISTS {{.Schema}};
5+
CREATE SCHEMA IF NOT EXISTS %s;
66

7-
CREATE TABLE {{.Schema}}.workflow_status (
7+
CREATE TABLE %s.workflow_status (
88
workflow_uuid TEXT PRIMARY KEY,
99
status TEXT,
1010
name TEXT,
@@ -31,39 +31,39 @@ CREATE TABLE {{.Schema}}.workflow_status (
3131
priority INTEGER NOT NULL DEFAULT 0
3232
);
3333

34-
CREATE INDEX workflow_status_created_at_index ON {{.Schema}}.workflow_status (created_at);
35-
CREATE INDEX workflow_status_executor_id_index ON {{.Schema}}.workflow_status (executor_id);
36-
CREATE INDEX workflow_status_status_index ON {{.Schema}}.workflow_status (status);
34+
CREATE INDEX workflow_status_created_at_index ON %s.workflow_status (created_at);
35+
CREATE INDEX workflow_status_executor_id_index ON %s.workflow_status (executor_id);
36+
CREATE INDEX workflow_status_status_index ON %s.workflow_status (status);
3737

38-
ALTER TABLE {{.Schema}}.workflow_status
38+
ALTER TABLE %s.workflow_status
3939
ADD CONSTRAINT uq_workflow_status_queue_name_dedup_id
4040
UNIQUE (queue_name, deduplication_id);
4141

42-
CREATE TABLE {{.Schema}}.operation_outputs (
42+
CREATE TABLE %s.operation_outputs (
4343
workflow_uuid TEXT NOT NULL,
4444
function_id INTEGER NOT NULL,
4545
function_name TEXT NOT NULL DEFAULT '',
4646
output TEXT,
4747
error TEXT,
4848
child_workflow_id TEXT,
4949
PRIMARY KEY (workflow_uuid, function_id),
50-
FOREIGN KEY (workflow_uuid) REFERENCES {{.Schema}}.workflow_status(workflow_uuid)
50+
FOREIGN KEY (workflow_uuid) REFERENCES %s.workflow_status(workflow_uuid)
5151
ON UPDATE CASCADE ON DELETE CASCADE
5252
);
5353

54-
CREATE TABLE {{.Schema}}.notifications (
54+
CREATE TABLE %s.notifications (
5555
destination_uuid TEXT NOT NULL,
5656
topic TEXT,
5757
message TEXT NOT NULL,
5858
created_at_epoch_ms BIGINT NOT NULL DEFAULT (EXTRACT(epoch FROM now()) * 1000::numeric)::bigint,
5959
message_uuid TEXT NOT NULL DEFAULT gen_random_uuid(), -- Built-in function
60-
FOREIGN KEY (destination_uuid) REFERENCES {{.Schema}}.workflow_status(workflow_uuid)
60+
FOREIGN KEY (destination_uuid) REFERENCES %s.workflow_status(workflow_uuid)
6161
ON UPDATE CASCADE ON DELETE CASCADE
6262
);
63-
CREATE INDEX idx_workflow_topic ON {{.Schema}}.notifications (destination_uuid, topic);
63+
CREATE INDEX idx_workflow_topic ON %s.notifications (destination_uuid, topic);
6464

6565
-- Create notification function
66-
CREATE OR REPLACE FUNCTION {{.Schema}}.notifications_function() RETURNS TRIGGER AS $$
66+
CREATE OR REPLACE FUNCTION %s.notifications_function() RETURNS TRIGGER AS $$
6767
DECLARE
6868
payload text := NEW.destination_uuid || '::' || NEW.topic;
6969
BEGIN
@@ -74,20 +74,20 @@ $$ LANGUAGE plpgsql;
7474

7575
-- Create notification trigger
7676
CREATE TRIGGER dbos_notifications_trigger
77-
AFTER INSERT ON {{.Schema}}.notifications
78-
FOR EACH ROW EXECUTE FUNCTION {{.Schema}}.notifications_function();
77+
AFTER INSERT ON %s.notifications
78+
FOR EACH ROW EXECUTE FUNCTION %s.notifications_function();
7979

80-
CREATE TABLE {{.Schema}}.workflow_events (
80+
CREATE TABLE %s.workflow_events (
8181
workflow_uuid TEXT NOT NULL,
8282
key TEXT NOT NULL,
8383
value TEXT NOT NULL,
8484
PRIMARY KEY (workflow_uuid, key),
85-
FOREIGN KEY (workflow_uuid) REFERENCES {{.Schema}}.workflow_status(workflow_uuid)
85+
FOREIGN KEY (workflow_uuid) REFERENCES %s.workflow_status(workflow_uuid)
8686
ON UPDATE CASCADE ON DELETE CASCADE
8787
);
8888

8989
-- Create events function
90-
CREATE OR REPLACE FUNCTION {{.Schema}}.workflow_events_function() RETURNS TRIGGER AS $$
90+
CREATE OR REPLACE FUNCTION %s.workflow_events_function() RETURNS TRIGGER AS $$
9191
DECLARE
9292
payload text := NEW.workflow_uuid || '::' || NEW.key;
9393
BEGIN
@@ -98,20 +98,20 @@ $$ LANGUAGE plpgsql;
9898

9999
-- Create events trigger
100100
CREATE TRIGGER dbos_workflow_events_trigger
101-
AFTER INSERT ON {{.Schema}}.workflow_events
102-
FOR EACH ROW EXECUTE FUNCTION {{.Schema}}.workflow_events_function();
101+
AFTER INSERT ON %s.workflow_events
102+
FOR EACH ROW EXECUTE FUNCTION %s.workflow_events_function();
103103

104-
CREATE TABLE {{.Schema}}.streams (
104+
CREATE TABLE %s.streams (
105105
workflow_uuid TEXT NOT NULL,
106106
key TEXT NOT NULL,
107107
value TEXT NOT NULL,
108108
"offset" INTEGER NOT NULL,
109109
PRIMARY KEY (workflow_uuid, key, "offset"),
110-
FOREIGN KEY (workflow_uuid) REFERENCES {{.Schema}}.workflow_status(workflow_uuid)
110+
FOREIGN KEY (workflow_uuid) REFERENCES %s.workflow_status(workflow_uuid)
111111
ON UPDATE CASCADE ON DELETE CASCADE
112112
);
113113

114-
CREATE TABLE {{.Schema}}.event_dispatch_kv (
114+
CREATE TABLE %s.event_dispatch_kv (
115115
service_name TEXT NOT NULL,
116116
workflow_fn_name TEXT NOT NULL,
117117
key TEXT NOT NULL,

dbos/system_database.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dbos
22

33
import (
4-
"bytes"
54
"context"
65
_ "embed"
76
"errors"
@@ -11,7 +10,6 @@ import (
1110
"math/rand"
1211
"strings"
1312
"sync"
14-
"text/template"
1513
"time"
1614

1715
"github.com/google/uuid"
@@ -121,8 +119,8 @@ func createDatabaseIfNotExists(ctx context.Context, databaseURL string, logger *
121119
return nil
122120
}
123121

124-
//go:embed migrations/1_initial_dbos_schema.sql.tmpl
125-
var migration1Template string
122+
//go:embed migrations/1_initial_dbos_schema.sql
123+
var migration1SQL string
126124

127125
type migrationFile struct {
128126
version int64
@@ -149,25 +147,18 @@ const (
149147
)
150148

151149
func runMigrations(databaseURL string, schema string) error {
152-
// Process the migration template with the schema
153-
tmpl, err := template.New("migration").Parse(migration1Template)
154-
if err != nil {
155-
return fmt.Errorf("failed to parse migration template: %v", err)
156-
}
157-
158-
var migrationSQL bytes.Buffer
159-
data := struct {
160-
Schema string
161-
}{
162-
Schema: schema,
163-
}
164-
if err := tmpl.Execute(&migrationSQL, data); err != nil {
165-
return fmt.Errorf("failed to execute migration template: %v", err)
166-
}
167-
168-
// Build migrations list with processed template
150+
// Process the migration SQL with fmt.Sprintf (22 schema placeholders)
151+
sanitizedSchema := pgx.Identifier{schema}.Sanitize()
152+
migrationSQL := fmt.Sprintf(migration1SQL,
153+
sanitizedSchema, sanitizedSchema, sanitizedSchema, sanitizedSchema, sanitizedSchema,
154+
sanitizedSchema, sanitizedSchema, sanitizedSchema, sanitizedSchema, sanitizedSchema,
155+
sanitizedSchema, sanitizedSchema, sanitizedSchema, sanitizedSchema, sanitizedSchema,
156+
sanitizedSchema, sanitizedSchema, sanitizedSchema, sanitizedSchema, sanitizedSchema,
157+
sanitizedSchema, sanitizedSchema)
158+
159+
// Build migrations list with processed SQL
169160
migrations := []migrationFile{
170-
{version: 1, sql: migrationSQL.String()},
161+
{version: 1, sql: migrationSQL},
171162
}
172163

173164
// Connect to the database

0 commit comments

Comments
 (0)