Skip to content

Commit cfca508

Browse files
committed
make db migrations idempotent
1 parent c89baea commit cfca508

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

dbos/migrations/000001_initial_dbos_schema.up.sql

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CREATE SCHEMA IF NOT EXISTS dbos;
77
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
88

99
-- Create workflow_status table
10-
CREATE TABLE dbos.workflow_status (
10+
CREATE TABLE IF NOT EXISTS dbos.workflow_status (
1111
workflow_uuid TEXT PRIMARY KEY,
1212
status TEXT,
1313
name TEXT,
@@ -34,17 +34,27 @@ CREATE TABLE dbos.workflow_status (
3434
);
3535

3636
-- Create indexes for workflow_status
37-
CREATE INDEX workflow_status_created_at_index ON dbos.workflow_status (created_at);
38-
CREATE INDEX workflow_status_executor_id_index ON dbos.workflow_status (executor_id);
39-
CREATE INDEX workflow_status_status_index ON dbos.workflow_status (status);
37+
CREATE INDEX IF NOT EXISTS workflow_status_created_at_index ON dbos.workflow_status (created_at);
38+
CREATE INDEX IF NOT EXISTS workflow_status_executor_id_index ON dbos.workflow_status (executor_id);
39+
CREATE INDEX IF NOT EXISTS workflow_status_status_index ON dbos.workflow_status (status);
4040

4141
-- Create unique constraint for queue_name and deduplication_id
42-
ALTER TABLE dbos.workflow_status
43-
ADD CONSTRAINT uq_workflow_status_queue_name_dedup_id
44-
UNIQUE (queue_name, deduplication_id);
42+
DO $$
43+
BEGIN
44+
IF NOT EXISTS (
45+
SELECT 1 FROM information_schema.table_constraints
46+
WHERE constraint_name = 'uq_workflow_status_queue_name_dedup_id'
47+
AND table_name = 'workflow_status'
48+
AND table_schema = 'dbos'
49+
) THEN
50+
ALTER TABLE dbos.workflow_status
51+
ADD CONSTRAINT uq_workflow_status_queue_name_dedup_id
52+
UNIQUE (queue_name, deduplication_id);
53+
END IF;
54+
END $$;
4555

4656
-- Create operation_outputs table
47-
CREATE TABLE dbos.operation_outputs (
57+
CREATE TABLE IF NOT EXISTS dbos.operation_outputs (
4858
workflow_uuid TEXT NOT NULL,
4959
function_id INTEGER NOT NULL,
5060
function_name TEXT NOT NULL DEFAULT '',
@@ -56,7 +66,7 @@ CREATE TABLE dbos.operation_outputs (
5666
ON UPDATE CASCADE ON DELETE CASCADE
5767
);
5868

59-
CREATE TABLE dbos.notifications (
69+
CREATE TABLE IF NOT EXISTS dbos.notifications (
6070
destination_uuid TEXT NOT NULL,
6171
topic TEXT,
6272
message TEXT NOT NULL,
@@ -66,7 +76,7 @@ CREATE TABLE dbos.notifications (
6676
ON UPDATE CASCADE ON DELETE CASCADE
6777
);
6878
-- Create index for notifications
69-
CREATE INDEX idx_workflow_topic ON dbos.notifications (destination_uuid, topic);
79+
CREATE INDEX IF NOT EXISTS idx_workflow_topic ON dbos.notifications (destination_uuid, topic);
7080

7181
-- Create notification function
7282
CREATE OR REPLACE FUNCTION dbos.notifications_function() RETURNS TRIGGER AS $$
@@ -79,12 +89,22 @@ END;
7989
$$ LANGUAGE plpgsql;
8090

8191
-- Create notification trigger
82-
CREATE TRIGGER dbos_notifications_trigger
83-
AFTER INSERT ON dbos.notifications
84-
FOR EACH ROW EXECUTE FUNCTION dbos.notifications_function();
92+
DO $$
93+
BEGIN
94+
IF NOT EXISTS (
95+
SELECT 1 FROM information_schema.triggers
96+
WHERE trigger_name = 'dbos_notifications_trigger'
97+
AND event_object_table = 'notifications'
98+
AND event_object_schema = 'dbos'
99+
) THEN
100+
CREATE TRIGGER dbos_notifications_trigger
101+
AFTER INSERT ON dbos.notifications
102+
FOR EACH ROW EXECUTE FUNCTION dbos.notifications_function();
103+
END IF;
104+
END $$;
85105

86106
-- Create workflow_events table
87-
CREATE TABLE dbos.workflow_events (
107+
CREATE TABLE IF NOT EXISTS dbos.workflow_events (
88108
workflow_uuid TEXT NOT NULL,
89109
key TEXT NOT NULL,
90110
value TEXT NOT NULL,
@@ -104,6 +124,16 @@ END;
104124
$$ LANGUAGE plpgsql;
105125

106126
-- Create events trigger
107-
CREATE TRIGGER dbos_workflow_events_trigger
108-
AFTER INSERT ON dbos.workflow_events
109-
FOR EACH ROW EXECUTE FUNCTION dbos.workflow_events_function();
127+
DO $$
128+
BEGIN
129+
IF NOT EXISTS (
130+
SELECT 1 FROM information_schema.triggers
131+
WHERE trigger_name = 'dbos_workflow_events_trigger'
132+
AND event_object_table = 'workflow_events'
133+
AND event_object_schema = 'dbos'
134+
) THEN
135+
CREATE TRIGGER dbos_workflow_events_trigger
136+
AFTER INSERT ON dbos.workflow_events
137+
FOR EACH ROW EXECUTE FUNCTION dbos.workflow_events_function();
138+
END IF;
139+
END $$;

0 commit comments

Comments
 (0)