small thing, but why would you await the migration storage calls? ideally you want all DO SQL calls
small thing, but why would you await the migration storage calls? ideally you want all DO SQL calls in the same request to run synch
Specifically for Durable Object classes with SQLite storage backend, KV operations which were previously asynchronous (for example, get, put, delete, deleteAll, list) are synchronous, even though they return promises. These methods will have completed their operations before they return the promise.
runDurableObjectAlarm() return true or a similar value?while (true) what about while (ran)? Declare ran as true before the first runawait to be consistent with the API that you need to await a Promise. The underlying behavior doesn't change.await of a promise will be more confused, rather than having to read all the DO docs to figure out that it doesn't matter.blockConcurrencyWhile when applying migration in constructor, and also I can put the entire migration into a transactionSync to prevent the rare event that migrations succeeded but writing migration number fails.transaction()? blockConcurrencyWhile has a different purpose than sync/async.storage.get/storage.set's implementations are sync, because applyMigration is async, .then puts continuation in the next event loop, so the log order will be before -> after -> initialized. I haven't tested it but unless DO has a different behavior to regular JS, that should be the case.this.someState is still undefined) by the end of current event loop. I don't know if DO is able to process messages in the same event loop as the one doing the construction, but if that's true, then that's an issue.SELECT name FROM sqlite_master WHERE type='table' AND name=?;async function applyMigrations(storage: DurableObjectStorage) {
await storage.get(...)
// Do migration stuffs
await storage.set(...)
}class MyDO extends DurableObject {
someState!: number
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env)
console.log('before')
applyMigrations(ctx.storage).then(() => {
this.someState = 42
console.log('initialized')
})
console.log('after')
}
}let ran = false;
ran = await runDurableObjectAlarm(stub) // ran = true
ran = await runDurableObjectAlarm(stub) // ran = true
ran = await runDurableObjectAlarm(stub) // ran = true
ran = await runDurableObjectAlarm(stub) // ran = true
ran = await runDurableObjectAlarm(stub) // ran = falsewhile (true) {
let ran = await runDurableObjectAlarm(stub)
if (!ran) {
break
};
}await this.#_config.doStorage.put<number>(this.#_lastMigrationIDKeyName(), this.#_lastMigrationMonotonicId);