From eb72c29b2ddd7e0b4ff399a42d15a04032b62e80 Mon Sep 17 00:00:00 2001 From: EthanYuan Date: Fri, 26 Jul 2024 13:13:06 +0800 Subject: [PATCH] fix: can't find migrations dir. --- Cargo.lock | 21 +++++++++++++++++++++ util/app-config/src/configs/rich_indexer.rs | 9 --------- util/rich-indexer/Cargo.toml | 2 ++ util/rich-indexer/src/store.rs | 16 ++++++++++++++-- util/rich-indexer/src/tests/mod.rs | 4 ---- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5db05f2e7..40f03b70a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1388,6 +1388,7 @@ dependencies = [ "ckb-types", "futures", "hex", + "include_dir", "log", "num-bigint", "once_cell", @@ -1395,6 +1396,7 @@ dependencies = [ "serde_json", "sql-builder", "sqlx", + "tempfile", "tokio", ] @@ -3112,6 +3114,25 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "include_dir" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "923d117408f1e49d914f1a379a309cffe4f18c05cf4e3d12e613a15fc81bd0dd" +dependencies = [ + "include_dir_macros", +] + +[[package]] +name = "include_dir_macros" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cab85a7ed0bd5f0e76d93846e0147172bed2e2d3f859bcc33a8d9699cad1a75" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "includedir" version = "0.6.0" diff --git a/util/app-config/src/configs/rich_indexer.rs b/util/app-config/src/configs/rich_indexer.rs index 65cd7c5a7b..bdf9a2fd5a 100644 --- a/util/app-config/src/configs/rich_indexer.rs +++ b/util/app-config/src/configs/rich_indexer.rs @@ -3,7 +3,6 @@ use std::{default::Default, path::PathBuf}; const PGSQL: &str = "postgres://"; const SQLITE: &str = "sqlite://"; -const SQL_MIGRATIONS_PATH: &str = "./util/rich-indexer/resources/migrations"; /// Rich indexer database type. #[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq, Eq, Copy)] @@ -50,9 +49,6 @@ pub struct RichIndexerConfig { /// The database password. #[serde(default = "default_db_password")] pub db_password: String, - /// The migrations path with a default value - #[serde(default = "default_migrations_path", skip_deserializing)] - pub migrations_path: PathBuf, } impl Default for RichIndexerConfig { @@ -65,7 +61,6 @@ impl Default for RichIndexerConfig { db_port: default_db_port(), db_user: default_db_user(), db_password: default_db_password(), - migrations_path: default_migrations_path(), } } } @@ -89,7 +84,3 @@ fn default_db_user() -> String { fn default_db_password() -> String { "123456".to_string() } - -fn default_migrations_path() -> PathBuf { - PathBuf::from(SQL_MIGRATIONS_PATH) -} diff --git a/util/rich-indexer/Cargo.toml b/util/rich-indexer/Cargo.toml index e43297572e..5633c510ff 100644 --- a/util/rich-indexer/Cargo.toml +++ b/util/rich-indexer/Cargo.toml @@ -24,6 +24,8 @@ num-bigint = "0.4" once_cell = "1.8.0" sql-builder = "3.1" sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "any", "sqlite", "postgres"] } +include_dir = "0.7" +tempfile = "3" [dev-dependencies] hex = "0.4" diff --git a/util/rich-indexer/src/store.rs b/util/rich-indexer/src/store.rs index d6626de3cc..22c910ff2a 100644 --- a/util/rich-indexer/src/store.rs +++ b/util/rich-indexer/src/store.rs @@ -1,6 +1,7 @@ use anyhow::{anyhow, Result}; use ckb_app_config::{DBDriver, RichIndexerConfig}; use futures::TryStreamExt; +use include_dir::{include_dir, Dir}; use log::LevelFilter; use once_cell::sync::OnceCell; use sqlx::{ @@ -9,8 +10,9 @@ use sqlx::{ query::{Query, QueryAs}, ConnectOptions, IntoArguments, Row, Transaction, }; +use tempfile::tempdir; -use std::fs::OpenOptions; +use std::fs::{self, OpenOptions}; use std::marker::{Send, Unpin}; use std::path::PathBuf; use std::str::FromStr; @@ -21,6 +23,7 @@ const SQL_SQLITE_CREATE_TABLE: &str = include_str!("../resources/create_sqlite_t const SQL_SQLITE_CREATE_INDEX: &str = include_str!("../resources/create_sqlite_index.sql"); const SQL_POSTGRES_CREATE_TABLE: &str = include_str!("../resources/create_postgres_table.sql"); const SQL_POSTGRES_CREATE_INDEX: &str = include_str!("../resources/create_postgres_index.sql"); +static MIGRATIONS_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/resources/migrations"); #[derive(Clone, Default)] pub struct SQLXPool { @@ -81,7 +84,16 @@ impl SQLXPool { // Run migrations log::info!("Running migrations..."); - let migrator = Migrator::new(db_config.migrations_path.clone()).await?; + let temp_dir = tempdir()?; + for file in MIGRATIONS_DIR.files() { + log::info!("Found migration file: {:?}", file.path()); + let file_path = temp_dir.path().join(file.path()); + if let Some(parent) = file_path.parent() { + fs::create_dir_all(parent)?; + } + fs::write(&file_path, file.contents())?; + } + let migrator = Migrator::new(temp_dir.path()).await?; migrator.run(&pool).await?; log::info!("Migrations are done."); diff --git a/util/rich-indexer/src/tests/mod.rs b/util/rich-indexer/src/tests/mod.rs index 4cdb9a963e..5493fe5291 100644 --- a/util/rich-indexer/src/tests/mod.rs +++ b/util/rich-indexer/src/tests/mod.rs @@ -14,17 +14,13 @@ use ckb_jsonrpc_types::{ use ckb_types::h256; use ckb_types::prelude::*; -use std::path::PathBuf; - const MEMORY_DB: &str = ":memory:"; const BLOCK_DIR: &str = "./src/tests/data/blocks/"; -const MIGRATIONS_PATH: &str = "./resources/migrations"; async fn connect_sqlite(store_path: &str) -> SQLXPool { let mut pool = SQLXPool::default(); let config = RichIndexerConfig { store: store_path.into(), - migrations_path: PathBuf::from(MIGRATIONS_PATH), ..Default::default() }; pool.connect(&config).await.unwrap();