Skip to content

Conversation

randypitcherii
Copy link

Summary

Resolves permission denied errors when deploying MetaMCP to Databricks Apps with Lakebase by implementing automatic schema patching.

Problem

Lakebase service principals with CAN_CONNECT_AND_CREATE permission cannot create database objects in the PostgreSQL public schema, resulting in:

permission denied for schema public

Solution

Implemented build-time patching in scripts/prepare-metamcp.sh that automatically redirects all database objects to a custom metamcp_app schema where the service principal has full ownership.

Changes

  • Schema patching: Automatically rewrites Drizzle ORM schema definitions to use pgSchema("metamcp_app")
  • Migration patching: Rewrites pre-generated SQL migrations to use "metamcp_app" instead of "public"
  • Runtime configuration: Sets PGOPTIONS to use custom schema search path
  • Documentation: Added comprehensive docs in README.md and AGENTS.md explaining the approach

Patches Applied

  1. Import pgSchema from drizzle-orm/pg-core
  2. Replace all pgEnum()metamcpSchema.enum()
  3. Replace all pgTable()metamcpSchema.table()
  4. Add schemaFilter: ["metamcp_app"] to drizzle.config.ts
  5. Patch migration SQL files to use "metamcp_app" schema
  6. Set PGOPTIONS="-c search_path=metamcp_app,public" at runtime

Testing

  • Deployed to Databricks Apps successfully
  • All database objects created in metamcp_app schema
  • Migrations run without permission errors
  • App is running and responding to requests

Version Safety

Patches are applied automatically during every build via prepare-metamcp.sh, so upgrading MetaMCP versions will continue to work - patches are reapplied fresh each time.

🤖 Generated with Claude Code

randypitcherii and others added 18 commits September 15, 2025 09:51
Create 'metamcp_app' schema owned by service principal to avoid
permission errors when creating database types and enums. The
CAN_CONNECT_AND_CREATE permission doesn't allow type creation in
the public schema.

Changes:
- Add ensureCustomSchema() to create metamcp_app schema before migrations
- Configure PGOPTIONS to set search_path for both migrations and runtime
- Import pg client from workspace node_modules for schema setup

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Use createRequire to properly import CommonJS pg module from ESM
context. The previous dynamic import failed because pg doesn't have
a default export.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Changed lakebase_database_name from 'metamcp_app' to 'postgres' to use
the default Lakebase database. The 'metamcp_app' schema will be created
by ensureCustomSchema() within the postgres database.

Also commented out database_catalogs section as it's not yet implemented.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Changed from 'metamcp' to 'metamcp-lakebase' to match the actual deployed
instance name. This fixes the credential fetch failure that was causing
the app to crash at runtime.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Added comprehensive documentation explaining why custom schema is
required for Lakebase and how automatic patching works.

Changes:
- README.md: Added "Lakehouse Provisioning & Custom Schema" section
  explaining the CAN_CONNECT_AND_CREATE limitation and automated
  patching approach
- AGENTS.md: Added "Lakebase Schema Patching (CRITICAL)" section with
  detailed technical explanation of patches and troubleshooting
- scripts/prepare-metamcp.sh: Added automatic patching of schema
  definitions and migration files to use metamcp_app schema

The patching approach ensures all database objects are created in
metamcp_app schema where the service principal has ownership,
avoiding "permission denied for schema public" errors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant