oblaka-iac 0.0.20 → 0.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -16
- package/package.json +1 -1
- package/src/commands/durable-objects.ts +169 -0
- package/src/commands/resource-processor.ts +36 -32
- package/src/resources/container.ts +2 -16
- package/src/resources/do.ts +8 -14
- package/src/resources/worker.ts +15 -1
- package/src/utils/migrations.ts +57 -0
package/README.md
CHANGED
|
@@ -41,7 +41,7 @@ export default define(({ env }) => {
|
|
|
41
41
|
EmailQueue: new Queue({ name: 'email-queue', binding: 'both' }),
|
|
42
42
|
Room: new DurableObject({ name: 'Room', className: 'Room' }),
|
|
43
43
|
|
|
44
|
-
// Sub-worker
|
|
44
|
+
// Sub-worker: gets its own wrangler.jsonc
|
|
45
45
|
Admin: new Worker({
|
|
46
46
|
dir: './packages/admin',
|
|
47
47
|
name: 'my-app-admin',
|
|
@@ -92,21 +92,77 @@ bunx oblaka oblaka.ts \
|
|
|
92
92
|
|
|
93
93
|
## Resources
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
|
98
|
-
|
|
|
99
|
-
| `
|
|
100
|
-
| `
|
|
101
|
-
| `
|
|
102
|
-
| `
|
|
103
|
-
| `
|
|
104
|
-
| `
|
|
105
|
-
| `
|
|
106
|
-
| `
|
|
107
|
-
| `
|
|
108
|
-
| `
|
|
109
|
-
| `
|
|
95
|
+
Resources marked as provisioned are created through the Cloudflare API with `--remote` and tracked in the state. Workflows and containers get a per-environment name in the state and are created by `wrangler deploy`. Analytics Engine datasets are created by Cloudflare on the first write. The rest only add a binding to the generated config.
|
|
96
|
+
|
|
97
|
+
| Resource | Description | Provisioned |
|
|
98
|
+
| ------------------ | --------------------------------------------------- | ----------- |
|
|
99
|
+
| `Worker` | Cloudflare Worker with bindings | yes |
|
|
100
|
+
| `D1Database` | D1 SQL database, optional `migrationsDir` | yes |
|
|
101
|
+
| `KVNamespace` | KV key-value storage | yes |
|
|
102
|
+
| `R2Bucket` | R2 object storage | yes |
|
|
103
|
+
| `Queue` | Queue (producer, consumer or both) | yes |
|
|
104
|
+
| `DurableObject` | Durable Object class, see [below](#durable-objects) | |
|
|
105
|
+
| `Workflow` | Cloudflare Workflow | |
|
|
106
|
+
| `Container` | Container attached to a Durable Object class | |
|
|
107
|
+
| `AnalyticsEngine` | Analytics Engine dataset | |
|
|
108
|
+
| `VectorizeIndex` | Vectorize index | yes |
|
|
109
|
+
| `AiGateway` | Workers AI binding (`ai`) | |
|
|
110
|
+
| `AiSearch` | AI Search namespace | |
|
|
111
|
+
| `Browser` | Browser Rendering API binding | |
|
|
112
|
+
| `Images` | Image Optimization binding | |
|
|
113
|
+
| `EmailService` | Send Email binding | |
|
|
114
|
+
| `Flagship` | Flagship feature flags | |
|
|
115
|
+
| `VpcNetwork` | VPC network or Cloudflare Tunnel (`mesh`, `tunnel`) | |
|
|
116
|
+
| `WorkerLoader` | Worker Loader for dynamic workers | |
|
|
117
|
+
| `ServiceReference` | Reference to another worker by name | |
|
|
118
|
+
| `VersionMetadata` | Version metadata binding | |
|
|
119
|
+
|
|
120
|
+
## Conditional bindings
|
|
121
|
+
|
|
122
|
+
A binding set to `false`, `null` or `undefined` is skipped, so it can depend on the environment:
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
bindings: {
|
|
126
|
+
Cache: new KVNamespace({ name: 'cache' }),
|
|
127
|
+
Analytics: env === 'production' && new AnalyticsEngine({ name: 'events' }),
|
|
128
|
+
},
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Don't make `DurableObject` or `Container` bindings conditional. All environments share one migration history in the generated file, so a run for an environment without the class would see it as removed (see [Durable Objects](#durable-objects)).
|
|
132
|
+
|
|
133
|
+
## Durable Objects
|
|
134
|
+
|
|
135
|
+
Oblaka writes the Durable Object migrations (`new_sqlite_classes`, `renamed_classes`, `deleted_classes`) into the generated config and reads the history back from it on the next run, so the generated file has to be kept between runs.
|
|
136
|
+
|
|
137
|
+
Deleting a class deletes all its stored data, so oblaka never does it just because a binding disappeared. When a class that was bound in the previous generated config is no longer bound, the run fails before anything is created or written, until you say what should happen:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
new Worker({
|
|
141
|
+
// ...
|
|
142
|
+
bindings: {
|
|
143
|
+
// Renamed from Room: the data moves to ChatRoom.
|
|
144
|
+
ChatRoom: new DurableObject({
|
|
145
|
+
name: 'chat-room',
|
|
146
|
+
className: 'ChatRoom',
|
|
147
|
+
renamedFrom: 'Room',
|
|
148
|
+
}),
|
|
149
|
+
},
|
|
150
|
+
// Deleted together with all their data.
|
|
151
|
+
deletedDurableObjects: ['OldCounter'],
|
|
152
|
+
})
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`renamedFrom` and `deletedDurableObjects` can stay in the definition after the change is deployed; once the migration is in the history they do nothing. Remove a `deletedDurableObjects` entry before binding a new class with the same name. `deleteDurableObjectsOnRemoval: true` deletes every removed class without listing it, and `false` keeps removed classes alive (for example a class used only through `ctx.exports`).
|
|
156
|
+
|
|
157
|
+
## Types for `env`
|
|
158
|
+
|
|
159
|
+
Generate the `Env` type from the generated config with Wrangler:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
bunx oblaka oblaka.ts && bunx wrangler types
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`wrangler types` reads `wrangler.jsonc` in the worker directory and writes `worker-configuration.d.ts`, including Durable Object namespaces typed by their class from `main`. Run it in each worker directory, and pass `-c` when the config has a different name.
|
|
110
166
|
|
|
111
167
|
## CLI options
|
|
112
168
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Container, DurableObject, type Worker } from '../resources'
|
|
2
|
+
import type { Config } from '../types'
|
|
3
|
+
import { type DurableObjectMigration, getLiveDurableObjectClasses, getNextMigrationTag, withDurableObjectClass } from '../utils/migrations'
|
|
4
|
+
|
|
5
|
+
export const getBoundDurableObjectClasses = (config: Config): Set<string> =>
|
|
6
|
+
new Set((config.durable_objects?.bindings ?? []).map((b: { class_name: string }) => b.class_name))
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks the worker's Durable Object changes against its existing generated config, so a
|
|
10
|
+
* mistake fails the run before any resource is created or any config is written.
|
|
11
|
+
*/
|
|
12
|
+
export const validateDurableObjects = (worker: Worker, existingConfig: Config): void => {
|
|
13
|
+
const workerName = worker.options.name
|
|
14
|
+
const deletedDurableObjects = worker.options.deletedDurableObjects ?? []
|
|
15
|
+
const classes = Object.values(worker.options.bindings ?? {}).flatMap(binding => {
|
|
16
|
+
if (binding instanceof DurableObject) {
|
|
17
|
+
return [{ className: binding.options.className, renamedFrom: binding.options.renamedFrom || undefined }]
|
|
18
|
+
}
|
|
19
|
+
if (binding instanceof Container) {
|
|
20
|
+
return [{ className: binding.options.className, renamedFrom: undefined }]
|
|
21
|
+
}
|
|
22
|
+
return []
|
|
23
|
+
})
|
|
24
|
+
const bound = new Set(classes.map(c => c.className))
|
|
25
|
+
const existingClasses = getLiveDurableObjectClasses(existingConfig.migrations)
|
|
26
|
+
|
|
27
|
+
// Bindings are configured one by one, so for a class that does not exist yet, differing renamedFrom values
|
|
28
|
+
// would make the outcome depend on their order. For an existing class renamedFrom does nothing.
|
|
29
|
+
const renamedFromByClass = new Map<string, string | undefined>()
|
|
30
|
+
for (const { className, renamedFrom } of classes) {
|
|
31
|
+
const other = renamedFromByClass.get(className)
|
|
32
|
+
if (renamedFromByClass.has(className) && other !== renamedFrom && !existingClasses.has(className)) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`Worker '${workerName}': Durable Object class '${className}' is bound more than once with a different renamedFrom `
|
|
35
|
+
+ `(${formatRenamedFrom(other)} and ${formatRenamedFrom(renamedFrom)}). `
|
|
36
|
+
+ 'Set the same renamedFrom on every DurableObject binding of the class. A Container binding cannot set renamedFrom.',
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
renamedFromByClass.set(className, other ?? renamedFrom)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const renameTargets = new Map<string, string>()
|
|
43
|
+
for (const [className, renamedFrom] of renamedFromByClass) {
|
|
44
|
+
if (!renamedFrom) {
|
|
45
|
+
continue
|
|
46
|
+
}
|
|
47
|
+
if (bound.has(renamedFrom)) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`Worker '${workerName}': Durable Object class '${className}' is renamed from '${renamedFrom}', which is still bound. `
|
|
50
|
+
+ `Remove the '${renamedFrom}' binding.`,
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
const otherTarget = renameTargets.get(renamedFrom)
|
|
54
|
+
if (otherTarget) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Worker '${workerName}': Durable Object classes '${otherTarget}' and '${className}' are both renamed from '${renamedFrom}'.`,
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
renameTargets.set(renamedFrom, className)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const deletedAndBound = deletedDurableObjects.filter(c => bound.has(c))
|
|
63
|
+
if (deletedAndBound.length > 0) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Worker '${workerName}': Durable Object class ${formatClasses(deletedAndBound)} is listed in deletedDurableObjects but bound. `
|
|
66
|
+
+ 'Remove the binding, or remove the class from deletedDurableObjects.',
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// The same migrations the bindings will produce, so renames are taken into account.
|
|
71
|
+
let migrations: DurableObjectMigration[] | undefined = existingConfig.migrations
|
|
72
|
+
for (const [className, renamedFrom] of renamedFromByClass) {
|
|
73
|
+
migrations = withDurableObjectClass({ migrations }, className, renamedFrom)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
getRemovedDurableObjects({
|
|
77
|
+
workerName,
|
|
78
|
+
migrations,
|
|
79
|
+
previouslyBound: getBoundDurableObjectClasses(existingConfig),
|
|
80
|
+
bound,
|
|
81
|
+
deletedDurableObjects,
|
|
82
|
+
deleteDurableObjectsOnRemoval: worker.options.deleteDurableObjectsOnRemoval,
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Returns the Durable Object classes to delete: live classes that are not bound and either
|
|
88
|
+
* listed in `deletedDurableObjects`, or bound in the previous config with
|
|
89
|
+
* `deleteDurableObjectsOnRemoval: true`. Throws for a class that was bound in the previous
|
|
90
|
+
* config and is no longer, when neither says what to do with it, because deleting a class
|
|
91
|
+
* deletes all its stored data. Live classes that were not bound before are left alone.
|
|
92
|
+
*/
|
|
93
|
+
export const getRemovedDurableObjects = (args: {
|
|
94
|
+
workerName: string
|
|
95
|
+
migrations: DurableObjectMigration[] | undefined
|
|
96
|
+
previouslyBound: Set<string>
|
|
97
|
+
bound: Set<string>
|
|
98
|
+
deletedDurableObjects: string[]
|
|
99
|
+
deleteDurableObjectsOnRemoval: boolean | undefined
|
|
100
|
+
}): string[] => {
|
|
101
|
+
const unbound = [...getLiveDurableObjectClasses(args.migrations)].filter(c => !args.bound.has(c))
|
|
102
|
+
const toDelete = unbound.filter(c =>
|
|
103
|
+
args.deletedDurableObjects.includes(c) || (args.previouslyBound.has(c) && args.deleteDurableObjectsOnRemoval === true)
|
|
104
|
+
)
|
|
105
|
+
const unresolved = unbound.filter(c => args.previouslyBound.has(c) && !toDelete.includes(c))
|
|
106
|
+
|
|
107
|
+
if (unresolved.length > 0 && args.deleteDurableObjectsOnRemoval !== false) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`Worker '${args.workerName}': Durable Object class ${formatClasses(unresolved)} is no longer bound. `
|
|
110
|
+
+ 'Deleting a class deletes all its stored data, so oblaka will not do it implicitly. '
|
|
111
|
+
+ 'Add the class to deletedDurableObjects to delete it, set renamedFrom on the new DurableObject binding if it was renamed '
|
|
112
|
+
+ '(a Container binding cannot rename a class), '
|
|
113
|
+
+ 'or set deleteDurableObjectsOnRemoval: false to keep it without a binding.',
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
return toDelete
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Appends a `deleted_classes` migration. A class that was created once and never renamed,
|
|
121
|
+
* transferred or deleted before is also dropped from its `new_sqlite_classes` entry; with a
|
|
122
|
+
* longer history the earlier migrations are kept, so they stay consistent with each other.
|
|
123
|
+
*/
|
|
124
|
+
export const deleteDurableObjectClasses = (config: Config, classes: string[]): Config => {
|
|
125
|
+
if (classes.length === 0) {
|
|
126
|
+
return config
|
|
127
|
+
}
|
|
128
|
+
const migrations = config.migrations ?? []
|
|
129
|
+
const stripped = new Set(classes.filter(c => hasSingleCreation(migrations, c)))
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
...config,
|
|
133
|
+
migrations: [
|
|
134
|
+
...(config.migrations ?? []).flatMap(m => {
|
|
135
|
+
if (!m.new_sqlite_classes) return [m]
|
|
136
|
+
const filtered = m.new_sqlite_classes.filter((c: string) => !stripped.has(c))
|
|
137
|
+
if (filtered.length === 0) {
|
|
138
|
+
const { new_sqlite_classes: _, ...rest } = m
|
|
139
|
+
return Object.keys(rest).length > 1 ? [rest as typeof m] : []
|
|
140
|
+
}
|
|
141
|
+
return [{ ...m, new_sqlite_classes: filtered }]
|
|
142
|
+
}),
|
|
143
|
+
{
|
|
144
|
+
tag: getNextMigrationTag(config),
|
|
145
|
+
deleted_classes: classes,
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const hasSingleCreation = (migrations: DurableObjectMigration[], className: string): boolean => {
|
|
152
|
+
let creations = 0
|
|
153
|
+
for (const m of migrations) {
|
|
154
|
+
creations += (m.new_sqlite_classes ?? []).filter(c => c === className).length
|
|
155
|
+
if (
|
|
156
|
+
m.new_classes?.includes(className)
|
|
157
|
+
|| m.deleted_classes?.includes(className)
|
|
158
|
+
|| m.renamed_classes?.some(r => r.from === className || r.to === className)
|
|
159
|
+
|| m.transferred_classes?.some(t => t.to === className)
|
|
160
|
+
) {
|
|
161
|
+
return false
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return creations === 1
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const formatClasses = (classes: string[]) => classes.map(c => `'${c}'`).join(', ')
|
|
168
|
+
|
|
169
|
+
const formatRenamedFrom = (renamedFrom: string | undefined) => (renamedFrom ? `'${renamedFrom}'` : 'none')
|
|
@@ -3,7 +3,7 @@ import * as fs from 'node:fs/promises'
|
|
|
3
3
|
import * as path from 'node:path'
|
|
4
4
|
import { Worker } from '../resources'
|
|
5
5
|
import type { Config, DefineFn, Resource } from '../types'
|
|
6
|
-
import {
|
|
6
|
+
import { deleteDurableObjectClasses, getBoundDurableObjectClasses, getRemovedDurableObjects, validateDurableObjects } from './durable-objects'
|
|
7
7
|
import { tryReadFile } from './utils/fs'
|
|
8
8
|
|
|
9
9
|
export const DEFAULT_CONFIG_FILE_NAME = 'wrangler.jsonc'
|
|
@@ -54,6 +54,7 @@ export class ResourceProcessor {
|
|
|
54
54
|
const generated: GeneratedConfig[] = []
|
|
55
55
|
|
|
56
56
|
if (definition) {
|
|
57
|
+
await this.validate(definition)
|
|
57
58
|
this.workers.push(definition)
|
|
58
59
|
}
|
|
59
60
|
|
|
@@ -67,14 +68,33 @@ export class ResourceProcessor {
|
|
|
67
68
|
return generated
|
|
68
69
|
}
|
|
69
70
|
|
|
71
|
+
/** Checks every worker in the tree before any resource is applied or any config is written. */
|
|
72
|
+
private async validate(root: Worker): Promise<void> {
|
|
73
|
+
const queue = [root]
|
|
74
|
+
while (queue.length) {
|
|
75
|
+
const worker = queue.shift()!
|
|
76
|
+
validateDurableObjects(worker, await this.readExistingConfig(worker))
|
|
77
|
+
for (const binding of Object.values(worker.options.bindings ?? {})) {
|
|
78
|
+
if (binding instanceof Worker) {
|
|
79
|
+
queue.push(binding)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private async readExistingConfig(worker: Worker): Promise<Config> {
|
|
86
|
+
const configPath = path.join(worker.options.dir, this.configFileName)
|
|
87
|
+
return jsoncParser.parse((await tryReadFile(configPath)) || '{}') as Config
|
|
88
|
+
}
|
|
89
|
+
|
|
70
90
|
private async processWorker(env: string): Promise<GeneratedConfig | undefined> {
|
|
71
91
|
const worker = this.workers.shift()
|
|
72
92
|
if (!worker) {
|
|
73
93
|
return undefined
|
|
74
94
|
}
|
|
75
95
|
const configPath = path.join(worker.options.dir, this.configFileName)
|
|
76
|
-
const existingConfig =
|
|
77
|
-
const { bindings, dir, deleteDurableObjectsOnRemoval, ...workerConfig } = worker.options
|
|
96
|
+
const existingConfig = await this.readExistingConfig(worker)
|
|
97
|
+
const { bindings, dir, deleteDurableObjectsOnRemoval, deletedDurableObjects, ...workerConfig } = worker.options
|
|
78
98
|
|
|
79
99
|
let config: Config = {
|
|
80
100
|
...workerConfig,
|
|
@@ -84,7 +104,10 @@ export class ResourceProcessor {
|
|
|
84
104
|
const selfState = await this.resourceHandler.applyResource(worker)
|
|
85
105
|
config = worker.configureSelf({ config, state: selfState })
|
|
86
106
|
|
|
87
|
-
for (const [bindingName, binding] of Object.entries(
|
|
107
|
+
for (const [bindingName, binding] of Object.entries(bindings ?? {})) {
|
|
108
|
+
if (!binding) {
|
|
109
|
+
continue
|
|
110
|
+
}
|
|
88
111
|
if (binding instanceof Worker) {
|
|
89
112
|
this.workers.push(binding)
|
|
90
113
|
}
|
|
@@ -92,34 +115,15 @@ export class ResourceProcessor {
|
|
|
92
115
|
config = binding.configureBinding({ config, binding: bindingName, state, env })
|
|
93
116
|
}
|
|
94
117
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
migrations: [
|
|
105
|
-
...(config.migrations ?? [])
|
|
106
|
-
.flatMap(m => {
|
|
107
|
-
if (!m.new_sqlite_classes) return [m]
|
|
108
|
-
const filtered = m.new_sqlite_classes.filter((c: string) => !deletedClassesSet.has(c))
|
|
109
|
-
if (filtered.length === 0) {
|
|
110
|
-
const { new_sqlite_classes: _, ...rest } = m
|
|
111
|
-
return Object.keys(rest).length > 1 ? [rest as typeof m] : []
|
|
112
|
-
}
|
|
113
|
-
return [{ ...m, new_sqlite_classes: filtered }]
|
|
114
|
-
}),
|
|
115
|
-
{
|
|
116
|
-
tag: getNextMigrationTag(config),
|
|
117
|
-
deleted_classes: deletedClasses,
|
|
118
|
-
},
|
|
119
|
-
],
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
118
|
+
const removedDurableObjects = getRemovedDurableObjects({
|
|
119
|
+
workerName: worker.options.name,
|
|
120
|
+
migrations: config.migrations,
|
|
121
|
+
previouslyBound: getBoundDurableObjectClasses(existingConfig),
|
|
122
|
+
bound: getBoundDurableObjectClasses(config),
|
|
123
|
+
deletedDurableObjects: deletedDurableObjects ?? [],
|
|
124
|
+
deleteDurableObjectsOnRemoval,
|
|
125
|
+
})
|
|
126
|
+
config = deleteDurableObjectClasses(config, removedDurableObjects)
|
|
123
127
|
|
|
124
128
|
const content = `/**\n * File is auto generated DO NOT EDIT\n */\n${JSON.stringify(config, null, '\t')}\n`
|
|
125
129
|
await this.configWriter(configPath, content)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BindableResource, Config, Context, ResourceDestroyer } from '../types'
|
|
2
|
-
import {
|
|
2
|
+
import { withDurableObjectClass } from '../utils/migrations'
|
|
3
3
|
|
|
4
4
|
export interface ContainerState {
|
|
5
5
|
name: string
|
|
@@ -47,23 +47,9 @@ export class Container implements BindableResource<ContainerState> {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
configureBinding(args: { config: Config; binding: string; state?: ContainerState; env: string }): Config {
|
|
50
|
-
const hasMigration = args.config?.migrations?.find((it: { new_sqlite_classes?: string[] }) =>
|
|
51
|
-
it.new_sqlite_classes?.includes(this.options.className)
|
|
52
|
-
)
|
|
53
|
-
|
|
54
50
|
return {
|
|
55
51
|
...args.config,
|
|
56
|
-
migrations:
|
|
57
|
-
...(args.config.migrations ?? []),
|
|
58
|
-
...(hasMigration
|
|
59
|
-
? []
|
|
60
|
-
: [
|
|
61
|
-
{
|
|
62
|
-
tag: getNextMigrationTag(args.config),
|
|
63
|
-
new_sqlite_classes: [this.options.className],
|
|
64
|
-
},
|
|
65
|
-
]),
|
|
66
|
-
],
|
|
52
|
+
migrations: withDurableObjectClass(args.config, this.options.className),
|
|
67
53
|
containers: [
|
|
68
54
|
...(args.config.containers ?? []),
|
|
69
55
|
{
|
package/src/resources/do.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BindableResource, Config, ResourceDestroyer } from '../types'
|
|
2
|
-
import {
|
|
2
|
+
import { withDurableObjectClass } from '../utils/migrations'
|
|
3
3
|
|
|
4
4
|
export interface DurableObjectState {
|
|
5
5
|
name: string
|
|
@@ -10,6 +10,12 @@ export class DurableObject implements BindableResource<DurableObjectState> {
|
|
|
10
10
|
public readonly options: {
|
|
11
11
|
name: string
|
|
12
12
|
className: string
|
|
13
|
+
/**
|
|
14
|
+
* Previous class name when the class was renamed. Generates a `renamed_classes`
|
|
15
|
+
* migration, so the stored data moves to the new class instead of being deleted.
|
|
16
|
+
* Safe to keep after the rename is deployed.
|
|
17
|
+
*/
|
|
18
|
+
renamedFrom?: string
|
|
13
19
|
},
|
|
14
20
|
) {}
|
|
15
21
|
|
|
@@ -24,21 +30,9 @@ export class DurableObject implements BindableResource<DurableObjectState> {
|
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
configureBinding(args: { config: Config; state?: DurableObjectState; binding: string; env: string }): Config {
|
|
27
|
-
const hasMigration = args.config?.migrations?.find(
|
|
28
|
-
(it: { new_sqlite_classes?: string[]; renamed_classes?: { from: string; to: string }[] }) =>
|
|
29
|
-
it.new_sqlite_classes?.includes(this.options.className)
|
|
30
|
-
|| it.renamed_classes?.some(r => r.to === this.options.className),
|
|
31
|
-
)
|
|
32
|
-
|
|
33
33
|
return {
|
|
34
34
|
...args.config,
|
|
35
|
-
migrations:
|
|
36
|
-
...(args.config.migrations ?? []),
|
|
37
|
-
...(hasMigration ? [] : [{
|
|
38
|
-
tag: getNextMigrationTag(args.config),
|
|
39
|
-
new_sqlite_classes: [this.options.className],
|
|
40
|
-
}]),
|
|
41
|
-
],
|
|
35
|
+
migrations: withDurableObjectClass(args.config, this.options.className, this.options.renamedFrom),
|
|
42
36
|
durable_objects: {
|
|
43
37
|
...(args.config?.durable_objects ?? {}),
|
|
44
38
|
bindings: [
|
package/src/resources/worker.ts
CHANGED
|
@@ -11,7 +11,21 @@ export class Worker implements BindableResource<WorkerState> {
|
|
|
11
11
|
dir: string
|
|
12
12
|
name: string
|
|
13
13
|
compatibility_flags: CompatibilityFlags[]
|
|
14
|
-
|
|
14
|
+
/** Entries set to `false`, `null` or `undefined` are skipped, so a binding can be conditional. */
|
|
15
|
+
bindings: Record<string, AnyBindable | false | null | undefined>
|
|
16
|
+
/**
|
|
17
|
+
* Durable Object classes to delete together with all their stored data. A class
|
|
18
|
+
* removed from `bindings` without being listed here fails the run, so data is never
|
|
19
|
+
* deleted by accident. Entries for classes that are already deleted do nothing; remove
|
|
20
|
+
* the entry before binding a new class with the same name.
|
|
21
|
+
*/
|
|
22
|
+
deletedDurableObjects?: string[]
|
|
23
|
+
/**
|
|
24
|
+
* What to do with a Durable Object class that was bound in the previous generated config,
|
|
25
|
+
* is no longer bound and is not listed in `deletedDurableObjects`. Unset fails the run,
|
|
26
|
+
* `true` deletes the class and its data, `false` keeps the class (for example when it is
|
|
27
|
+
* used only through `ctx.exports`).
|
|
28
|
+
*/
|
|
15
29
|
deleteDurableObjectsOnRemoval?: boolean
|
|
16
30
|
}
|
|
17
31
|
& Partial<
|
package/src/utils/migrations.ts
CHANGED
|
@@ -10,3 +10,60 @@ export const getNextMigrationTag = (config: { migrations?: { tag: string }[] })
|
|
|
10
10
|
const nextVersion = currentVersion + 1
|
|
11
11
|
return `v${String(nextVersion).padStart(4, '0')}`
|
|
12
12
|
}
|
|
13
|
+
|
|
14
|
+
export interface DurableObjectMigration {
|
|
15
|
+
tag: string
|
|
16
|
+
new_classes?: string[]
|
|
17
|
+
new_sqlite_classes?: string[]
|
|
18
|
+
renamed_classes?: { from: string; to: string }[]
|
|
19
|
+
deleted_classes?: string[]
|
|
20
|
+
transferred_classes?: { from: string; from_script: string; to: string }[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Replays the migration history in tag order and returns the Durable Object classes
|
|
25
|
+
* that exist after the last migration.
|
|
26
|
+
*/
|
|
27
|
+
export const getLiveDurableObjectClasses = (migrations: DurableObjectMigration[] | undefined): Set<string> => {
|
|
28
|
+
const live = new Set<string>()
|
|
29
|
+
// Same ordering as getNextMigrationTag.
|
|
30
|
+
const ordered = [...(migrations ?? [])].sort((a, b) => (a.tag < b.tag ? -1 : a.tag > b.tag ? 1 : 0))
|
|
31
|
+
for (const migration of ordered) {
|
|
32
|
+
for (const className of [...(migration.new_classes ?? []), ...(migration.new_sqlite_classes ?? [])]) {
|
|
33
|
+
live.add(className)
|
|
34
|
+
}
|
|
35
|
+
for (const { from, to } of migration.renamed_classes ?? []) {
|
|
36
|
+
live.delete(from)
|
|
37
|
+
live.add(to)
|
|
38
|
+
}
|
|
39
|
+
for (const { to } of migration.transferred_classes ?? []) {
|
|
40
|
+
live.add(to)
|
|
41
|
+
}
|
|
42
|
+
for (const className of migration.deleted_classes ?? []) {
|
|
43
|
+
live.delete(className)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return live
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Returns the migrations with one appended that creates `className`, or renames
|
|
51
|
+
* `renamedFrom` to it when that class still exists. Returns them unchanged when
|
|
52
|
+
* `className` already exists.
|
|
53
|
+
*/
|
|
54
|
+
export const withDurableObjectClass = <TMigration extends { tag: string }>(
|
|
55
|
+
config: { migrations?: TMigration[] },
|
|
56
|
+
className: string,
|
|
57
|
+
renamedFrom?: string,
|
|
58
|
+
): (TMigration | DurableObjectMigration)[] => {
|
|
59
|
+
const migrations = config.migrations ?? []
|
|
60
|
+
const live = getLiveDurableObjectClasses(migrations)
|
|
61
|
+
if (live.has(className)) {
|
|
62
|
+
return migrations
|
|
63
|
+
}
|
|
64
|
+
const tag = getNextMigrationTag(config)
|
|
65
|
+
if (renamedFrom && live.has(renamedFrom)) {
|
|
66
|
+
return [...migrations, { tag, renamed_classes: [{ from: renamedFrom, to: className }] }]
|
|
67
|
+
}
|
|
68
|
+
return [...migrations, { tag, new_sqlite_classes: [className] }]
|
|
69
|
+
}
|