Skip to content

Commit

Permalink
fix: can't find migrations dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanYuan committed Jul 26, 2024
1 parent abbf267 commit eb72c29
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions util/app-config/src/configs/rich_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand All @@ -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(),
}
}
}
Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions util/rich-indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 14 additions & 2 deletions util/rich-indexer/src/store.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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.");

Expand Down
4 changes: 0 additions & 4 deletions util/rich-indexer/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit eb72c29

Please sign in to comment.