Skip to content

Consolidate Database Schema Seeding to Backend Service #691

Closed

Description

@malibio

Overview

Consolidate database schema seeding to use the backend service layer, eliminating hardcoded constants and ensuring spoke table DDL is derived from schema definitions rather than static configuration.

Self-Contained Scope

  • Move core schema definitions to Rust using existing SchemaNode struct
  • Create seed_core_schemas() method for idempotent seeding
  • Remove TYPES_WITH_SPOKE_TABLES hardcoded constant - derive spoke table needs from schema definitions
  • Update database initialization to call the seed method
  • Remove redundant seeding from schema.surql and TypeScript scripts
  • Comprehensive testing of schema initialization

Key Architectural Decisions

Core Node Types (8 total)

  1. task - has schema entry + spoke table (has fields)
  2. text - has schema entry, no spoke table (no fields)
  3. date - has schema entry, no spoke table (no fields)
  4. header - has schema entry, no spoke table (no fields)
  5. code-block - has schema entry, no spoke table (no fields)
  6. quote-block - has schema entry, no spoke table (no fields)
  7. ordered-list - has schema entry, no spoke table (no fields)
  8. schema - NO schema entry (meta-type, handled specially by system)

Schema Nodes Stored in Database (7 total)

  • task, text, date, header, code-block, quote-block, ordered-list
  • The schema type is structural - it defines other types but doesn't have a schema definition for itself (would be circular)

Spoke Table Logic

  • A node type needs a spoke table if its schema has fields.len() > 0
  • Currently hardcoded in TYPES_WITH_SPOKE_TABLES = ["task", "schema"]
  • Should be derived dynamically: query schema node, check if fields exist
  • The schema spoke table is structural (stores schema definitions), not derived from a schema node

Implementation Approach

Phase 1: Core Schema Definitions

  • Create get_core_schemas() function returning Vec<SchemaNode>
  • Use existing SchemaNode struct (no new types needed)
  • 7 schema definitions for the 7 node types that need schema entries

Phase 2: Dynamic Spoke Table Detection

  • Add method to check if a node type needs a spoke table by querying its schema
  • Cache schema info after initialization to avoid repeated queries
  • Handle special case: schema type always has spoke table (structural)
  • Remove TYPES_WITH_SPOKE_TABLES constant entirely

Phase 3: Seed Method with DDL Sync

  • seed_core_schemas() creates schema nodes via SchemaNode.into_node()
  • For schemas with fields (task), call SchemaTableManager.sync_schema_to_database()
  • Idempotent: check if schemas exist before creating

Phase 4: Remove Redundant Sources

  • Remove task spoke table DDL from schema.surql (generated from schema definition)
  • Keep schema spoke table DDL in schema.surql (structural, not schema-derived)
  • Delete init-schemas-via-proxy.ts
  • Update package.json to remove dev:browser:init script

Acceptance Criteria

  • Core schema definitions use existing SchemaNode struct
  • seed_core_schemas() method creates 7 schema nodes
  • seed_core_schemas() syncs spoke table DDL for schemas with fields
  • TYPES_WITH_SPOKE_TABLES constant removed
  • Spoke table needs derived from schema definitions at runtime
  • Task spoke table DDL removed from schema.surql
  • init-schemas-via-proxy.ts deleted
  • Database initializes correctly with all core schemas
  • All tests pass (bun run test:all)
  • Quality checks pass (bun run quality:fix)

Technical Implementation Notes

Files to modify:

  • packages/core/src/db/surreal_store.rs - remove TYPES_WITH_SPOKE_TABLES, add dynamic lookup
  • packages/core/src/db/schema.surql - remove task spoke table definitions
  • packages/desktop-app/package.json - remove dev:browser:init script

Files to delete:

  • packages/desktop-app/scripts/init-schemas-via-proxy.ts

Files to create:

  • packages/core/src/models/core_schemas.rs - get_core_schemas() function

Migration Strategy

The TYPES_WITH_SPOKE_TABLES constant is used in 13 places in surreal_store.rs. Each usage needs to be replaced with a dynamic check:

  1. Option A: Query-based - Query schema node, check fields.len() > 0
  2. Option B: Cached - Build cache at startup after seeding, use for all operations

Option B preferred for performance (avoid repeated queries during CRUD operations).

Dependencies

Definition of Done

  • Single source of truth for core schema definitions
  • No hardcoded type lists - all derived from schema data
  • Database initialization works end-to-end
  • All tests pass
  • Code review completed

Activity

  1. malibio commented on Dec 1, 2025

    @malibio
    CollaboratorAuthor

    Test baseline (post #690 merge): Frontend: 1440 passed, Rust: 477 passed (16 ignored)

  2. self-assigned this
    on Dec 1, 2025
  3. malibio commented on Dec 1, 2025

    @malibio
    CollaboratorAuthor

    ✅ Implementation complete. Expanded scope to include:

    • Dynamic spoke table detection (removed TYPES_WITH_SPOKE_TABLES hardcoding)
    • Atomic schema node creation with DDL sync
    • Fixed SchemaTableManager DDL generation (SCHEMAFULL, FLEXIBLE, proper syntax)
    • All tests passing (Frontend: 1440, Rust: 482)

    Created follow-up issue #703 for relational fields architecture design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions