This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
Tagged Durable Objects Migrations #1992
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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)?; | ||
|
@@ -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()?; | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these only show up with |
||
|
||
Ok(tag) | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct ListScriptsV4ApiResponse { | ||
pub result: Vec<ScriptResponse>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct ScriptResponse { | ||
pub id: String, | ||
pub migration_tag: Option<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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