Repository navigation
refactor: Decouple the database implementation from the Serverpod package - #4748
Merged
marcelomendoncasoares merged 19 commits intoFeb 23, 2026
Merged
marcelomendoncasoares merged 19 commits into
marcelomendoncasoares merged 19 commits into
Conversation
marcelomendoncasoares
marked this pull request as draft
February 19, 2026 13:00
marcelomendoncasoares
force-pushed
the
reorganize-database-code
branch
from
February 19, 2026 13:20
148f0b2 to
a4a8205
Compare
…stead of static attribute
…he database layer
marcelomendoncasoares
force-pushed
the
reorganize-database-code
branch
from
February 19, 2026 15:13
04adf2e to
a45e4ea
Compare
marcelomendoncasoares
marked this pull request as ready for review
February 19, 2026 15:37
nielsenko
requested changes
Feb 20, 2026
nielsenko
left a comment
Collaborator
There was a problem hiding this comment.
Really like the direction, but I have some comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR is part of the #4721 tech spike and serves two purposes:
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
serverpodpackage 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
lockRowsmethod 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:
DatabaseConfigto support new dialects.serverpod_cli.serverpodpackage.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
DatabaseDialectclass. 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
DatabaseConfigThe ideal way to implement this would be to have a base
DatabaseConfigclass and implement subclasses for each dialect. However, this would be a breaking change, since this class is exposed in the public API of theserverpod_sharedpackage and is part of theServerpodConfigclass.Instead, this PR adds only an optional
dialectparameter to theDatabaseConfigconstructor 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_clito be able to generate the correct SQL for the chosen dialect, a newdatabaseDialectkey was added to thegenerator.yamlfile that can accept any entry from theDatabaseDialectenum.Centralization of the SQL generation for migrations
Inside the
serverpod_clipackage, all SQL generation is under asrc/database/dialectsfolder with a centralSqlGeneratorinterface 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
serverpodpackageDespite already having a
src/database/adaptersfolder in theserverpodpackage, a lot of the Postgres-specific logic was scattered across the package. Now, it is centralized in thesrc/database/adapters/postgresfolder and completely abstracted by interfaces declared in thesrc/database/interfacefolder.A new
DatabaseProviderinterface 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/databaseAs a last step, all database interface classes were completely decoupled from the rest of the package by abstracting a few pieces:
DatabaseSessioninstead of aSessionto allow the database layer to be used without a Serverpod instance. This is a replacement for the previousDatabaseAccessorwith very few changes. The generated repository methods were not changed and still depend on theSessionclass, since this would be a major breaking change.SerializationManagerServerwas moved to thesrc/database/interfacefolder and is now a dependency of theDatabaseProviderinterface. Since it only contains database-related getter methods, it makes sense to declare it as parte of the database implementation.ValueEncoderinterface 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
DatabaseConfigand theValueEncoder) classes were covered by new tests. All of the rest is covered by existing tests.Next steps
The only dependency on the
serverpodpackage 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 sharedserverpod_database- together with all files fromserverpod/lib/src/database. This package will depend only on theserverpod_serializationpackage, making the database safe to be used on the CLI and even in the client.Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.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.