Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(crm): repair copied Sales pipeline column positions (#7759)

teo37b04d285061
diff --git a/crates/crm/tests/sales_backfill.rs b/crates/crm/tests/sales_backfill.rs
index 49adeab975..46a87055cc 100644
--- a/crates/crm/tests/sales_backfill.rs
+++ b/crates/crm/tests/sales_backfill.rs
@@ -1,6 +1,10 @@
//! Migration coverage with real source properties, database constraints and access.
#![cfg(feature = "outbound")]
+use databases::{
+ domain::storage::DatabaseStorageService,
+ outbound::gateway_event_publisher::NoOpTableEventPublisher, wiring::build_service,
+};
use entity_access::{
domain::{
models::{EntityType as AccessEntityType, ViewAccessLevel},
@@ -10,11 +14,14 @@ use entity_access::{
outbound::PgAccessRepository,
};
use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use macro_event_broker::NoopMacroEventBroker;
use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
+use models_databases::{DatabaseId, TableId};
use models_properties::{EntityReference, EntityType, service::property_value::PropertyValue};
use properties::{PropertiesPgRepo, domain::database_cell_writer::DatabaseCellWriter};
use serde_json::json;
use sqlx::PgPool;
+use std::sync::Arc;
use system_properties::{StageOption, SystemPropertyKey};
use uuid::Uuid;
@@ -129,6 +136,18 @@ async fn pipeline(pool: &PgPool, team: Uuid) -> (Uuid, Uuid, Uuid) {
(row.id, row.database_id, row.table_id)
}
+async fn other_database_column(pool: &PgPool, position: &str) -> Uuid {
+ let [database, table, definition, column] = [(); 4].map(|_| macro_uuid::generate_uuid_v7());
+ sqlx::query!("INSERT INTO databases (id) VALUES ($1)", database)
+ .execute(pool)
+ .await
+ .unwrap();
+ sqlx::query!("INSERT INTO database_tables (id, database_id, name, position) VALUES ($1, $2, 'Notes', '80')", table, database).execute(pool).await.unwrap();
+ sqlx::query!("INSERT INTO property_definitions (id, database_id, display_name, data_type, is_multi_select) VALUES ($1, $2, 'Note', 'STRING', false)", definition, database).execute(pool).await.unwrap();
+ sqlx::query!("INSERT INTO database_columns (id, table_id, property_definition_id, position) VALUES ($1, $2, $3, $4)", column, table, definition, position).execute(pool).await.unwrap();
+ column
+}
+
async fn cell(pool: &PgPool, company: Uuid, column: &str) -> Option<serde_json::Value> {
sqlx::query_scalar!(
r#"SELECT ep.values FROM crm_pipeline_entities primary_column
@@ -256,6 +275,44 @@ async fn copies_all_visible_companies_with_values_and_team_access(pool: PgPool)
}
}
+#[sqlx::test(migrations = false)]
+async fn copied_pipelines_open_in_order_and_other_databases_are_untouched(pool: PgPool) {
+ let team = fixture(&pool).await;
+ company(&pool, team, false).await;
+ let other = other_database_column(&pool, "20").await;
+ migrate(&pool).await.unwrap();
+ let (_, database, table) = pipeline(&pool, team).await;
+ let access = Arc::new(EntityAccessServiceImpl::new(PgAccessRepository::new(
+ pool.clone(),
+ )));
+ let tables = build_service(
+ pool.clone(),
+ access,
+ NoOpTableEventPublisher,
+ NoopMacroEventBroker,
+ )
+ .storage_tables(DatabaseId::from_uuid(database))
+ .await
+ .unwrap();
+ assert_eq!(tables.len(), 1);
+ assert_eq!(tables[0].table.id, TableId::from_uuid(table));
+ assert_eq!(
+ tables[0]
+ .columns
+ .iter()
+ .map(|column| column.definition.definition.display_name.as_str())
+ .collect::<Vec<_>>(),
+ ["Company", "Stage", "Owner", "Revenue"]
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT position FROM database_columns WHERE id = $1", other)
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ "20"
+ );
+}
+
#[sqlx::test(migrations = false)]
async fn preserves_custom_stages_and_legacy_rename_mapping(pool: PgPool) {
let team = fixture(&pool).await;
diff --git a/crates/macro_db_client/migrations/20261007223904_repair_sales_column_positions.sql b/crates/macro_db_client/migrations/20261007223904_repair_sales_column_positions.sql
new file mode 100644
index 0000000000..f5b2798ceb
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261007223904_repair_sales_column_positions.sql
@@ -0,0 +1,9 @@
+-- A column position is a fractional-index key, written as lowercase hex that
+-- always ends in '80'. The Sales copy (20261007172435) stored Company, Stage,
+-- and Owner at '20', '40', and '60'. Those are not keys, so reading a copied
+-- Sales pipeline fails. Appending '80' gives '2080', '4080', and '6080'. They
+-- are valid keys and keep the copied order ahead of Revenue at '80'.
+UPDATE database_columns
+SET position = position || '80'
+WHERE position IN ('20', '40', '60')
+ AND table_id IN (SELECT table_id FROM crm_pipeline_entities);