Skip to content

Separate "migration" and "legacy support" #3753

Description

@tp

Currently, the way serverpod_auth_migration is designed, driven by some challenges with the legacy data1, a developer would have to keep a dependency on it to support eventual migrations, of e.g. the email passwords, in their endpoints.

But since the migration package has a dependency on the legacy serverpod_auth and all its counterpart packages from the new serverpod_auth_* packages, the migration part drags on for a long time, as it's only "done" when all account details have been fully migrated (some currently requiring a login into that account, to e.g. get the old password), or when the developer decides that "enough" account details have been migrated to drop the old tables and thus any chance of further imports.

Thus we should split the migration and legacy support into 2 distinct packages and steps.
The goal is to have the migration finished as fast as possible, and then be able to completely drop serverpod_auth from the codebase (also transitively!) so the developer can be sure that they are not relying on any old code anymore (and for example the legacy auth endpoints disappear, as do the legacy tables from the original auth package).


Packages

server_auth_migration

Should only support the actual migration of the data, from serverpod_auth to the new packages, and a new "legacy support" package for data that can not be fully migrated.

To run the migration as a one-off (by default) we can discard the current configuration approach and instead just provide a single function (which runs a single transaction) to run the migration. This function would take the configuration (whether to import the profile for example) and a callback for any custom migrations to be done.

Dependencies:

  • serverpod_auth
  • serverpod_auth_legacy_support
  • serverpod_auth_user
  • serverpod_auth_session
  • serverpod_auth_profile (might not be used, depending on the configuration)
  • serverpod_auth_email, … (Apple, Firebase, Google account packages)

Tables:

  • MigratedUser for the mapping of legacy to new user IDs
    • Only used during the migration to keep track of migrated users, in case the developer uses smaller batch sizes
    • If the code fully changes to migrate an entire user "at once" (which the current serverpod_auth_migration does not do), then it would even suffice to just store the old migrated user IDs here and don't need to worry about the mapping (a developer could still store such a mapping in a way that makes sense for them via the user migration hook callback)

serverpod_auth_legacy_support

Supports ongoing look-ups / migrations for legacy user data that can not yet be migrated to the new system.
For example:

  • Legacy sessions will be stored here indefinitely and not be migrated to the new serverpod_auth_session, as there is no way to tell the client about the new session key (and we don't want to support the legacy format in the new system)
    • We could add a "last used at" column there (with corse resolution), so developers could easily identify unused legacy sessions and thus find out when these are no longer practically useful
  • Email account hashes in the old format. This package would have a copy of the old hashing algorithm to check the password on login endpoint calls. If it's correct, the new endpoint will set the incoming password for the email account in the new email account package.
  • Since external user identifiers don't store their provider yet, we have to retain them here in an unstructured way. New social provider endpoints could then look up an incoming ID in this module and see which user it belongs to, and then store it properly in their module.

For the email passwords and social logins, any "used" row should be deleted, so that the diminishing reliance on the legacy data can be tracked over time.

Dependencies:

  • TBD
  • Maybe this should depend on the new endpoint packages (e.g. serverpod_auth_email to give developers a pre-configured base endpoint, and e.g. an "augmented" authenticationHandler)
    • The drawback is that this might pull in an unused provider (e.g. Firebase), but really only creates an empty table for those.
    • On the plus side this would make the configuration and usage much simpler. Without a dependency we'd have to somehow share interfaces between the packages in a "dependency free" way and the developer would have to hook them up correctly.

Tables:

  • LegacySession(String hash, Uuid AuthUserId)
  • LegacyEmailPassword(String hash, Uuid emailAccountID)
  • LegacyExternalUserIdentifier(String userIdentifier, Uuid AuthUserId)

Process

(This assumes no backwards compatibility requirements for the legacy login/registration endpoints, but does retain compatibility with logged-in clients and their sessions.)

  1. Add the desired new auth packages to the application
  2. Remove any direct dependencies on serverpod_auth
  3. Add the migration package, configure the custom migration callback to replace legacy user IDs with the new ones in the application's own entities
  4. Add legacy support where desired (e.g. session handler, email login endpoint, etc.)
  5. Deploy. Wait for the migration code run to completion
  6. Remove serverpod_auth_migration package from the codebase, ensure that no more indirect dependency on serverpod_auth exists either
  7. Deploy again, dropping the unused tables

The dependency on the serverpod_auth_legacy_support has to be kept for a while then though, as the migration for the email password and social logins can only take place at the speed at which users log in with those methods. Likewise, the sessions will only be migrated when users re-login or switch devices. But since that package and its data are minimal and focused, that is still a huge improvement over keeping the entire migration package with its dependency on the legacy auth.

Footnotes

  1. E.g. the password storage changed and is configurable for the legacy email accounts, and thus we can not copy the hashes over without supporting all previous options as well. Further the social login "user IDs" (externally set) lack any information about which provider they came from, and thus can not be migrated into the individual packages. ↩

Activity

  1. moved this from Inbox 📫 to In Progress 🔥 in Serverpod Frameworkson Jul 3, 2025
  2. self-assigned this
    on Jul 3, 2025
  3. added
    area: authentication
    on Jul 3, 2025
  4. added theissue type on Jul 3, 2025
  5. changed the issue type fromtoon Jul 3, 2025
  6. christerswahn commented on Jul 3, 2025

    @christerswahn
    Contributor

    Sounds good!

    Regarding dependencies, it would be valuable to not create unused database tables - this would be surprising to users that are for example not using Firebase provider. Maybe worth while to look if it can be both simple to configure and use, and not have that dependency being required?

    Should there be a followup path where the Serverpod user removes the serverpod_auth_legacy_support package and its tables, after having determined remaining table rows (if any) are not worth keeping?

    Also, for users not migrating from the old auth module, the DB should be free from legacy stuff, are they able to avoid dependency on the serverpod_auth_legacy_support package and legacy DB tables?

  7. tp commented on Jul 3, 2025

    @tp
    ContributorAuthor

    Maybe worth while to look if it can be both simple to configure and use, and not have that dependency being required?

    I think that would be really tricky, like (currently) only with the "shared interface" (but no direct dependencies) approach alluded to above.

    The same goes for example with the new profile package. Currently the migration allows configuring whether to copy the data over, and in case the developer opts out they still have that empty table.

    We could of course instead switch this around, say a callback onProfileMigration to configure, where the developer could plug in the matching from from the serverpod_auth_profile package.
    But such configurations do add up quickly (if every "target" package/provider requires an extra configuration step).

    On the other hand the empty table (and especially the needlessly noisy migrations) are not great either, but at least there is less chance to just "forget" some connection.

    Should there be a followup path where the Serverpod user removes the serverpod_auth_legacy_support package and its tables, after having determined remaining table rows (if any) are not worth keeping?

    Yes, this is basically "step 8" in the free text. I'll make sure to clarify this in the docs for this feature.

    Also, for users not migrating from the old auth module, the DB should be free from legacy stuff, are they able to avoid dependency on the serverpod_auth_legacy_support package and legacy DB tables?

    Yes, users just using a new auth package (like serverpod_auth_email) will not see any of the legacy support classes or tables. The legacy support package might depend on the new ones (in order to pre-configured that for the developer), but not vice-versa.

  8. changed the issue type fromtoon Jul 9, 2025
  9. moved this from In Progress 🔥 to Done 🚀 in Serverpod Frameworkson Jul 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: authentication

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions