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

ya-procachal-tvoi-search-chyvak #73

Merged
merged 13 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'",
tikitko marked this conversation as resolved.
Show resolved Hide resolved
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)
tikitko marked this conversation as resolved.
Show resolved Hide resolved
);
END IF;
END $$

;