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.

refactor(databases): decouple core storage and UI from app entities (#7527)

Wolf Mermelstein167c4b123b8d
diff --git a/.agents/skills/collab-surface-core/SKILL.md b/.agents/skills/collab-surface-core/SKILL.md
new file mode 100644
index 0000000000..fa43791f9e
--- /dev/null
+++ b/.agents/skills/collab-surface-core/SKILL.md
@@ -0,0 +1,45 @@
+---
+name: collab-surface-core
+description: Use when adding collaborative markdown, notes, or descriptions to a feature with collab surfaces. Covers parent entities, service entry points, authorization, content storage, and lifecycle.
+---
+
+# Collab surface core
+
+Use collab surfaces for collaborative markdown within a feature. Each surface
+has its own Loro session in sync-service and can exist without a `Document` row.
+The surface is a child resource of an app entity: product names, owners, sharing,
+and discovery belong to the parent.
+
+## Choose the entry point
+
+- **A field managed by its parent domain**, such as an initiative description:
+ use `OwnedSurfaceService` through the feature's domain port. The owning domain
+ authorizes callers and chooses the surface id before calling
+ `ensure_owned_surface`, `owned_surface_markdown`, or `retire_surface`.
+- **A surface managed through the public API**, such as a channel's shared input:
+ use `CollabSurfaceService` with an access receipt for the parent. Surface reads,
+ connection tokens, and deletion derive their permissions from that parent.
+
+Check `surface_ownership` and the parent's access resolution when adding a new
+parent type. Parents marked `ParentDomain` use their domain's creation and
+retirement paths; the public API still provides connection tokens. Keep the
+trusted `OwnedSurfaceService` behind domain authorization, with adapters supplied
+by the composition root.
+
+## Keep content and lifecycle with their owners
+
+- The Loro session is the source of truth for content. `collab_surfaces` records
+ the id, parent, initialization state, timestamps, and soft deletion.
+- Use application-generated UUIDv7 ids and the service's ensure operation. It
+ handles namespace collisions and retries initialization from `pending` to
+ `ready`. The id also names the sync session and must never be recycled.
+- Initial markdown seeds a new session. Ensuring an existing surface preserves
+ its content; subsequent edits go through the collaborative session.
+- Wire retirement into the owning feature's cleanup. The parent reference is
+ polymorphic and has no foreign-key cascade. Retirement blocks new connection
+ tokens; it does not reclaim the sync-service session.
+
+Read the [domain ports](../../../crates/collab_surface/src/domain/ports.rs) and
+[parent policy](../../../crates/collab_surface/src/domain/models.rs) before wiring
+a consumer. The [initiative adapter](../../../crates/initiative_description/src/lib.rs)
+shows how a feature reuses the service through its own domain port.
diff --git a/.agents/skills/collab-surface-core/agents/openai.yaml b/.agents/skills/collab-surface-core/agents/openai.yaml
new file mode 100644
index 0000000000..97bab758c8
--- /dev/null
+++ b/.agents/skills/collab-surface-core/agents/openai.yaml
@@ -0,0 +1,3 @@
+interface:
+ display_name: "Collab Surface Core"
+ short_description: "Add collaborative content to your own feature"
diff --git a/.agents/skills/database-core/SKILL.md b/.agents/skills/database-core/SKILL.md
new file mode 100644
index 0000000000..25599b59be
--- /dev/null
+++ b/.agents/skills/database-core/SKILL.md
@@ -0,0 +1,50 @@
+---
+name: database-core
+description: Use when building a feature on reusable database storage or UI, such as CRM, or deciding whether it needs a Macro database entity. Covers storage and UI entry points, ownership, authorization, and the _entities naming convention.
+---
+
+# Database core
+
+The database engine is a reusable resource. A feature can use its tables,
+columns, cells, versions, and journal with its own ownership, permissions, and
+lifecycle.
+
+## Choose the entry point
+
+- **Building a feature such as CRM:** reference `databases.id` from that
+ feature's domain model. Use the `DatabaseStorage` port to `create_storage`,
+ `apply_storage_writes`, and `delete_storage`.
+- **Working on the Macro database app:** use `DatabasesService`. Its repository
+ creates the app entity and owner grant together; `CellStore` enforces the
+ entity's lifecycle during writes.
+
+A new consumer authorizes access to its own resources, validates writes, and
+controls discovery and cleanup. `DatabaseStorage` expects calls from a trusted
+domain service; the composition root supplies its adapter. Creating core storage
+requires no row in `database_entities` or database app grants and does not expose
+it in Soup.
+
+## Keep product metadata on the entity
+
+`databases` holds the core resource identity. An optional `database_entities` row
+holds the Macro app's name, owner (`user_id`), lifecycle timestamps, and trash
+state. Its `database_id` is both its primary key and a foreign key to the resource.
+
+Use this pattern for new entities: a plural `<kind>_entities` table owns product
+identity and lifecycle and references any reusable resource it uses. The naming
+convention starts with databases; existing unrelated entity tables keep their names.
+Rust type names stay singular.
+
+## Reuse the records UI
+
+Use [DatabaseRecordsView](../../../apps/web/src/features/block-database/views/database-records-view.tsx)
+with a host-provided `DatabaseRowsSource`, query/layout state, and action callbacks.
+It needs no Macro database entity, saved-view metadata, or block context. Omit
+schema actions when a feature allows record editing with a fixed schema.
+The Macro app's `DatabaseGrid` supplies its API, Soup, and saved-view adapters;
+other domains supply their own authorized reads, writes, and change notifications.
+
+Read the [storage boundary](../../../crates/databases/README.md#storage-and-app-entities)
+and [domain ports](../../../crates/databases/src/domain/ports.rs) before wiring a
+consumer. Follow the [database guide](../../../docs/DATABASE_DEVELOPMENT.md) for
+schema and migration work.
diff --git a/.agents/skills/database-core/agents/openai.yaml b/.agents/skills/database-core/agents/openai.yaml
new file mode 100644
index 0000000000..149a77d35f
--- /dev/null
+++ b/.agents/skills/database-core/agents/openai.yaml
@@ -0,0 +1,3 @@
+interface:
+ display_name: "Database Core"
+ short_description: "Build features on the reusable database engine"
diff --git a/.claude/skills/collab-surface-core b/.claude/skills/collab-surface-core
new file mode 120000
index 0000000000..7d646ac1fd
--- /dev/null
+++ b/.claude/skills/collab-surface-core
@@ -0,0 +1 @@
+../../.agents/skills/collab-surface-core
\ No newline at end of file
diff --git a/.claude/skills/database-core b/.claude/skills/database-core
new file mode 120000
index 0000000000..2c6783b3a7
--- /dev/null
+++ b/.claude/skills/database-core
@@ -0,0 +1 @@
+../../.agents/skills/database-core
\ No newline at end of file
diff --git a/.sqlx/query-02c898e4229992983c1b7929d163e8854ced8857cd4dc6734cd2ddff1dec1559.json b/.sqlx/query-02c898e4229992983c1b7929d163e8854ced8857cd4dc6734cd2ddff1dec1559.json
deleted file mode 100644
index d14ff9dbce..0000000000
--- a/.sqlx/query-02c898e4229992983c1b7929d163e8854ced8857cd4dc6734cd2ddff1dec1559.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "UPDATE databases SET name = $2, updated_at = now() WHERE id = $1",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Text"
- ]
- },
- "nullable": []
- },
- "hash": "02c898e4229992983c1b7929d163e8854ced8857cd4dc6734cd2ddff1dec1559"
-}
diff --git a/.sqlx/query-04df7cb7569ca100a4292c2949b939f9592e6b46b34f2fd6744ba815a25a5ccc.json b/.sqlx/query-04df7cb7569ca100a4292c2949b939f9592e6b46b34f2fd6744ba815a25a5ccc.json
deleted file mode 100644
index 2b475e69a0..0000000000
--- a/.sqlx/query-04df7cb7569ca100a4292c2949b939f9592e6b46b34f2fd6744ba815a25a5ccc.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "INSERT INTO databases (id, name, owner_id) VALUES ($1, 'Guests', 'macro|[email protected]')",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "04df7cb7569ca100a4292c2949b939f9592e6b46b34f2fd6744ba815a25a5ccc"
-}
diff --git a/.sqlx/query-0edfe62a94be7007bbfd5c636cb7835f2f16cb6643fc81b5abc019b1e1e8e5f1.json b/.sqlx/query-0edfe62a94be7007bbfd5c636cb7835f2f16cb6643fc81b5abc019b1e1e8e5f1.json
new file mode 100644
index 0000000000..2065732abe
--- /dev/null
+++ b/.sqlx/query-0edfe62a94be7007bbfd5c636cb7835f2f16cb6643fc81b5abc019b1e1e8e5f1.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT (SELECT COUNT(*) FROM databases)\n + (SELECT COUNT(*) FROM database_entities)\n + (SELECT COUNT(*) FROM database_tables)\n + (SELECT COUNT(*) FROM database_columns)\n + (SELECT COUNT(*) FROM database_rows)\n + (SELECT COUNT(*) FROM database_views)\n + (SELECT COUNT(*) FROM database_view_positions)\n + (SELECT COUNT(*) FROM property_definitions WHERE database_id IS NOT NULL)\n + (SELECT COUNT(*) FROM entity_properties WHERE entity_type = 'DATABASE_ROW')\n + (SELECT COUNT(*) FROM entity_access WHERE entity_type = 'database')\n + (SELECT COUNT(*) FROM database_changes)\n + (SELECT COUNT(*) FROM database_change_rows)\n + (SELECT COUNT(*) FROM database_change_columns)\n + (SELECT COUNT(*) FROM database_queries)\n + (SELECT COUNT(*) FROM database_starter_seeds) AS \"count!\"",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "0edfe62a94be7007bbfd5c636cb7835f2f16cb6643fc81b5abc019b1e1e8e5f1"
+}
diff --git a/.sqlx/query-1b88b8938b102b5b2fa12adbc35910948b7bc5beada3985ad4a5e31b3d4231da.json b/.sqlx/query-1b88b8938b102b5b2fa12adbc35910948b7bc5beada3985ad4a5e31b3d4231da.json
new file mode 100644
index 0000000000..42b1f29e2d
--- /dev/null
+++ b/.sqlx/query-1b88b8938b102b5b2fa12adbc35910948b7bc5beada3985ad4a5e31b3d4231da.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE database_entities SET trashed_at = NULL, updated_at = now() WHERE database_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "1b88b8938b102b5b2fa12adbc35910948b7bc5beada3985ad4a5e31b3d4231da"
+}
diff --git a/.sqlx/query-e11b691f6cbee8ed1ec1d81754c4687a57adf6c3866c14028ad2ca7de39995de.json b/.sqlx/query-20975086e9fc836bdfd6e13636613a3585106f9ae9cba8bfdae8f5363ed2c361.json
similarity index 51%
rename from .sqlx/query-e11b691f6cbee8ed1ec1d81754c4687a57adf6c3866c14028ad2ca7de39995de.json
rename to .sqlx/query-20975086e9fc836bdfd6e13636613a3585106f9ae9cba8bfdae8f5363ed2c361.json
index 2e300729b5..027a9b4fec 100644
--- a/.sqlx/query-e11b691f6cbee8ed1ec1d81754c4687a57adf6c3866c14028ad2ca7de39995de.json
+++ b/.sqlx/query-20975086e9fc836bdfd6e13636613a3585106f9ae9cba8bfdae8f5363ed2c361.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "UPDATE databases SET trashed_at = now() WHERE id = $1",
+ "query": "DELETE FROM database_entities WHERE database_id = $1",
"describe": {
"columns": [],
"parameters": {
@@ -10,5 +10,5 @@
},
"nullable": []
},
- "hash": "e11b691f6cbee8ed1ec1d81754c4687a57adf6c3866c14028ad2ca7de39995de"
+ "hash": "20975086e9fc836bdfd6e13636613a3585106f9ae9cba8bfdae8f5363ed2c361"
}
diff --git a/.sqlx/query-252e517571fef697082b4e2a8084123f1e1eedf96eec93052034e9ea0ee5edea.json b/.sqlx/query-252e517571fef697082b4e2a8084123f1e1eedf96eec93052034e9ea0ee5edea.json
new file mode 100644
index 0000000000..683e32866e
--- /dev/null
+++ b/.sqlx/query-252e517571fef697082b4e2a8084123f1e1eedf96eec93052034e9ea0ee5edea.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT COUNT(*) FROM entity_properties WHERE entity_id = $1 AND entity_type = 'DATABASE_ROW'",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "252e517571fef697082b4e2a8084123f1e1eedf96eec93052034e9ea0ee5edea"
+}
diff --git a/.sqlx/query-3754907df9d3a1d898cacb0cdb55bea8d0ce30df20f66b55324f63f6de4b89e9.json b/.sqlx/query-2ff1e25803bb1e35cb53fd6a8b29d4c9e2ef13d66bd6d294bfd504831f69d8c9.json
similarity index 76%
rename from .sqlx/query-3754907df9d3a1d898cacb0cdb55bea8d0ce30df20f66b55324f63f6de4b89e9.json
rename to .sqlx/query-2ff1e25803bb1e35cb53fd6a8b29d4c9e2ef13d66bd6d294bfd504831f69d8c9.json
index b9e592fa40..205818d608 100644
--- a/.sqlx/query-3754907df9d3a1d898cacb0cdb55bea8d0ce30df20f66b55324f63f6de4b89e9.json
+++ b/.sqlx/query-2ff1e25803bb1e35cb53fd6a8b29d4c9e2ef13d66bd6d294bfd504831f69d8c9.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "SELECT id, name, owner_id, created_at, trashed_at FROM databases WHERE id = $1",
+ "query": "SELECT database_id AS id, name, user_id AS owner_id, created_at, trashed_at FROM database_entities WHERE database_id = $1",
"describe": {
"columns": [
{
@@ -42,5 +42,5 @@
true
]
},
- "hash": "3754907df9d3a1d898cacb0cdb55bea8d0ce30df20f66b55324f63f6de4b89e9"
+ "hash": "2ff1e25803bb1e35cb53fd6a8b29d4c9e2ef13d66bd6d294bfd504831f69d8c9"
}
diff --git a/.sqlx/query-3d037ae65f9e64e575f9c1111106e38c568909b9b16a6019ae0851484e30e428.json b/.sqlx/query-3d037ae65f9e64e575f9c1111106e38c568909b9b16a6019ae0851484e30e428.json
deleted file mode 100644
index 1d10c48617..0000000000
--- a/.sqlx/query-3d037ae65f9e64e575f9c1111106e38c568909b9b16a6019ae0851484e30e428.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "UPDATE databases SET trashed_at = NULL, updated_at = now() WHERE id = $1",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "3d037ae65f9e64e575f9c1111106e38c568909b9b16a6019ae0851484e30e428"
-}
diff --git a/.sqlx/query-452679773633600344e0618a01b5d653a5f4cd42e6150402eb4adbb8f3f894dc.json b/.sqlx/query-452679773633600344e0618a01b5d653a5f4cd42e6150402eb4adbb8f3f894dc.json
new file mode 100644
index 0000000000..89082691b0
--- /dev/null
+++ b/.sqlx/query-452679773633600344e0618a01b5d653a5f4cd42e6150402eb4adbb8f3f894dc.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO database_rows (id, table_id, position) VALUES ($1, $2, '80')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "452679773633600344e0618a01b5d653a5f4cd42e6150402eb4adbb8f3f894dc"
+}
diff --git a/.sqlx/query-47d118ba5d5da6c03cecf0649f7e2ad12859cd8e2f670c8d01cd511336c4a5ab.json b/.sqlx/query-47d118ba5d5da6c03cecf0649f7e2ad12859cd8e2f670c8d01cd511336c4a5ab.json
new file mode 100644
index 0000000000..a535447fb7
--- /dev/null
+++ b/.sqlx/query-47d118ba5d5da6c03cecf0649f7e2ad12859cd8e2f670c8d01cd511336c4a5ab.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT database_id FROM database_entities WHERE database_id = $1 AND trashed_at IS NULL FOR SHARE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "database_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "47d118ba5d5da6c03cecf0649f7e2ad12859cd8e2f670c8d01cd511336c4a5ab"
+}
diff --git a/.sqlx/query-7a4a89c5cbaf299687f77e0ec890e005127662cb581023e72d29dedce6de8e9a.json b/.sqlx/query-48ce5ce24d9cd5574f31c541f515ac415386872c51a2ca9e8e5c185549846d12.json
similarity index 68%
rename from .sqlx/query-7a4a89c5cbaf299687f77e0ec890e005127662cb581023e72d29dedce6de8e9a.json
rename to .sqlx/query-48ce5ce24d9cd5574f31c541f515ac415386872c51a2ca9e8e5c185549846d12.json
index d5bd02d009..7ecd9279bd 100644
--- a/.sqlx/query-7a4a89c5cbaf299687f77e0ec890e005127662cb581023e72d29dedce6de8e9a.json
+++ b/.sqlx/query-48ce5ce24d9cd5574f31c541f515ac415386872c51a2ca9e8e5c185549846d12.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT r.id, ea.access_level AS \"access_level!: AccessLevel\"\n FROM database_rows r\n JOIN database_tables t ON t.id = r.table_id\n JOIN databases d ON d.id = t.database_id\n JOIN entity_access ea ON ea.entity_id = d.id\n WHERE r.id = ANY($1)\n AND ea.entity_type = 'database'\n AND ea.source_id = ANY($2)\n ",
+ "query": "\n SELECT r.id, ea.access_level AS \"access_level!: AccessLevel\"\n FROM database_rows r\n JOIN database_tables t ON t.id = r.table_id\n JOIN database_entities d ON d.database_id = t.database_id\n JOIN entity_access ea ON ea.entity_id = d.database_id\n WHERE r.id = ANY($1)\n AND ea.entity_type = 'database'\n AND ea.source_id = ANY($2)\n ",
"describe": {
"columns": [
{
@@ -37,5 +37,5 @@
false
]
},
- "hash": "7a4a89c5cbaf299687f77e0ec890e005127662cb581023e72d29dedce6de8e9a"
+ "hash": "48ce5ce24d9cd5574f31c541f515ac415386872c51a2ca9e8e5c185549846d12"
}
diff --git a/.sqlx/query-9e99f20806b8a2f28ce547bfa9e7fbcbd317879b538dc0665c1186fb44c4729f.json b/.sqlx/query-51c8d69a02ae069b5f1526e0f3c313a323774b4d6678f2cb927c095e0217b27e.json
similarity index 69%
rename from .sqlx/query-9e99f20806b8a2f28ce547bfa9e7fbcbd317879b538dc0665c1186fb44c4729f.json
rename to .sqlx/query-51c8d69a02ae069b5f1526e0f3c313a323774b4d6678f2cb927c095e0217b27e.json
index aa76d627d4..cba565314c 100644
--- a/.sqlx/query-9e99f20806b8a2f28ce547bfa9e7fbcbd317879b538dc0665c1186fb44c4729f.json
+++ b/.sqlx/query-51c8d69a02ae069b5f1526e0f3c313a323774b4d6678f2cb927c095e0217b27e.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT t.database_id\n FROM database_rows r\n JOIN database_tables t ON t.id = r.table_id\n WHERE r.id = $1\n ",
+ "query": "\n SELECT t.database_id\n FROM database_rows r\n JOIN database_tables t ON t.id = r.table_id\n JOIN database_entities d ON d.database_id = t.database_id\n WHERE r.id = $1\n ",
"describe": {
"columns": [
{
@@ -18,5 +18,5 @@
false
]
},
- "hash": "9e99f20806b8a2f28ce547bfa9e7fbcbd317879b538dc0665c1186fb44c4729f"
+ "hash": "51c8d69a02ae069b5f1526e0f3c313a323774b4d6678f2cb927c095e0217b27e"
}
diff --git a/.sqlx/query-53aab496584e71041d3d2c380e6bb8b82d175b956b857bde826728c726f98718.json b/.sqlx/query-53aab496584e71041d3d2c380e6bb8b82d175b956b857bde826728c726f98718.json
new file mode 100644
index 0000000000..83cda06d30
--- /dev/null
+++ b/.sqlx/query-53aab496584e71041d3d2c380e6bb8b82d175b956b857bde826728c726f98718.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO \"User\" (id, email, macro_user_id)\n VALUES ($1, $1, $2)\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "53aab496584e71041d3d2c380e6bb8b82d175b956b857bde826728c726f98718"
+}
diff --git a/.sqlx/query-6b9a8a5b523f52de39d36898d8eabcca74980932e9d94a089a0e56a52fe6f76f.json b/.sqlx/query-6b9a8a5b523f52de39d36898d8eabcca74980932e9d94a089a0e56a52fe6f76f.json
deleted file mode 100644
index 1b07a1dde8..0000000000
--- a/.sqlx/query-6b9a8a5b523f52de39d36898d8eabcca74980932e9d94a089a0e56a52fe6f76f.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "INSERT INTO databases (id, name, owner_id) VALUES ($1, 'Property definition test', 'macro|[email protected]')",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "6b9a8a5b523f52de39d36898d8eabcca74980932e9d94a089a0e56a52fe6f76f"
-}
diff --git a/.sqlx/query-6e4046f3e490a8efe3ffe072da4c282f367b490b0f7fe6adf4a67ab10cc78e59.json b/.sqlx/query-6e4046f3e490a8efe3ffe072da4c282f367b490b0f7fe6adf4a67ab10cc78e59.json
new file mode 100644
index 0000000000..3b8e41f713
--- /dev/null
+++ b/.sqlx/query-6e4046f3e490a8efe3ffe072da4c282f367b490b0f7fe6adf4a67ab10cc78e59.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE database_entities SET trashed_at = now() WHERE database_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "6e4046f3e490a8efe3ffe072da4c282f367b490b0f7fe6adf4a67ab10cc78e59"
+}
diff --git a/.sqlx/query-6ea116bc34d74d84cf91f0e9ab41f1831024a5659d858372888b68374d699b47.json b/.sqlx/query-6ea116bc34d74d84cf91f0e9ab41f1831024a5659d858372888b68374d699b47.json
new file mode 100644
index 0000000000..22d2f128e4
--- /dev/null
+++ b/.sqlx/query-6ea116bc34d74d84cf91f0e9ab41f1831024a5659d858372888b68374d699b47.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT id FROM databases",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "6ea116bc34d74d84cf91f0e9ab41f1831024a5659d858372888b68374d699b47"
+}
diff --git a/.sqlx/query-708a6d4141258264cd97bebaa89538094109ed0d11e6daa9475fb9a9960ab258.json b/.sqlx/query-708a6d4141258264cd97bebaa89538094109ed0d11e6daa9475fb9a9960ab258.json
new file mode 100644
index 0000000000..32eca519c9
--- /dev/null
+++ b/.sqlx/query-708a6d4141258264cd97bebaa89538094109ed0d11e6daa9475fb9a9960ab258.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO database_tables (id, database_id, name, position) VALUES ($1, $2, 'Contacts', '80')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "708a6d4141258264cd97bebaa89538094109ed0d11e6daa9475fb9a9960ab258"
+}
diff --git a/.sqlx/query-775868ef05798d2ba33d35d9e8cb66753fe8662dc0c30a58ead2c5ec16258afb.json b/.sqlx/query-775868ef05798d2ba33d35d9e8cb66753fe8662dc0c30a58ead2c5ec16258afb.json
new file mode 100644
index 0000000000..0c0389b3c8
--- /dev/null
+++ b/.sqlx/query-775868ef05798d2ba33d35d9e8cb66753fe8662dc0c30a58ead2c5ec16258afb.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level) VALUES ($1, 'database', $2, 'user', 'owner')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "775868ef05798d2ba33d35d9e8cb66753fe8662dc0c30a58ead2c5ec16258afb"
+}
diff --git a/.sqlx/query-20ce54477bbe7fdcb68a4dcdd7486b6d0202192bb8470bc410219a751cf272b9.json b/.sqlx/query-79322a28d9fa2e8ac47c0fddba4e67387b89933fe11e6d4cad9d40b858b8d78c.json
similarity index 62%
rename from .sqlx/query-20ce54477bbe7fdcb68a4dcdd7486b6d0202192bb8470bc410219a751cf272b9.json
rename to .sqlx/query-79322a28d9fa2e8ac47c0fddba4e67387b89933fe11e6d4cad9d40b858b8d78c.json
index 89852a47b9..c8848b8d4a 100644
--- a/.sqlx/query-20ce54477bbe7fdcb68a4dcdd7486b6d0202192bb8470bc410219a751cf272b9.json
+++ b/.sqlx/query-79322a28d9fa2e8ac47c0fddba4e67387b89933fe11e6d4cad9d40b858b8d78c.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "SELECT id FROM databases WHERE id = $1 AND trashed_at IS NULL FOR SHARE",
+ "query": "SELECT id FROM databases WHERE id = $1 FOR SHARE",
"describe": {
"columns": [
{
@@ -18,5 +18,5 @@
false
]
},
- "hash": "20ce54477bbe7fdcb68a4dcdd7486b6d0202192bb8470bc410219a751cf272b9"
+ "hash": "79322a28d9fa2e8ac47c0fddba4e67387b89933fe11e6d4cad9d40b858b8d78c"
}
diff --git a/.sqlx/query-7a084d02b69403f03a5e1a4a59487bf2e66bd41543908d873d602a9d23b03ca8.json b/.sqlx/query-7a084d02b69403f03a5e1a4a59487bf2e66bd41543908d873d602a9d23b03ca8.json
new file mode 100644
index 0000000000..94500204f5
--- /dev/null
+++ b/.sqlx/query-7a084d02b69403f03a5e1a4a59487bf2e66bd41543908d873d602a9d23b03ca8.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO macro_user (id, username, email, stripe_customer_id)\n VALUES ($1, $2, $2, $2)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "7a084d02b69403f03a5e1a4a59487bf2e66bd41543908d873d602a9d23b03ca8"
+}
diff --git a/.sqlx/query-bbcbffe65a5642ee3aea46c38cbd1d93a45f94e5601d21ba0fa0c73868c25182.json b/.sqlx/query-8282c0ae4fcec705705fbdd6b2eabd9dacaf83e6d0ecb1e3193a2ddbe58a25d1.json
similarity index 68%
rename from .sqlx/query-bbcbffe65a5642ee3aea46c38cbd1d93a45f94e5601d21ba0fa0c73868c25182.json
rename to .sqlx/query-8282c0ae4fcec705705fbdd6b2eabd9dacaf83e6d0ecb1e3193a2ddbe58a25d1.json
index 4c5a7326d7..71b7b7bbdf 100644
--- a/.sqlx/query-bbcbffe65a5642ee3aea46c38cbd1d93a45f94e5601d21ba0fa0c73868c25182.json
+++ b/.sqlx/query-8282c0ae4fcec705705fbdd6b2eabd9dacaf83e6d0ecb1e3193a2ddbe58a25d1.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "WITH user_source_ids AS (\n SELECT cp.channel_id::text AS source_id FROM comms_channel_participants cp\n WHERE cp.user_id = $1 AND cp.left_at IS NULL\n UNION ALL SELECT team_id::text FROM team_user WHERE user_id = $1\n UNION ALL SELECT $1\n)\nSELECT r.id, r.table_id, row_table.database_id, r.position, r.created_by,\n row_database.owner_id, r.created_at, r.updated_at\nFROM database_rows r\nJOIN database_tables row_table ON row_table.id = r.table_id\nJOIN databases row_database ON row_database.id = row_table.database_id\nWHERE r.id = ANY($2)\n AND row_database.trashed_at IS NULL\n AND EXISTS (SELECT 1 FROM entity_access ea\n WHERE ea.entity_id = row_database.id AND ea.entity_type = 'database'\n AND ea.source_id IN (SELECT source_id FROM user_source_ids))\nORDER BY r.created_at DESC, r.id DESC\n",
+ "query": "WITH user_source_ids AS (\n SELECT cp.channel_id::text AS source_id FROM comms_channel_participants cp\n WHERE cp.user_id = $1 AND cp.left_at IS NULL\n UNION ALL SELECT team_id::text FROM team_user WHERE user_id = $1\n UNION ALL SELECT $1\n)\nSELECT r.id, r.table_id, row_table.database_id, r.position, r.created_by,\n row_database.user_id AS owner_id, r.created_at, r.updated_at\nFROM database_rows r\nJOIN database_tables row_table ON row_table.id = r.table_id\nJOIN database_entities row_database ON row_database.database_id = row_table.database_id\nWHERE r.id = ANY($2)\n AND row_database.trashed_at IS NULL\n AND EXISTS (SELECT 1 FROM entity_access ea\n WHERE ea.entity_id = row_database.database_id AND ea.entity_type = 'database'\n AND ea.source_id IN (SELECT source_id FROM user_source_ids))\nORDER BY r.created_at DESC, r.id DESC\n",
"describe": {
"columns": [
{
@@ -61,5 +61,5 @@
false
]
},
- "hash": "bbcbffe65a5642ee3aea46c38cbd1d93a45f94e5601d21ba0fa0c73868c25182"
+ "hash": "8282c0ae4fcec705705fbdd6b2eabd9dacaf83e6d0ecb1e3193a2ddbe58a25d1"
}
diff --git a/.sqlx/query-c76a5f3bc5fb5f53fb5d8c8debe9872ad73a147566c79eca799b6bee7b417ae0.json b/.sqlx/query-88aa610133145f553828c38dfae508b495911952266cebd5126113d4f2e9b145.json
similarity index 61%
rename from .sqlx/query-c76a5f3bc5fb5f53fb5d8c8debe9872ad73a147566c79eca799b6bee7b417ae0.json
rename to .sqlx/query-88aa610133145f553828c38dfae508b495911952266cebd5126113d4f2e9b145.json
index a38c3fe62b..3ceaa6ed4b 100644
--- a/.sqlx/query-c76a5f3bc5fb5f53fb5d8c8debe9872ad73a147566c79eca799b6bee7b417ae0.json
+++ b/.sqlx/query-88aa610133145f553828c38dfae508b495911952266cebd5126113d4f2e9b145.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "SELECT EXISTS(SELECT 1 FROM databases WHERE owner_id = $1) AS \"exists!\"",
+ "query": "SELECT EXISTS(SELECT 1 FROM database_entities WHERE user_id = $1) AS \"exists!\"",
"describe": {
"columns": [
{
@@ -18,5 +18,5 @@
null
]
},
- "hash": "c76a5f3bc5fb5f53fb5d8c8debe9872ad73a147566c79eca799b6bee7b417ae0"
+ "hash": "88aa610133145f553828c38dfae508b495911952266cebd5126113d4f2e9b145"
}
diff --git a/.sqlx/query-92725cc3017d1e9b233927931f7e30e991781607b220d53fad753007417ad78e.json b/.sqlx/query-92725cc3017d1e9b233927931f7e30e991781607b220d53fad753007417ad78e.json
deleted file mode 100644
index 260a89e745..0000000000
--- a/.sqlx/query-92725cc3017d1e9b233927931f7e30e991781607b220d53fad753007417ad78e.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "SELECT d.id FROM database_starter_seeds s JOIN databases d ON d.id = s.database_id\n WHERE s.user_id = $1 AND d.trashed_at IS NULL",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "92725cc3017d1e9b233927931f7e30e991781607b220d53fad753007417ad78e"
-}
diff --git a/.sqlx/query-1f07a64ab5e947f0659d999e391f8e31c2f60eca3b3092f5fc5a947324168c7e.json b/.sqlx/query-9554073198722abd3f3554bd0505716da593dce78d3b845c74a6b4f6d73cb8cb.json
similarity index 65%
rename from .sqlx/query-1f07a64ab5e947f0659d999e391f8e31c2f60eca3b3092f5fc5a947324168c7e.json
rename to .sqlx/query-9554073198722abd3f3554bd0505716da593dce78d3b845c74a6b4f6d73cb8cb.json
index a95ba4f417..bdef0d6a53 100644
--- a/.sqlx/query-1f07a64ab5e947f0659d999e391f8e31c2f60eca3b3092f5fc5a947324168c7e.json
+++ b/.sqlx/query-9554073198722abd3f3554bd0505716da593dce78d3b845c74a6b4f6d73cb8cb.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "SELECT t.id, t.version FROM database_tables t\n JOIN databases d ON d.id = t.database_id\n WHERE t.id = ANY($1) AND d.trashed_at IS NULL\n ORDER BY t.id\n FOR UPDATE OF t",
+ "query": "SELECT t.id, t.version FROM database_tables t\n WHERE t.id = ANY($1)\n ORDER BY t.id\n FOR UPDATE OF t",
"describe": {
"columns": [
{
@@ -24,5 +24,5 @@
false
]
},
- "hash": "1f07a64ab5e947f0659d999e391f8e31c2f60eca3b3092f5fc5a947324168c7e"
+ "hash": "9554073198722abd3f3554bd0505716da593dce78d3b845c74a6b4f6d73cb8cb"
}
diff --git a/.sqlx/query-969742dfee2f5e99ff77b9a03dbd832d9c3bd4bcaee0409e52da5268f8bbe5dc.json b/.sqlx/query-969742dfee2f5e99ff77b9a03dbd832d9c3bd4bcaee0409e52da5268f8bbe5dc.json
new file mode 100644
index 0000000000..c9702cd910
--- /dev/null
+++ b/.sqlx/query-969742dfee2f5e99ff77b9a03dbd832d9c3bd4bcaee0409e52da5268f8bbe5dc.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT database_id FROM database_tables WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "database_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "969742dfee2f5e99ff77b9a03dbd832d9c3bd4bcaee0409e52da5268f8bbe5dc"
+}
diff --git a/.sqlx/query-e83365682a23325ba1eff7bfc6e2fda9fb98554f5ea6f9efed2bfbec2fbd336a.json b/.sqlx/query-9eabf069a6aa0cf2b15cb10f0c55133d67962a4d425db3735549104fa94a1133.json
similarity index 50%
rename from .sqlx/query-e83365682a23325ba1eff7bfc6e2fda9fb98554f5ea6f9efed2bfbec2fbd336a.json
rename to .sqlx/query-9eabf069a6aa0cf2b15cb10f0c55133d67962a4d425db3735549104fa94a1133.json
index a0537d9494..7987837724 100644
--- a/.sqlx/query-e83365682a23325ba1eff7bfc6e2fda9fb98554f5ea6f9efed2bfbec2fbd336a.json
+++ b/.sqlx/query-9eabf069a6aa0cf2b15cb10f0c55133d67962a4d425db3735549104fa94a1133.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "SELECT t.id FROM database_tables t\n JOIN databases d ON d.id = t.database_id\n WHERE t.id = $1 AND d.trashed_at IS NULL FOR UPDATE OF t",
+ "query": "SELECT t.id FROM database_tables t\n WHERE t.id = $1 FOR UPDATE OF t",
"describe": {
"columns": [
{
@@ -18,5 +18,5 @@
false
]
},
- "hash": "e83365682a23325ba1eff7bfc6e2fda9fb98554f5ea6f9efed2bfbec2fbd336a"
+ "hash": "9eabf069a6aa0cf2b15cb10f0c55133d67962a4d425db3735549104fa94a1133"
}
diff --git a/.sqlx/query-ad07abaa21067e9e35987d6e2a057f5b96ee008d7b54959989b04d0193a971ee.json b/.sqlx/query-ad07abaa21067e9e35987d6e2a057f5b96ee008d7b54959989b04d0193a971ee.json
new file mode 100644
index 0000000000..f7143a9515
--- /dev/null
+++ b/.sqlx/query-ad07abaa21067e9e35987d6e2a057f5b96ee008d7b54959989b04d0193a971ee.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO databases (id) VALUES ($1)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "ad07abaa21067e9e35987d6e2a057f5b96ee008d7b54959989b04d0193a971ee"
+}
diff --git a/.sqlx/query-0b013d1968d94ff4e2e3105ec19541b5794bbfc1ec5fee7bdffada0b64270f10.json b/.sqlx/query-afe7d83870243aa15340d87264017550c66c2a83eb51093a7380735a77ff5525.json
similarity index 76%
rename from .sqlx/query-0b013d1968d94ff4e2e3105ec19541b5794bbfc1ec5fee7bdffada0b64270f10.json
rename to .sqlx/query-afe7d83870243aa15340d87264017550c66c2a83eb51093a7380735a77ff5525.json
index 57f09d638f..16ef6ec1d8 100644
--- a/.sqlx/query-0b013d1968d94ff4e2e3105ec19541b5794bbfc1ec5fee7bdffada0b64270f10.json
+++ b/.sqlx/query-afe7d83870243aa15340d87264017550c66c2a83eb51093a7380735a77ff5525.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT entity_id AS \"entity_id!\", access_level AS \"access_level!: AccessLevel\"\n FROM entity_access\n WHERE entity_type = 'database'\n AND source_id = ANY($1)\n ",
+ "query": "\n SELECT entity_id AS \"entity_id!\", access_level AS \"access_level!: AccessLevel\"\n FROM entity_access\n JOIN database_entities d ON d.database_id = entity_access.entity_id\n WHERE entity_type = 'database'\n AND source_id = ANY($1)\n ",
"describe": {
"columns": [
{
@@ -36,5 +36,5 @@
false
]
},
- "hash": "0b013d1968d94ff4e2e3105ec19541b5794bbfc1ec5fee7bdffada0b64270f10"
+ "hash": "afe7d83870243aa15340d87264017550c66c2a83eb51093a7380735a77ff5525"
}
diff --git a/.sqlx/query-12e5b6d3da08ec639d64793546be89d17c461b42d2ea36b2f7ed48e0365bf48b.json b/.sqlx/query-aff5c0d4800f217ac175958099a229449436692ffe9ca187e57566f000c76ea3.json
similarity index 61%
rename from .sqlx/query-12e5b6d3da08ec639d64793546be89d17c461b42d2ea36b2f7ed48e0365bf48b.json
rename to .sqlx/query-aff5c0d4800f217ac175958099a229449436692ffe9ca187e57566f000c76ea3.json
index b3e2bae234..8e9ec874f0 100644
--- a/.sqlx/query-12e5b6d3da08ec639d64793546be89d17c461b42d2ea36b2f7ed48e0365bf48b.json
+++ b/.sqlx/query-aff5c0d4800f217ac175958099a229449436692ffe9ca187e57566f000c76ea3.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "SELECT id FROM databases WHERE id = $1 AND trashed_at IS NULL FOR UPDATE",
+ "query": "SELECT id FROM property_definitions WHERE id = $1",
"describe": {
"columns": [
{
@@ -18,5 +18,5 @@
false
]
},
- "hash": "12e5b6d3da08ec639d64793546be89d17c461b42d2ea36b2f7ed48e0365bf48b"
+ "hash": "aff5c0d4800f217ac175958099a229449436692ffe9ca187e57566f000c76ea3"
}
diff --git a/.sqlx/query-c0b8bde295ddadd94fec6b5a6c39dcb745ff7fe4041633f4d5cd8399253f4d5b.json b/.sqlx/query-c0b8bde295ddadd94fec6b5a6c39dcb745ff7fe4041633f4d5cd8399253f4d5b.json
deleted file mode 100644
index ab9cbb646c..0000000000
--- a/.sqlx/query-c0b8bde295ddadd94fec6b5a6c39dcb745ff7fe4041633f4d5cd8399253f4d5b.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "INSERT INTO databases (id, name, owner_id) VALUES ($1, 'Guests', $2)",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Text"
- ]
- },
- "nullable": []
- },
- "hash": "c0b8bde295ddadd94fec6b5a6c39dcb745ff7fe4041633f4d5cd8399253f4d5b"
-}
diff --git a/.sqlx/query-826e4cf1e15bf4bc5d1757e4c6f87634ba4577b76cdb6703396752a7e63897a9.json b/.sqlx/query-c91626de6a6510e49f42505ea3e6bb725b94b2003f1eaa23279e72f14a7a5193.json
similarity index 52%
rename from .sqlx/query-826e4cf1e15bf4bc5d1757e4c6f87634ba4577b76cdb6703396752a7e63897a9.json
rename to .sqlx/query-c91626de6a6510e49f42505ea3e6bb725b94b2003f1eaa23279e72f14a7a5193.json
index a9ba825753..55c65fe82e 100644
--- a/.sqlx/query-826e4cf1e15bf4bc5d1757e4c6f87634ba4577b76cdb6703396752a7e63897a9.json
+++ b/.sqlx/query-c91626de6a6510e49f42505ea3e6bb725b94b2003f1eaa23279e72f14a7a5193.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT d.owner_id, d.trashed_at\n FROM database_rows r\n JOIN database_tables t ON t.id = r.table_id\n JOIN databases d ON d.id = t.database_id\n WHERE r.id = $1\n ",
+ "query": "\n SELECT d.user_id AS owner_id, d.trashed_at\n FROM database_rows r\n JOIN database_tables t ON t.id = r.table_id\n JOIN database_entities d ON d.database_id = t.database_id\n WHERE r.id = $1\n ",
"describe": {
"columns": [
{
@@ -24,5 +24,5 @@
true
]
},
- "hash": "826e4cf1e15bf4bc5d1757e4c6f87634ba4577b76cdb6703396752a7e63897a9"
+ "hash": "c91626de6a6510e49f42505ea3e6bb725b94b2003f1eaa23279e72f14a7a5193"
}
diff --git a/.sqlx/query-ce62375cb40af276953d4d4726ffca80bcac00fc0ae0ce7a9838a9484533ea07.json b/.sqlx/query-ce62375cb40af276953d4d4726ffca80bcac00fc0ae0ce7a9838a9484533ea07.json
new file mode 100644
index 0000000000..b2d3406bcc
--- /dev/null
+++ b/.sqlx/query-ce62375cb40af276953d4d4726ffca80bcac00fc0ae0ce7a9838a9484533ea07.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE database_entities SET name = $2, updated_at = now() WHERE database_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "ce62375cb40af276953d4d4726ffca80bcac00fc0ae0ce7a9838a9484533ea07"
+}
diff --git a/.sqlx/query-cefa5b8092f178c95116ad602af85ecb68c189a71a55d8a89f5286f3a2ee956d.json b/.sqlx/query-cefa5b8092f178c95116ad602af85ecb68c189a71a55d8a89f5286f3a2ee956d.json
deleted file mode 100644
index 3ddea19267..0000000000
--- a/.sqlx/query-cefa5b8092f178c95116ad602af85ecb68c189a71a55d8a89f5286f3a2ee956d.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "INSERT INTO databases (id, name, owner_id) VALUES ($1, 'db', 'macro|[email protected]')",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "cefa5b8092f178c95116ad602af85ecb68c189a71a55d8a89f5286f3a2ee956d"
-}
diff --git a/.sqlx/query-cf56b7fe06552a170266462ef0645043a7dc11b998140b715fec5a532486872c.json b/.sqlx/query-cf56b7fe06552a170266462ef0645043a7dc11b998140b715fec5a532486872c.json
new file mode 100644
index 0000000000..a38ba710bc
--- /dev/null
+++ b/.sqlx/query-cf56b7fe06552a170266462ef0645043a7dc11b998140b715fec5a532486872c.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT (SELECT COUNT(*) FROM database_entities)\n + (SELECT COUNT(*) FROM entity_access) AS \"count!\"",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "cf56b7fe06552a170266462ef0645043a7dc11b998140b715fec5a532486872c"
+}
diff --git a/.sqlx/query-d1a6b2b870e158988c48aa0ba123edd22c48f2ab1de91cc6c7a62230fa989d6b.json b/.sqlx/query-d1a6b2b870e158988c48aa0ba123edd22c48f2ab1de91cc6c7a62230fa989d6b.json
new file mode 100644
index 0000000000..54d0b3385f
--- /dev/null
+++ b/.sqlx/query-d1a6b2b870e158988c48aa0ba123edd22c48f2ab1de91cc6c7a62230fa989d6b.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "WITH storage AS (INSERT INTO databases (id) VALUES ($1) RETURNING id)\n INSERT INTO database_entities (database_id, name, user_id) SELECT id, 'db', $2 FROM storage",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "d1a6b2b870e158988c48aa0ba123edd22c48f2ab1de91cc6c7a62230fa989d6b"
+}
diff --git a/.sqlx/query-d27ee24ca44e21098086c82327fc39422cae3c76992dfbb6d9d9be198d05e105.json b/.sqlx/query-d27ee24ca44e21098086c82327fc39422cae3c76992dfbb6d9d9be198d05e105.json
deleted file mode 100644
index ee22073ff9..0000000000
--- a/.sqlx/query-d27ee24ca44e21098086c82327fc39422cae3c76992dfbb6d9d9be198d05e105.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "INSERT INTO databases (id, name, owner_id) VALUES ($1, 'db', $2)",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Text"
- ]
- },
- "nullable": []
- },
- "hash": "d27ee24ca44e21098086c82327fc39422cae3c76992dfbb6d9d9be198d05e105"
-}
diff --git a/.sqlx/query-f8e22f2e956d94467897a2a0f02f85ccbf624b5d9796ccded0fcbb42f918eaee.json b/.sqlx/query-d3f49ff7d68128de8122f431ea9ea61f708233f45db84b12e93341dccdc036b5.json
similarity index 71%
rename from .sqlx/query-f8e22f2e956d94467897a2a0f02f85ccbf624b5d9796ccded0fcbb42f918eaee.json
rename to .sqlx/query-d3f49ff7d68128de8122f431ea9ea61f708233f45db84b12e93341dccdc036b5.json
index 640725adc3..f5578b6051 100644
--- a/.sqlx/query-f8e22f2e956d94467897a2a0f02f85ccbf624b5d9796ccded0fcbb42f918eaee.json
+++ b/.sqlx/query-d3f49ff7d68128de8122f431ea9ea61f708233f45db84b12e93341dccdc036b5.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n INSERT INTO databases (id, name, owner_id)\n VALUES ($1, $2, $3)\n RETURNING id, name, owner_id, created_at, trashed_at\n ",
+ "query": "\n INSERT INTO database_entities (database_id, name, user_id)\n VALUES ($1, $2, $3)\n RETURNING database_id AS id, name, user_id AS owner_id, created_at, trashed_at\n ",
"describe": {
"columns": [
{
@@ -44,5 +44,5 @@
true
]
},
- "hash": "f8e22f2e956d94467897a2a0f02f85ccbf624b5d9796ccded0fcbb42f918eaee"
+ "hash": "d3f49ff7d68128de8122f431ea9ea61f708233f45db84b12e93341dccdc036b5"
}
diff --git a/.sqlx/query-d4ba6ea68c7e2d39b8eb5df6d63037400f888f00eec3d4b1d9bbb87e81c916e7.json b/.sqlx/query-d4ba6ea68c7e2d39b8eb5df6d63037400f888f00eec3d4b1d9bbb87e81c916e7.json
new file mode 100644
index 0000000000..c50c4ff924
--- /dev/null
+++ b/.sqlx/query-d4ba6ea68c7e2d39b8eb5df6d63037400f888f00eec3d4b1d9bbb87e81c916e7.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT d.database_id FROM database_starter_seeds s JOIN database_entities d ON d.database_id = s.database_id\n WHERE s.user_id = $1 AND d.trashed_at IS NULL",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "database_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "d4ba6ea68c7e2d39b8eb5df6d63037400f888f00eec3d4b1d9bbb87e81c916e7"
+}
diff --git a/.sqlx/query-e24bd98ae4e783519395ea81118302d86c7bc3944b470d80fb8bd2d08af33af2.json b/.sqlx/query-e24bd98ae4e783519395ea81118302d86c7bc3944b470d80fb8bd2d08af33af2.json
new file mode 100644
index 0000000000..df0e536713
--- /dev/null
+++ b/.sqlx/query-e24bd98ae4e783519395ea81118302d86c7bc3944b470d80fb8bd2d08af33af2.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT id FROM databases WHERE id = $1 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "e24bd98ae4e783519395ea81118302d86c7bc3944b470d80fb8bd2d08af33af2"
+}
diff --git a/.sqlx/query-70632e61f5e84f7f17e71aa0ea5d791719e671a6c94a7b3bccb5ebfd65fb29e1.json b/.sqlx/query-e47436b70c82eed6a6663b4409ba433efd38fb474369d145b9c356a9b0909711.json
similarity index 56%
rename from .sqlx/query-70632e61f5e84f7f17e71aa0ea5d791719e671a6c94a7b3bccb5ebfd65fb29e1.json
rename to .sqlx/query-e47436b70c82eed6a6663b4409ba433efd38fb474369d145b9c356a9b0909711.json
index 5eac01d865..c8113e9b04 100644
--- a/.sqlx/query-70632e61f5e84f7f17e71aa0ea5d791719e671a6c94a7b3bccb5ebfd65fb29e1.json
+++ b/.sqlx/query-e47436b70c82eed6a6663b4409ba433efd38fb474369d145b9c356a9b0909711.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT ea.access_level::text\n FROM database_rows r\n JOIN database_tables t ON t.id = r.table_id\n JOIN databases d ON d.id = t.database_id\n JOIN entity_access ea ON ea.entity_id = d.id\n WHERE r.id = $1\n AND ea.entity_type = 'database'\n AND ea.source_id = ANY($2)\n ",
+ "query": "\n SELECT ea.access_level::text\n FROM database_rows r\n JOIN database_tables t ON t.id = r.table_id\n JOIN database_entities d ON d.database_id = t.database_id\n JOIN entity_access ea ON ea.entity_id = d.database_id\n WHERE r.id = $1\n AND ea.entity_type = 'database'\n AND ea.source_id = ANY($2)\n ",
"describe": {
"columns": [
{
@@ -19,5 +19,5 @@
null
]
},
- "hash": "70632e61f5e84f7f17e71aa0ea5d791719e671a6c94a7b3bccb5ebfd65fb29e1"
+ "hash": "e47436b70c82eed6a6663b4409ba433efd38fb474369d145b9c356a9b0909711"
}
diff --git a/.sqlx/query-4ef0544f6ddd8a625481413705bba931219a21f3eaef3488ff8e7984c44c7efe.json b/.sqlx/query-ea83fc13768d482c4057d787cf4e74252ddde74f127f1f0760798f0154ae19ec.json
similarity index 83%
rename from .sqlx/query-4ef0544f6ddd8a625481413705bba931219a21f3eaef3488ff8e7984c44c7efe.json
rename to .sqlx/query-ea83fc13768d482c4057d787cf4e74252ddde74f127f1f0760798f0154ae19ec.json
index 17d72146f3..2e3b9b9db2 100644
--- a/.sqlx/query-4ef0544f6ddd8a625481413705bba931219a21f3eaef3488ff8e7984c44c7efe.json
+++ b/.sqlx/query-ea83fc13768d482c4057d787cf4e74252ddde74f127f1f0760798f0154ae19ec.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "UPDATE database_columns SET property_definition_id = $4, infer_type = FALSE\n WHERE id = $1 AND table_id = $2 AND property_definition_id = $3 AND infer_type\n AND NOT EXISTS (\n SELECT 1 FROM entity_properties p\n JOIN database_rows r ON r.id::text = p.entity_id\n WHERE r.table_id = $2 AND p.property_definition_id = $3\n AND p.entity_type = 'DATABASE_ROW')\n AND EXISTS (SELECT 1 FROM databases WHERE id = $5 AND trashed_at IS NULL)\n RETURNING id",
+ "query": "UPDATE database_columns SET property_definition_id = $4, infer_type = FALSE\n WHERE id = $1 AND table_id = $2 AND property_definition_id = $3 AND infer_type\n AND NOT EXISTS (\n SELECT 1 FROM entity_properties p\n JOIN database_rows r ON r.id::text = p.entity_id\n WHERE r.table_id = $2 AND p.property_definition_id = $3\n AND p.entity_type = 'DATABASE_ROW')\n AND EXISTS (SELECT 1 FROM database_entities WHERE database_id = $5 AND trashed_at IS NULL)\n RETURNING id",
"describe": {
"columns": [
{
@@ -22,5 +22,5 @@
false
]
},
- "hash": "4ef0544f6ddd8a625481413705bba931219a21f3eaef3488ff8e7984c44c7efe"
+ "hash": "ea83fc13768d482c4057d787cf4e74252ddde74f127f1f0760798f0154ae19ec"
}
diff --git a/.sqlx/query-eb08b9e4d30641ae932d5a572e4239f5fbc5a471fe5811e6521a2ab157008681.json b/.sqlx/query-eb08b9e4d30641ae932d5a572e4239f5fbc5a471fe5811e6521a2ab157008681.json
new file mode 100644
index 0000000000..57d597d6d2
--- /dev/null
+++ b/.sqlx/query-eb08b9e4d30641ae932d5a572e4239f5fbc5a471fe5811e6521a2ab157008681.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO macro_user (id, username, email, stripe_customer_id)\n VALUES ($1, $2, $2, $2)\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "eb08b9e4d30641ae932d5a572e4239f5fbc5a471fe5811e6521a2ab157008681"
+}
diff --git a/.sqlx/query-2b91822984b3e81e344bdfa3d26f9279e07fdc78e9dd3ae87690e32d24e31bbd.json b/.sqlx/query-f01abd5acf0f8a9c98f414dc5d20f269de0db25b8bb8e8b5db3c3ae723bab81d.json
similarity index 50%
rename from .sqlx/query-2b91822984b3e81e344bdfa3d26f9279e07fdc78e9dd3ae87690e32d24e31bbd.json
rename to .sqlx/query-f01abd5acf0f8a9c98f414dc5d20f269de0db25b8bb8e8b5db3c3ae723bab81d.json
index dc2105d159..00f5597c70 100644
--- a/.sqlx/query-2b91822984b3e81e344bdfa3d26f9279e07fdc78e9dd3ae87690e32d24e31bbd.json
+++ b/.sqlx/query-f01abd5acf0f8a9c98f414dc5d20f269de0db25b8bb8e8b5db3c3ae723bab81d.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "UPDATE databases SET trashed_at = $2, updated_at = now() WHERE id = $1",
+ "query": "UPDATE database_entities SET trashed_at = $2, updated_at = now() WHERE database_id = $1",
"describe": {
"columns": [],
"parameters": {
@@ -11,5 +11,5 @@
},
"nullable": []
},
- "hash": "2b91822984b3e81e344bdfa3d26f9279e07fdc78e9dd3ae87690e32d24e31bbd"
+ "hash": "f01abd5acf0f8a9c98f414dc5d20f269de0db25b8bb8e8b5db3c3ae723bab81d"
}
diff --git a/.sqlx/query-56320f1e57837e5e867413be6031f2e044340d8e1e9fc557729312856b548a90.json b/.sqlx/query-f103a2c9c4bb3b3403f73f002e5dfc94815dd44aba907382b45163280c9e53f2.json
similarity index 56%
rename from .sqlx/query-56320f1e57837e5e867413be6031f2e044340d8e1e9fc557729312856b548a90.json
rename to .sqlx/query-f103a2c9c4bb3b3403f73f002e5dfc94815dd44aba907382b45163280c9e53f2.json
index ed6f933838..882943eba9 100644
--- a/.sqlx/query-56320f1e57837e5e867413be6031f2e044340d8e1e9fc557729312856b548a90.json
+++ b/.sqlx/query-f103a2c9c4bb3b3403f73f002e5dfc94815dd44aba907382b45163280c9e53f2.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT access_level::text\n FROM entity_access\n WHERE entity_id = $1\n AND entity_type = 'database'\n AND source_id = ANY($2)\n ",
+ "query": "\n SELECT access_level::text\n FROM entity_access\n JOIN database_entities d ON d.database_id = entity_access.entity_id\n WHERE entity_id = $1\n AND entity_type = 'database'\n AND source_id = ANY($2)\n ",
"describe": {
"columns": [
{
@@ -19,5 +19,5 @@
null
]
},
- "hash": "56320f1e57837e5e867413be6031f2e044340d8e1e9fc557729312856b548a90"
+ "hash": "f103a2c9c4bb3b3403f73f002e5dfc94815dd44aba907382b45163280c9e53f2"
}
diff --git a/.sqlx/query-c9c89fbec08c86b1159628749eefb1e98c08e89ac8be0fc569f0809e5fc33403.json b/.sqlx/query-f2af84dbfa64cd884cae988fb76db056c11f32b0cff76152c069724b1fcf035c.json
similarity index 70%
rename from .sqlx/query-c9c89fbec08c86b1159628749eefb1e98c08e89ac8be0fc569f0809e5fc33403.json
rename to .sqlx/query-f2af84dbfa64cd884cae988fb76db056c11f32b0cff76152c069724b1fcf035c.json
index 8c728035a1..81dff081ff 100644
--- a/.sqlx/query-c9c89fbec08c86b1159628749eefb1e98c08e89ac8be0fc569f0809e5fc33403.json
+++ b/.sqlx/query-f2af84dbfa64cd884cae988fb76db056c11f32b0cff76152c069724b1fcf035c.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT id, name, owner_id, created_at, trashed_at\n FROM databases\n WHERE id = ANY($1)\n ORDER BY created_at\n ",
+ "query": "\n SELECT database_id AS id, name, user_id AS owner_id, created_at, trashed_at\n FROM database_entities\n WHERE database_id = ANY($1)\n ORDER BY created_at\n ",
"describe": {
"columns": [
{
@@ -42,5 +42,5 @@
true
]
},
- "hash": "c9c89fbec08c86b1159628749eefb1e98c08e89ac8be0fc569f0809e5fc33403"
+ "hash": "f2af84dbfa64cd884cae988fb76db056c11f32b0cff76152c069724b1fcf035c"
}
diff --git a/.sqlx/query-fa4718f2b7b0c4a6174affe0da6949a0f2410bbab7454f38ddaa2b29df927158.json b/.sqlx/query-fa4718f2b7b0c4a6174affe0da6949a0f2410bbab7454f38ddaa2b29df927158.json
new file mode 100644
index 0000000000..7a55728fd0
--- /dev/null
+++ b/.sqlx/query-fa4718f2b7b0c4a6174affe0da6949a0f2410bbab7454f38ddaa2b29df927158.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "WITH storage AS (INSERT INTO databases (id) VALUES ($1) RETURNING id)\n INSERT INTO database_entities (database_id, name, user_id) SELECT id, 'db', 'macro|[email protected]' FROM storage",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "fa4718f2b7b0c4a6174affe0da6949a0f2410bbab7454f38ddaa2b29df927158"
+}
diff --git a/CLAUDE.md b/CLAUDE.md
index 504499d543..2484aa3d7c 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -29,6 +29,8 @@ This file is the shared entry point: `AGENTS.md` symlinks to `CLAUDE.md`. Edit
| Task | Guide |
| --- | --- |
+| Reusing database storage or separating it from an app entity | [Database core skill](.agents/skills/database-core/SKILL.md) |
+| Adding collaborative markdown to a feature | [Collab surface core skill](.agents/skills/collab-surface-core/SKILL.md) |
| Rust code, builds, or tests | [Rust development](docs/RUST_DEVELOPMENT.md) |
| SQLx queries, migrations, DB tests, or cache errors | [Database development](docs/DATABASE_DEVELOPMENT.md) |
| Web frontend | [Web agent guide](apps/web/AGENTS.md) |
diff --git a/apps/web/src/features/block-database/browser-test/records.browser.e2e.ts b/apps/web/src/features/block-database/browser-test/records.browser.e2e.ts
new file mode 100644
index 0000000000..e192aecea0
--- /dev/null
+++ b/apps/web/src/features/block-database/browser-test/records.browser.e2e.ts
@@ -0,0 +1,27 @@
+import { expect, test } from '@playwright/test';
+
+test('edits embedded records without entity metadata or schema controls', async ({
+ page,
+}) => {
+ await page.routeWebSocket('**', (socket) => socket.close());
+ await page.route('https://**', (route) => route.abort());
+ await page.route('**/__macro_dev/**', (route) => route.abort());
+ await page.goto('/src/features/block-database/browser-test/records.html', {
+ waitUntil: 'domcontentloaded',
+ });
+ const grid = page.getByRole('grid', { name: 'Embedded records' });
+ await expect(grid).toBeVisible();
+ await expect(grid).toHaveAttribute('aria-colcount', '2');
+ await expect(page.getByRole('columnheader')).toHaveCount(2);
+ await page.getByRole('button', { name: /^Name: First record\./ }).dblclick();
+ const input = page.getByRole('textbox', { name: 'Edit Name' });
+ await input.fill('Updated record');
+ await input.press('Tab');
+ await expect(page.getByLabel('Stored name')).toHaveText('Updated record');
+ await page.getByRole('button', { name: 'Read only', exact: true }).click();
+ await page
+ .getByRole('button', { name: 'Name: Updated record', exact: true })
+ .dblclick();
+ await expect(page.getByRole('textbox', { name: 'Edit Name' })).toHaveCount(0);
+ await expect(page.getByLabel('Stored name')).toHaveText('Updated record');
+});
diff --git a/apps/web/src/features/block-database/browser-test/records.html b/apps/web/src/features/block-database/browser-test/records.html
new file mode 100644
index 0000000000..ea4514b3b1
--- /dev/null
+++ b/apps/web/src/features/block-database/browser-test/records.html
@@ -0,0 +1,5 @@
+<!doctype html>
+<html lang="en">
+ <head><meta charset="utf-8" /><title>Embedded database records regression</title></head>
+ <body><div id="root"></div><script type="module" src="./records.tsx"></script></body>
+</html>
diff --git a/apps/web/src/features/block-database/browser-test/records.tsx b/apps/web/src/features/block-database/browser-test/records.tsx
new file mode 100644
index 0000000000..1ba88b43ef
--- /dev/null
+++ b/apps/web/src/features/block-database/browser-test/records.tsx
@@ -0,0 +1,85 @@
+import '@app/index.css';
+import { okAsync } from 'neverthrow';
+import { createSignal } from 'solid-js';
+import { render } from 'solid-js/web';
+import type {
+ DatabaseRowsSnapshot,
+ DatabaseRowsSource,
+} from '../context/table-source';
+import type { DatabaseViewColumn } from '../core/database-view';
+import { optimisticRows } from '../core/table';
+import { DatabaseRecordsView } from '../views/database-records-view';
+
+// A host owns the records and writes. No database entity, saved view, or app provider.
+function RecordsFixture() {
+ const columns: DatabaseViewColumn[] = [
+ {
+ id: 'name',
+ name: 'Name',
+ dataType: 'STRING',
+ writable: true,
+ isMultiSelect: false,
+ options: [],
+ },
+ ];
+ const [snapshot, setSnapshot] = createSignal<DatabaseRowsSnapshot>({
+ rows: [{ rowId: 'one', cells: { name: 'First record' } }],
+ retained: [],
+ version: 1,
+ });
+ const [canEdit, setCanEdit] = createSignal(true);
+ let nextRow = 0;
+ const source: DatabaseRowsSource = {
+ columns: () => columns,
+ snapshot,
+ read: () => undefined,
+ loading: () => false,
+ refreshing: () => false,
+ error: () => undefined,
+ refresh: () => okAsync(undefined),
+ addOption: () => okAsync(undefined),
+ retain: () => {},
+ write: (mutation) => {
+ const current = snapshot();
+ const version = (current.version ?? 0) + 1;
+ const insertedRowIds =
+ mutation.kind === 'create' ? [`created-${++nextRow}`] : [];
+ const rows =
+ mutation.kind === 'create'
+ ? [
+ ...current.rows,
+ { rowId: insertedRowIds[0], cells: mutation.values },
+ ]
+ : optimisticRows(current.rows, [mutation]);
+ setSnapshot({ rows, retained: [], version });
+ return okAsync({ version, insertedRowIds });
+ },
+ };
+ return (
+ <main class="flex h-screen flex-col bg-panel text-ink">
+ <div class="flex gap-4 p-4">
+ <button onClick={() => setCanEdit(false)}>Read only</button>
+ <output aria-label="Stored name">
+ {snapshot().rows[0]?.cells.name}
+ </output>
+ </div>
+ <DatabaseRecordsView
+ name="Embedded records"
+ source={source}
+ canEdit={canEdit()}
+ view={{
+ query: { filter: null },
+ layout: { kind: 'table', columns: [] },
+ }}
+ stored={false}
+ boardPositions={{
+ state: () => ({ kind: 'ready', positions: [] }),
+ setPositions: () => {},
+ move: () => okAsync({ positions: [], tableVersion: 1 }),
+ }}
+ />
+ </main>
+ );
+}
+
+render(() => <RecordsFixture />, document.getElementById('root')!);
diff --git a/apps/web/src/features/block-database/component/Block.tsx b/apps/web/src/features/block-database/component/Block.tsx
index 32400488c0..9e6e8a6a06 100644
--- a/apps/web/src/features/block-database/component/Block.tsx
+++ b/apps/web/src/features/block-database/component/Block.tsx
@@ -48,6 +48,7 @@ import { DatabaseToolbar } from '../components/database-toolbar';
import type { NewView } from '../components/new-view-dialog';
import { databaseChat } from '../core/chat-context';
import type { DatabaseRelatedDestination } from '../core/database-relations';
+import type { ViewChange } from '../core/view-state';
import { allRecordsView, boardLayout } from '../core/views';
import { databaseOpMessage } from '../core/write-failure';
import { createDatabaseSearch } from '../primitives/database-search';
@@ -63,7 +64,6 @@ import {
reorderDatabaseViews,
showAsBoardWithStatusColumn,
updateDatabaseView,
- type ViewChange,
} from '../queries/views';
import { DatabaseGrid } from './DatabaseGrid';
import { DatabasePageShell } from './DatabasePageShell';
diff --git a/apps/web/src/features/block-database/component/DatabaseGrid.tsx b/apps/web/src/features/block-database/component/DatabaseGrid.tsx
index 34db578123..6feb33b741 100644
--- a/apps/web/src/features/block-database/component/DatabaseGrid.tsx
+++ b/apps/web/src/features/block-database/component/DatabaseGrid.tsx
@@ -35,6 +35,7 @@ import type { DatabaseEntityType } from '../core/column-inference';
import { mergeDatabaseColumnOrder } from '../core/column-order';
import { convertedColumnName } from '../core/column-schema';
import type { DatabaseRelatedDestination } from '../core/database-relations';
+import type { ViewChange } from '../core/view-state';
import {
DatabaseMentionPicker,
DatabaseMentionValue,
@@ -59,7 +60,6 @@ import {
refreshCardPositions,
setCardPositions,
useCardPositions,
- type ViewChange,
} from '../queries/views';
import type { BoardPositions } from '../views/database-board-view';
import {
@@ -251,7 +251,7 @@ function TableAdapter(props: DatabaseGridProps & { tableId: string }) {
const viewId = boardViewId();
if (viewId) setCardPositions(databaseId, viewId, change);
},
- move: moveDatabaseCard,
+ move: (move) => moveDatabaseCard(props.view, move),
};
const optionEditing: OptionEditing = {
update: (columnId, optionId, change) =>
diff --git a/apps/web/src/features/block-database/component/GridCell.test.tsx b/apps/web/src/features/block-database/component/GridCell.test.tsx
index fd71d25102..5d70143855 100644
--- a/apps/web/src/features/block-database/component/GridCell.test.tsx
+++ b/apps/web/src/features/block-database/component/GridCell.test.tsx
@@ -20,9 +20,8 @@ import {
GridCell,
} from './GridCell';
-// The date selector's focus helper waits on IntersectionObserver, and the
-// property utils barrel pulls in live clients; neither exists under jsdom.
-vi.mock('@property/utils', () => ({
+// JSDOM has no intersection observer to drive the date selector's focus.
+vi.mock('@property/utils/focus', () => ({
// Like the real helper, focus after the menu has taken focus on open.
useSearchInputFocus: (input: () => HTMLElement | undefined) =>
setTimeout(() => input()?.focus(), 100),
diff --git a/apps/web/src/features/block-database/components/database-table.test.tsx b/apps/web/src/features/block-database/components/database-table.test.tsx
index a510c4c364..f049b1806f 100644
--- a/apps/web/src/features/block-database/components/database-table.test.tsx
+++ b/apps/web/src/features/block-database/components/database-table.test.tsx
@@ -16,8 +16,8 @@ import type { DatabaseRow } from '../core/table';
import { DatabaseTableView as DatabaseTable } from '../views/database-table-view';
import type { DatabaseTableControls } from './database-table';
-// The property utils barrel pulls in live clients, which open sockets under jsdom.
-vi.mock('@property/utils', () => ({
+// JSDOM has no intersection observer to drive the date selector's focus.
+vi.mock('@property/utils/focus', () => ({
useSearchInputFocus: (input: () => HTMLElement | undefined) =>
setTimeout(() => input()?.focus(), 100),
}));
@@ -52,6 +52,11 @@ const rows: DatabaseRow[] = [
{ rowId: 'two', cells: { name: 'Second', notes: 'Second note' } },
];
function setup(canEdit = true) {
+ const onAddColumnMount = vi.fn();
+ function AddColumn() {
+ onAddColumnMount();
+ return <button>Add column</button>;
+ }
const onOpen = vi.fn();
const onClearCells = vi.fn(async () => true);
const onCellFocus = vi.fn();
@@ -80,7 +85,7 @@ function setup(canEdit = true) {
widths={{}}
canEdit={canEdit}
pending={false}
- addColumn={<button>Add column</button>}
+ addColumn={<AddColumn />}
controlsRef={(tableControls) => {
controls = tableControls;
}}
@@ -106,6 +111,7 @@ function setup(canEdit = true) {
/>
));
return {
+ onAddColumnMount,
onOpen,
onClearCells,
onCellFocus,
@@ -143,6 +149,16 @@ afterEach(() => {
});
describe('spreadsheet interactions', () => {
+ it('creates the add-column control once across grid layout and row updates', () => {
+ const fixture = setup();
+ expect(fixture.onAddColumnMount).toHaveBeenCalledOnce();
+ fixture.setRecords([...rows, { rowId: 'three', cells: { name: 'Third' } }]);
+ expect(fixture.onAddColumnMount).toHaveBeenCalledOnce();
+ expect(screen.getAllByRole('button', { name: 'Add column' })).toHaveLength(
+ 1
+ );
+ });
+
function dragGeometry(scrollLeft = () => 0) {
const positions: Record<string, number> = {
Name: 40,
diff --git a/apps/web/src/features/block-database/components/database-table.tsx b/apps/web/src/features/block-database/components/database-table.tsx
index 13d174704c..3ac1f19568 100644
--- a/apps/web/src/features/block-database/components/database-table.tsx
+++ b/apps/web/src/features/block-database/components/database-table.tsx
@@ -19,6 +19,7 @@ import { DragDropProvider, DragOverlay } from '@thisbeyond/solid-dnd';
import { getHashedPaletteColor } from '@ui/utils/palette';
import {
type Accessor,
+ children,
createEffect,
createMemo,
createSignal,
@@ -97,7 +98,7 @@ export type DatabaseTableProps = {
resizable?: boolean;
canEdit: boolean;
pending: boolean;
- addColumn: JSX.Element;
+ addColumn?: JSX.Element;
emptyState?: JSX.Element;
controlsRef?: (controls: DatabaseTableControls) => void;
renderCell: (
@@ -137,6 +138,13 @@ export type DatabaseTableProps = {
export function DatabaseTable(props: DatabaseTableProps) {
const columns = () => props.model.visibleColumns();
+ const addColumn = children(() =>
+ props.canEdit ? props.addColumn : undefined
+ );
+ const hasAddColumn = () =>
+ addColumn
+ .toArray()
+ .some((child) => child != null && typeof child !== 'boolean');
const rows = () => props.model.table.getRowModel().rows;
let scrollContainer!: HTMLDivElement;
let gridElement!: HTMLDivElement;
@@ -326,7 +334,7 @@ export function DatabaseTable(props: DatabaseTableProps) {
? 'min(var(--database-title-column-width, 18rem), max(9rem, calc(100cqw - 11.5rem)))'
: '12rem';
})
- .join(' ')} ${props.canEdit ? '8.75rem' : ''}`
+ .join(' ')} ${hasAddColumn() ? '8.75rem' : ''}`
);
function moveFocus(event: KeyboardEvent) {
if (
@@ -478,7 +486,7 @@ export function DatabaseTable(props: DatabaseTableProps) {
aria-multiselectable="true"
aria-label={props.name}
aria-rowcount={rows().length + 1}
- aria-colcount={columns().length + 1 + Number(props.canEdit)}
+ aria-colcount={columns().length + 1 + Number(hasAddColumn())}
data-grid
class="relative flex min-h-full min-w-fit flex-col"
onKeyDown={moveFocus}
@@ -575,12 +583,12 @@ export function DatabaseTable(props: DatabaseTableProps) {
);
}}
</Key>
- <Show when={props.canEdit}>
+ <Show when={hasAddColumn()}>
<div
role="columnheader"
class="flex items-center justify-start px-2"
>
- {props.addColumn}
+ {addColumn()}
</div>
</Show>
</div>
@@ -750,7 +758,7 @@ export function DatabaseTable(props: DatabaseTableProps) {
);
}}
</Key>
- <Show when={props.canEdit}>
+ <Show when={hasAddColumn()}>
<div role="gridcell" />
</Show>
</div>
@@ -877,7 +885,7 @@ export function DatabaseTable(props: DatabaseTableProps) {
<For each={columns()}>
{() => <div class="border-r border-edge-muted/40" />}
</For>
- <Show when={props.canEdit}>
+ <Show when={hasAddColumn()}>
<div />
</Show>
</div>
diff --git a/apps/web/src/features/block-database/components/database-toolbar.test.tsx b/apps/web/src/features/block-database/components/database-toolbar.test.tsx
index 60110fb526..2880b85373 100644
--- a/apps/web/src/features/block-database/components/database-toolbar.test.tsx
+++ b/apps/web/src/features/block-database/components/database-toolbar.test.tsx
@@ -9,7 +9,7 @@ import {
import { errAsync, okAsync } from 'neverthrow';
import { createSignal } from 'solid-js';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import type { ViewChange } from '../queries/views';
+import type { ViewChange } from '../core/view-state';
import { DatabaseToolbar } from './database-toolbar';
vi.mock('@core/mobile/isMobile', () => ({ isMobile: () => false }));
diff --git a/apps/web/src/features/block-database/components/database-toolbar.tsx b/apps/web/src/features/block-database/components/database-toolbar.tsx
index 70ed6ed555..dfbcff1bfb 100644
--- a/apps/web/src/features/block-database/components/database-toolbar.tsx
+++ b/apps/web/src/features/block-database/components/database-toolbar.tsx
@@ -25,12 +25,12 @@ import type { ResultAsync } from 'neverthrow';
import { createSignal, For, type JSX, Show } from 'solid-js';
import type { BoardGrouping } from '../core/board-grouping';
import type { DatabaseViewColumn } from '../core/database-view';
+import type { ViewChange } from '../core/view-state';
import { boardGroupColumns, movedViewOrder } from '../core/views';
import {
type DatabaseOpFailure,
databaseOpMessage,
} from '../core/write-failure';
-import type { ViewChange } from '../queries/views';
import { FilterPanel, filterConditionCount } from './database-view-filters';
import { createInlineRename } from './inline-rename';
import { type NewView, NewViewDialog } from './new-view-dialog';
diff --git a/apps/web/src/features/block-database/components/sort-panel.tsx b/apps/web/src/features/block-database/components/sort-panel.tsx
index 6ec3f507ae..ec13d83c0c 100644
--- a/apps/web/src/features/block-database/components/sort-panel.tsx
+++ b/apps/web/src/features/block-database/components/sort-panel.tsx
@@ -14,7 +14,7 @@ import { DragSessionSensors } from '../../../components/drag-drop/drag-session-s
import { VerticalInsertionLine } from '../../../components/drag-drop/insertion-line';
import type { DatabaseViewColumn } from '../core/database-view';
import { withSort, withSortMoved } from '../core/view-query';
-import type { ViewChange } from '../queries/views';
+import type { ViewChange } from '../core/view-state';
import { ViewSelect } from './view-select';
/** A view's sort levels, first first; drag a level's handle to change its priority. */
diff --git a/apps/web/src/features/block-database/core/view-state.ts b/apps/web/src/features/block-database/core/view-state.ts
new file mode 100644
index 0000000000..effa2a8ef6
--- /dev/null
+++ b/apps/web/src/features/block-database/core/view-state.ts
@@ -0,0 +1,15 @@
+import type { CardPosition } from '@service-storage/generated/schemas/cardPosition';
+import type { ViewLayout } from '@service-storage/generated/schemas/viewLayout';
+import type { ViewQuery } from '@service-storage/generated/schemas/viewQuery';
+
+/** What the records UI needs to draw a view, whether the host saves it or not. */
+export type DatabaseViewState = {
+ query: ViewQuery;
+ layout: ViewLayout;
+};
+
+/** A view change for its host to apply locally or persist. */
+export type ViewChange = Partial<DatabaseViewState> & { name?: string };
+
+/** The places a card move wrote and the table version it left. */
+export type CardMoved = { positions: CardPosition[]; tableVersion: number };
diff --git a/apps/web/src/features/block-database/core/views.ts b/apps/web/src/features/block-database/core/views.ts
index c3d236c710..3c9794f281 100644
--- a/apps/web/src/features/block-database/core/views.ts
+++ b/apps/web/src/features/block-database/core/views.ts
@@ -8,6 +8,7 @@ import { match } from 'ts-pattern';
import { type DatabaseViewColumn, isBoardGroupColumn } from './database-view';
import { moveBeside } from './move-beside';
import { titleColumn } from './table';
+import type { DatabaseViewState } from './view-state';
/** A table layout's column, with its width. */
type LayoutColumn = {
@@ -252,10 +253,10 @@ function withoutColumnNode(
* entry go. A board grouped by it has nothing else to group by, so the
* column cannot be removed from it, as the service refuses too.
*/
-export function withoutColumn(
- view: DatabaseView,
+export function withoutColumn<T extends DatabaseViewState>(
+ view: T,
columnId: string
-): Result<DatabaseView, { kind: 'board-groups-by-column' }> {
+): Result<T, { kind: 'board-groups-by-column' }> {
if (view.layout.kind === 'board' && view.layout.groupBy === columnId)
return err({ kind: 'board-groups-by-column' });
const filter = view.query.filter;
diff --git a/apps/web/src/features/block-database/primitives/column-layout.ts b/apps/web/src/features/block-database/primitives/column-layout.ts
index 9d6127954e..ac7dba3fc1 100644
--- a/apps/web/src/features/block-database/primitives/column-layout.ts
+++ b/apps/web/src/features/block-database/primitives/column-layout.ts
@@ -1,4 +1,3 @@
-import type { DatabaseView } from '@service-storage/generated/schemas/databaseView';
import { until } from '@solid-primitives/promise';
import { Mutex } from 'async-mutex';
import { type Accessor, createSignal, onCleanup } from 'solid-js';
@@ -9,20 +8,20 @@ import {
} from '../core/column-schema';
import type { DatabaseViewColumn } from '../core/database-view';
import { withSort } from '../core/view-query';
+import type { DatabaseViewState, ViewChange } from '../core/view-state';
import {
layoutColumns,
withLayoutColumn,
withLayoutOrder,
withoutColumn,
} from '../core/views';
-import type { ViewChange } from '../queries/views';
/**
* A view's columns as the table shows them, and the header actions that
* change them: sorting, resizing, reordering and inserting.
*/
export function createColumnLayout(options: {
- view: Accessor<DatabaseView>;
+ view: Accessor<DatabaseViewState>;
columns: Accessor<DatabaseViewColumn[]>;
canEdit: Accessor<boolean>;
/** Whether the view is stored, so its own layout holds the column order. */
diff --git a/apps/web/src/features/block-database/queries/views.ts b/apps/web/src/features/block-database/queries/views.ts
index 183e3f16c5..de4860d3ea 100644
--- a/apps/web/src/features/block-database/queries/views.ts
+++ b/apps/web/src/features/block-database/queries/views.ts
@@ -1,3 +1,4 @@
+import type { CardMoved, ViewChange } from '../core/view-state';
/** A table's typed views and a board's card places, written as ops and shown in the cached detail ahead of the answer. */
import type { DatabaseOp } from '@core/database-sql/generated/types';
@@ -11,7 +12,6 @@ import { storageServiceClient } from '@service-storage/client';
import type { CardPosition } from '@service-storage/generated/schemas/cardPosition';
import type { DatabaseView } from '@service-storage/generated/schemas/databaseView';
import type { NewView } from '@service-storage/generated/schemas/newView';
-import type { ViewLayout } from '@service-storage/generated/schemas/viewLayout';
import type { ViewQuery } from '@service-storage/generated/schemas/viewQuery';
import { useQuery } from '@tanstack/solid-query';
import { errAsync, okAsync, ResultAsync } from 'neverthrow';
@@ -154,12 +154,6 @@ export function showAsBoardWithStatusColumn(
);
}
-export type ViewChange = {
- name?: string;
- query?: ViewQuery;
- layout?: ViewLayout;
-};
-
/** Change a view; a later change made before this one answers keeps its place. */
export function updateDatabaseView(
view: DatabaseView,
@@ -309,9 +303,6 @@ export function refreshCardPositions(databaseId: string, viewId: string) {
});
}
-/** The places a card move wrote and the table version it left. */
-export type CardMoved = { positions: CardPosition[]; tableVersion: number };
-
/**
* Move a board's card, which also sets its row's grouping cell. The places
* the server wrote replace the ones shown ahead of its answer.
diff --git a/apps/web/src/features/block-database/views/database-board-view.test.tsx b/apps/web/src/features/block-database/views/database-board-view.test.tsx
index 8647565c7a..21b09b6988 100644
--- a/apps/web/src/features/block-database/views/database-board-view.test.tsx
+++ b/apps/web/src/features/block-database/views/database-board-view.test.tsx
@@ -16,9 +16,9 @@ import {
import { errAsync, ok, okAsync, ResultAsync } from 'neverthrow';
import { createSignal } from 'solid-js';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import type { CardMoved } from '../core/view-state';
import type { DatabaseOpFailure } from '../core/write-failure';
import type { BoardPositionsState } from '../primitives/board-layout';
-import type { CardMoved } from '../queries/views';
import { DatabaseBoardView } from './database-board-view';
const engine = vi.hoisted(() => ({
@@ -213,7 +213,7 @@ describe('database board view', () => {
fireEvent.mouseDown(card, { button: 0, clientX: 560, clientY: 80 });
fireEvent.mouseMove(document, { clientX: 30, clientY: 150 });
fireEvent.mouseUp(document, { button: 0, clientX: 30, clientY: 150 });
- expect(move).toHaveBeenCalledWith(view, {
+ expect(move).toHaveBeenCalledWith({
row: 'moving',
lane: { kind: 'option', id: 'done' },
before: 'first',
@@ -539,7 +539,7 @@ describe('database board view', () => {
key: 'Enter',
});
await waitFor(() =>
- expect(move).toHaveBeenCalledWith(view, {
+ expect(move).toHaveBeenCalledWith({
row: 'moving',
lane: { kind: 'option', id: 'done' },
before: 'first',
@@ -691,7 +691,7 @@ describe('database board view', () => {
expect(onViewChange).toHaveBeenCalledWith({
query: { filter: null, sort: [] },
});
- expect(move).toHaveBeenCalledWith(view, {
+ expect(move).toHaveBeenCalledWith({
row: 'moving',
lane: { kind: 'option', id: 'done' },
before: 'first',
diff --git a/apps/web/src/features/block-database/views/database-board-view.tsx b/apps/web/src/features/block-database/views/database-board-view.tsx
index 587031ff78..4e604b7b7a 100644
--- a/apps/web/src/features/block-database/views/database-board-view.tsx
+++ b/apps/web/src/features/block-database/views/database-board-view.tsx
@@ -3,7 +3,6 @@ import { engineFailure } from '@core/database-sql/driver';
import type { LaneKey } from '@core/database-sql/generated/types';
import type { DatabaseOpsError } from '@service-storage/databases';
import type { CardPosition } from '@service-storage/generated/schemas/cardPosition';
-import type { DatabaseView } from '@service-storage/generated/schemas/databaseView';
import type { ViewLayout } from '@service-storage/generated/schemas/viewLayout';
import { Button } from '@ui/components/Button';
import { ConfirmDialog } from '@ui/components/ConfirmDialog';
@@ -44,6 +43,11 @@ import {
isOptionColumn,
} from '../core/database-view';
import type { DatabaseRow } from '../core/table';
+import type {
+ CardMoved,
+ DatabaseViewState,
+ ViewChange,
+} from '../core/view-state';
import { laneValue, withLaneHidden, withLaneOrder } from '../core/views';
import {
type DatabaseOpFailure,
@@ -55,7 +59,6 @@ import {
createBoardEngine,
} from '../primitives/board-layout';
import type { RecordCreation } from '../primitives/record-actions';
-import type { CardMoved, ViewChange } from '../queries/views';
type BoardLayout = Extract<ViewLayout, { kind: 'board' }>;
@@ -63,14 +66,11 @@ type BoardLayout = Extract<ViewLayout, { kind: 'board' }>;
export type BoardPositions = {
state: Accessor<BoardPositionsState>;
setPositions: (change: (positions: CardPosition[]) => CardPosition[]) => void;
- move: (
- view: DatabaseView,
- move: CardMove
- ) => ResultAsync<CardMoved, DatabaseOpFailure>;
+ move: (move: CardMove) => ResultAsync<CardMoved, DatabaseOpFailure>;
};
type DatabaseBoardViewProps = {
- view: DatabaseView;
+ view: DatabaseViewState;
source: DatabaseRowsSource;
rows: DatabaseRow[];
columns: DatabaseViewColumn[];
@@ -243,7 +243,7 @@ function GroupedBoard(
);
setMoves((current) => [...current, { move }]);
void props.positions
- .move(props.view, move)
+ .move(move)
.map(({ positions, tableVersion }) => {
props.positions.setPositions((current) =>
withPositions(current, positions)
diff --git a/apps/web/src/features/block-database/views/database-records-view.test.tsx b/apps/web/src/features/block-database/views/database-records-view.test.tsx
index a3fc830785..c39da61215 100644
--- a/apps/web/src/features/block-database/views/database-records-view.test.tsx
+++ b/apps/web/src/features/block-database/views/database-records-view.test.tsx
@@ -26,9 +26,9 @@ import { createSignal } from 'solid-js';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { DatabaseWriteResult } from '../context/table-source';
import type { DatabaseViewColumn } from '../core/database-view';
+import type { ViewChange } from '../core/view-state';
import { allRecordsView } from '../core/views';
import type { DatabaseWriteFailure } from '../core/write-failure';
-import type { ViewChange } from '../queries/views';
import { createFakeRowsSource, titleContains } from '../tests/fake-rows-source';
import type { BoardPositions } from './database-board-view';
import {
@@ -36,7 +36,7 @@ import {
DatabaseRecordsView,
} from './database-records-view';
-// The real date selector opens the app's websockets on import.
+// Date interactions are covered by the GridCell tests.
vi.mock('@property/editors/selectors/PropertyDateSelector', () => ({
PropertyDateSelector: () => null,
}));
@@ -234,6 +234,47 @@ afterEach(() => {
});
describe('database table view', () => {
+ it('edits host-owned records with only layout state and no schema actions', async () => {
+ const fixture = createFakeRowsSource({
+ columns: [columns[0]],
+ table: {
+ version: 1,
+ rows: [{ rowId: 'row', cells: { title: 'Plan launch' } }],
+ },
+ view: allRecords,
+ });
+ fixture.persistWrites();
+ render(() => (
+ <DatabaseRecordsView
+ name="Embedded records"
+ source={fixture.source}
+ canEdit
+ view={{
+ query: { filter: null },
+ layout: { kind: 'table', columns: [] },
+ }}
+ stored={false}
+ boardPositions={unplacedCards}
+ />
+ ));
+ expect(screen.getAllByRole('columnheader')).toHaveLength(2);
+ expect(screen.getByRole('grid').getAttribute('aria-colcount')).toBe('2');
+ const user = userEvent.setup();
+ await user.dblClick(
+ screen.getByRole('button', { name: /^Name: Plan launch\./ })
+ );
+ const input = screen.getByRole('textbox', { name: 'Edit Name' });
+ await user.clear(input);
+ await user.type(input, 'Launch ready');
+ await user.keyboard('{Tab}');
+ await waitFor(() =>
+ expect(fixture.source.snapshot()?.rows[0].cells.title).toBe(
+ 'Launch ready'
+ )
+ );
+ expect(screen.queryByRole('button', { name: 'Add column' })).toBeNull();
+ });
+
it('offers refresh and draft recovery after a lost create response without a duplicate Retry', async () => {
const fixture = createFakeRowsSource({
columns,
@@ -2085,8 +2126,6 @@ describe('database table view', () => {
canEdit
view={{
...allRecords,
- id: 'by-status',
- name: 'By status',
layout: {
kind: 'table',
columns: [
diff --git a/apps/web/src/features/block-database/views/database-records-view.tsx b/apps/web/src/features/block-database/views/database-records-view.tsx
index d573906d34..df5da22b41 100644
--- a/apps/web/src/features/block-database/views/database-records-view.tsx
+++ b/apps/web/src/features/block-database/views/database-records-view.tsx
@@ -1,4 +1,3 @@
-import type { DatabaseView } from '@service-storage/generated/schemas/databaseView';
import { until } from '@solid-primitives/promise';
import { Button } from '@ui/components/Button';
import { DeleteDialog } from '@ui/components/DeleteDialog';
@@ -51,6 +50,7 @@ import {
rowValue,
titleColumn,
} from '../core/table';
+import type { DatabaseViewState, ViewChange } from '../core/view-state';
import {
databaseReadMessage,
databaseWriteMessage,
@@ -60,7 +60,6 @@ import { createDraftRows } from '../primitives/draft-rows';
import { createHeldGridRows } from '../primitives/held-grid-rows';
import { createRecordActions } from '../primitives/record-actions';
import { createTableController } from '../primitives/table-controller';
-import type { ViewChange } from '../queries/views';
import { type BoardPositions, DatabaseBoardView } from './database-board-view';
import { DatabaseTableView } from './database-table-view';
@@ -90,12 +89,12 @@ export type RelationCellProps = GridCellEditorOptions & {
};
/** A table's records in the view's layout — a grid or a board — with the record panel over them. */
-export function DatabaseRecordsView(props: {
+export type DatabaseRecordsViewProps = {
name: string;
source: DatabaseRowsSource;
canEdit: boolean;
/** The view on screen: a stored one, or the table's own All records. */
- view: DatabaseView;
+ view: DatabaseViewState;
/** Whether the view is stored, so changing it changes it for everyone. */
preparingView?: boolean;
stored: boolean;
@@ -133,11 +132,13 @@ export function DatabaseRecordsView(props: {
renderToolbar?: (actions: DatabaseRecordsActions) => JSX.Element;
/** Create a new column at the end of the table and return its id. */
createColumn?: () => DatabaseSchemaChange<string>;
- addColumn: (
+ addColumn?: (
label?: string,
onCreated?: (columnId: string) => boolean
) => JSX.Element;
-}) {
+};
+
+export function DatabaseRecordsView(props: DatabaseRecordsViewProps) {
const controller = createTableController(props.source, (mutation, result) =>
records.recordSaved(mutation, result)
);
@@ -527,7 +528,7 @@ export function DatabaseRecordsView(props: {
}
canEdit={props.canEdit}
pending={controller.pending()}
- addColumn={props.addColumn(
+ addColumn={props.addColumn?.(
columns().length ? undefined : 'Add first column',
focusColumn
)}
diff --git a/apps/web/src/features/property/editors/selectors/PropertyDateSelector.tsx b/apps/web/src/features/property/editors/selectors/PropertyDateSelector.tsx
index 147d03edf2..a3e7a56791 100644
--- a/apps/web/src/features/property/editors/selectors/PropertyDateSelector.tsx
+++ b/apps/web/src/features/property/editors/selectors/PropertyDateSelector.tsx
@@ -3,7 +3,7 @@ import { useDateSearch } from '@core/util/dateSearch/useDateSearch';
import { useKeyPressed } from '@core/util/useKeyPressed';
import SearchIcon from '@phosphor/magnifying-glass.svg';
import type { DateProperty } from '@property/types';
-import { useSearchInputFocus } from '@property/utils';
+import { useSearchInputFocus } from '@property/utils/focus';
import { cn } from '@ui';
import {
createEffect,
diff --git a/apps/web/src/lib/core/database-sql/generated/types.ts b/apps/web/src/lib/core/database-sql/generated/types.ts
index 4af9f48edf..ec1a35c4aa 100644
--- a/apps/web/src/lib/core/database-sql/generated/types.ts
+++ b/apps/web/src/lib/core/database-sql/generated/types.ts
@@ -327,7 +327,10 @@ export type DataType =
/** References to entities. */
| 'ENTITY';
-/** Identifier of a database (the shareable entity users see). */
+/**
+ * Identifier of reusable database storage. Its optional Macro entity uses
+ * the same identity, so existing routes and grants keep their IDs.
+ */
export type DatabaseId = string;
/**
diff --git a/crates/databases/README.md b/crates/databases/README.md
index e46db813f4..4ec3211c35 100644
--- a/crates/databases/README.md
+++ b/crates/databases/README.md
@@ -1,8 +1,47 @@
# Database backend
-Databases contain tables of typed rows. A row is a `DATABASE_ROW` entity whose
-cells are its entity properties, and column placements bind to the existing
-property definitions and value types. There is no second store.
+Databases contain tables of typed rows. Cells use the properties system's
+`DATABASE_ROW` discriminator, and column placements bind to existing property
+definitions and value types. Rows of an app database participate as entities;
+rows of core storage alone do not acquire app access. There is one cell store.
+
+## Storage and app entities
+
+Each `databases` row identifies reusable storage. Tables, column definitions, views,
+saved queries, rows and their cells belong to that resource. A resource has no
+app name, owner, or trash state and can exist without any Macro entity.
+
+An optional `database_entities` row holds the app metadata: `name`, `user_id` (the owner),
+`created_at`, `updated_at`, and `trashed_at`. Its `database_id` is both its primary
+key and a foreign key to `databases.id`. The app uses that shared identity for
+routes, grants, references and API responses. `Database` remains the app-facing
+Rust model. Soup discovery and database/row access require this entity; merely
+creating core storage does not make it a shareable Macro database.
+
+This is the first use of the plural `<kind>_entities` convention for databases.
+New app entities should follow it, keeping reusable technology separate from
+app ownership and lifecycle. Existing unrelated entities are not renamed.
+Collaboration surfaces are a similar reusable resource; this change does not
+turn them into entities or change their parent-based access model.
+
+`DatabaseStorage` exposes allocation, validated storage batches, and deletion
+without app metadata. A future CRM domain can hold its own reference to a core
+database and authorize access through its own service. Its composition root
+supplies the storage adapter. The port is trusted persistence, not a new public
+API: consumers validate their own commands and authorize their own resources.
+The Macro database service creates its entity and owner grant atomically through
+its repository and uses `CellStore` to hold the entity's lifecycle lock during writes.
+Both paths use the same transaction engine, table versions, cells and journal.
+An app entity's foreign key prevents deleting its storage through the core port.
+
+The migration is a destructive cutover: it discards existing database content,
+grants, saved queries, journal entries, and starter markers, then removes app
+metadata from `databases`. Deploy it with the updated service; old binaries are
+not supported. Rollback also discards database content.
+
+Deleting an app entity (including through owner deletion) cleans up its owned
+storage, journal, and grants. Storage deletion cascades to tables, definitions,
+and cells. The core storage adapter purges its journal in the same transaction.
## Reads and writes
diff --git a/crates/databases/src/domain/ports.rs b/crates/databases/src/domain/ports.rs
index 1c2e78034c..783c18fe0b 100644
--- a/crates/databases/src/domain/ports.rs
+++ b/crates/databases/src/domain/ports.rs
@@ -170,6 +170,32 @@ pub trait DatabasesRepo: Send + Sync + 'static {
) -> impl Future<Output = Result<Vec<JournaledRowChange>, Self::Error>> + Send;
}
+/// Reusable database storage without a Macro entity, owner grant, or trash state.
+/// Callers are trusted domain services: each consumer must authorize its own
+/// resource and validate writes before using this port. Macro database requests
+/// use [`CellStore`] to enforce the app entity's lifecycle in the transaction.
+pub trait DatabaseStorage: Send + Sync + 'static {
+ /// Persistence failure, including an attempt to delete an attached resource.
+ type Error: std::error::Error + Send + Sync + 'static;
+
+ /// Allocate an empty core database without creating an app entity or grants.
+ fn create_storage(&self) -> impl Future<Output = Result<DatabaseId, Self::Error>> + Send;
+
+ /// Delete unbound storage, its tables, cells, definitions and journal.
+ /// An attached app entity prevents deletion through its foreign key.
+ fn delete_storage(
+ &self,
+ id: DatabaseId,
+ ) -> impl Future<Output = Result<(), Self::Error>> + Send;
+
+ /// Apply a validated batch atomically, checking resource existence and table
+ /// versions. Consumer-specific entity lifecycle and access are the caller's.
+ fn apply_storage_writes(
+ &self,
+ writes: &Writes,
+ ) -> impl Future<Output = Result<WritesOutcome, Self::Error>> + Send;
+}
+
/// A row's cells, kept by the properties system as entity properties of the
/// `DATABASE_ROW` entity the row id names. Authorization is the domain
/// service's, resolved through the row's database; the store trusts its
diff --git a/crates/databases/src/outbound/pg_cell_store.rs b/crates/databases/src/outbound/pg_cell_store.rs
index 9444efeea6..389a04edaf 100644
--- a/crates/databases/src/outbound/pg_cell_store.rs
+++ b/crates/databases/src/outbound/pg_cell_store.rs
@@ -2,6 +2,7 @@
//! shares one transaction across the schema, row identities, cells and
//! options.
+mod core;
mod transfer;
use std::collections::HashMap;
@@ -102,6 +103,18 @@ fn row_of(entity_id: &str) -> Result<RowId, PgCellStoreError> {
.map_err(|_| PgCellStoreError::CorruptRowId(entity_id.to_string()))
}
+/// Preserve the batch API's missing-table outcome when its parent is absent.
+fn missing_database(writes: &Writes) -> WritesOutcome {
+ WritesOutcome::TableNotFound(
+ writes
+ .writes
+ .iter()
+ .flat_map(|write| write.versioned_tables().iter().copied())
+ .next()
+ .unwrap_or_default(),
+ )
+}
+
fn cells_error(error: impl std::error::Error + Send + Sync + 'static) -> PgCellStoreError {
PgCellStoreError::Cells(Box::new(error))
}
@@ -180,6 +193,36 @@ where
// everything back.
let mut transaction = self.pool.begin().await?;
+ if !rows::lock_live_database(&mut transaction, writes.database_id).await? {
+ return Ok(missing_database(writes));
+ }
+ self.apply_in(transaction, writes).await
+ }
+}
+
+/// What one write did inside its batch: the rows it inserted, or the
+/// outcome refusing the whole batch.
+enum Applied {
+ Rows(Vec<RowId>),
+ Refused(WritesOutcome),
+}
+
+impl<Properties> PgCellStore<Properties>
+where
+ Properties: PropertiesRepo<Err = anyhow::Error>
+ + DatabaseCellWriter<Transaction = Transaction<'static, Postgres>>
+ + DatabaseOptionWriter
+ + DatabaseDefinitionWriter<Transaction = Transaction<'static, Postgres>>
+ + Send
+ + Sync
+ + 'static,
+{
+ /// Commit a core storage batch; the app wrapper takes its entity lock first.
+ async fn apply_in(
+ &self,
+ mut transaction: Transaction<'static, Postgres>,
+ writes: &Writes,
+ ) -> Result<WritesOutcome, PgCellStoreError> {
// Parent locks precede table locks, as every writer takes them.
if !schema::lock_database(
&mut transaction,
@@ -188,13 +231,7 @@ where
)
.await?
{
- let table = writes
- .writes
- .iter()
- .flat_map(|write| write.versioned_tables().iter().copied())
- .next()
- .unwrap_or_default();
- return Ok(WritesOutcome::TableNotFound(table));
+ return Ok(missing_database(writes));
}
let created: Vec<TableId> = writes
.writes
@@ -371,25 +408,7 @@ where
changes,
})
}
-}
-/// What one write did inside its batch: the rows it inserted, or the
-/// outcome refusing the whole batch.
-enum Applied {
- Rows(Vec<RowId>),
- Refused(WritesOutcome),
-}
-
-impl<Properties> PgCellStore<Properties>
-where
- Properties: PropertiesRepo<Err = anyhow::Error>
- + DatabaseCellWriter<Transaction = Transaction<'static, Postgres>>
- + DatabaseOptionWriter
- + DatabaseDefinitionWriter<Transaction = Transaction<'static, Postgres>>
- + Send
- + Sync
- + 'static,
-{
/// Apply one write of a batch inside its transaction.
async fn apply_write(
&self,
diff --git a/crates/databases/src/outbound/pg_cell_store/core.rs b/crates/databases/src/outbound/pg_cell_store/core.rs
new file mode 100644
index 0000000000..f6b296d103
--- /dev/null
+++ b/crates/databases/src/outbound/pg_cell_store/core.rs
@@ -0,0 +1,44 @@
+//! Storage entry points for consumers with their own entity and authorization.
+
+use super::*;
+use crate::domain::models::DatabaseId;
+use crate::domain::ports::DatabaseStorage;
+
+impl<Properties> DatabaseStorage for PgCellStore<Properties>
+where
+ Properties: PropertiesRepo<Err = anyhow::Error>
+ + DatabaseCellWriter<Transaction = Transaction<'static, Postgres>>
+ + DatabaseOptionWriter
+ + DatabaseDefinitionWriter<Transaction = Transaction<'static, Postgres>>
+ + Send
+ + Sync
+ + 'static,
+{
+ type Error = PgCellStoreError;
+
+ #[tracing::instrument(err, skip(self))]
+ async fn create_storage(&self) -> Result<DatabaseId, Self::Error> {
+ let id = DatabaseId::new();
+ sqlx::query!("INSERT INTO databases (id) VALUES ($1)", id.into_uuid())
+ .execute(&self.pool)
+ .await?;
+ Ok(id)
+ }
+
+ #[tracing::instrument(err, skip(self))]
+ async fn delete_storage(&self, id: DatabaseId) -> Result<(), Self::Error> {
+ let mut transaction = self.pool.begin().await?;
+ schema::lock_database(&mut transaction, id, true).await?;
+ journal::purge(&mut *transaction, id).await?;
+ sqlx::query!("DELETE FROM databases WHERE id = $1", id.into_uuid())
+ .execute(&mut *transaction)
+ .await?;
+ transaction.commit().await?;
+ Ok(())
+ }
+
+ #[tracing::instrument(err, skip(self, writes), fields(writes = writes.writes.len()))]
+ async fn apply_storage_writes(&self, writes: &Writes) -> Result<WritesOutcome, Self::Error> {
+ self.apply_in(self.pool.begin().await?, writes).await
+ }
+}
diff --git a/crates/databases/src/outbound/pg_cell_store/transfer.rs b/crates/databases/src/outbound/pg_cell_store/transfer.rs
index 732e8e4b43..16bcbc31b2 100644
--- a/crates/databases/src/outbound/pg_cell_store/transfer.rs
+++ b/crates/databases/src/outbound/pg_cell_store/transfer.rs
@@ -87,7 +87,9 @@ where
) -> Result<ImportOutcome, Self::Error> {
let fingerprint = fingerprint.0.as_str();
let mut transaction = self.pool.begin().await?;
- if !rows::lock_live_database(&mut *transaction, database_id).await? {
+ if !rows::lock_live_database(&mut transaction, database_id).await?
+ || !schema::lock_database(&mut transaction, database_id, true).await?
+ {
return Ok(ImportOutcome::NotFound);
}
let replayed = sqlx::query_as!(
diff --git a/crates/databases/src/outbound/pg_databases_repo.rs b/crates/databases/src/outbound/pg_databases_repo.rs
index 46cc794e38..92c94fab5b 100644
--- a/crates/databases/src/outbound/pg_databases_repo.rs
+++ b/crates/databases/src/outbound/pg_databases_repo.rs
@@ -78,7 +78,7 @@ pub(crate) fn stored_positions(positions: &[Position]) -> Vec<String> {
positions.iter().map(Position::to_string).collect()
}
-/// A `databases` row, read by `query_as!` and mapped onto [`Database`].
+/// A `database_entities` row, read by `query_as!` and mapped onto [`Database`].
pub(crate) struct DatabaseRecord {
pub(crate) id: Uuid,
pub(crate) name: String,
@@ -139,8 +139,7 @@ fn position_after(last: Option<&Position>) -> Result<Position, PositionError> {
key_between(last, None)
}
-/// Insert a database, its first table and its owner's grant inside
-/// `transaction`, so no database can exist that nobody can open.
+/// Insert a core database, its app entity, first table and owner grant atomically.
pub(crate) async fn insert_owned_database(
transaction: &mut Transaction<'static, Postgres>,
database_id: DatabaseId,
@@ -150,12 +149,18 @@ pub(crate) async fn insert_owned_database(
table_name: &str,
) -> Result<Database, PgDatabasesRepoError> {
let position = position_after(None)?;
+ sqlx::query!(
+ "INSERT INTO databases (id) VALUES ($1)",
+ database_id.into_uuid()
+ )
+ .execute(&mut **transaction)
+ .await?;
let database: Database = sqlx::query_as!(
DatabaseRecord,
r#"
- INSERT INTO databases (id, name, owner_id)
+ INSERT INTO database_entities (database_id, name, user_id)
VALUES ($1, $2, $3)
- RETURNING id, name, owner_id, created_at, trashed_at
+ RETURNING database_id AS id, name, user_id AS owner_id, created_at, trashed_at
"#,
database_id.into_uuid(),
name,
@@ -319,7 +324,7 @@ where
) -> Result<Option<(Database, Vec<Table>)>, Self::Error> {
let Some(database) = sqlx::query_as!(
DatabaseRecord,
- r#"SELECT id, name, owner_id, created_at, trashed_at FROM databases WHERE id = $1"#,
+ r#"SELECT database_id AS id, name, user_id AS owner_id, created_at, trashed_at FROM database_entities WHERE database_id = $1"#,
id.into_uuid()
)
.fetch_optional(&self.pool)
@@ -348,7 +353,7 @@ where
#[tracing::instrument(err, skip(self))]
async fn rename_database(&self, id: DatabaseId, name: &str) -> Result<bool, Self::Error> {
let renamed = sqlx::query!(
- r#"UPDATE databases SET name = $2, updated_at = now() WHERE id = $1"#,
+ r#"UPDATE database_entities SET name = $2, updated_at = now() WHERE database_id = $1"#,
id.into_uuid(),
name,
)
@@ -364,7 +369,7 @@ where
trashed_at: chrono::DateTime<chrono::Utc>,
) -> Result<bool, Self::Error> {
let trashed = sqlx::query!(
- r#"UPDATE databases SET trashed_at = $2, updated_at = now() WHERE id = $1"#,
+ r#"UPDATE database_entities SET trashed_at = $2, updated_at = now() WHERE database_id = $1"#,
id.into_uuid(),
trashed_at,
)
@@ -376,7 +381,7 @@ where
#[tracing::instrument(err, skip(self))]
async fn restore_database(&self, id: DatabaseId) -> Result<bool, Self::Error> {
let restored = sqlx::query!(
- r#"UPDATE databases SET trashed_at = NULL, updated_at = now() WHERE id = $1"#,
+ r#"UPDATE database_entities SET trashed_at = NULL, updated_at = now() WHERE database_id = $1"#,
id.into_uuid(),
)
.execute(&self.pool)
@@ -384,29 +389,16 @@ where
Ok(restored.rows_affected() == 1)
}
- /// Tables, columns, rows, views and database-owned property definitions
- /// go with the database through `ON DELETE CASCADE`, and the rows' cells
- /// by trigger; `entity_access` rows are a generic side table with no
- /// foreign key to `databases`, and the change journal outlives what it
- /// describes by design, so both are purged explicitly in the same
- /// transaction.
+ /// Deleting the app entity cleans up its owned storage, grants and journal,
+ /// including the same cleanup used when its owner is deleted.
#[tracing::instrument(err, skip(self))]
async fn delete_database(&self, id: DatabaseId) -> Result<(), Self::Error> {
- let mut transaction = self.pool.begin().await?;
-
- entity_access_db_utils::delete_entity_access_rows(
- &mut transaction,
- id.as_uuid(),
- EntityType::Database,
+ sqlx::query!(
+ r#"DELETE FROM database_entities WHERE database_id = $1"#,
+ id.into_uuid()
)
+ .execute(&self.pool)
.await?;
- journal::purge(&mut *transaction, id).await?;
-
- sqlx::query!(r#"DELETE FROM databases WHERE id = $1"#, id.into_uuid())
- .execute(&mut *transaction)
- .await?;
-
- transaction.commit().await?;
Ok(())
}
@@ -419,6 +411,9 @@ where
actor: &crate::domain::journal::JournalActor,
) -> Result<Option<TableVersion>, Self::Error> {
let mut transaction = self.pool.begin().await?;
+ if !rows::lock_live_database(&mut transaction, table.database_id).await? {
+ return Ok(None);
+ }
// Row writers take this same lock before checking versions and cells.
let current = sqlx::query_scalar!(
"SELECT version FROM database_tables WHERE id = $1 AND database_id = $2 FOR UPDATE",
@@ -438,7 +433,7 @@ where
JOIN database_rows r ON r.id::text = p.entity_id
WHERE r.table_id = $2 AND p.property_definition_id = $3
AND p.entity_type = 'DATABASE_ROW')
- AND EXISTS (SELECT 1 FROM databases WHERE id = $5 AND trashed_at IS NULL)
+ AND EXISTS (SELECT 1 FROM database_entities WHERE database_id = $5 AND trashed_at IS NULL)
RETURNING id"#,
column.id.into_uuid(),
table.id.into_uuid(),
@@ -491,9 +486,9 @@ where
Ok(sqlx::query_as!(
DatabaseRecord,
r#"
- SELECT id, name, owner_id, created_at, trashed_at
- FROM databases
- WHERE id = ANY($1)
+ SELECT database_id AS id, name, user_id AS owner_id, created_at, trashed_at
+ FROM database_entities
+ WHERE database_id = ANY($1)
ORDER BY created_at
"#,
&uuids(ids),
diff --git a/crates/databases/src/outbound/pg_databases_repo/rows.rs b/crates/databases/src/outbound/pg_databases_repo/rows.rs
index 892a854ca2..9ea2e961bb 100644
--- a/crates/databases/src/outbound/pg_databases_repo/rows.rs
+++ b/crates/databases/src/outbound/pg_databases_repo/rows.rs
@@ -11,23 +11,22 @@ use crate::domain::models::{
DatabaseId, PropertyDefinitionId, RowId, RowRef, TableId, TableVersion,
};
-/// Lock a database's row against concurrent table naming, ordering and
-/// deletion; `false` when it is gone or trashed.
+/// Hold the app entity live while a batch writes its core storage.
pub(crate) async fn lock_live_database(
- executor: impl PgExecutor<'_>,
+ connection: &mut PgConnection,
database_id: DatabaseId,
) -> Result<bool, sqlx::Error> {
let live = sqlx::query_scalar!(
- "SELECT id FROM databases WHERE id = $1 AND trashed_at IS NULL FOR UPDATE",
+ "SELECT database_id FROM database_entities WHERE database_id = $1 AND trashed_at IS NULL FOR SHARE",
database_id.into_uuid()
)
- .fetch_optional(executor)
+ .fetch_optional(connection)
.await?;
Ok(live.is_some())
}
/// Append `count` empty rows to a live table, in order; `None` when the
-/// table is gone or its database is trashed.
+/// table is gone. The app wrapper holds the entity lifecycle lock.
pub(crate) async fn append_rows(
connection: &mut PgConnection,
table_id: TableId,
@@ -38,8 +37,7 @@ pub(crate) async fn append_rows(
// row, so positions are minted under the same lock.
let live = sqlx::query_scalar!(
r#"SELECT t.id FROM database_tables t
- JOIN databases d ON d.id = t.database_id
- WHERE t.id = $1 AND d.trashed_at IS NULL FOR UPDATE OF t"#,
+ WHERE t.id = $1 FOR UPDATE OF t"#,
table_id.into_uuid(),
)
.fetch_optional(&mut *connection)
@@ -75,7 +73,7 @@ pub(crate) async fn append_rows(
pub(crate) enum Restored {
/// They are back.
Applied(Vec<RowRef>),
- /// The table is gone, or its database trashed.
+ /// The table is gone.
TableGone,
/// A row has one of their ids again.
Taken(RowId),
@@ -90,8 +88,7 @@ pub(crate) async fn restore_rows(
) -> Result<Restored, PgDatabasesRepoError> {
let live = sqlx::query_scalar!(
r#"SELECT t.id FROM database_tables t
- JOIN databases d ON d.id = t.database_id
- WHERE t.id = $1 AND d.trashed_at IS NULL FOR UPDATE OF t"#,
+ WHERE t.id = $1 FOR UPDATE OF t"#,
table_id.into_uuid(),
)
.fetch_optional(&mut *connection)
diff --git a/crates/databases/src/outbound/pg_databases_repo/schema.rs b/crates/databases/src/outbound/pg_databases_repo/schema.rs
index be6b802f23..6dfc810560 100644
--- a/crates/databases/src/outbound/pg_databases_repo/schema.rs
+++ b/crates/databases/src/outbound/pg_databases_repo/schema.rs
@@ -34,17 +34,23 @@ pub(crate) enum Inserted {
/// Lock the database row: for update when the batch adds, renames, removes
/// or reorders tables, else shared, so its tables cannot change under the
-/// batch. `false` when it is gone or trashed.
+/// batch. `false` when the core resource is gone.
pub(crate) async fn lock_database(
connection: &mut PgConnection,
database_id: DatabaseId,
exclusive: bool,
) -> Result<bool, sqlx::Error> {
if exclusive {
- return rows::lock_live_database(connection, database_id).await;
+ return Ok(sqlx::query_scalar!(
+ "SELECT id FROM databases WHERE id = $1 FOR UPDATE",
+ database_id.into_uuid()
+ )
+ .fetch_optional(connection)
+ .await?
+ .is_some());
}
let live = sqlx::query_scalar!(
- "SELECT id FROM databases WHERE id = $1 AND trashed_at IS NULL FOR SHARE",
+ "SELECT id FROM databases WHERE id = $1 FOR SHARE",
database_id.into_uuid()
)
.fetch_optional(connection)
@@ -60,8 +66,7 @@ pub(crate) async fn lock_table_versions(
) -> Result<HashMap<TableId, TableVersion>, sqlx::Error> {
let rows = sqlx::query!(
r#"SELECT t.id, t.version FROM database_tables t
- JOIN databases d ON d.id = t.database_id
- WHERE t.id = ANY($1) AND d.trashed_at IS NULL
+ WHERE t.id = ANY($1)
ORDER BY t.id
FOR UPDATE OF t"#,
&uuids(table_ids),
diff --git a/crates/databases/src/outbound/pg_databases_repo/sharing.rs b/crates/databases/src/outbound/pg_databases_repo/sharing.rs
index 1f6161738b..34688db03a 100644
--- a/crates/databases/src/outbound/pg_databases_repo/sharing.rs
+++ b/crates/databases/src/outbound/pg_databases_repo/sharing.rs
@@ -29,7 +29,7 @@ impl<Properties: Send + Sync + 'static> DatabaseSharingRepo for PgDatabasesRepo<
) -> Result<bool, Self::Error> {
let mut transaction = self.pool.begin().await?;
let live = sqlx::query_scalar!(
- "SELECT id FROM databases WHERE id = $1 AND trashed_at IS NULL FOR SHARE",
+ "SELECT database_id FROM database_entities WHERE database_id = $1 AND trashed_at IS NULL FOR SHARE",
database_id.into_uuid(),
)
.fetch_optional(&mut *transaction)
diff --git a/crates/databases/src/outbound/pg_databases_repo/test.rs b/crates/databases/src/outbound/pg_databases_repo/test.rs
index 8ff2d0668f..cbe40eb18a 100644
--- a/crates/databases/src/outbound/pg_databases_repo/test.rs
+++ b/crates/databases/src/outbound/pg_databases_repo/test.rs
@@ -16,6 +16,7 @@ mod apply_ops;
mod cell_triggers;
#[cfg(feature = "gateway")]
mod changes;
+mod entity_split;
#[cfg(feature = "gateway")]
mod journal;
#[cfg(feature = "gateway")]
@@ -46,6 +47,18 @@ impl<Properties> PgDatabasesRepo<Properties> {
count: usize,
) -> Result<Option<Vec<RowRef>>, PgDatabasesRepoError> {
let mut transaction = self.pool.begin().await?;
+ let database = sqlx::query_scalar!(
+ "SELECT database_id FROM database_tables WHERE id = $1",
+ table_id.into_uuid()
+ )
+ .fetch_optional(&mut *transaction)
+ .await?;
+ let Some(database) = database else {
+ return Ok(None);
+ };
+ if !rows::lock_live_database(&mut transaction, DatabaseId::from_uuid(database)).await? {
+ return Ok(None);
+ }
let rows = rows::append_rows(&mut transaction, table_id, created_by, count).await?;
if rows.is_some() {
transaction.commit().await?;
diff --git a/crates/databases/src/outbound/pg_databases_repo/test/entity_split.rs b/crates/databases/src/outbound/pg_databases_repo/test/entity_split.rs
new file mode 100644
index 0000000000..667014f86b
--- /dev/null
+++ b/crates/databases/src/outbound/pg_databases_repo/test/entity_split.rs
@@ -0,0 +1,253 @@
+//! Storage without app metadata, app cleanup, and the destructive schema cutover.
+
+use super::*;
+use crate::domain::journal::JournalPlan;
+use crate::domain::models::NewDefinition;
+use crate::domain::ports::DatabaseStorage;
+use models_properties::service::property_value::PropertyValue;
+
+const SPLIT_UP: &str = include_str!(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/../macro_db_client/migrations/20261005183743_separate_database_entity.up.sql"
+));
+const SPLIT_DOWN: &str = include_str!(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/../macro_db_client/migrations/20261005183743_separate_database_entity.down.sql"
+));
+
+fn batch(database_id: DatabaseId, writes: Vec<Write>) -> Writes {
+ Writes {
+ database_id,
+ created_by: user(),
+ writes,
+ related_rows: Vec::new(),
+ expected_versions: Vec::new(),
+ journal: JournalPlan::default(),
+ }
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn core_storage_supports_cells_and_versions_without_an_app_entity(pool: PgPool) {
+ let properties = PropertiesPgRepo::new(pool.clone());
+ let store = PgCellStore::new(pool.clone(), properties.clone());
+ let repo = PgDatabasesRepo::new(pool.clone(), properties);
+ // Storage allocation itself needs no user, entity or owner grant.
+ let database = store.create_storage().await.unwrap();
+ assert!(repo.get_database(database).await.unwrap().is_none());
+ let app_rows = sqlx::query_scalar!(
+ r#"SELECT (SELECT COUNT(*) FROM database_entities)
+ + (SELECT COUNT(*) FROM entity_access) AS "count!""#
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(app_rows, 0);
+
+ insert_user(&pool).await;
+ let table = TableId::new();
+ let definition = macro_uuid::generate_uuid_v7();
+ let Write::CreateColumn { column, .. } = bind(table, definition, "80") else {
+ unreachable!()
+ };
+ let writes = batch(
+ database,
+ vec![
+ Write::CreateTable {
+ table_id: table,
+ name: "Contacts".into(),
+ },
+ Write::CreateColumn {
+ column,
+ definition: Some(NewDefinition {
+ id: definition,
+ name: "Name".into(),
+ data_type: DataType::String,
+ is_multi_select: false,
+ specific_entity_type: None,
+ options: Vec::new(),
+ }),
+ },
+ Write::InsertRows {
+ table_id: table,
+ rows: vec![vec![(definition, PropertyValue::Str("Acme".into()))]],
+ restored: Vec::new(),
+ },
+ ],
+ );
+ assert!(matches!(
+ store.apply_writes(&writes).await.unwrap(),
+ WritesOutcome::TableNotFound(_)
+ ));
+ let WritesOutcome::Applied {
+ inserted,
+ table_versions,
+ ..
+ } = store.apply_storage_writes(&writes).await.unwrap()
+ else {
+ panic!("a core database should support the same storage batch");
+ };
+ let row = inserted[2][0];
+ assert_eq!(table_versions[&table], TableVersion(1));
+ assert_eq!(
+ store.cells(&[row]).await.unwrap()[&row][&definition],
+ PropertyValue::Str("Acme".into())
+ );
+
+ let mut stale = batch(
+ database,
+ vec![Write::InsertRows {
+ table_id: table,
+ rows: vec![Vec::new()],
+ restored: Vec::new(),
+ }],
+ );
+ stale.expected_versions.push((table, TableVersion(0)));
+ assert_eq!(
+ store.apply_storage_writes(&stale).await.unwrap(),
+ WritesOutcome::VersionConflict(table)
+ );
+ assert_eq!(repo.row_refs(table).await.unwrap().len(), 1);
+
+ // The app lifecycle cannot delete a resource that has no app entity.
+ repo.delete_database(database).await.unwrap();
+ assert_eq!(
+ repo.tables_for_databases(&[database]).await.unwrap().len(),
+ 1
+ );
+ store.delete_storage(database).await.unwrap();
+ assert!(
+ repo.tables_for_databases(&[database])
+ .await
+ .unwrap()
+ .is_empty()
+ );
+ let cells = sqlx::query_scalar!(
+ "SELECT COUNT(*) FROM entity_properties WHERE entity_id = $1 AND entity_type = 'DATABASE_ROW'", row.to_string()
+ ).fetch_one(&pool).await.unwrap();
+ assert_eq!(cells, Some(0));
+ let definitions = sqlx::query_scalar!(
+ "SELECT COUNT(*) FROM property_definitions WHERE id = $1",
+ definition
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(definitions, Some(0));
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn attached_storage_cannot_be_deleted_through_the_core_port(pool: PgPool) {
+ let (repo, table, _) = fixture(&pool).await;
+ let store = PgCellStore::new(pool.clone(), PropertiesPgRepo::new(pool.clone()));
+ assert!(store.delete_storage(table.database_id).await.is_err());
+ assert!(
+ repo.get_database(table.database_id)
+ .await
+ .unwrap()
+ .is_some()
+ );
+ assert_eq!(
+ repo.tables_for_databases(&[table.database_id])
+ .await
+ .unwrap()
+ .len(),
+ 2
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn removing_an_owner_deletes_their_app_storage_but_leaves_unowned_storage(pool: PgPool) {
+ let (repo, table, _) = fixture(&pool).await;
+ let store = PgCellStore::new(pool.clone(), PropertiesPgRepo::new(pool.clone()));
+ let unowned = store.create_storage().await.unwrap();
+ sqlx::query!("DELETE FROM \"User\" WHERE id = $1", USER)
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert!(
+ repo.get_database(table.database_id)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ let resources = sqlx::query_scalar!("SELECT id FROM databases")
+ .fetch_all(&pool)
+ .await
+ .unwrap();
+ assert_eq!(resources, vec![unowned.into_uuid()]);
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn schema_cutover_discards_database_content_and_allows_a_fresh_start(pool: PgPool) {
+ let (repo, _, shared_definition) = fixture(&pool).await;
+ let store = PgCellStore::new(pool.clone(), PropertiesPgRepo::new(pool.clone()));
+ store.create_storage().await.unwrap();
+ let mut transaction = pool.begin().await.unwrap();
+ // Down also resets populated app and core storage, without a preservation guard.
+ sqlx::raw_sql(SPLIT_DOWN)
+ .execute(&mut *transaction)
+ .await
+ .unwrap();
+ sqlx::raw_sql(include_str!("entity_split/before_split.sql"))
+ .execute(&mut *transaction)
+ .await
+ .unwrap();
+ sqlx::raw_sql(SPLIT_UP)
+ .execute(&mut *transaction)
+ .await
+ .unwrap();
+ let remaining = sqlx::query_scalar!(
+ r#"SELECT (SELECT COUNT(*) FROM databases)
+ + (SELECT COUNT(*) FROM database_entities)
+ + (SELECT COUNT(*) FROM database_tables)
+ + (SELECT COUNT(*) FROM database_columns)
+ + (SELECT COUNT(*) FROM database_rows)
+ + (SELECT COUNT(*) FROM database_views)
+ + (SELECT COUNT(*) FROM database_view_positions)
+ + (SELECT COUNT(*) FROM property_definitions WHERE database_id IS NOT NULL)
+ + (SELECT COUNT(*) FROM entity_properties WHERE entity_type = 'DATABASE_ROW')
+ + (SELECT COUNT(*) FROM entity_access WHERE entity_type = 'database')
+ + (SELECT COUNT(*) FROM database_changes)
+ + (SELECT COUNT(*) FROM database_change_rows)
+ + (SELECT COUNT(*) FROM database_change_columns)
+ + (SELECT COUNT(*) FROM database_queries)
+ + (SELECT COUNT(*) FROM database_starter_seeds) AS "count!""#
+ )
+ .fetch_one(&mut *transaction)
+ .await
+ .unwrap();
+ assert_eq!(remaining, 0);
+ let shared = sqlx::query_scalar!(
+ "SELECT id FROM property_definitions WHERE id = $1",
+ shared_definition
+ )
+ .fetch_optional(&mut *transaction)
+ .await
+ .unwrap();
+ assert_eq!(shared, Some(shared_definition));
+ transaction.commit().await.unwrap();
+ // The owner remains and can immediately create and write a fresh app database.
+ let database = repo
+ .create_database(
+ &CreateDatabase {
+ name: "Fresh start".into(),
+ owner_id: user(),
+ acting_bot: None,
+ },
+ FirstTable {
+ name: "Contacts",
+ title_column: "Name",
+ },
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.get_database(database.id)
+ .await
+ .unwrap()
+ .unwrap()
+ .0
+ .name,
+ "Fresh start"
+ );
+}
diff --git a/crates/databases/src/outbound/pg_databases_repo/test/entity_split/before_split.sql b/crates/databases/src/outbound/pg_databases_repo/test/entity_split/before_split.sql
new file mode 100644
index 0000000000..1e0f3f572b
--- /dev/null
+++ b/crates/databases/src/outbound/pg_databases_repo/test/entity_split/before_split.sql
@@ -0,0 +1,41 @@
+-- The destructive down migration must leave the previous schema empty.
+DO $$
+BEGIN
+ IF EXISTS (SELECT 1 FROM databases) THEN
+ RAISE EXCEPTION 'rollback retained database content';
+ END IF;
+END;
+$$;
+
+INSERT INTO databases (id, name, owner_id) VALUES
+('019a0000-0000-7000-8000-000000000001', 'Disposable', 'macro|[email protected]');
+INSERT INTO database_tables (id, database_id, name, position) VALUES
+('019a0000-0000-7000-8000-000000000002', '019a0000-0000-7000-8000-000000000001', 'Contacts', '80');
+INSERT INTO property_definitions (id, database_id, display_name, data_type, is_multi_select) VALUES
+('019a0000-0000-7000-8000-000000000003', '019a0000-0000-7000-8000-000000000001', 'Name', 'STRING', false);
+INSERT INTO database_columns (id, table_id, property_definition_id, position) VALUES
+('019a0000-0000-7000-8000-000000000004', '019a0000-0000-7000-8000-000000000002', '019a0000-0000-7000-8000-000000000003', '80');
+INSERT INTO database_rows (id, table_id, position) VALUES
+('019a0000-0000-7000-8000-000000000005', '019a0000-0000-7000-8000-000000000002', '80');
+INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values) VALUES
+('019a0000-0000-7000-8000-000000000006', '019a0000-0000-7000-8000-000000000005', 'DATABASE_ROW', '019a0000-0000-7000-8000-000000000003', '{"type":"String","value":"Disposable"}');
+INSERT INTO database_views (id, database_id, table_id, name, position, query, layout) VALUES
+('019a0000-0000-7000-8000-000000000007', '019a0000-0000-7000-8000-000000000001', '019a0000-0000-7000-8000-000000000002', 'Contacts', '80', '{}', '{}');
+INSERT INTO database_view_positions (view_id, row_id, lane, position) VALUES
+('019a0000-0000-7000-8000-000000000007', '019a0000-0000-7000-8000-000000000005', '', '80');
+INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level) VALUES
+('019a0000-0000-7000-8000-000000000001', 'database', 'macro|[email protected]', 'user', 'owner');
+WITH change AS (
+ INSERT INTO database_changes (database_id, table_id, version, ops, inverse) VALUES
+ ('019a0000-0000-7000-8000-000000000001', '019a0000-0000-7000-8000-000000000002', 1, '[]', '{}')
+ RETURNING id
+), changed_row AS (
+ INSERT INTO database_change_rows (change_id, row_id, kind, columns)
+ SELECT id, '019a0000-0000-7000-8000-000000000005', 'insert', '{}' FROM change
+)
+INSERT INTO database_change_columns (change_id, column_id, kind)
+SELECT id, '019a0000-0000-7000-8000-000000000004', 'create' FROM change;
+INSERT INTO database_queries (id, database_id, definition) VALUES
+('019a0000-0000-7000-8000-000000000008', '019a0000-0000-7000-8000-000000000001', '{"version":1,"query":"SELECT * FROM Contacts"}');
+INSERT INTO database_starter_seeds (user_id, database_id) VALUES
+('macro|[email protected]', '019a0000-0000-7000-8000-000000000001');
diff --git a/crates/databases/src/outbound/pg_databases_repo/test/tables.rs b/crates/databases/src/outbound/pg_databases_repo/test/tables.rs
index db4ce0368d..efb2d1017e 100644
--- a/crates/databases/src/outbound/pg_databases_repo/test/tables.rs
+++ b/crates/databases/src/outbound/pg_databases_repo/test/tables.rs
@@ -17,7 +17,7 @@ async fn table_ops_wait_for_a_concurrent_trash_and_then_find_the_database_gone(p
let before = version(&pool, guests.table_id).await;
let mut trash = pool.begin().await.unwrap();
sqlx::query!(
- "UPDATE databases SET trashed_at = now() WHERE id = $1",
+ "UPDATE database_entities SET trashed_at = now() WHERE database_id = $1",
guests.database_id.into_uuid(),
)
.execute(&mut *trash)
diff --git a/crates/databases/src/outbound/pg_definition_store/test.rs b/crates/databases/src/outbound/pg_definition_store/test.rs
index ecef193da4..aca16a2057 100644
--- a/crates/databases/src/outbound/pg_definition_store/test.rs
+++ b/crates/databases/src/outbound/pg_definition_store/test.rs
@@ -42,18 +42,14 @@ async fn insert_user(pool: &PgPool, id: &str) {
.expect("user should insert");
}
-/// A database owned by [`USER`], which every database-scoped definition needs.
+/// A core database resource for database-scoped definitions.
async fn insert_database(pool: &PgPool) -> DatabaseId {
insert_user(pool, USER).await;
let id = macro_uuid::generate_uuid_v7();
- sqlx::query!(
- "INSERT INTO databases (id, name, owner_id) VALUES ($1, 'Guests', $2)",
- id,
- USER,
- )
- .execute(pool)
- .await
- .expect("database should insert");
+ sqlx::query!("INSERT INTO databases (id) VALUES ($1)", id,)
+ .execute(pool)
+ .await
+ .expect("database should insert");
DatabaseId::from_uuid(id)
}
diff --git a/crates/databases/src/outbound/pg_starter.rs b/crates/databases/src/outbound/pg_starter.rs
index 1eeba5f90b..e414fb30e8 100644
--- a/crates/databases/src/outbound/pg_starter.rs
+++ b/crates/databases/src/outbound/pg_starter.rs
@@ -86,7 +86,7 @@ where
.is_some();
if !claimed {
let database_id = sqlx::query_scalar!(
- r#"SELECT d.id FROM database_starter_seeds s JOIN databases d ON d.id = s.database_id
+ r#"SELECT d.database_id FROM database_starter_seeds s JOIN database_entities d ON d.database_id = s.database_id
WHERE s.user_id = $1 AND d.trashed_at IS NULL"#,
user_id,
)
@@ -101,7 +101,7 @@ where
}
// Existing and trashed databases both mean the user already started.
if sqlx::query_scalar!(
- "SELECT EXISTS(SELECT 1 FROM databases WHERE owner_id = $1) AS \"exists!\"",
+ "SELECT EXISTS(SELECT 1 FROM database_entities WHERE user_id = $1) AS \"exists!\"",
user_id
)
.fetch_one(&mut *transaction)
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/database_access.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/database_access.rs
index 9a57d25b38..5dbd9ac2ea 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries/database_access.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/database_access.rs
@@ -25,6 +25,7 @@ pub async fn get_database_access(
r#"
SELECT access_level::text
FROM entity_access
+ JOIN database_entities d ON d.database_id = entity_access.entity_id
WHERE entity_id = $1
AND entity_type = 'database'
AND source_id = ANY($2)
@@ -69,6 +70,7 @@ pub async fn list_database_access(
r#"
SELECT entity_id AS "entity_id!", access_level AS "access_level!: AccessLevel"
FROM entity_access
+ JOIN database_entities d ON d.database_id = entity_access.entity_id
WHERE entity_type = 'database'
AND source_id = ANY($1)
"#,
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/database_access/test.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/database_access/test.rs
index 61d21e3f99..f0e4d48e9a 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries/database_access/test.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/database_access/test.rs
@@ -7,10 +7,62 @@ const OWNER: &str = "macro|[email protected]";
const MEMBER: &str = "macro|[email protected]";
const MEMBER_TEAM: &str = "00000000-0000-0000-0000-0000000ea001";
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../../../fixtures", scripts("user_team"))
+)]
+async fn core_storage_does_not_become_an_entity_through_a_stray_grant(pool: PgPool) {
+ use super::super::database_row_access::{
+ get_database_row_access, get_database_row_database, get_database_rows_access,
+ };
+ let database = Uuid::now_v7();
+ let table = Uuid::now_v7();
+ let row = Uuid::now_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, 'Contacts', '80')", table, database)
+ .execute(&pool).await.unwrap();
+ sqlx::query!(
+ "INSERT INTO database_rows (id, table_id, position) VALUES ($1, $2, '80')",
+ row,
+ table
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ sqlx::query!("INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level) VALUES ($1, 'database', $2, 'user', 'owner')", database, OWNER)
+ .execute(&pool).await.unwrap();
+ let owner = source_ids(&pool, "[email protected]").await;
+ assert_eq!(
+ get_database_access(&pool, &database, &owner).await.unwrap(),
+ None
+ );
+ assert!(
+ list_database_access(&pool, &owner)
+ .await
+ .unwrap()
+ .is_empty()
+ );
+ assert_eq!(
+ get_database_row_access(&pool, &row, &owner).await.unwrap(),
+ None
+ );
+ assert!(
+ get_database_rows_access(&pool, &[row], &owner)
+ .await
+ .unwrap()
+ .is_empty()
+ );
+ assert_eq!(get_database_row_database(&pool, &row).await.unwrap(), None);
+}
+
async fn insert_database(pool: &PgPool) -> Uuid {
let database_id = Uuid::now_v7();
sqlx::query!(
- r#"INSERT INTO databases (id, name, owner_id) VALUES ($1, 'db', $2)"#,
+ r#"WITH storage AS (INSERT INTO databases (id) VALUES ($1) RETURNING id)
+ INSERT INTO database_entities (database_id, name, user_id) SELECT id, 'db', $2 FROM storage"#,
database_id,
OWNER,
)
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/database_row_access.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/database_row_access.rs
index 6f7232bc36..78beafd044 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries/database_row_access.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/database_row_access.rs
@@ -31,8 +31,8 @@ pub async fn get_database_row_access(
SELECT ea.access_level::text
FROM database_rows r
JOIN database_tables t ON t.id = r.table_id
- JOIN databases d ON d.id = t.database_id
- JOIN entity_access ea ON ea.entity_id = d.id
+ JOIN database_entities d ON d.database_id = t.database_id
+ JOIN entity_access ea ON ea.entity_id = d.database_id
WHERE r.id = $1
AND ea.entity_type = 'database'
AND ea.source_id = ANY($2)
@@ -62,8 +62,8 @@ pub async fn get_database_rows_access(
SELECT r.id, ea.access_level AS "access_level!: AccessLevel"
FROM database_rows r
JOIN database_tables t ON t.id = r.table_id
- JOIN databases d ON d.id = t.database_id
- JOIN entity_access ea ON ea.entity_id = d.id
+ JOIN database_entities d ON d.database_id = t.database_id
+ JOIN entity_access ea ON ea.entity_id = d.database_id
WHERE r.id = ANY($1)
AND ea.entity_type = 'database'
AND ea.source_id = ANY($2)
@@ -93,6 +93,7 @@ pub async fn get_database_row_database(
SELECT t.database_id
FROM database_rows r
JOIN database_tables t ON t.id = r.table_id
+ JOIN database_entities d ON d.database_id = t.database_id
WHERE r.id = $1
"#,
row_id,
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/database_row_access/test.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/database_row_access/test.rs
index e938b63932..b40d3e54b3 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries/database_row_access/test.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/database_row_access/test.rs
@@ -14,7 +14,8 @@ async fn insert_row(pool: &PgPool) -> (Uuid, Uuid) {
let table_id = Uuid::now_v7();
let row_id = Uuid::now_v7();
sqlx::query!(
- r#"INSERT INTO databases (id, name, owner_id) VALUES ($1, 'db', $2)"#,
+ r#"WITH storage AS (INSERT INTO databases (id) VALUES ($1) RETURNING id)
+ INSERT INTO database_entities (database_id, name, user_id) SELECT id, 'db', $2 FROM storage"#,
database_id,
OWNER,
)
diff --git a/crates/entity_access/src/outbound/pg_explain_access_repo/test.rs b/crates/entity_access/src/outbound/pg_explain_access_repo/test.rs
index 2720326a22..8071494f62 100644
--- a/crates/entity_access/src/outbound/pg_explain_access_repo/test.rs
+++ b/crates/entity_access/src/outbound/pg_explain_access_repo/test.rs
@@ -110,7 +110,8 @@ async fn database_row_explanation_is_its_database_grants(pool: PgPool) {
let member = MacroUserIdStr::try_from_email("[email protected]").unwrap();
sqlx::query!(
- r#"INSERT INTO databases (id, name, owner_id) VALUES ($1, 'db', 'macro|[email protected]')"#,
+ r#"WITH storage AS (INSERT INTO databases (id) VALUES ($1) RETURNING id)
+ INSERT INTO database_entities (database_id, name, user_id) SELECT id, 'db', 'macro|[email protected]' FROM storage"#,
database_id,
)
.execute(&pool)
diff --git a/crates/macro_db_client/migrations/20261005183743_separate_database_entity.down.sql b/crates/macro_db_client/migrations/20261005183743_separate_database_entity.down.sql
new file mode 100644
index 0000000000..0fa6033bce
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261005183743_separate_database_entity.down.sql
@@ -0,0 +1,23 @@
+-- Rollback also discards database content; no data is migrated back.
+DELETE FROM database_entities;
+DELETE FROM databases;
+DELETE FROM database_changes;
+DELETE FROM database_queries;
+DELETE FROM database_starter_seeds;
+DELETE FROM entity_access WHERE entity_type = 'database';
+
+ALTER TABLE databases
+ ADD COLUMN name TEXT NOT NULL,
+ ADD COLUMN owner_id TEXT NOT NULL REFERENCES "User"(id) ON DELETE CASCADE ON UPDATE CASCADE,
+ ADD COLUMN created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ ADD COLUMN updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ ADD COLUMN trashed_at TIMESTAMPTZ;
+CREATE INDEX idx_databases_owner ON databases(owner_id);
+
+ALTER TABLE database_starter_seeds DROP CONSTRAINT database_starter_seeds_database_id_fkey;
+ALTER TABLE database_starter_seeds ADD CONSTRAINT database_starter_seeds_database_id_fkey
+ FOREIGN KEY (database_id) REFERENCES databases(id) ON DELETE SET NULL;
+
+DROP TABLE database_entities;
+DROP FUNCTION delete_database_entity_storage();
+COMMENT ON TABLE databases IS NULL;
diff --git a/crates/macro_db_client/migrations/20261005183743_separate_database_entity.up.sql b/crates/macro_db_client/migrations/20261005183743_separate_database_entity.up.sql
new file mode 100644
index 0000000000..bbee67f5ec
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261005183743_separate_database_entity.up.sql
@@ -0,0 +1,50 @@
+-- Destructive cutover: existing databases are disposable. Deploy this schema
+-- with the updated service; old binaries are not supported.
+DELETE FROM entity_access WHERE entity_type = 'database';
+DELETE FROM database_changes;
+DELETE FROM database_queries;
+DELETE FROM database_starter_seeds;
+-- Existing cascades remove tables, definitions, rows and their cells.
+DELETE FROM databases;
+
+-- Database storage is reusable without a Macro entity.
+ALTER TABLE databases
+ DROP COLUMN name,
+ DROP COLUMN owner_id,
+ DROP COLUMN created_at,
+ DROP COLUMN updated_at,
+ DROP COLUMN trashed_at;
+
+CREATE TABLE database_entities (
+ database_id UUID PRIMARY KEY REFERENCES databases(id) ON DELETE RESTRICT,
+ name TEXT NOT NULL,
+ user_id TEXT NOT NULL REFERENCES "User"(id) ON DELETE CASCADE ON UPDATE CASCADE,
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ trashed_at TIMESTAMPTZ
+);
+
+CREATE INDEX database_entities_user_id_idx ON database_entities(user_id);
+
+-- Starter provisioning is an app concern, so it points at the entity.
+ALTER TABLE database_starter_seeds DROP CONSTRAINT database_starter_seeds_database_id_fkey;
+ALTER TABLE database_starter_seeds ADD CONSTRAINT database_starter_seeds_database_id_fkey
+ FOREIGN KEY (database_id) REFERENCES database_entities(database_id) ON DELETE SET NULL;
+
+-- An app entity owns its resource, including when its owner is deleted.
+CREATE FUNCTION delete_database_entity_storage()
+RETURNS TRIGGER LANGUAGE plpgsql AS $$
+BEGIN
+ DELETE FROM databases WHERE id = OLD.database_id;
+ DELETE FROM database_changes WHERE database_id = OLD.database_id;
+ DELETE FROM entity_access WHERE entity_type = 'database' AND entity_id = OLD.database_id;
+ RETURN NULL;
+END;
+$$;
+
+CREATE TRIGGER database_entities_storage_cleanup
+ AFTER DELETE ON database_entities
+ FOR EACH ROW EXECUTE FUNCTION delete_database_entity_storage();
+
+COMMENT ON TABLE databases IS 'Reusable database storage identity; no app ownership, display metadata, or trash state.';
+COMMENT ON TABLE database_entities IS 'Optional Macro entity for a database resource; its primary key is also the resource foreign key.';
diff --git a/crates/models_databases/src/ids.rs b/crates/models_databases/src/ids.rs
index 4a80e69aa2..8a06b964a6 100644
--- a/crates/models_databases/src/ids.rs
+++ b/crates/models_databases/src/ids.rs
@@ -108,7 +108,8 @@ macro_rules! database_id {
}
database_id!(
- /// Identifier of a database (the shareable entity users see).
+ /// Identifier of reusable database storage. Its optional Macro entity uses
+ /// the same identity, so existing routes and grants keep their IDs.
DatabaseId
);
database_id!(
diff --git a/crates/properties/src/outbound/definitions_test.rs b/crates/properties/src/outbound/definitions_test.rs
index dbb9ed5db0..86316f4a80 100644
--- a/crates/properties/src/outbound/definitions_test.rs
+++ b/crates/properties/src/outbound/definitions_test.rs
@@ -548,12 +548,9 @@ async fn create_database_property_definition_preserves_type_and_isolates_ownersh
pool: Pool<Postgres>,
) -> anyhow::Result<()> {
let database_id = macro_uuid::generate_uuid_v7();
- sqlx::query!(
- "INSERT INTO databases (id, name, owner_id) VALUES ($1, 'Property definition test', 'macro|[email protected]')",
- database_id
- )
- .execute(&pool)
- .await?;
+ sqlx::query!("INSERT INTO databases (id) VALUES ($1)", database_id)
+ .execute(&pool)
+ .await?;
let repo = PropertiesPgRepo::new(pool);
for (data_type, specific_entity_type) in [
(DataType::String, None),
@@ -616,12 +613,9 @@ async fn a_database_binds_system_own_team_and_its_own_definitions_only(
let database_id = Uuid::now_v7();
let other_database_id = Uuid::now_v7();
for id in [database_id, other_database_id] {
- sqlx::query!(
- "INSERT INTO databases (id, name, owner_id) VALUES ($1, 'Guests', 'macro|[email protected]')",
- id,
- )
- .execute(&pool)
- .await?;
+ sqlx::query!("INSERT INTO databases (id) VALUES ($1)", id,)
+ .execute(&pool)
+ .await?;
}
let repo = PropertiesPgRepo::new(pool);
let own_column = repo
@@ -667,12 +661,9 @@ async fn definitions_by_id_include_database_owned_ones_with_their_options(
pool: Pool<Postgres>,
) -> anyhow::Result<()> {
let database_id = Uuid::now_v7();
- sqlx::query!(
- "INSERT INTO databases (id, name, owner_id) VALUES ($1, 'Guests', 'macro|[email protected]')",
- database_id,
- )
- .execute(&pool)
- .await?;
+ sqlx::query!("INSERT INTO databases (id) VALUES ($1)", database_id,)
+ .execute(&pool)
+ .await?;
let repo = PropertiesPgRepo::new(pool);
let column = repo
.create_database_property_definition(
diff --git a/crates/properties/src/outbound/permission_queries.rs b/crates/properties/src/outbound/permission_queries.rs
index 3393d9be9d..371ef011e6 100644
--- a/crates/properties/src/outbound/permission_queries.rs
+++ b/crates/properties/src/outbound/permission_queries.rs
@@ -71,10 +71,10 @@ pub async fn get_owner_and_deleted(
EntityType::DatabaseRow => {
sqlx::query!(
r#"
- SELECT d.owner_id, d.trashed_at
+ SELECT d.user_id AS owner_id, d.trashed_at
FROM database_rows r
JOIN database_tables t ON t.id = r.table_id
- JOIN databases d ON d.id = t.database_id
+ JOIN database_entities d ON d.database_id = t.database_id
WHERE r.id = $1
"#,
Uuid::parse_str(entity_id)?,
diff --git a/crates/properties/src/outbound/permission_queries/test.rs b/crates/properties/src/outbound/permission_queries/test.rs
index 8e0451103b..68f5663659 100644
--- a/crates/properties/src/outbound/permission_queries/test.rs
+++ b/crates/properties/src/outbound/permission_queries/test.rs
@@ -13,7 +13,8 @@ async fn insert_row(pool: &Pool<Postgres>) -> anyhow::Result<(Uuid, Uuid)> {
let table_id = Uuid::now_v7();
let row_id = Uuid::now_v7();
sqlx::query!(
- r#"INSERT INTO databases (id, name, owner_id) VALUES ($1, 'db', $2)"#,
+ r#"WITH storage AS (INSERT INTO databases (id) VALUES ($1) RETURNING id)
+ INSERT INTO database_entities (database_id, name, user_id) SELECT id, 'db', $2 FROM storage"#,
database_id,
OWNER,
)
@@ -59,7 +60,7 @@ async fn database_row_owner_and_trash_come_from_its_database(
assert!(!deleted);
sqlx::query!(
- "UPDATE databases SET trashed_at = now() WHERE id = $1",
+ "UPDATE database_entities SET trashed_at = now() WHERE database_id = $1",
database_id
)
.execute(&pool)
diff --git a/crates/soup/src/outbound/pg_soup_repo/database_row/by_ids.sql b/crates/soup/src/outbound/pg_soup_repo/database_row/by_ids.sql
index db8e169479..cef6fd66ee 100644
--- a/crates/soup/src/outbound/pg_soup_repo/database_row/by_ids.sql
+++ b/crates/soup/src/outbound/pg_soup_repo/database_row/by_ids.sql
@@ -5,13 +5,13 @@ WITH user_source_ids AS (
UNION ALL SELECT $1
)
SELECT r.id, r.table_id, row_table.database_id, r.position, r.created_by,
- row_database.owner_id, r.created_at, r.updated_at
+ row_database.user_id AS owner_id, r.created_at, r.updated_at
FROM database_rows r
JOIN database_tables row_table ON row_table.id = r.table_id
-JOIN databases row_database ON row_database.id = row_table.database_id
+JOIN database_entities row_database ON row_database.database_id = row_table.database_id
WHERE r.id = ANY($2)
AND row_database.trashed_at IS NULL
AND EXISTS (SELECT 1 FROM entity_access ea
- WHERE ea.entity_id = row_database.id AND ea.entity_type = 'database'
+ WHERE ea.entity_id = row_database.database_id AND ea.entity_type = 'database'
AND ea.source_id IN (SELECT source_id FROM user_source_ids))
ORDER BY r.created_at DESC, r.id DESC
diff --git a/crates/soup/src/outbound/pg_soup_repo/database_row/test.rs b/crates/soup/src/outbound/pg_soup_repo/database_row/test.rs
index 8d2d332bef..020286b46c 100644
--- a/crates/soup/src/outbound/pg_soup_repo/database_row/test.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/database_row/test.rs
@@ -284,3 +284,49 @@ async fn a_row_carries_every_cell_as_a_property(pool: PgPool) {
vec![(STAGE, Some(PropertyValue::SelectOption(vec![WON])))]
);
}
+
+#[sqlx::test(fixtures("test/database_rows.sql"), migrator = "MACRO_DB_MIGRATIONS")]
+async fn core_rows_stay_out_of_soup_even_with_a_stray_database_grant(pool: PgPool) {
+ let database = Uuid::now_v7();
+ let table = Uuid::now_v7();
+ let row = Uuid::now_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, 'Contacts', '80')", table, database)
+ .execute(&pool).await.unwrap();
+ sqlx::query!(
+ "INSERT INTO database_rows (id, table_id, position) VALUES ($1, $2, '80')",
+ row,
+ table
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let user = viewer();
+ sqlx::query!("INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level) VALUES ($1, 'database', $2, 'user', 'owner')", database, user.as_ref())
+ .execute(&pool).await.unwrap();
+ assert!(
+ page(
+ &pool,
+ viewer(),
+ rows_of(Some(Expr::val(DatabaseRowLiteral::TableId(table))))
+ )
+ .await
+ .is_empty()
+ );
+ let entities = [EntityType::DatabaseRow.with_entity_string(row.to_string())];
+ assert!(
+ by_ids(
+ &pool,
+ AdvancedSortParams {
+ user_id: viewer(),
+ entities: &entities
+ }
+ )
+ .await
+ .unwrap()
+ .is_empty()
+ );
+}
diff --git a/crates/soup/src/outbound/pg_soup_repo/database_row/test/database_rows.sql b/crates/soup/src/outbound/pg_soup_repo/database_row/test/database_rows.sql
index ac7331d573..a8d41362a0 100644
--- a/crates/soup/src/outbound/pg_soup_repo/database_row/test/database_rows.sql
+++ b/crates/soup/src/outbound/pg_soup_repo/database_row/test/database_rows.sql
@@ -9,7 +9,10 @@ INSERT INTO "User" (id, email, macro_user_id) VALUES
('macro|[email protected]', '[email protected]', 'd0000000-0000-0000-0000-000000000002'),
('macro|[email protected]', '[email protected]', 'd0000000-0000-0000-0000-000000000003');
-INSERT INTO databases (id, name, owner_id) VALUES
+INSERT INTO databases (id) VALUES
+('db000000-0000-0000-0000-000000000001'),
+('db000000-0000-0000-0000-000000000002');
+INSERT INTO database_entities (database_id, name, user_id) VALUES
('db000000-0000-0000-0000-000000000001', 'CRM', 'macro|[email protected]'),
('db000000-0000-0000-0000-000000000002', 'Private', 'macro|[email protected]');
INSERT INTO database_tables (id, database_id, name, position) VALUES
diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row.rs
index 516c3b1d66..1e0ee324bf 100644
--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row.rs
@@ -44,8 +44,8 @@ pub(super) fn database_row_top_clause(sort: SimpleSortMethod, grouped: bool) ->
{sort_ts}::timestamptz as sort_ts {group_columns}
FROM database_rows r
JOIN database_tables row_table ON row_table.id = r.table_id
- JOIN databases row_database ON row_database.id = row_table.database_id
+ JOIN database_entities row_database ON row_database.database_id = row_table.database_id
WHERE row_database.trashed_at IS NULL AND {}"#,
- access_semi_join("row_database.id::text", "database")
+ access_semi_join("row_database.database_id::text", "database")
)
}
diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row_detail.sql b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row_detail.sql
index 58b663ac8b..3be6194d32 100644
--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row_detail.sql
+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row_detail.sql
@@ -3,7 +3,7 @@
'database_row' as "item_type",
r.id::text as "id",
NULL as "document_version_id",
- row_database.owner_id as "user_id",
+ row_database.user_id as "user_id",
NULL::text as "name",
NULL as "branched_from_id",
NULL as "branched_from_version_id",
@@ -32,5 +32,5 @@
FROM TopItems t
INNER JOIN database_rows r ON r.id::text = t.id
INNER JOIN database_tables row_table ON row_table.id = r.table_id
- INNER JOIN databases row_database ON row_database.id = row_table.database_id
+ INNER JOIN database_entities row_database ON row_database.database_id = row_table.database_id
WHERE t.item_type = 'database_row'
diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row_grouped_detail.sql b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row_grouped_detail.sql
index 57794b9ff4..3b3925fa51 100644
--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row_grouped_detail.sql
+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/database_row_grouped_detail.sql
@@ -3,7 +3,7 @@
'database_row' as "item_type",
r.id::text as "id",
NULL::text as "document_version_id",
- row_database.owner_id as "user_id",
+ row_database.user_id as "user_id",
NULL::text as "name",
NULL::text as "branched_from_id",
NULL::bigint as "branched_from_version_id",
@@ -33,5 +33,5 @@
FROM GroupedItems gi
INNER JOIN database_rows r ON r.id::text = gi.id
INNER JOIN database_tables row_table ON row_table.id = r.table_id
- INNER JOIN databases row_database ON row_database.id = row_table.database_id
+ INNER JOIN database_entities row_database ON row_database.database_id = row_table.database_id
WHERE gi.item_type = 'database_row'
diff --git a/docs/AGENT_GUIDE/databases.md b/docs/AGENT_GUIDE/databases.md
index 915aeb8f9d..76128ad796 100644
--- a/docs/AGENT_GUIDE/databases.md
+++ b/docs/AGENT_GUIDE/databases.md
@@ -58,6 +58,10 @@ PostHog unless the variable was set when it was built. With the flag off:
## Properties and records
+An embedded records editor can allow cell edits while its host controls the
+schema. In that case, column creation and schema actions are absent; record
+editing still works. The standalone database app keeps its full column controls.
+
Use **Add column** immediately after the table’s headers. It creates an **Unnamed**
Text column (**Unnamed 2**, and so on if that name exists), selects its name in the
header, and lets you type immediately. Enter saves; Escape keeps the default name.
diff --git a/docs/DATABASE_DEVELOPMENT.md b/docs/DATABASE_DEVELOPMENT.md
index c9c1729f15..1482120b5c 100644
--- a/docs/DATABASE_DEVELOPMENT.md
+++ b/docs/DATABASE_DEVELOPMENT.md
@@ -37,6 +37,22 @@ Apply any new migrations before testing.
## Safe database schema changes
+### Entity tables and reusable storage
+
+New app entities use a plural `<kind>_entities` table for identity, display name,
+ownership, lifecycle timestamps, and trash state. When an entity uses a reusable
+storage resource, its entity table references that resource with a foreign key;
+storage must be usable without creating the app entity or its access grants.
+
+This convention starts with `database_entities`; it is new for databases and is
+the convention for new entities going forward. It does not require renaming
+existing entity tables. `database_entities.database_id` is both the entity's key
+and a reference to `databases.id`. See the
+[database backend](../crates/databases/README.md) for the storage boundary and
+the explicitly destructive database cutover.
+
+### Compatible migrations
+
**Database migrations deploy before service changes.** Every migration must remain
compatible with the service code currently deployed, not just the code in its PR.