Data: Console Database Schema
Relevant source files
The following files were used as context for generating this wiki page:
The OpenCode Console's database schema is designed to manage users, workspaces, and billing information. It is implemented using Drizzle ORM with a mysql2 driver, targeting a PlanetScale database. The schema is defined across several files, primarily separating concerns for users, workspaces, and billing.
The infrastructure for this database is provisioned using SST (Serverless Stack Toolkit), which handles the creation and configuration of PlanetScale database branches and credentials based on the deployment stage. This ensures a clear separation between production and development environments.
Schema Overview
The database is composed of several interconnected tables that handle authentication, user management, workspace organization, and Stripe-based billing. The core entities are users and workspaces, linked by a many-to-many relationship. Billing information is attached to workspaces.
The following Entity-Relationship Diagram provides a high-level overview of the main tables and their relationships.
Infrastructure and Configuration
The database infrastructure is defined in infra/console.ts using SST and the PlanetScale provider.
Database Provisioning
- A PlanetScale database named
opencodeis retrieved. - For the
productionstage, theproductionbranch is used. For all other stages (e.g.,dev,staging), a new branch is created from theproductionbranch, named after the stage. - A password and credentials are generated for the specific database branch.
- These database properties (host, name, username, password) are encapsulated into a
sst.Linkableresource namedDatabase, making them available to other parts of the application, such as the Console and Auth API.
The following diagram illustrates the database provisioning flow.
Sources: infra/console.ts:10-36
Drizzle Configuration
Drizzle ORM is configured in packages/console/core/drizzle.config.ts. This file specifies the location of the schema definitions and the output directory for generated migrations.
| Option | Value | Description |
|---|---|---|
schema | ./src/schema/*.sql.ts | Glob pattern for finding all schema definition files. |
out | ./migrations | Directory where migration files will be generated. |
driver | mysql2 | The database driver to use. |
dbCredentials | (from process.env) | Database connection string, loaded from environment variables. |
Sources: packages/console/core/drizzle.config.ts:1-12
Core Schemas
User Schema
The user schema defines entities related to user accounts and authentication sessions.
Sources: packages/console/core/src/schema/user.sql.ts
users Table
Stores basic information about a user.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | varchar(21) | Primary Key | Unique identifier for the user. |
email | varchar(255) | Not Null, Unique | The user's email address. |
time_created | timestamp | Not Null, Default now() | Timestamp of when the user was created. |
time_updated | timestamp | Not Null, Default now() | Timestamp of the last update. |
sessions Table
Stores session information for authenticated users, linking back to the users table.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | varchar(255) | Primary Key | Unique session identifier. |
user_id | varchar(21) | Not Null, Foreign Key to users.id | The ID of the user this session belongs to. |
expires_at | datetime | Not Null | The expiration timestamp for the session. |
Workspace Schema
The workspace schema defines workspaces and the relationship between users and workspaces.
Sources: packages/console/core/src/schema/workspace.sql.ts
workspace Table
Represents an isolated environment for a user or a team.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | varchar(21) | Primary Key | Unique identifier for the workspace. |
slug | varchar(255) | Not Null, Unique | A URL-friendly slug for the workspace. |
time_created | timestamp | Not Null, Default now() | Timestamp of when the workspace was created. |
time_updated | timestamp | Not Null, Default now() | Timestamp of the last update. |
workspace_user Table
A join table that establishes a many-to-many relationship between users and workspaces, assigning a role to each user within a workspace.
| Column | Type | Constraints | Description |
|---|---|---|---|
user_id | varchar(21) | Foreign Key to users.id | The ID of the user. |
workspace_id | varchar(21) | Foreign Key to workspace.id | The ID of the workspace. |
role | enum('admin', 'member') | Not Null | The user's role in the workspace. |
The relationship is defined with a composite primary key on (user_id, workspace_id).
Sources: packages/console/core/src/schema/workspace.sql.ts:25-32
Billing Schema
The billing schema integrates with Stripe to manage customer and subscription data. It links Stripe's entities to the workspace entity.
Sources: packages/console/core/src/schema/billing.sql.ts
stripe_customer Table
Maps a workspace to a Stripe Customer ID.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | varchar(255) | Primary Key | The Stripe Customer ID. |
workspace_id | varchar(21) | Not Null, Unique, Foreign Key to workspace.id | The workspace associated with this Stripe customer. |
stripe_product and stripe_price Tables
These tables mirror Stripe's Product and Price objects.
stripe_product: Stores product information.stripe_price: Stores pricing details for a product, including amount, currency, and type (one-time or recurring).
stripe_subscription Table
Represents a customer's subscription to a specific price.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | varchar(255) | Primary Key | The Stripe Subscription ID. |
customer_id | varchar(255) | Not Null, Foreign Key to stripe_customer.id | The ID of the Stripe customer. |
price_id | varchar(255) | Not Null, Foreign Key to stripe_price.id | The ID of the subscribed price. |
status | enum(...) | Not Null | The current status of the subscription (e.g., active, canceled). |
... | ... | Other fields mirroring Stripe's subscription object. |
The status column is an enum with possible values: trialing, active, canceled, incomplete, incomplete_expired, past_due, unpaid, paused.
Sources: packages/console/core/src/schema/billing.sql.ts:10-25
Summary
The OpenCode Console database schema provides a robust foundation for managing users, collaborative workspaces, and subscription-based billing. By leveraging Drizzle ORM and a structured, multi-file approach, the schema is maintainable and scalable. The integration with SST for infrastructure provisioning ensures that the database environment is consistently and reliably deployed across different stages.