Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Tagged Durable Objects Migrations #1992

Merged
merged 3 commits into from Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/cli/mod.rs
Expand Up @@ -38,7 +38,7 @@ use crate::commands::dev::Protocol;
use crate::commands::tail::websocket::TailFormat;
use crate::preview::HttpMethod;
use crate::settings::toml::migrations::{
DurableObjectsMigration, Migration, MigrationConfig, Migrations, RenameClass, TransferClass,
DurableObjectsMigration, Migration, MigrationTag, Migrations, RenameClass, TransferClass,
};
use crate::settings::toml::TargetType;

Expand Down Expand Up @@ -293,17 +293,25 @@ pub struct AdhocMigration {
delete_class: Vec<String>,

/// Rename a durable object class
#[structopt(name = "rename-class", long, number_of_values = 2)]
#[structopt(name = "rename-class", long, number_of_values = 2, value_names(&["from class", "to class"]))]
rename_class: Vec<String>,

/// Transfer all durable objects associated with a class in another script to a class in
/// this script
#[structopt(name = "transfer-class", long, number_of_values = 3)]
#[structopt(name = "transfer-class", long, number_of_values = 3, value_names(&["from script", "from class", "to class"]))]
transfer_class: Vec<String>,

/// Specify the existing migration tag for the script.
#[structopt(name = "old-tag", long)]
old_tag: Option<String>,

/// Specify the new migration tag for the script
#[structopt(name = "new-tag", long)]
new_tag: Option<String>,
}

impl AdhocMigration {
pub fn into_migration_config(self) -> Option<MigrationConfig> {
pub fn into_migrations(self) -> Option<Migrations> {
let migration = DurableObjectsMigration {
new_classes: self.new_class,
deleted_classes: self.delete_class,
Expand Down Expand Up @@ -343,12 +351,20 @@ impl AdhocMigration {
&& migration.renamed_classes.is_empty()
&& migration.transferred_classes.is_empty();

if !is_migration_empty {
Some(MigrationConfig {
tag: None,
migration: Migration {
if !is_migration_empty || self.old_tag.is_some() || self.new_tag.is_some() {
let migration = if !is_migration_empty {
Some(Migration {
durable_objects: migration,
},
})
} else {
None
};

Some(Migrations::Adhoc {
script_tag: MigrationTag::Unknown,
provided_old_tag: self.old_tag,
new_tag: self.new_tag,
migration,
})
} else {
None
Expand Down Expand Up @@ -390,6 +406,10 @@ mod tests {
let command = Cli::from_iter(&[
"wrangler",
"publish",
"--old-tag",
"oldTag",
"--new-tag",
"newTag",
"--new-class",
"newA",
"--new-class",
Expand Down Expand Up @@ -417,17 +437,19 @@ mod tests {

if let Command::Publish { migration, .. } = command {
assert_eq!(
migration.into_migration_config(),
Some(MigrationConfig {
tag: None,
migration: Migration {
migration.into_migrations(),
Some(Migrations::Adhoc {
script_tag: MigrationTag::Unknown,
provided_old_tag: Some(String::from("oldTag")),
new_tag: Some(String::from("newTag")),
migration: Some(Migration {
durable_objects: DurableObjectsMigration {
new_classes: vec![String::from("newA"), String::from("newB")],
deleted_classes: vec![String::from("deleteA"), String::from("deleteB")],
renamed_classes: vec![rename_class("A"), rename_class("B")],
transferred_classes: vec![transfer_class("A"), transfer_class("B")],
}
}
})
})
);
} else {
Expand Down
8 changes: 3 additions & 5 deletions src/cli/publish.rs
@@ -1,5 +1,5 @@
use super::AdhocMigration;
use super::Cli;
use super::{AdhocMigration, Migrations};
use crate::commands;
use crate::settings::{global_user::GlobalUser, toml::Manifest};
use crate::terminal::message::{Message, Output, StdOut};
Expand Down Expand Up @@ -30,10 +30,8 @@ pub fn publish(
let manifest = Manifest::new(&cli_params.config)?;
let mut target = manifest.get_target(cli_params.environment.as_deref(), false)?;

if let Some(migration) = migration.into_migration_config() {
target.migrations = Some(Migrations {
migrations: vec![migration],
});
if let Some(migration) = migration.into_migrations() {
target.migrations = Some(migration);
}

let output = if output.as_deref() == Some("json") {
Expand Down
56 changes: 55 additions & 1 deletion src/commands/publish.rs
Expand Up @@ -3,13 +3,15 @@ use std::path::Path;

use anyhow::Result;
use indicatif::{ProgressBar, ProgressStyle};
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};

use crate::build::build_target;
use crate::deploy::{self, DeploymentSet};
use crate::http::{self, Feature};
use crate::kv::bulk;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::migrations::{MigrationTag, Migrations};
use crate::settings::toml::Target;
use crate::sites;
use crate::terminal::emoji;
Expand Down Expand Up @@ -50,11 +52,21 @@ pub fn publish(
Err(e) => Err(e),
}?;

// We verify early here, so we don't perform pre-upload tasks if the upload will fail
if let Some(build_config) = &target.build {
build_config.verify_upload_dir()?;
}

if target.migrations.is_some() {
// Can't do this in the if below, since that one takes a mutable borrow on target
let client = http::legacy_auth_client(user);
let script_migration_tag = get_migration_tag(&client, target)?;

match target.migrations.as_mut().unwrap() {
Migrations::Adhoc { script_tag, .. } => *script_tag = script_migration_tag,
Migrations::List { script_tag, .. } => *script_tag = script_migration_tag,
};
}

if let Some(site_config) = &target.site {
let path = &site_config.bucket.clone();
validate_bucket_location(path)?;
Expand Down Expand Up @@ -216,3 +228,45 @@ fn validate_target_required_fields_present(target: &Target) -> Result<()> {

Ok(())
}

fn get_migration_tag(client: &Client, target: &Target) -> Result<MigrationTag, anyhow::Error> {
// Today, the easiest way to get metadata about a script (including the migration tag)
// is the list endpoint, as the individual script endpoint just returns the source code for a
// given script (and doesn't work at all for DOs). Once we add an individual script metadata
// endpoint, we could use that here instead of listing all of the scripts. Listing isn't too bad
// today though, as most accounts are limited to 30 scripts anyways.

let addr = format!(
"https://api.cloudflare.com/client/v4/accounts/{}/workers/scripts",
target.account_id.load()?
);

let res: ListScriptsV4ApiResponse = client.get(&addr).send()?.json()?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we talked about it, but I'd personally leave a comment here on why we have to list the scripts here rather than something more targeted. It'll help some poor future developer understand why we're doing this and whether it's safe to change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll add one


let tag = match res.result.into_iter().find(|s| s.id == target.name) {
Some(ScriptResponse {
migration_tag: Some(tag),
..
}) => MigrationTag::HasTag(tag),
Some(ScriptResponse {
migration_tag: None,
..
}) => MigrationTag::NoTag,
None => MigrationTag::NoScript,
};

log::info!("Current MigrationTag: {:#?}", tag);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this just here for debugging purposes or is the intent to leave it in? I'm fine either way, just calling it out in case it wasn't intentional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these only show up with RUST_LOG set -- it's nice to have around for issues that get filed


Ok(tag)
}

#[derive(Debug, Deserialize)]
struct ListScriptsV4ApiResponse {
pub result: Vec<ScriptResponse>,
}

#[derive(Debug, Deserialize)]
struct ScriptResponse {
pub id: String,
pub migration_tag: Option<String>,
}
7 changes: 6 additions & 1 deletion src/settings/toml/manifest.rs
Expand Up @@ -11,6 +11,7 @@ use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use serde_with::rust::string_empty_as_none;

use super::migrations::{MigrationConfig, MigrationTag, Migrations};
use super::UsageModel;
use crate::commands::whoami::fetch_accounts;
use crate::commands::{validate_worker_name, whoami, DEFAULT_CONFIG_PATH};
Expand Down Expand Up @@ -57,6 +58,7 @@ pub struct Manifest {
pub kv_namespaces: Option<Vec<ConfigKvNamespace>>,
pub triggers: Option<Triggers>,
pub durable_objects: Option<DurableObjects>,
pub migrations: Option<Vec<MigrationConfig>>,
#[serde(default, with = "string_empty_as_none")]
pub usage_model: Option<UsageModel>,
pub compatibility_date: Option<String>,
Expand Down Expand Up @@ -369,7 +371,10 @@ impl Manifest {
name: self.name.clone(), // Inherited
kv_namespaces: get_namespaces(self.kv_namespaces.clone(), preview)?, // Not inherited
durable_objects: self.durable_objects.clone(), // Not inherited
migrations: None, // TODO(soon) Allow migrations in wrangler.toml
migrations: self.migrations.as_ref().map(|migrations| Migrations::List {
script_tag: MigrationTag::Unknown,
migrations: migrations.clone(),
}), // Top Level
site: self.site.clone(), // Inherited
vars: self.vars.clone(), // Not inherited
text_blobs: self.text_blobs.clone(), // Inherited
Expand Down