Skip to content

Have Serverpod auto-add custom SQL commands on initial migration #1730

Open

Description

@lukehutch

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.

Activity

  1. added
    enhancement
    on Jan 9, 2024
  2. SandPod commented on Jan 9, 2024

    @SandPod
    Contributor

    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.

  3. lukehutch commented on Jan 9, 2024

    @lukehutch
    ContributorAuthor

    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.

  4. Isakdl commented on Jan 9, 2024

    @Isakdl
    Collaborator

    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.

  5. SandPod commented on Apr 25, 2024

    @SandPod
    Contributor

    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.

  6. added
    area: database
    on Dec 19, 2024
  7. mathis6787 commented on Nov 27, 2025

    @mathis6787
    Contributor

    Hi!
    I’ve opened a PR #4319 for this issue.

    #4319

    Happy to iterate based on feedback!

  8. moved this from Backlog 🗂️ to Contributor In Progress 👩‍💻 in Serverpod Frameworkson Dec 8, 2025
  9. moved this from Contributor In Progress 👩‍💻 to Backlog 🗂️ in Serverpod Frameworkson Feb 5, 2026
  10. marcelomendoncasoares commented on Feb 5, 2026

    @marcelomendoncasoares
    Collaborator

    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.

  11. added
    area: migrations
    on Feb 5, 2026
  12. mathis6787 commented on Feb 11, 2026

    @mathis6787
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: databasearea: migrationsenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions