Skip to content

Commit

Permalink
chore: add example for referencing complex migration in another migra…
Browse files Browse the repository at this point in the history
…tion using virtual migration

Signed-off-by: Saurav Sharma <[email protected]>
  • Loading branch information
iamsauravsharma committed Oct 21, 2024
1 parent bef860f commit 18ef121
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 3 deletions.
44 changes: 44 additions & 0 deletions examples/mysql/migrations/m0005_reference_complex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use sqlx::{MySql, MySqlConnection};
use sqlx_migrator::error::Error;
use sqlx_migrator::migration::Migration;
use sqlx_migrator::operation::Operation;

pub(crate) struct M0005Operation;

#[async_trait::async_trait]
impl Operation<MySql> for M0005Operation {
async fn up(&self, connection: &mut MySqlConnection) -> Result<(), Error> {
sqlx::query("INSERT INTO sample (id, name) VALUES (888, 'complex')")
.execute(connection)
.await?;
Ok(())
}

async fn down(&self, connection: &mut MySqlConnection) -> Result<(), Error> {
sqlx::query("DELETE FROM sample WHERE id = 888")
.execute(connection)
.await?;
Ok(())
}
}

pub(crate) struct M0005Migration;

#[async_trait::async_trait]
impl Migration<MySql> for M0005Migration {
fn app(&self) -> &'static str {
"main"
}

fn name(&self) -> &'static str {
"m0005_reference_complex"
}

fn parents(&self) -> Vec<Box<dyn Migration<MySql>>> {
vec![Box::new(("main", "m0004_complex_operation"))]
}

fn operations(&self) -> Vec<Box<dyn Operation<MySql>>> {
vec![Box::new(M0005Operation)]
}
}
4 changes: 3 additions & 1 deletion examples/mysql/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) mod m0001_simple;
pub(crate) mod m0002_with_parents;
pub(crate) mod m0003_use_macros;
pub(crate) mod m0004_complex_operation;
pub(crate) mod m0005_reference_complex;

pub(crate) fn migrations() -> Vec<Box<dyn Migration<MySql>>> {
vec_box![
Expand All @@ -15,6 +16,7 @@ pub(crate) fn migrations() -> Vec<Box<dyn Migration<MySql>>> {
m0004_complex_operation::M0004Migration {
id: 23,
message: "Custom String".to_string()
}
},
m0005_reference_complex::M0005Migration,
]
}
44 changes: 44 additions & 0 deletions examples/postgres/migrations/m0005_reference_complex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use sqlx::{PgConnection, Postgres};
use sqlx_migrator::error::Error;
use sqlx_migrator::migration::Migration;
use sqlx_migrator::operation::Operation;

pub(crate) struct M0005Operation;

#[async_trait::async_trait]
impl Operation<Postgres> for M0005Operation {
async fn up(&self, connection: &mut PgConnection) -> Result<(), Error> {
sqlx::query("INSERT INTO sample (id, name) VALUES (888, 'complex')")
.execute(connection)
.await?;
Ok(())
}

async fn down(&self, connection: &mut PgConnection) -> Result<(), Error> {
sqlx::query("DELETE FROM sample WHERE id = 888")
.execute(connection)
.await?;
Ok(())
}
}

pub(crate) struct M0005Migration;

#[async_trait::async_trait]
impl Migration<Postgres> for M0005Migration {
fn app(&self) -> &'static str {
"main"
}

fn name(&self) -> &'static str {
"m0005_reference_complex"
}

fn parents(&self) -> Vec<Box<dyn Migration<Postgres>>> {
vec![Box::new(("main", "m0004_complex_operation"))]
}

fn operations(&self) -> Vec<Box<dyn Operation<Postgres>>> {
vec![Box::new(M0005Operation)]
}
}
4 changes: 3 additions & 1 deletion examples/postgres/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) mod m0001_simple;
pub(crate) mod m0002_with_parents;
pub(crate) mod m0003_use_macros;
pub(crate) mod m0004_complex_operation;
pub(crate) mod m0005_reference_complex;

pub(crate) fn migrations() -> Vec<Box<dyn Migration<Postgres>>> {
vec_box![
Expand All @@ -15,6 +16,7 @@ pub(crate) fn migrations() -> Vec<Box<dyn Migration<Postgres>>> {
m0004_complex_operation::M0004Migration {
id: 23,
message: "Custom String".to_string()
}
},
m0005_reference_complex::M0005Migration,
]
}
44 changes: 44 additions & 0 deletions examples/sqlite/migrations/m0005_reference_complex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use sqlx::{Sqlite, SqliteConnection};
use sqlx_migrator::error::Error;
use sqlx_migrator::migration::Migration;
use sqlx_migrator::operation::Operation;

pub(crate) struct M0005Operation;

#[async_trait::async_trait]
impl Operation<Sqlite> for M0005Operation {
async fn up(&self, connection: &mut SqliteConnection) -> Result<(), Error> {
sqlx::query("INSERT INTO sample (id, name) VALUES (888, 'complex')")
.execute(connection)
.await?;
Ok(())
}

async fn down(&self, connection: &mut SqliteConnection) -> Result<(), Error> {
sqlx::query("DELETE FROM sample WHERE id = 888")
.execute(connection)
.await?;
Ok(())
}
}

pub(crate) struct M0005Migration;

#[async_trait::async_trait]
impl Migration<Sqlite> for M0005Migration {
fn app(&self) -> &'static str {
"main"
}

fn name(&self) -> &'static str {
"m0005_reference_complex"
}

fn parents(&self) -> Vec<Box<dyn Migration<Sqlite>>> {
vec![Box::new(("main", "m0004_complex_operation"))]
}

fn operations(&self) -> Vec<Box<dyn Operation<Sqlite>>> {
vec![Box::new(M0005Operation)]
}
}
4 changes: 3 additions & 1 deletion examples/sqlite/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) mod m0001_simple;
pub(crate) mod m0002_with_parents;
pub(crate) mod m0003_use_macros;
pub(crate) mod m0004_complex_operation;
pub(crate) mod m0005_reference_complex;

pub(crate) fn migrations() -> Vec<Box<dyn Migration<Sqlite>>> {
vec_box![
Expand All @@ -15,6 +16,7 @@ pub(crate) fn migrations() -> Vec<Box<dyn Migration<Sqlite>>> {
m0004_complex_operation::M0004Migration {
id: 23,
message: "Custom String".to_string()
}
},
m0005_reference_complex::M0005Migration,
]
}

0 comments on commit 18ef121

Please sign in to comment.