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

Make a query overridable. #358

Merged
merged 2 commits into from
Feb 18, 2025
Merged

Make a query overridable. #358

merged 2 commits into from
Feb 18, 2025

Conversation

masato-hi
Copy link
Contributor

  • If implement to the migrate function, make returnable to Report.
  • Make allow override multiple queries for a migration.

For example, it is possible to support Cassandra's CQL by implementing the following.


#[derive(Debug)]
struct CassandraConnection;

#[async_trait]
impl AsyncMigrate for CassandraConnection {
    fn assert_migrations_table_query(migration_table_name: &str) -> String {
        const ASSERT_MIGRATIONS_TABLE_QUERY: &'static str = r#"
            CREATE TABLE IF NOT EXISTS %MIGRATION_TABLE_NAME%(
                version INT,
                name TEXT,
                applied_on TEXT,
                checksum TEXT,
                PRIMARY KEY(version)
            )
        "#;
        ASSERT_MIGRATIONS_TABLE_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
    }

    fn get_last_applied_migration_query(migration_table_name: &str) -> String {
        const GET_LAST_APPLIED_MIGRATION_QUERY: &'static str = r#"
            SELECT version, name, applied_on, checksum
            FROM %MIGRATION_TABLE_NAME%
        "#;
        GET_LAST_APPLIED_MIGRATION_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
    }

    fn get_applied_migrations_query(migration_table_name: &str) -> String {
        const GET_APPLIED_MIGRATIONS_QUERY: &'static str = r#"
            SELECT version, name, applied_on, checksum
            FROM %MIGRATION_TABLE_NAME%;
        "#;
        GET_APPLIED_MIGRATIONS_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
    }

    async fn get_last_applied_migration(
        &mut self,
        migration_table_name: &str,
    ) -> Result<Option<Migration>, Error> {
        let mut migrations = self
            .query(Self::get_last_applied_migration_query(migration_table_name).as_str())
            .await
            .migration_err("error getting last applied migration", None)?;

        migrations.sort_by_key(|m| m.version());

        Ok(migrations.pop())
    }

    async fn get_applied_migrations(
        &mut self,
        migration_table_name: &str,
    ) -> Result<Vec<Migration>, Error> {
        let mut migrations = self
            .query(Self::get_applied_migrations_query(migration_table_name).as_str())
            .await
            .migration_err("error getting applied migrations", None)?;

        migrations.sort_by_key(|m| m.version());

        Ok(migrations)
    }
}

Copy link
Member

@jxs jxs left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!
Are you implementing a Cassandra driver?
If so feel free to PR a README.md update with it!

@jxs jxs merged commit cbdece5 into rust-db:main Feb 18, 2025
29 checks passed
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.

2 participants