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(databases): mark current column types and hide impossible conversions (#7412)

Wolf Mermelstein78f84be8e1ef
diff --git a/apps/web/src/features/block-database/components/column-type-menu.test.tsx b/apps/web/src/features/block-database/components/column-type-menu.test.tsx
index e62fa18f4f..ff55d6a526 100644
--- a/apps/web/src/features/block-database/components/column-type-menu.test.tsx
+++ b/apps/web/src/features/block-database/components/column-type-menu.test.tsx
@@ -93,7 +93,7 @@ afterEach(() => {
vi.unstubAllGlobals();
});
-function renderHeader() {
+function renderHeader(shownColumn = column, shownCasts = casts) {
const changeType = vi.fn<
(columnId: string, change: DatabaseColumnTypeChange) => DatabaseSchemaChange
>(() => okAsync(undefined));
@@ -106,7 +106,7 @@ function renderHeader() {
const opened: string[] = [];
render(() => (
<DatabaseColumnHeader
- column={column}
+ column={shownColumn}
canRename
onRename={vi.fn(() => okAsync(undefined))}
onSort={vi.fn()}
@@ -114,7 +114,7 @@ function renderHeader() {
onConvert={convert}
columnCasts={(columnId, open) => () => {
if (open()) opened.push(columnId);
- return casts;
+ return shownCasts;
}}
/>
));
@@ -143,6 +143,65 @@ it('lists only the types the column can become', async () => {
expect(opened).toContain('price');
});
+it.each([
+ ['SELECT_STRING', false, 'Select'],
+ ['SELECT_NUMBER', false, 'Select'],
+ ['SELECT_NUMBER', true, 'Multi-select'],
+ ['TAG', true, 'Multi-select'],
+] as const)(
+ 'marks %s, multi=%s as current without rewriting it',
+ async (dataType, isMultiSelect, label) => {
+ const { changeType, convert } = renderHeader({
+ ...column,
+ dataType,
+ isMultiSelect,
+ });
+ await openTypeMenu();
+ const current = await screen.findByRole('menuitem', {
+ name: label,
+ current: true,
+ });
+ expect(current.querySelector('svg:last-child')).toBeTruthy();
+ choose(current);
+ expect(changeType).not.toHaveBeenCalled();
+ expect(convert).not.toHaveBeenCalled();
+ }
+);
+
+it.each(['USER', 'DOCUMENT', 'TASK'] as const)(
+ 'marks a multi-valued %s reference as current without rewriting it',
+ async (specificEntityType) => {
+ const { changeType, convert } = renderHeader({
+ ...column,
+ dataType: 'ENTITY',
+ specificEntityType,
+ isMultiSelect: true,
+ });
+ await openTypeMenu();
+ const current = await screen.findByRole('menuitem', { current: true });
+ choose(current);
+ expect(changeType).not.toHaveBeenCalled();
+ expect(convert).not.toHaveBeenCalled();
+ }
+);
+
+it('keeps the current type visible when conversion is blocked', async () => {
+ renderHeader(column, {
+ status: 'ready',
+ version: 4,
+ casts: [
+ {
+ target: { dataType: 'STRING', isMultiSelect: false, relation: false },
+ cast: { verdict: 'never', reason: 'A board groups by this column.' },
+ },
+ ],
+ });
+ await openTypeMenu();
+ expect(
+ await screen.findByRole('menuitem', { name: 'Text', current: true })
+ ).toBeTruthy();
+});
+
it('offers a checked type with failures as a new column, never as a type change', async () => {
const { changeType, convert } = renderHeader();
await openTypeMenu();
diff --git a/apps/web/src/features/block-database/components/column-type-menu.tsx b/apps/web/src/features/block-database/components/column-type-menu.tsx
index 3912e3f783..f15f10d9e3 100644
--- a/apps/web/src/features/block-database/components/column-type-menu.tsx
+++ b/apps/web/src/features/block-database/components/column-type-menu.tsx
@@ -57,17 +57,36 @@ export function ColumnTypeMenu(props: {
const castOf = (to: DatabaseColumnKind) =>
casts ? castFor(casts(), to) : undefined;
const selected = (to: DatabaseColumnKind) => {
+ if (to.type === 'relation')
+ return props.column.relation?.tableId === to.table;
const target = castTargetOf(to);
+ // Numeric options and tags use the same single/multiple choice controls.
+ const dataType =
+ props.column.dataType === 'SELECT_NUMBER' ||
+ props.column.dataType === 'TAG'
+ ? 'SELECT_STRING'
+ : props.column.dataType;
return (
!props.column.relation &&
- props.column.dataType === target.dataType &&
+ dataType === target.dataType &&
props.column.isMultiSelect === target.isMultiSelect &&
(props.column.specificEntityType ?? undefined) ===
target.specificEntityType
);
};
- /** A type no value converts to is left out; until the dry run answers, nothing is listed. */
+ // Keep a multi-valued reference column represented by its current menu item.
+ const menuTypes = () =>
+ types.map((item) =>
+ item.to.type === 'entity' &&
+ props.column.dataType === 'ENTITY' &&
+ !props.column.relation &&
+ props.column.specificEntityType === item.to.target
+ ? { ...item, to: { ...item.to, multi: props.column.isMultiSelect } }
+ : item
+ );
+ /** Keep the current type visible; leave out types no value converts to. */
const offered = (to: DatabaseColumnKind) => {
+ if (selected(to)) return true;
const cast = castOf(to);
if (cast?.verdict === 'never') return false;
return !misfits(cast) || !!props.onConvertToNewColumn;
@@ -78,6 +97,7 @@ export function ColumnTypeMenu(props: {
offered({ type: 'relation', table: table.id })
);
const choose = (label: string, to: DatabaseColumnKind) => {
+ if (selected(to)) return;
const cast = castOf(to);
if (misfits(cast)) {
props.onConvertToNewColumn?.({ to, label, cast });
@@ -108,7 +128,7 @@ export function ColumnTypeMenu(props: {
</Show>
<Show when={!checking()}>
<Dropdown.Group>
- <For each={types.filter((type) => offered(type.to))}>
+ <For each={menuTypes().filter((type) => offered(type.to))}>
{(type) => (
<TypeItem
label={type.label}
@@ -139,7 +159,7 @@ export function ColumnTypeMenu(props: {
label={table.name}
cast={castOf(to)}
icon={<PropertyIcon type="ENTITY" relation />}
- selected={props.column.relation?.tableId === table.id}
+ selected={selected(to)}
onSelect={() => choose(table.name, to)}
/>
);
@@ -176,7 +196,10 @@ function TypeItem(props: {
.join(' · ');
};
return (
- <Dropdown.Item onSelect={props.onSelect}>
+ <Dropdown.Item
+ aria-current={props.selected ? 'true' : undefined}
+ onSelect={props.onSelect}
+ >
{props.icon}
<span class="flex min-w-0 flex-1 flex-col">
<Dropdown.ItemLabel class="truncate">{props.label}</Dropdown.ItemLabel>
diff --git a/crates/databases/src/domain/service/casts.rs b/crates/databases/src/domain/service/casts.rs
index 0419517230..f91bc59cde 100644
--- a/crates/databases/src/domain/service/casts.rs
+++ b/crates/databases/src/domain/service/casts.rs
@@ -110,6 +110,10 @@ where
casts[index].failures = converter.failures();
casts[index].summary = converter.summary();
casts[index].examples = converter.examples();
+ if converter.cells.is_empty() && converter.failures() > 0 {
+ casts[index].cast = CastVerdict::Never;
+ casts[index].reason = converter.summary();
+ }
}
Ok(casts)
}
diff --git a/crates/databases/src/domain/service/test/casts.rs b/crates/databases/src/domain/service/test/casts.rs
index 5b55160878..a13b1c4380 100644
--- a/crates/databases/src/domain/service/test/casts.rs
+++ b/crates/databases/src/domain/service/test/casts.rs
@@ -599,6 +599,11 @@ async fn the_dry_run_answers_every_menu_target_for_a_viewer() {
examples: examples.iter().map(|example| example.to_string()).collect(),
..target(data_type, is_multi_select)
};
+ let no_values_fit = |data_type, summary: &str, examples: &[&str]| ColumnCast {
+ cast: CastVerdict::Never,
+ reason: Some(summary.to_owned()),
+ ..checked(data_type, false, 3, Some(summary), examples)
+ };
let casts = seeded
.service
@@ -623,18 +628,14 @@ async fn the_dry_run_answers_every_menu_target_for_a_viewer() {
),
checked(DataType::SelectString, false, 0, None, &[]),
checked(DataType::SelectString, true, 0, None, &[]),
- checked(
+ no_values_fit(
DataType::Date,
- false,
- 3,
- Some("3 values aren't dates"),
+ "3 values aren't dates",
&["Sam", "12", "https://macro.com"]
),
- checked(
+ no_values_fit(
DataType::Boolean,
- false,
- 3,
- Some("3 values aren't true or false"),
+ "3 values aren't true or false",
&["Sam", "12", "https://macro.com"]
),
checked(
@@ -1031,3 +1032,90 @@ async fn a_text_column_converts_into_a_new_select_column_with_its_labels_as_opti
Some(PropertyValue::Str("Nut-free".into()))
);
}
+
+#[tokio::test]
+async fn numeric_selects_offer_dates_only_while_the_column_is_empty() {
+ for multi in [false, true] {
+ let seeded = seeded().await;
+ let column = ColumnId::new();
+ seeded
+ .service
+ .apply_ops(
+ edit(seeded.database_id),
+ viewer(OWNER),
+ OpBatch::from(vec![DatabaseOp::Column {
+ table: seeded.table_id,
+ column,
+ change: ColumnChange::Create {
+ definition: NewColumn::New {
+ name: "Rating".into(),
+ kind: ColumnKind::SelectNumber { multi },
+ options: (1..=4)
+ .map(|n| NewOption {
+ id: OptionId::new(),
+ label: n.to_string(),
+ })
+ .collect(),
+ infer_type: false,
+ },
+ after: None,
+ },
+ }]),
+ )
+ .await
+ .unwrap();
+ let empty = seeded
+ .service
+ .column_casts(
+ receipt(seeded.database_id, VIEWER, AccessLevel::View),
+ seeded.table_id,
+ column,
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ empty
+ .iter()
+ .find(|cast| cast.data_type == DataType::Date)
+ .unwrap()
+ .cast,
+ CastVerdict::Safe
+ );
+ seeded
+ .service
+ .apply_ops(
+ edit(seeded.database_id),
+ viewer(OWNER),
+ OpBatch::from(vec![DatabaseOp::Rows {
+ table: seeded.table_id,
+ change: RowsChange::Update {
+ changes: RowChanges::Uniform {
+ rows: vec![seeded.row_id],
+ cells: vec![CellWrite {
+ column,
+ value: CellValue::Options(vec![OptionRef::Label("1".into())]),
+ }],
+ },
+ },
+ }]),
+ )
+ .await
+ .unwrap();
+ let filled = seeded
+ .service
+ .column_casts(
+ receipt(seeded.database_id, VIEWER, AccessLevel::View),
+ seeded.table_id,
+ column,
+ )
+ .await
+ .unwrap();
+ let date = filled
+ .iter()
+ .find(|cast| cast.data_type == DataType::Date)
+ .unwrap();
+ assert_eq!(date.cast, CastVerdict::Never);
+ assert_eq!(date.failures, 1);
+ assert_eq!(date.reason.as_deref(), Some("1 value isn't a date"));
+ }
+}
diff --git a/docs/AGENT_GUIDE/databases.md b/docs/AGENT_GUIDE/databases.md
index 7e8b9aa6d1..8f8b5d4106 100644
--- a/docs/AGENT_GUIDE/databases.md
+++ b/docs/AGENT_GUIDE/databases.md
@@ -67,6 +67,8 @@ Its type icon stays in place while editing. SQL refers to tables and columns by
their display names (double-quoted), so a rename changes the name a saved query
must use.
+The current type has a checkmark in **Change type**; selecting it leaves the column unchanged. Types that cannot convert any existing values are omitted, while empty columns can still choose a new type.
+
The header arrow menu groups schema and view actions. **Change type** checks the
column's values against Text, Number, Select, Multi-select, Date, Checkbox, URL,
People, Documents, Tasks, and relations to tables in this database, showing