Skip to content

refactor: Decouple the database implementation from the Serverpod package - #4748

Merged
marcelomendoncasoares merged 19 commits into
serverpod:mainfrom
marcelomendoncasoares:reorganize-database-code
Feb 23, 2026
Merged

marcelomendoncasoares merged 19 commits into
serverpod:mainfrom
marcelomendoncasoares:reorganize-database-code

Conversation

@marcelomendoncasoares

@marcelomendoncasoares marcelomendoncasoares commented Feb 19, 2026 •

Copy link
Copy Markdown
Collaborator

This PR is part of the #4721 tech spike and serves two purposes:

  1. Allow an easy implementation of new database adapters.
  2. Prepare the database layer for being separated from the Serverpod package.

The second purpose is part of #4564 and the #4604 RFC since we'll need access to the database from the CLI to run migrations and we can't push the serverpod package as a CLI dependency if we want to decouple them.

One core premise of this refactor is that Serverpod will always be Postgres-first. Which means that our interfaces are allowed to contain methods/properties that are not supported by all databases - like the lockRows method for row-level locking or certain runtime parameters. Specific methods/properties for features that Postgres does not support are also possible in the future.

Core changes

The changes in this PR can be grouped into four main categories:

  1. Update to the DatabaseConfig to support new dialects.
  2. Centralization of the SQL generation (DDL) for migrations in the serverpod_cli.
  3. Centralization of the database adapter in the serverpod package.
  4. Decoupling from all server-specific logic inside serverpod/src/database.

The commits follow this order to facilitate the review process, since a lot of the code is being moved around and it's easier to review the changes in small chunks.

Implementing new database adapters

The central piece to facilitate the implementation of new database adapters is the DatabaseDialect class. This is a simple enum that contains all the supported dialects. By adding a new entry to this enum, all the places that need to be updated to support the new adapter will be highlighted. This change closes #4721.

Update to the DatabaseConfig

The ideal way to implement this would be to have a base DatabaseConfig class and implement subclasses for each dialect. However, this would be a breaking change, since this class is exposed in the public API of the serverpod_shared package and is part of the ServerpodConfig class.

Instead, this PR adds only an optional dialect parameter to the DatabaseConfig constructor that is not exposed in the public API and will be set according to the specific database configuration being parsed from the configuration file.

For the serverpod_cli to be able to generate the correct SQL for the chosen dialect, a new databaseDialect key was added to the generator.yaml file that can accept any entry from the DatabaseDialect enum.

Centralization of the SQL generation for migrations

Inside the serverpod_cli package, all SQL generation is under a src/database/dialects folder with a central SqlGenerator interface that must be implemented by each dialect. This interface is also a factory that returns the correct implementation for the chosen dialect.

Centralization of the database adapter in the serverpod package

Despite already having a src/database/adapters folder in the serverpod package, a lot of the Postgres-specific logic was scattered across the package. Now, it is centralized in the src/database/adapters/postgres folder and completely abstracted by interfaces declared in the src/database/interface folder.

A new DatabaseProvider interface was created to abstract the creation of the database elements (pool manager, connection, analyzer, migration runner). This interface is also a factory that returns the correct implementation for the chosen dialect.

Decoupling from all server-specific logic inside serverpod/src/database

As a last step, all database interface classes were completely decoupled from the rest of the package by abstracting a few pieces:

  • The database methods now receive a DatabaseSession instead of a Session to allow the database layer to be used without a Serverpod instance. This is a replacement for the previous DatabaseAccessor with very few changes. The generated repository methods were not changed and still depend on the Session class, since this would be a major breaking change.
  • The SerializationManagerServer was moved to the src/database/interface folder and is now a dependency of the DatabaseProvider interface. Since it only contains database-related getter methods, it makes sense to declare it as parte of the database implementation.
  • A new ValueEncoder interface was created to abstract the encoding of values for the database. This interface is also a singleton that returns the encoder for the current database pool. This is a replacement for the previous access to the Postgres-specific encoder as a static attribute.

Regarding testing

Few tests were needed for this PR since most of the changes are internal. The changes to public interfaces (such as the DatabaseConfig and the ValueEncoder) classes were covered by new tests. All of the rest is covered by existing tests.

Next steps

The only dependency on the serverpod package left on the database layer is due to the database-related models (TableDefinition, ColumnDefinition, DatabaseMigration, etc.). Now that we can have packages with shared models, such models will be moved to a shared serverpod_database - together with all files from serverpod/lib/src/database. This package will depend only on the serverpod_serialization package, making the database safe to be used on the CLI and even in the client.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

If you need help, consider asking for advice on the discussion board.

Breaking changes

None, since all the changes are in the internal implementation of the database layer.

@marcelomendoncasoares
marcelomendoncasoares marked this pull request as draft February 19, 2026 13:00
@marcelomendoncasoares
marcelomendoncasoares marked this pull request as ready for review February 19, 2026 15:37
@marcelomendoncasoares
marcelomendoncasoares requested a review from a team February 19, 2026 15:37

@nielsenko nielsenko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really like the direction, but I have some comments

@nielsenko nielsenko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@marcelomendoncasoares
marcelomendoncasoares merged commit 6b470e4 into serverpod:main Feb 23, 2026
74 checks passed
@marcelomendoncasoares
marcelomendoncasoares deleted the reorganize-database-code branch February 23, 2026 21:34
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tech spike: Evaluate the changes needed to adapt Serverpod to support SQLite and other databases

2 participants