Skip to content

Commit

Permalink
migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
tikitko committed Dec 27, 2023
1 parent 3c592db commit a31789d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 34 deletions.
41 changes: 7 additions & 34 deletions blog-server-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod endpoints;
mod extensions;
mod migrations;
mod router;
mod utils;

Expand Down Expand Up @@ -36,44 +37,16 @@ pub async fn init_db() -> rbatis::RBatis {
let rb = rbatis::RBatis::new();
rb.init(rbdc_pg::driver::PgDriver {}, PG_URL)
.expect("DB init failed");
migrate_db(&rb).await.expect("DB migration failed");
migrations::exec(&rb).await.expect("DB migration failed");
return rb;
}

async fn migrate_db(rb: &rbatis::RBatis) -> Result<(), Box<dyn std::error::Error>> {
let sql = std::fs::read_to_string("./table_pg.sql")?;
rb.exec(&sql, vec![]).await?;

let posts: Vec<blog_server_services::traits::post_service::Post> =
rb.query_decode("select * from post", vec![]).await?;
for post in posts {
let content = post
.base
.content
.as_ref()
.map(|c| blog_server_services::utils::html::clean(c));
let plain_text_content = content
.as_ref()
.map(|c| blog_server_services::utils::html::to_plain(c));
rb.query(
"update post set content=?, plain_text_content=? where id=?",
vec![
rbs::to_value!(content),
rbs::to_value!(plain_text_content),
rbs::to_value!(post.id),
],
)
.await?;
}

Ok(())
}

pub async fn init_rabbit(
) -> Box<dyn blog_server_services::traits::event_bus_service::EventBusService> {
if RABBIT_URL.is_empty() {
blog_server_services::impls::create_rabbit_event_bus_service(None).await
blog_server_services::impls::create_rabbit_event_bus_service(if RABBIT_URL.is_empty() {
None
} else {
blog_server_services::impls::create_rabbit_event_bus_service(Some(RABBIT_URL)).await
}
Some(RABBIT_URL)
})
.await
}
6 changes: 6 additions & 0 deletions blog-server-api/src/migrations/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub async fn exec(rb: &rbatis::RBatis) -> Result<(), Box<dyn std::error::Error>> {
let sql = std::fs::read_to_string("./table_pg.sql")?;
rb.exec(&sql, vec![]).await?;

Ok(())
}
40 changes: 40 additions & 0 deletions blog-server-api/src/migrations/content.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pub async fn exec(rb: &rbatis::RBatis) -> Result<(), Box<dyn std::error::Error>> {
let is_content_migrated: bool = rb
.query_decode::<u64>(
"select count(1) as count from migration where key='content_migration'",
vec![],
)
.await?
> 0;

if !is_content_migrated {
let posts: Vec<blog_server_services::traits::post_service::Post> =
rb.query_decode("select * from post", vec![]).await?;
for post in posts {
let content = post
.base
.content
.as_ref()
.map(|c| blog_server_services::utils::html::clean(c));
let plain_text_content = content
.as_ref()
.map(|c| blog_server_services::utils::html::to_plain(c));
rb.query(
"update post set content=?, plain_text_content=? where id=?",
vec![
rbs::to_value!(content),
rbs::to_value!(plain_text_content),
rbs::to_value!(post.id),
],
)
.await?;
}
rb.query(
"insert into migration (key) values ('content_migration')",
vec![],
)
.await?;
}

Ok(())
}
8 changes: 8 additions & 0 deletions blog-server-api/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod base;
mod content;

pub async fn exec(rb: &rbatis::RBatis) -> Result<(), Box<dyn std::error::Error>> {
base::exec(rb).await?;
content::exec(rb).await?;
Ok(())
}
11 changes: 11 additions & 0 deletions table_pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,15 @@ DO $$ BEGIN
END IF;
END $$

;

DO $$ BEGIN
IF NOT EXISTS(select * from pg_tables where schemaname = 'public' and tablename = 'migration') THEN
CREATE TABLE migration (
key VARCHAR(100) NOT NULL,
PRIMARY KEY (key)
);
END IF;
END $$

;

0 comments on commit a31789d

Please sign in to comment.