Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migration support group name #2457

Closed
wants to merge 6 commits into from

Conversation

cuipeiyu
Copy link

PR Info

  • Closes
  • Dependencies:
  • Dependents:

New Features

  • Extend the seaql_migrations table with a 'group' field so that multiple independent projects can use the same database

Bug Fixes

Breaking Changes

Changes

@tyt2y3 tyt2y3 requested a review from billy1624 December 23, 2024 23:40
Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

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

thank you. it's such a useful feature, however, I think we need to perform create column if not exists on startup, otherwise it'd break existing users.

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

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

Hey @cuipeiyu, thanks for the PR!! Sorry for the delay.

I think we already have this feature build in but it works in a different way. Instead of adding an extra group field in the existing seaql_migrations table. We can create multiple seaql_migrations table in a single database.

By default the migration table is named seaql_migrations:

use crate::common::migration::*;
use sea_orm_migration::prelude::*;

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
    fn migrations() -> Vec<Box<dyn MigrationTrait>> {
        vec![
            Box::new(m20220118_000001_create_cake_table::Migration),
            Box::new(m20220118_000002_create_fruit_table::Migration),
            Box::new(m20220118_000003_seed_cake_table::Migration),
            Box::new(m20220118_000004_create_tea_enum::Migration),
            Box::new(m20220923_000001_seed_cake_table::Migration),
            Box::new(m20230109_000001_seed_cake_table::Migration),
        ]
    }
}

We can override the name of the migration table using the migration_table_name method:

use crate::common::migration::*;
use sea_orm_migration::prelude::*;

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
    fn migrations() -> Vec<Box<dyn MigrationTrait>> {
        vec![
            Box::new(m20220118_000001_create_cake_table::Migration),
            Box::new(m20220118_000002_create_fruit_table::Migration),
            Box::new(m20220118_000003_seed_cake_table::Migration),
            Box::new(m20220118_000004_create_tea_enum::Migration),
            Box::new(m20220923_000001_seed_cake_table::Migration),
            Box::new(m20230109_000001_seed_cake_table::Migration),
        ]
    }

    fn migration_table_name() -> sea_orm::DynIden {
        Alias::new("override_migration_table_name").into_iden()
    }
}

So, we will have 2 SeaORM migration table in the database and we can execute each set of migration files independently:

// Migration table named `seaql_migrations`
default::Migrator::up(db).await?;

// Migration table named `override_migration_table_name`
override_migration_table_name::Migrator::up(db).await?;

@billy1624 billy1624 marked this pull request as draft April 1, 2025 06:12
@cuipeiyu cuipeiyu closed this Apr 1, 2025
@cuipeiyu
Copy link
Author

cuipeiyu commented Apr 1, 2025

Thanks for your review, glad there is a more elegant way to implement it. I have closed this PR!

@cuipeiyu cuipeiyu deleted the feat/migration_with_group branch April 1, 2025 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants