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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

masato-hi
Copy link

  • 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)
    }
}

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.

1 participant