Skip to content

Commit bd937e7

Browse files
nipunn1313Convex, Inc.
authored and
Convex, Inc.
committed
Add migration to copy crons to cron_next_run table (#36050)
Copies data over to new table. GitOrigin-RevId: fdafe37f0d78d778e89f30db9aab4bb103dc3c11
1 parent e74ef5b commit bd937e7

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

crates/model/src/cron_jobs/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl<'a, RT: Runtime> CronModel<'a, RT> {
272272
Ok(())
273273
}
274274

275-
async fn next_run(
275+
pub async fn next_run(
276276
&mut self,
277277
cron_job_id: DeveloperDocumentId,
278278
) -> anyhow::Result<Option<ParsedDocument<CronNextRun>>> {

crates/model/src/cron_jobs/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ impl TryFrom<JsonValue> for CronSpec {
445445
}
446446
}
447447

448-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
448+
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
449449
#[cfg_attr(any(test, feature = "testing"), derive(proptest_derive::Arbitrary))]
450450
#[serde(rename_all = "camelCase", tag = "type")]
451451
pub enum CronJobState {

crates/model/src/migrations.rs

+53-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77

88
use anyhow::Context;
99
use common::{
10+
self,
1011
backoff::Backoff,
1112
document::{
1213
ParseDocument,
@@ -38,6 +39,12 @@ use value::{
3839

3940
use crate::{
4041
canonical_urls::CANONICAL_URLS_TABLE,
42+
cron_jobs::{
43+
types::CronNextRun,
44+
CronModel,
45+
CRON_JOBS_TABLE,
46+
CRON_NEXT_RUN_TABLE,
47+
},
4148
database_globals::{
4249
types::DatabaseVersion,
4350
DatabaseGlobalsModel,
@@ -81,7 +88,7 @@ impl fmt::Display for MigrationCompletionCriterion {
8188
// migrations unless explicitly dropping support.
8289
// Add a user name next to the version when you make a change to highlight merge
8390
// conflicts.
84-
pub const DATABASE_VERSION: DatabaseVersion = 118; // nipunn
91+
pub const DATABASE_VERSION: DatabaseVersion = 119; // nipunn
8592

8693
pub struct MigrationWorker<RT: Runtime> {
8794
rt: RT,
@@ -394,6 +401,51 @@ impl<RT: Runtime> MigrationWorker<RT> {
394401
},
395402
// Empty migration for 118 - represents creation of CronNextRun table
396403
118 => MigrationCompletionCriterion::MigrationComplete(to_version),
404+
119 => {
405+
let mut tx = self.db.begin_system().await?;
406+
let namespaces: Vec<_> = tx
407+
.table_mapping()
408+
.iter()
409+
.filter_map(|(_, namespace, _, table_name)| {
410+
if table_name == &*CRON_JOBS_TABLE {
411+
Some(namespace)
412+
} else {
413+
None
414+
}
415+
})
416+
.collect();
417+
418+
for namespace in namespaces {
419+
let crons = CronModel::new(&mut tx, namespace.into()).list().await?;
420+
for cron in crons.values() {
421+
let next_run = CronNextRun {
422+
cron_job_id: cron.id().developer_id,
423+
state: cron.state,
424+
prev_ts: cron.prev_ts,
425+
next_ts: cron.next_ts,
426+
};
427+
if let Some(existing_next_run) = CronModel::new(&mut tx, namespace.into())
428+
.next_run(cron.id().developer_id)
429+
.await?
430+
.map(|next_run| next_run.into_value())
431+
{
432+
if existing_next_run != next_run {
433+
SystemMetadataModel::new(&mut tx, namespace)
434+
.replace(cron.id(), next_run.try_into()?)
435+
.await?;
436+
}
437+
} else {
438+
SystemMetadataModel::new(&mut tx, namespace)
439+
.insert(&CRON_NEXT_RUN_TABLE, next_run.try_into()?)
440+
.await?;
441+
}
442+
}
443+
}
444+
self.db
445+
.commit_with_write_source(tx, "migration_119")
446+
.await?;
447+
MigrationCompletionCriterion::MigrationComplete(to_version)
448+
},
397449
// NOTE: Make sure to increase DATABASE_VERSION when adding new migrations.
398450
_ => anyhow::bail!("Version did not define a migration! {}", to_version),
399451
};

0 commit comments

Comments
 (0)