navree_queue/
migration.rs1use navree_db::MigrationSet;
10use navree_db::navree_schema::{Migration, Schema, SchemaMigration, SchemaResult};
11
12pub const JOBS_TABLE: &str = "jobs";
14
15pub const FAILED_JOBS_TABLE: &str = "failed_jobs";
17
18pub const JOB_BATCHES_TABLE: &str = "job_batches";
20
21pub const OPERATIONS_TABLE: &str = "operations";
23
24pub(crate) fn migrations() -> MigrationSet {
26 MigrationSet::new("queue")
27 .migration(SchemaMigration::new(
28 "m20261025_000001_create_queue_tables",
29 CreateQueueTables,
30 ))
31 .migration(SchemaMigration::new(
32 "m20261026_000001_add_jobs_trace_parent",
33 AddJobsTraceParent,
34 ))
35 .migration(SchemaMigration::new(
36 "m20261027_000001_create_job_batches",
37 CreateJobBatches,
38 ))
39 .migration(SchemaMigration::new(
40 "m20261027_000002_create_operations",
41 CreateOperations,
42 ))
43}
44
45struct AddJobsTraceParent;
49
50impl Migration for AddJobsTraceParent {
51 async fn up(&self, schema: &Schema<'_>) -> SchemaResult {
52 schema
53 .table(JOBS_TABLE, |t| {
54 t.string("trace_parent").nullable();
55 })
56 .await
57 }
58
59 async fn down(&self, schema: &Schema<'_>) -> SchemaResult {
60 schema
61 .table(JOBS_TABLE, |t| t.drop_column("trace_parent"))
62 .await
63 }
64}
65
66struct CreateJobBatches;
68
69impl Migration for CreateJobBatches {
70 async fn up(&self, schema: &Schema<'_>) -> SchemaResult {
71 schema
72 .create(JOB_BATCHES_TABLE, |t| {
73 t.string("id").primary();
74 t.string("name");
75 t.big_integer("total_jobs");
76 t.big_integer("pending_jobs");
77 t.big_integer("failed_jobs");
78 t.text("failed_job_ids");
79 t.text("callbacks");
80 t.big_integer("callbacks_pending");
81 t.big_integer("callbacks_failed");
82 t.boolean("allow_failures");
83 t.string("operation_id").nullable();
84 t.big_integer("created_at");
85 t.big_integer("cancelled_at").nullable();
86 t.big_integer("finished_at").nullable();
87 t.index(["created_at"]).name("job_batches_created_at_index");
88 })
89 .await?;
90 schema
91 .table(JOBS_TABLE, |t| {
92 t.string("batch_id").nullable();
93 t.boolean("batch_callback").default(false);
94 t.string("operation_id").nullable();
95 t.index(["operation_id"]).name("jobs_operation_id_index");
96 })
97 .await
98 }
99
100 async fn down(&self, schema: &Schema<'_>) -> SchemaResult {
101 schema
102 .table(JOBS_TABLE, |t| {
103 t.drop_index("jobs_operation_id_index");
104 t.drop_column(["operation_id", "batch_callback", "batch_id"]);
105 })
106 .await?;
107 schema.drop_if_exists(JOB_BATCHES_TABLE).await
108 }
109}
110
111struct CreateOperations;
114
115impl Migration for CreateOperations {
116 async fn up(&self, schema: &Schema<'_>) -> SchemaResult {
117 schema
118 .create(OPERATIONS_TABLE, |t| {
119 t.string("id").primary();
120 t.string("kind");
121 t.string("status");
122 t.string("owner_id").nullable();
123 t.string("workspace_id").nullable();
124 t.big_integer("progress_done");
125 t.big_integer("progress_total");
126 t.text("progress_note");
127 t.text("result").nullable();
128 t.text("error").nullable();
129 t.string("batch_id").nullable();
130 t.text("job").nullable();
131 t.boolean("idempotent");
132 t.big_integer("redispatches");
133 t.big_integer("version");
134 t.big_integer("created_at");
135 t.big_integer("started_at").nullable();
136 t.big_integer("heartbeat_at").nullable();
137 t.big_integer("finished_at").nullable();
138 t.big_integer("updated_at");
139 t.index(["owner_id", "created_at"])
140 .name("operations_owner_id_index");
141 t.index(["status", "heartbeat_at"])
142 .name("operations_status_heartbeat_at_index");
143 })
144 .await
145 }
146
147 async fn down(&self, schema: &Schema<'_>) -> SchemaResult {
148 schema.drop_if_exists(OPERATIONS_TABLE).await
149 }
150}
151
152struct CreateQueueTables;
154
155impl Migration for CreateQueueTables {
156 async fn up(&self, schema: &Schema<'_>) -> SchemaResult {
157 schema
158 .create(JOBS_TABLE, |t| {
159 t.string("id").primary();
160 t.string("queue");
161 t.string("job_type");
162 t.text("payload");
163 t.text("options");
164 t.big_integer("attempts").default(0);
165 t.big_integer("max_attempts");
166 t.big_integer("timeout_secs");
167 t.big_integer("available_at");
168 t.big_integer("reserved_at").nullable();
169 t.string("reserved_by").nullable();
170 t.string("unique_key").nullable().unique();
171 t.big_integer("created_at");
172 t.index(["queue", "available_at"])
173 .name("jobs_queue_available_at_index");
174 })
175 .await?;
176 schema
177 .create(FAILED_JOBS_TABLE, |t| {
178 t.string("id").primary();
179 t.string("queue");
180 t.string("job_type");
181 t.text("payload");
182 t.text("options");
183 t.text("error");
184 t.big_integer("attempts");
185 t.big_integer("failed_at");
186 t.index(["failed_at"]).name("failed_jobs_failed_at_index");
187 })
188 .await
189 }
190
191 async fn down(&self, schema: &Schema<'_>) -> SchemaResult {
192 schema.drop_if_exists(FAILED_JOBS_TABLE).await?;
193 schema.drop_if_exists(JOBS_TABLE).await
194 }
195}