Skip to content

Commit

Permalink
add migration sql.
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanYuan committed Jun 30, 2024
1 parent bf483bf commit 1cde270
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
3 changes: 1 addition & 2 deletions util/rich-indexer/resources/create_postgres_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions util/rich-indexer/resources/create_sqlite_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
31 changes: 17 additions & 14 deletions util/rich-indexer/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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 {
Expand All @@ -36,21 +38,14 @@ 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)
.min_connections(0)
.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);
Expand All @@ -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?;
Expand All @@ -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<u64> {
Expand Down

0 comments on commit 1cde270

Please sign in to comment.