mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-17 16:20:25 +00:00
feat: add DISABLE_RT_SYNC to stop resource type syncs and seeds (#12188)
* feat: add DISABLE_RT_SYNC to stop every resource type sync and migration seed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test: pin that the migration seed strip removes whole statements Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e676b091d8
commit
7ca618f704
@@ -18,6 +18,7 @@ pub use windmill_common::db::DB;
|
||||
use windmill_common::{
|
||||
error::Error,
|
||||
global_settings::INSTANCE_PYTHON_VERSION_SETTING,
|
||||
hub_resource_types::DISABLE_RT_SYNC,
|
||||
utils::{generate_lock_id, GIT_VERSION},
|
||||
worker::PyVAlias,
|
||||
};
|
||||
@@ -126,6 +127,12 @@ lazy_static::lazy_static! {
|
||||
"../../migrations/20261012213357_widen_variable_value.up.sql"
|
||||
).replace("ALTER TABLE variable ALTER COLUMN value TYPE VARCHAR(15040);", "")),
|
||||
].into_iter().collect();
|
||||
|
||||
/// A whole `INSERT INTO resource_type` statement, quoted `;` included, which
|
||||
/// `DISABLE_RT_SYNC` strips from migrations. Matched rather than marked in the files:
|
||||
/// editing an applied migration changes its checksum, which sqlx refuses at startup.
|
||||
static ref RESOURCE_TYPE_SEED: regex::Regex =
|
||||
regex::Regex::new(r"(?i)INSERT\s+INTO\s+resource_type\b(?:[^;']|'(?:[^']|'')*')*;").unwrap();
|
||||
}
|
||||
|
||||
pub struct CustomMigrator {
|
||||
@@ -244,7 +251,21 @@ impl Migrate for CustomMigrator {
|
||||
);
|
||||
|
||||
|
||||
if let Some(migration_sql) = OVERRIDDEN_MIGRATIONS.get(&migration.version) {
|
||||
let mut overridden = OVERRIDDEN_MIGRATIONS
|
||||
.get(&migration.version)
|
||||
.map(|sql| std::borrow::Cow::Borrowed(sql.as_str()));
|
||||
if *DISABLE_RT_SYNC {
|
||||
let sql = overridden.as_deref().unwrap_or(&migration.sql);
|
||||
if let std::borrow::Cow::Owned(stripped) = RESOURCE_TYPE_SEED.replace_all(sql, "") {
|
||||
tracing::info!(
|
||||
"DISABLE_RT_SYNC: not seeding the resource types of migration {}",
|
||||
migration.version
|
||||
);
|
||||
overridden = Some(std::borrow::Cow::Owned(stripped));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(migration_sql) = overridden {
|
||||
tracing::info!("Using custom migration for version {}", migration.version);
|
||||
|
||||
if migration_sql.contains("CONCURRENTLY") {
|
||||
@@ -271,7 +292,7 @@ impl Migrate for CustomMigrator {
|
||||
}
|
||||
}
|
||||
} else if !migration_sql.is_empty() {
|
||||
self.inner.execute(&**migration_sql).await?;
|
||||
self.inner.execute(&*migration_sql).await?;
|
||||
}
|
||||
let _ = sqlx::query(
|
||||
r#"
|
||||
@@ -502,3 +523,15 @@ pub async fn wait_for_migrations(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn resource_type_seed_is_stripped_whole() {
|
||||
// Its description carries a `;` inside a quoted string, and statements follow the seed.
|
||||
let sql = include_str!("../../migrations/20260831120907_skills_resource_type.up.sql");
|
||||
let stripped = super::RESOURCE_TYPE_SEED.replace_all(sql, "");
|
||||
assert!(!stripped.to_lowercase().contains("insert into resource_type"));
|
||||
assert!(stripped.contains("INSERT INTO folder"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,13 @@ use crate::{
|
||||
DB, DEFAULT_HUB_BASE_URL, HUB_BASE_URL,
|
||||
};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
/// `DISABLE_RT_SYNC`: nothing writes the `admins` types but the instance's own users, so no
|
||||
/// sync of any kind runs and migrations skip the types they would seed.
|
||||
pub static ref DISABLE_RT_SYNC: bool = std::env::var("DISABLE_RT_SYNC")
|
||||
.is_ok_and(|v| v.eq_ignore_ascii_case("true") || v == "1");
|
||||
}
|
||||
|
||||
const SYNC_INTERVAL: chrono::TimeDelta = chrono::TimeDelta::hours(24);
|
||||
const RETRY_INTERVAL: chrono::TimeDelta = chrono::TimeDelta::hours(1);
|
||||
const CHECK_INTERVAL: std::time::Duration = std::time::Duration::from_secs(600);
|
||||
@@ -352,6 +359,11 @@ async fn sync_from_hub(db: &DB) -> Result<SyncOutcome> {
|
||||
/// Syncs from the configured hub, falling back to the image's cache when the hub is disabled
|
||||
/// or unreachable and the instance has never synced from a hub.
|
||||
pub async fn sync(db: &DB) -> Result<SyncOutcome> {
|
||||
if *DISABLE_RT_SYNC {
|
||||
return Err(Error::BadRequest(
|
||||
"Resource type sync is disabled on this instance".to_string(),
|
||||
));
|
||||
}
|
||||
let hub_err = if setting_enabled(db, DISABLE_HUB_SETTING).await? {
|
||||
Error::BadRequest("The hub is disabled on this instance".to_string())
|
||||
} else {
|
||||
@@ -386,6 +398,9 @@ async fn fall_back_to_image_cache(db: &DB, path: &str) -> Result<Option<SyncOutc
|
||||
/// hub, a private hub listing only its own types included, takes each new image's public types.
|
||||
/// `None` when the image carries no cache.
|
||||
pub async fn sync_from_image_cache(db: &DB) -> Result<Option<SyncOutcome>> {
|
||||
if *DISABLE_RT_SYNC {
|
||||
return Ok(None);
|
||||
}
|
||||
sync_from_cache_file(db, &cache_path()).await
|
||||
}
|
||||
|
||||
@@ -433,7 +448,8 @@ pub fn spawn_daily_sync(db: DB, mut killpill_rx: tokio::sync::broadcast::Receive
|
||||
}
|
||||
|
||||
async fn sync_if_due(db: &DB) -> Result<()> {
|
||||
if !setting_enabled(db, SYNC_HUB_RESOURCE_TYPES_DAILY_SETTING).await?
|
||||
if *DISABLE_RT_SYNC
|
||||
|| !setting_enabled(db, SYNC_HUB_RESOURCE_TYPES_DAILY_SETTING).await?
|
||||
|| setting_enabled(db, DISABLE_HUB_SETTING).await?
|
||||
{
|
||||
return Ok(());
|
||||
|
||||
Reference in New Issue
Block a user