fix: widen owner columns at runtime instead of in a migration

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-26 17:57:54 +01:00
co-authored by Claude Opus 5.5
parent 1e3e92942c
commit fb44cef0ff
23 changed files with 242 additions and 167 deletions
@@ -0,0 +1,58 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT \n owner::text AS \"owner!\",\n premium,\n usage.usage as \"usage?\", \n workspace_settings.customer_id, \n workspace_settings.plan, \n COALESCE(cw.is_past_due, false) as \"is_past_due!\", \n cw.max_tolerated_executions\n FROM workspace\n LEFT JOIN workspace_settings \n ON workspace_settings.workspace_id = $1\n LEFT JOIN usage \n ON usage.id = $1\n AND month_ = EXTRACT(YEAR FROM current_date) * 12 + EXTRACT(MONTH FROM current_date)\n AND usage.is_workspace IS true\n LEFT JOIN cloud_workspace_settings cw\n ON cw.workspace_id = $1\n WHERE workspace.id = $1\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "owner!",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "premium",
"type_info": "Bool"
},
{
"ordinal": 2,
"name": "usage?",
"type_info": "Int4"
},
{
"ordinal": 3,
"name": "customer_id",
"type_info": "Varchar"
},
{
"ordinal": 4,
"name": "plan",
"type_info": "Varchar"
},
{
"ordinal": 5,
"name": "is_past_due!",
"type_info": "Bool"
},
{
"ordinal": 6,
"name": "max_tolerated_executions",
"type_info": "Int4"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
null,
false,
false,
true,
true,
null,
true
]
},
"hash": "10f4db109ccc6bc9dda756472f5fd1e975d6d01bb50ea8b0d1e160c95ed35ba6"
}
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT\n workspace.id AS \"id!\",\n workspace.name AS \"name!\",\n workspace.owner AS \"owner!\",\n workspace.deleted AS \"deleted!\",\n workspace.premium AS \"premium!\",\n workspace_settings.color AS \"color\",\n workspace.parent_workspace_id AS \"parent_workspace_id\",\n workspace.is_dev_workspace AS \"is_dev_workspace!\",\n workspace.dev_workspace_label AS \"dev_workspace_label\"\n FROM workspace\n LEFT JOIN workspace_settings ON workspace.id = workspace_settings.workspace_id\n LIMIT $1 OFFSET $2",
"query": "SELECT\n workspace.id AS \"id!\",\n workspace.name AS \"name!\",\n workspace.owner::text AS \"owner!\",\n workspace.deleted AS \"deleted!\",\n workspace.premium AS \"premium!\",\n workspace_settings.color AS \"color\",\n workspace.parent_workspace_id AS \"parent_workspace_id\",\n workspace.is_dev_workspace AS \"is_dev_workspace!\",\n workspace.dev_workspace_label AS \"dev_workspace_label\"\n FROM workspace\n LEFT JOIN workspace_settings ON workspace.id = workspace_settings.workspace_id\n LIMIT $1 OFFSET $2",
"describe": {
"columns": [
{
@@ -16,7 +16,7 @@
{
"ordinal": 2,
"name": "owner!",
"type_info": "Varchar"
"type_info": "Text"
},
{
"ordinal": 3,
@@ -58,7 +58,7 @@
"nullable": [
false,
false,
false,
null,
false,
false,
true,
@@ -67,5 +67,5 @@
true
]
},
"hash": "8c33cbe3dc0a7539835fab61f981f5117da38c3410ad6e34ad67f5441deaf89e"
"hash": "1545c3c0826aed7c69d5c91dd065536f8bea1f5724fa689f934e6321dedb876b"
}
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT \n trigger_config AS \"trigger_config: _\", \n owner, \n email\n FROM \n capture_config\n WHERE \n workspace_id = $1\n AND path = $2\n AND is_flow = $3\n AND trigger_kind = $4\n AND last_client_ping > NOW() - INTERVAL '10 seconds'\n AND (\n $5::bool IS FALSE\n OR (\n trigger_config IS NOT NULL\n AND trigger_config ->> 'delivery_type' = 'push'\n )\n )\n ",
"query": "\n SELECT \n trigger_config AS \"trigger_config: _\", \n owner::text AS \"owner!\",\n email\n FROM \n capture_config\n WHERE \n workspace_id = $1\n AND path = $2\n AND is_flow = $3\n AND trigger_kind = $4\n AND last_client_ping > NOW() - INTERVAL '10 seconds'\n AND (\n $5::bool IS FALSE\n OR (\n trigger_config IS NOT NULL\n AND trigger_config ->> 'delivery_type' = 'push'\n )\n )\n ",
"describe": {
"columns": [
{
@@ -10,8 +10,8 @@
},
{
"ordinal": 1,
"name": "owner",
"type_info": "Varchar"
"name": "owner!",
"type_info": "Text"
},
{
"ordinal": 2,
@@ -54,9 +54,9 @@
},
"nullable": [
true,
false,
null,
false
]
},
"hash": "23419adcd74c326d716527293eff518b42f4cdb33e034441015494bd26c172d2"
"hash": "35ea74b6a50f7755129595951ac04d93e7406f8fb099234b8c825960d2d75a44"
}
@@ -1,41 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT \n owner, \n email,\n path,\n is_flow\n FROM \n capture_config\n WHERE \n workspace_id = $1\n AND trigger_config->>'local_part' = $2\n AND trigger_kind = 'email'::TRIGGER_KIND \n AND last_client_ping > NOW() - INTERVAL '10 seconds'\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "owner",
"type_info": "Varchar"
},
{
"ordinal": 1,
"name": "email",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "path",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "is_flow",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Text",
"Text"
]
},
"nullable": [
false,
false,
false,
false
]
},
"hash": "4c70aac8f83e9b76628a65df351c16cb630aa963d20898b084b043a59bfc4ba8"
}
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT workspace.id, workspace.name, workspace.owner, workspace.deleted, workspace.premium, workspace_settings.color, workspace.parent_workspace_id, workspace.is_dev_workspace, workspace.dev_workspace_label\n FROM workspace\n LEFT JOIN workspace_settings ON workspace.id = workspace_settings.workspace_id\n JOIN usr ON usr.workspace_id = workspace.id\n WHERE usr.email = $1 AND workspace.deleted = false",
"query": "SELECT workspace.id, workspace.name, workspace.owner::text AS \"owner!\", workspace.deleted, workspace.premium, workspace_settings.color, workspace.parent_workspace_id, workspace.is_dev_workspace, workspace.dev_workspace_label\n FROM workspace\n LEFT JOIN workspace_settings ON workspace.id = workspace_settings.workspace_id\n JOIN usr ON usr.workspace_id = workspace.id\n WHERE usr.email = $1 AND workspace.deleted = false",
"describe": {
"columns": [
{
@@ -15,8 +15,8 @@
},
{
"ordinal": 2,
"name": "owner",
"type_info": "Varchar"
"name": "owner!",
"type_info": "Text"
},
{
"ordinal": 3,
@@ -57,7 +57,7 @@
"nullable": [
false,
false,
false,
null,
false,
false,
true,
@@ -66,5 +66,5 @@
true
]
},
"hash": "4f266373d5acdbcedc81e8ed3eb1fb95861ef17ca5570ef06b9903f04e9adb5e"
"hash": "5e614e236ebc473c140e1a74fab44991e97cddfe3da972768f8d469bcd683171"
}
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT\n workspace.id AS \"id!\",\n workspace.name AS \"name!\",\n workspace.owner AS \"owner!\",\n workspace.deleted AS \"deleted!\",\n workspace.premium AS \"premium!\",\n workspace_settings.color AS \"color\",\n workspace.parent_workspace_id AS \"parent_workspace_id\",\n workspace.is_dev_workspace AS \"is_dev_workspace!\",\n workspace.dev_workspace_label AS \"dev_workspace_label\"\n FROM workspace\n LEFT JOIN workspace_settings ON workspace.id = workspace_settings.workspace_id\n WHERE workspace.id = $1",
"query": "SELECT\n workspace.id AS \"id!\",\n workspace.name AS \"name!\",\n workspace.owner::text AS \"owner!\",\n workspace.deleted AS \"deleted!\",\n workspace.premium AS \"premium!\",\n workspace_settings.color AS \"color\",\n workspace.parent_workspace_id AS \"parent_workspace_id\",\n workspace.is_dev_workspace AS \"is_dev_workspace!\",\n workspace.dev_workspace_label AS \"dev_workspace_label\"\n FROM workspace\n LEFT JOIN workspace_settings ON workspace.id = workspace_settings.workspace_id\n WHERE workspace.id = $1",
"describe": {
"columns": [
{
@@ -16,7 +16,7 @@
{
"ordinal": 2,
"name": "owner!",
"type_info": "Varchar"
"type_info": "Text"
},
{
"ordinal": 3,
@@ -57,7 +57,7 @@
"nullable": [
false,
false,
false,
null,
false,
false,
true,
@@ -66,5 +66,5 @@
true
]
},
"hash": "1c472a4740a41ae8985f865e15b0c4edb4d5af2281c22955121cb794428c3cde"
"hash": "78292b889c8611e6c672eb47d8c89f4dcea1e101d2b5d912ae2b22c58bbeb317"
}
@@ -1,22 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "SELECT owner FROM workspace WHERE id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "owner",
"type_info": "Varchar"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
false
]
},
"hash": "93fc5c228ec6d32594b32c5f56bfb3f93779c528c235dda83b68beea71463f8e"
}
@@ -1,12 +1,12 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT\n owner,\n email\n FROM\n capture_config\n WHERE\n workspace_id = $1\n AND path = $2\n AND is_flow = $3\n AND trigger_kind = $4\n AND last_client_ping > NOW() - INTERVAL '10 seconds'\n ",
"query": "\n SELECT\n owner::text AS \"owner!\",\n email\n FROM\n capture_config\n WHERE\n workspace_id = $1\n AND path = $2\n AND is_flow = $3\n AND trigger_kind = $4\n AND last_client_ping > NOW() - INTERVAL '10 seconds'\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "owner",
"type_info": "Varchar"
"name": "owner!",
"type_info": "Text"
},
{
"ordinal": 1,
@@ -47,9 +47,9 @@
]
},
"nullable": [
false,
null,
false
]
},
"hash": "66a0e51cf149ba532463e29dd361a803e1bced2f8e1a12f8933b7598ee85a147"
"hash": "a50d17532b71370f164568175e495a44904483d5922d6177c435a1e60d29dbdc"
}
@@ -1,58 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT \n owner, \n premium, \n usage.usage as \"usage?\", \n workspace_settings.customer_id, \n workspace_settings.plan, \n COALESCE(cw.is_past_due, false) as \"is_past_due!\", \n cw.max_tolerated_executions\n FROM workspace\n LEFT JOIN workspace_settings \n ON workspace_settings.workspace_id = $1\n LEFT JOIN usage \n ON usage.id = $1\n AND month_ = EXTRACT(YEAR FROM current_date) * 12 + EXTRACT(MONTH FROM current_date)\n AND usage.is_workspace IS true\n LEFT JOIN cloud_workspace_settings cw\n ON cw.workspace_id = $1\n WHERE workspace.id = $1\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "owner",
"type_info": "Varchar"
},
{
"ordinal": 1,
"name": "premium",
"type_info": "Bool"
},
{
"ordinal": 2,
"name": "usage?",
"type_info": "Int4"
},
{
"ordinal": 3,
"name": "customer_id",
"type_info": "Varchar"
},
{
"ordinal": 4,
"name": "plan",
"type_info": "Varchar"
},
{
"ordinal": 5,
"name": "is_past_due!",
"type_info": "Bool"
},
{
"ordinal": 6,
"name": "max_tolerated_executions",
"type_info": "Int4"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
false,
false,
false,
true,
true,
null,
true
]
},
"hash": "ac4a375035049304db7ae084b2d1789afb8250adde6f38f458b115e787ed876f"
}
@@ -1,12 +1,12 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT \n owner, \n email\n FROM \n capture_config\n WHERE \n workspace_id = $1 \n AND path = $2 \n AND is_flow = $3 \n AND trigger_kind = $4 \n AND last_client_ping > NOW() - INTERVAL '10 seconds'\n ",
"query": "\n SELECT \n owner::text AS \"owner!\",\n email\n FROM \n capture_config\n WHERE \n workspace_id = $1 \n AND path = $2 \n AND is_flow = $3 \n AND trigger_kind = $4 \n AND last_client_ping > NOW() - INTERVAL '10 seconds'\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "owner",
"type_info": "Varchar"
"name": "owner!",
"type_info": "Text"
},
{
"ordinal": 1,
@@ -47,9 +47,9 @@
]
},
"nullable": [
false,
null,
false
]
},
"hash": "87564a196a1662f524407d853db506bf08c28efe82b68b3d44bafbd3d0e91c29"
"hash": "c62d88065070ea456c2dac79276a414114590c568e365bb54d5df10832dbf817"
}
@@ -0,0 +1,41 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT \n owner::text AS \"owner!\",\n email,\n path,\n is_flow\n FROM \n capture_config\n WHERE \n workspace_id = $1\n AND trigger_config->>'local_part' = $2\n AND trigger_kind = 'email'::TRIGGER_KIND \n AND last_client_ping > NOW() - INTERVAL '10 seconds'\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "owner!",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "email",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "path",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "is_flow",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Text",
"Text"
]
},
"nullable": [
null,
false,
false,
false
]
},
"hash": "e6836a8ca5f0b18e6c392ca1281643a01729964f7eb9d75e0910b9cb65d7e3e4"
}
@@ -0,0 +1,22 @@
{
"db_name": "PostgreSQL",
"query": "SELECT owner::text AS \"owner!\" FROM workspace WHERE id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "owner!",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
null
]
},
"hash": "f31d801c977091db48f9a468881a8da967b7d94a27a5e06afe5de874b1f2fe61"
}
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT workspace.id, workspace.name, usr.username, workspace_settings.color, workspace.parent_workspace_id,\n workspace.is_dev_workspace, workspace.dev_workspace_label,\n workspace.owner AS \"created_by?\",\n usr.is_admin, usr.operator, usr.workspace_role,\n usr.disabled, usr.is_service_account\n FROM workspace\n JOIN usr ON usr.workspace_id = workspace.id\n JOIN workspace_settings ON workspace_settings.workspace_id = workspace.id\n WHERE usr.email = $1 AND workspace.deleted = false",
"query": "SELECT workspace.id, workspace.name, usr.username, workspace_settings.color, workspace.parent_workspace_id,\n workspace.is_dev_workspace, workspace.dev_workspace_label,\n workspace.owner::text AS \"created_by?\",\n usr.is_admin, usr.operator, usr.workspace_role,\n usr.disabled, usr.is_service_account\n FROM workspace\n JOIN usr ON usr.workspace_id = workspace.id\n JOIN workspace_settings ON workspace_settings.workspace_id = workspace.id\n WHERE usr.email = $1 AND workspace.deleted = false",
"describe": {
"columns": [
{
@@ -41,7 +41,7 @@
{
"ordinal": 7,
"name": "created_by?",
"type_info": "Varchar"
"type_info": "Text"
},
{
"ordinal": 8,
@@ -82,7 +82,7 @@
true,
false,
true,
false,
null,
false,
false,
true,
@@ -90,5 +90,5 @@
false
]
},
"hash": "2e8a53e73c3b5c2662efd0e2876f168c2f79258fb900199e8575973d5b3d32bb"
"hash": "fd235f2fe3deb3c5b57bccff2dde5fd5712e00c8c0a674bc6ccdca5782ef3a62"
}
+1 -1
View File
@@ -1 +1 @@
a403f3ce12fd150dd649098e61562eda1e6a4d81
22a7cdb889e4942e084f1601a54b08be110dd4de
@@ -1 +0,0 @@
-- No-op: narrowing back to VARCHAR(50) would fail on, or require truncating, owners longer than 50 characters.
@@ -1,2 +0,0 @@
ALTER TABLE workspace ALTER COLUMN owner TYPE VARCHAR(255);
ALTER TABLE capture_config ALTER COLUMN owner TYPE VARCHAR(255);
+16
View File
@@ -4745,6 +4745,21 @@ pub async fn monitor_db(
}
};
// Tried every iteration until done: a user whose email is longer than the narrow column
// cannot create a workspace before it is.
let widen_owner_columns_f = async {
if server_mode
&& iteration.is_some()
&& windmill_common::workspaces::owner_columns_widen_pending()
{
if let Some(db) = conn.as_sql() {
if let Err(e) = windmill_common::workspaces::widen_owner_columns(db).await {
tracing::warn!("could not widen the owner columns, retrying later: {e:#}");
}
}
}
};
// Delete the AI session backups older than their workspace's retention. Every ~40 min
// (240 iterations at the default 10 s, the most a u8 `should_run` counts): the retention
// counts in days. Spawned for the same reason as the credential maintenance above, a
@@ -4841,6 +4856,7 @@ pub async fn monitor_db(
datatable_role_set_sweep_f,
ai_session_retention_f,
legacy_secret_upgrade_f,
widen_owner_columns_f,
pipeline_freshness_watchdog_f,
reconcile_unarmed_schedules_f,
stranded_jobs_f,
@@ -983,7 +983,7 @@ async fn list_workspaces(
let mut tx = user_db.begin(&authed).await?;
let workspaces = sqlx::query_as!(
Workspace,
"SELECT workspace.id, workspace.name, workspace.owner, workspace.deleted, workspace.premium, workspace_settings.color, workspace.parent_workspace_id, workspace.is_dev_workspace, workspace.dev_workspace_label
"SELECT workspace.id, workspace.name, workspace.owner::text AS \"owner!\", workspace.deleted, workspace.premium, workspace_settings.color, workspace.parent_workspace_id, workspace.is_dev_workspace, workspace.dev_workspace_label
FROM workspace
LEFT JOIN workspace_settings ON workspace.id = workspace_settings.workspace_id
JOIN usr ON usr.workspace_id = workspace.id
@@ -7066,7 +7066,7 @@ async fn get_workspace_as_superadmin(
"SELECT
workspace.id AS \"id!\",
workspace.name AS \"name!\",
workspace.owner AS \"owner!\",
workspace.owner::text AS \"owner!\",
workspace.deleted AS \"deleted!\",
workspace.premium AS \"premium!\",
workspace_settings.color AS \"color\",
@@ -7101,7 +7101,7 @@ async fn list_workspaces_as_super_admin(
"SELECT
workspace.id AS \"id!\",
workspace.name AS \"name!\",
workspace.owner AS \"owner!\",
workspace.owner::text AS \"owner!\",
workspace.deleted AS \"deleted!\",
workspace.premium AS \"premium!\",
workspace_settings.color AS \"color\",
@@ -7127,7 +7127,7 @@ async fn user_workspaces(
let rows = sqlx::query!(
"SELECT workspace.id, workspace.name, usr.username, workspace_settings.color, workspace.parent_workspace_id,
workspace.is_dev_workspace, workspace.dev_workspace_label,
workspace.owner AS \"created_by?\",
workspace.owner::text AS \"created_by?\",
usr.is_admin, usr.operator, usr.workspace_role,
usr.disabled, usr.is_service_account
FROM workspace
@@ -2552,9 +2552,12 @@ async fn is_workspace_owner(
w_id: &str,
tx: &mut Transaction<'_, Postgres>,
) -> Result<bool> {
let owner = sqlx::query_scalar!("SELECT owner FROM workspace WHERE id = $1", w_id)
.fetch_optional(&mut **tx)
.await?;
let owner = sqlx::query_scalar!(
"SELECT owner::text AS \"owner!\" FROM workspace WHERE id = $1",
w_id
)
.fetch_optional(&mut **tx)
.await?;
Ok(owner.map(|o| o == authed.email).unwrap_or(false))
}
+2 -2
View File
@@ -894,7 +894,7 @@ pub async fn get_active_capture_owner_and_email(
ActiveCaptureOwner,
r#"
SELECT
owner,
owner::text AS "owner!",
email
FROM
capture_config
@@ -944,7 +944,7 @@ async fn get_capture_trigger_config_and_owner<T: DeserializeOwned>(
r#"
SELECT
trigger_config AS "trigger_config: _",
owner,
owner::text AS "owner!",
email
FROM
capture_config
@@ -34,6 +34,10 @@ pub const MIN_VERSION_SUPPORTS_AEAD_SECRETS: VC = vc(1, 832, 0, "AES-GCM secret
// closes. Must name the release this ships in.
pub const MIN_VERSION_SUPPORTS_VARIABLE_VALUE_WIDENING: VC =
vc(1, 848, 0, "variable.value widening");
// Gates widening `workspace.owner` and `capture_config.owner`, for the same reason as
// `variable.value` above: every query returning them casts them to text from this release.
// Must name the release this ships in.
pub const MIN_VERSION_SUPPORTS_OWNER_WIDENING: VC = vc(1, 885, 0, "owner column widening");
// A renewed license key replaces the one every process reads, and a worker that predates
// Ed25519 support rejects an Ed25519-signed key and stops pulling jobs, so renewal only offers
// to accept one once this is met. Only worker versions are seen here, not servers. Must name
+55
View File
@@ -4025,6 +4025,61 @@ async fn transform_json_unchecked(
Ok(value)
}
/// `workspace.owner` holds the creator's email, which every email column allows 255 chars of.
const OWNER_WIDTH: i32 = 255;
static OWNER_COLUMNS_WIDENED: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
/// Whether `workspace.owner` and `capture_config.owner` may still have to be widened.
pub fn owner_columns_widen_pending() -> bool {
!OWNER_COLUMNS_WIDENED.load(std::sync::atomic::Ordering::Relaxed)
}
/// Widen `workspace.owner` and `capture_config.owner` to [`OWNER_WIDTH`] once no live process
/// returns them uncast. Not a migration: changing a column's length fails every statement a live
/// process prepared returning it, until its connection closes. Any new query returning either
/// column must select `owner::text`.
pub async fn widen_owner_columns(db: &DB) -> Result<()> {
if !owner_columns_widen_pending()
|| !crate::min_version::MIN_VERSION_SUPPORTS_OWNER_WIDENING.met_conservatively()
|| crate::db::process_uptime() < crate::db::PREVIOUS_RELEASE_ROLLOUT_UPTIME
{
return Ok(());
}
let mut narrow = vec![];
for table in ["workspace", "capture_config"] {
// varchar's typmod is its length + 4, -1 when unbounded.
let typmod: i32 = sqlx::query_scalar(
"SELECT atttypmod FROM pg_attribute WHERE attrelid = $1::regclass AND attname = 'owner'",
)
.bind(table)
.fetch_one(db)
.await?;
if typmod >= 0 && typmod - 4 < OWNER_WIDTH {
narrow.push(table);
}
}
if !narrow.is_empty() {
tracing::info!("widening the owner column of {narrow:?} to VARCHAR({OWNER_WIDTH})");
let mut tx = db.begin().await?;
// Queued behind a long transaction, the ALTER would block every read of the table meanwhile.
sqlx::query("SET LOCAL lock_timeout = '3s'")
.execute(&mut *tx)
.await?;
for table in narrow {
sqlx::query(&format!(
"ALTER TABLE {table} ALTER COLUMN owner TYPE VARCHAR({OWNER_WIDTH})"
))
.execute(&mut *tx)
.await?;
}
tx.commit().await?;
crate::db::retire_connections_opened_before_now();
}
OWNER_COLUMNS_WIDENED.store(true, std::sync::atomic::Ordering::Relaxed);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
+1 -1
View File
@@ -30,7 +30,7 @@ pub async fn get_active_capture_owner_and_email(
ActiveCaptureOwner,
r#"
SELECT
owner,
owner::text AS "owner!",
email
FROM
capture_config