Migration information methods (#1519)
Co-authored-by: Sergei Ivankov <sergeiivankov@pm.me>
This commit is contained in:
parent
ba2a2fd91d
commit
5737e023a2
@ -17,7 +17,7 @@ use sea_schema::{mysql::MySql, postgres::Postgres, probe::SchemaProbe, sqlite::S
|
|||||||
|
|
||||||
use super::{seaql_migrations, IntoSchemaManagerConnection, MigrationTrait, SchemaManager};
|
use super::{seaql_migrations, IntoSchemaManagerConnection, MigrationTrait, SchemaManager};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
/// Status of migration
|
/// Status of migration
|
||||||
pub enum MigrationStatus {
|
pub enum MigrationStatus {
|
||||||
/// Not yet applied
|
/// Not yet applied
|
||||||
@ -26,11 +26,6 @@ pub enum MigrationStatus {
|
|||||||
Applied,
|
Applied,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Migration {
|
|
||||||
migration: Box<dyn MigrationTrait>,
|
|
||||||
status: MigrationStatus,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for MigrationStatus {
|
impl Display for MigrationStatus {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let status = match self {
|
let status = match self {
|
||||||
@ -41,6 +36,23 @@ impl Display for MigrationStatus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Migration {
|
||||||
|
migration: Box<dyn MigrationTrait>,
|
||||||
|
status: MigrationStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Migration {
|
||||||
|
/// Get migration name from MigrationName trait implementation
|
||||||
|
pub fn name(&self) -> &str {
|
||||||
|
self.migration.name()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get migration status
|
||||||
|
pub fn status(&self) -> MigrationStatus {
|
||||||
|
self.status
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Performing migrations on a database
|
/// Performing migrations on a database
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait MigratorTrait: Send {
|
pub trait MigratorTrait: Send {
|
||||||
|
@ -2,7 +2,7 @@ mod migrator;
|
|||||||
use migrator::Migrator;
|
use migrator::Migrator;
|
||||||
|
|
||||||
use sea_orm::{ConnectOptions, ConnectionTrait, Database, DbBackend, DbErr, Statement};
|
use sea_orm::{ConnectOptions, ConnectionTrait, Database, DbBackend, DbErr, Statement};
|
||||||
use sea_orm_migration::prelude::*;
|
use sea_orm_migration::{migrator::MigrationStatus, prelude::*};
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn main() -> Result<(), DbErr> {
|
async fn main() -> Result<(), DbErr> {
|
||||||
@ -92,6 +92,14 @@ async fn run_migration(url: &str, db_name: &str, schema: &str) -> Result<(), DbE
|
|||||||
println!("\nMigrator::up");
|
println!("\nMigrator::up");
|
||||||
Migrator::up(db, Some(1)).await?;
|
Migrator::up(db, Some(1)).await?;
|
||||||
|
|
||||||
|
println!("\nMigrator::get_pending_migrations");
|
||||||
|
let migrations = Migrator::get_pending_migrations(db).await?;
|
||||||
|
assert_eq!(migrations.len(), 5);
|
||||||
|
|
||||||
|
let migration = migrations.get(0).unwrap();
|
||||||
|
assert_eq!(migration.name(), "m20220118_000002_create_fruit_table");
|
||||||
|
assert_eq!(migration.status(), MigrationStatus::Pending);
|
||||||
|
|
||||||
assert!(manager.has_table("cake").await?);
|
assert!(manager.has_table("cake").await?);
|
||||||
assert!(!manager.has_table("fruit").await?);
|
assert!(!manager.has_table("fruit").await?);
|
||||||
|
|
||||||
@ -137,6 +145,14 @@ async fn run_migration(url: &str, db_name: &str, schema: &str) -> Result<(), DbE
|
|||||||
println!("\nMigrator::up");
|
println!("\nMigrator::up");
|
||||||
Migrator::up(db, None).await?;
|
Migrator::up(db, None).await?;
|
||||||
|
|
||||||
|
println!("\nMigrator::get_applied_migrations");
|
||||||
|
let migrations = Migrator::get_applied_migrations(db).await?;
|
||||||
|
assert_eq!(migrations.len(), 6);
|
||||||
|
|
||||||
|
let migration = migrations.get(0).unwrap();
|
||||||
|
assert_eq!(migration.name(), "m20220118_000001_create_cake_table");
|
||||||
|
assert_eq!(migration.status(), MigrationStatus::Applied);
|
||||||
|
|
||||||
println!("\nMigrator::status");
|
println!("\nMigrator::status");
|
||||||
Migrator::status(db).await?;
|
Migrator::status(db).await?;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user