From 1cde270b4ca1db22b08a52628a4660349e35e1c2 Mon Sep 17 00:00:00 2001 From: EthanYuan Date: Sun, 30 Jun 2024 23:59:12 +0800 Subject: [PATCH] add migration sql. --- .../resources/create_postgres_table.sql | 3 +- .../resources/create_sqlite_table.sql | 3 +- .../20240603_add_is_spent_to_output.sql | 9 ++++++ util/rich-indexer/src/store.rs | 31 ++++++++++--------- 4 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 util/rich-indexer/resources/migrations/20240603_add_is_spent_to_output.sql diff --git a/util/rich-indexer/resources/create_postgres_table.sql b/util/rich-indexer/resources/create_postgres_table.sql index 26bdb5e721..f743425a06 100644 --- a/util/rich-indexer/resources/create_postgres_table.sql +++ b/util/rich-indexer/resources/create_postgres_table.sql @@ -58,8 +58,7 @@ CREATE TABLE output( capacity BIGINT NOT NULL, lock_script_id BIGINT, type_script_id BIGINT, - data BYTEA, - is_spent BOOLEAN DEFAULT FALSE + data BYTEA ); CREATE TABLE input( diff --git a/util/rich-indexer/resources/create_sqlite_table.sql b/util/rich-indexer/resources/create_sqlite_table.sql index 29c474414c..ca85b20e5e 100644 --- a/util/rich-indexer/resources/create_sqlite_table.sql +++ b/util/rich-indexer/resources/create_sqlite_table.sql @@ -58,8 +58,7 @@ CREATE TABLE output( capacity INTEGER NOT NULL, lock_script_id INTEGER, type_script_id INTEGER, - data BLOB, - is_spent INTEGER DEFAULT 0 + data BLOB ); CREATE TABLE input( diff --git a/util/rich-indexer/resources/migrations/20240603_add_is_spent_to_output.sql b/util/rich-indexer/resources/migrations/20240603_add_is_spent_to_output.sql new file mode 100644 index 0000000000..a7cd72d02c --- /dev/null +++ b/util/rich-indexer/resources/migrations/20240603_add_is_spent_to_output.sql @@ -0,0 +1,9 @@ +-- 20240630_add_is_spent_to_output.sql + +ALTER TABLE output +ADD COLUMN is_spent INTEGER DEFAULT 0; + +UPDATE output +SET is_spent = 1 +FROM input +WHERE input.output_id = output.id; diff --git a/util/rich-indexer/src/store.rs b/util/rich-indexer/src/store.rs index d9caa7f6fe..4ac79af061 100644 --- a/util/rich-indexer/src/store.rs +++ b/util/rich-indexer/src/store.rs @@ -5,13 +5,14 @@ use log::LevelFilter; use once_cell::sync::OnceCell; use sqlx::{ any::{Any, AnyArguments, AnyConnectOptions, AnyPool, AnyPoolOptions, AnyRow}, + migrate::Migrator, query::{Query, QueryAs}, ConnectOptions, IntoArguments, Row, Transaction, }; use std::fs::OpenOptions; use std::marker::{Send, Unpin}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::str::FromStr; use std::{fmt::Debug, sync::Arc, time::Duration}; @@ -20,6 +21,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"); +const SQL_MIGRATIONS: &str = "./util/rich-indexer/resources/migrations"; #[derive(Clone, Default)] pub struct SQLXPool { @@ -36,13 +38,6 @@ impl Debug for SQLXPool { } impl SQLXPool { - pub fn new() -> Self { - SQLXPool { - pool: Arc::new(OnceCell::new()), - db_driver: DBDriver::default(), - } - } - pub async fn connect(&mut self, db_config: &RichIndexerConfig) -> Result<()> { let pool_options = AnyPoolOptions::new() .max_connections(10) @@ -50,7 +45,7 @@ impl SQLXPool { .acquire_timeout(Duration::from_secs(60)) .max_lifetime(Duration::from_secs(1800)) .idle_timeout(Duration::from_secs(30)); - match db_config.db_type { + let pool = match db_config.db_type { DBDriver::Sqlite => { let require_init = is_sqlite_require_init(db_config); let uri = build_url_for_sqlite(db_config); @@ -59,13 +54,13 @@ impl SQLXPool { let pool = pool_options.connect_with(connection_options).await?; log::info!("SQLite is connected."); self.pool - .set(pool) + .set(pool.clone()) .map_err(|_| anyhow!("set pool failed!"))?; if require_init { self.create_tables_for_sqlite().await?; } self.db_driver = DBDriver::Sqlite; - Ok(()) + pool } DBDriver::Postgres => { let require_init = self.is_postgres_require_init(db_config).await?; @@ -75,15 +70,23 @@ impl SQLXPool { let pool = pool_options.connect_with(connection_options).await?; log::info!("PostgreSQL is connected."); self.pool - .set(pool) + .set(pool.clone()) .map_err(|_| anyhow!("set pool failed"))?; if require_init { self.create_tables_for_postgres().await?; } self.db_driver = DBDriver::Postgres; - Ok(()) + pool } - } + }; + + // Run migrations + log::info!("Running migrations..."); + let migrator = Migrator::new(Path::new(SQL_MIGRATIONS)).await?; + migrator.run(&pool).await?; + log::info!("Migrations are done."); + + Ok(()) } pub async fn fetch_count(&self, table_name: &str) -> Result {