You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Have Serverpod auto-add custom SQL commands on initial migration #1730
Is your feature request related to a problem? Please describe.
When creating an initial migration (initializing a database), I have to manually run additional SQL commands, to create tables and indexes that Serverpod doesn't yet know how to create (e.g. for Geolocation columns, or to create a partial index -- there are issues for each of these).
Describe the solution you'd like
It would be nice if there was a directory that custom .sql files could be put in, which are merged in with Serverpod's own SQL files when a migration is created.
Reacted by Simon Wienhöfer, Dylan Vaughn and Mathis Desmeules
Great suggestion, we have been discussing this in the team as well and are planning to expand the migrations system with the capability of adding custom SQL commands.
Thanks. The custom commands will have to be run after the Serverpod commands, because a user might have to override something due to lack of Serverpod support.
However, the table diff system is going to have to handle these sorts of manual modifications though, to avoid "the table is not like the protocol definition" type warnings, and migration problems.
I think there might be a need for one pre- and one post- SQL file. Imagine you have a manually managed model and you want to create a relation to it for example. Then the table must exist before the serverpod SQL is executed.
A possible solution is that the Serverpod Migration system generates four additional SQL files pre_database_setup.sql, post_database_setup.sql, pre_migration.sql and post_migration.sql.
The pre/post_database_setup.sql files are utilized if a database is created from scratch. Any SQL code contained in these files would be applied as a single transaction with the definition.sql contents. The pre_database_setup.sql contents would be inlined before the definition.sql contents and the postDatabaseSetup contents would be inlined after.
The pre/post_migration.sql files are utilized when the database is rolled forward. These files live inside of each migration. Any SQL code in these fields would be applied as a single transaction with the migration.sql contents. The pre_migration.sql contents would be inlined before the migration.sql contents and the post_migration.sql contents would be inlined after.
Modules
Special consideration would need to be taken for modules. Any custom commands added to modules would need to be inlined in the correct places as well. And if custom code is supported in modules, module changes would need to always be applied migration by migration in order to catch any custom code.
As commented in the proposed implementation, we can't simply go with the pre/post SQL files until we have a good solution to handle custom user-created objects on the analyzer. Otherwise, this feature can leave users with an inconsistent database.
Hi @marcelomendoncasoares , Would it be possible to a at least avoid crashing the server when running in devmode ? If I create custom sql either by , writing sql in my server , wriring directly in the db, or in the definition.sql the server will exit. I can avoid the server to exit by doing something like:
try {
await pod.start(runInGuardedZone:false);
} catch (_) {
log('Server failed to start — check the warnings above for details.');
return;
}
However the server won't get started. The only solution I found is working in production mode in local dev.
Would it be possible to introduce a more flexible behavior in dev mode?
For example: Provide a --ignore-schema-validation flag
This would make working with advanced PostgreSQL features (generated columns, custom indexes like BM25, extensions, etc.) much smoother during development.
Activity
SandPod commentedon Jan 9, 2024
Great suggestion, we have been discussing this in the team as well and are planning to expand the migrations system with the capability of adding custom SQL commands.
lukehutch commentedon Jan 9, 2024
Thanks. The custom commands will have to be run after the Serverpod commands, because a user might have to override something due to lack of Serverpod support.
However, the table diff system is going to have to handle these sorts of manual modifications though, to avoid "the table is not like the protocol definition" type warnings, and migration problems.
Isakdl commentedon Jan 9, 2024
I think there might be a need for one pre- and one post- SQL file. Imagine you have a manually managed model and you want to create a relation to it for example. Then the table must exist before the serverpod SQL is executed.
SandPod commentedon Apr 25, 2024
A possible solution is that the Serverpod Migration system generates four additional SQL files
pre_database_setup.sql,post_database_setup.sql,pre_migration.sqlandpost_migration.sql.The
pre/post_database_setup.sqlfiles are utilized if a database is created from scratch. Any SQL code contained in these files would be applied as a single transaction with thedefinition.sqlcontents. Thepre_database_setup.sqlcontents would be inlined before thedefinition.sqlcontents and thepostDatabaseSetupcontents would be inlined after.The
pre/post_migration.sqlfiles are utilized when the database is rolled forward. These files live inside of each migration. Any SQL code in these fields would be applied as a single transaction with themigration.sqlcontents. Thepre_migration.sqlcontents would be inlined before themigration.sqlcontents and thepost_migration.sqlcontents would be inlined after.Modules
Special consideration would need to be taken for modules. Any custom commands added to modules would need to be inlined in the correct places as well. And if custom code is supported in modules, module changes would need to always be applied migration by migration in order to catch any custom code.
mathis6787 commentedon Nov 27, 2025
Hi!
I’ve opened a PR #4319 for this issue.
#4319
Happy to iterate based on feedback!
marcelomendoncasoares commentedon Feb 5, 2026
As commented in the proposed implementation, we can't simply go with the pre/post SQL files until we have a good solution to handle custom user-created objects on the analyzer. Otherwise, this feature can leave users with an inconsistent database.
mathis6787 commentedon Feb 11, 2026
Hi @marcelomendoncasoares , Would it be possible to a at least avoid crashing the server when running in devmode ? If I create custom sql either by , writing sql in my server , wriring directly in the db, or in the definition.sql the server will exit. I can avoid the server to exit by doing something like:
However the server won't get started. The only solution I found is working in production mode in local dev.
Would it be possible to introduce a more flexible behavior in dev mode?
For example: Provide a --ignore-schema-validation flag
This would make working with advanced PostgreSQL features (generated columns, custom indexes like BM25, extensions, etc.) much smoother during development.