Skip to content

Commit

Permalink
separate temp pg own crate
Browse files Browse the repository at this point in the history
  • Loading branch information
gegaowp committed Nov 1, 2024
1 parent 2875599 commit d4b5e07
Show file tree
Hide file tree
Showing 22 changed files with 99 additions and 390 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ members = [
"crates/sui-package-dump",
"crates/sui-package-management",
"crates/sui-package-resolver",
"crates/sui-pg-temp-db",
"crates/sui-proc-macros",
"crates/sui-protocol-config",
"crates/sui-protocol-config-macros",
Expand Down Expand Up @@ -666,6 +667,7 @@ sui-open-rpc-macros = { path = "crates/sui-open-rpc-macros" }
sui-package-dump = { path = "crates/sui-package-dump" }
sui-package-management = { path = "crates/sui-package-management" }
sui-package-resolver = { path = "crates/sui-package-resolver" }
sui-pg-temp-db = { path = "crates/sui-pg-temp-db" }
sui-proc-macros = { path = "crates/sui-proc-macros" }
sui-protocol-config = { path = "crates/sui-protocol-config" }
sui-protocol-config-macros = { path = "crates/sui-protocol-config-macros" }
Expand Down
1 change: 1 addition & 0 deletions crates/sui-bridge-indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sui-bridge.workspace = true
sui-sdk.workspace = true
sui-json-rpc-types.workspace = true
sui-data-ingestion-core.workspace = true
sui-pg-temp-db.workspace = true
sui-types.workspace = true
telemetry-subscribers.workspace = true
tracing.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-bridge-indexer/tests/indexer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use sui_bridge_indexer::storage::PgBridgePersistent;
use sui_bridge_indexer::{create_sui_indexer, schema};
use sui_data_ingestion_core::DataIngestionMetrics;
use sui_indexer::database::Connection;
use sui_indexer::tempdb::TempDb;
use sui_indexer_builder::indexer_builder::IndexerProgressStore;
use sui_pg_temp_db::TempDb;

const MIGRATIONS: EmbeddedMigrations = embed_migrations!("src/migrations");

Expand Down
1 change: 1 addition & 0 deletions crates/sui-cluster-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ regex.workspace = true
sui-indexer.workspace = true
sui-faucet.workspace = true
sui-graphql-rpc.workspace = true
sui-pg-temp-db.workspace = true
sui-swarm.workspace = true
sui-swarm-config.workspace = true
sui-json-rpc-types.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-cluster-test/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use sui_config::Config;
use sui_config::{PersistedConfig, SUI_KEYSTORE_FILENAME, SUI_NETWORK_CONFIG};
use sui_graphql_rpc::config::{ConnectionConfig, ServiceConfig};
use sui_graphql_rpc::test_infra::cluster::start_graphql_server_with_fn_rpc;
use sui_indexer::tempdb::TempDb;
use sui_indexer::test_utils::{
start_indexer_jsonrpc_for_testing, start_indexer_writer_for_testing,
};
use sui_keys::keystore::{AccountKeystore, FileBasedKeystore, Keystore};
use sui_pg_temp_db::TempDb;
use sui_sdk::sui_client_config::{SuiClientConfig, SuiEnv};
use sui_sdk::wallet_context::WalletContext;
use sui_swarm::memory::Swarm;
Expand Down
1 change: 1 addition & 0 deletions crates/sui-graphql-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ serde_with.workspace = true
serde_yaml.workspace = true
shared-crypto.workspace = true
similar.workspace = true
sui-pg-temp-db.workspace = true
sui-sdk.workspace = true
sui-types.workspace = true
tap.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/sui-graphql-rpc/src/data/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,9 @@ mod tests {
use sui_framework::BuiltInFramework;
use sui_indexer::{
database::Connection, db::reset_database, models::objects::StoredObject, schema::objects,
tempdb::TempDb, types::IndexedObject,
types::IndexedObject,
};
use sui_pg_temp_db::TempDb;

#[tokio::test]
async fn test_query_cost() {
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-graphql-rpc/src/server/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ pub mod tests {
use serde_json::json;
use std::sync::Arc;
use std::time::Duration;
use sui_indexer::tempdb::get_available_port;
use sui_pg_temp_db::get_available_port;
use sui_sdk::SuiClient;
use sui_types::digests::get_mainnet_chain_identifier;
use sui_types::transaction::TransactionData;
Expand Down
3 changes: 1 addition & 2 deletions crates/sui-graphql-rpc/src/test_infra/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ pub use sui_indexer::config::RetentionConfig;
pub use sui_indexer::config::SnapshotLagConfig;
use sui_indexer::errors::IndexerError;
use sui_indexer::store::PgIndexerStore;
use sui_indexer::tempdb::get_available_port;
use sui_indexer::tempdb::TempDb;
use sui_indexer::test_utils::start_indexer_writer_for_testing;
use sui_pg_temp_db::{get_available_port, TempDb};
use sui_swarm_config::genesis_config::{AccountConfig, DEFAULT_GAS_AMOUNT};
use sui_types::storage::RestStateReader;
use tempfile::tempdir;
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-graphql-rpc/tests/move_registry_e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use sui_graphql_rpc::{
},
};
use sui_graphql_rpc_client::simple_client::SimpleClient;
use sui_indexer::tempdb::get_available_port;
use sui_json_rpc::name_service::{Domain, DomainFormat};
use sui_json_rpc_types::ObjectChange;
use sui_move_build::BuildConfig;
use sui_pg_temp_db::get_available_port;
use sui_types::{
base_types::{ObjectID, SequenceNumber},
digests::ObjectDigest,
Expand Down
3 changes: 2 additions & 1 deletion crates/sui-indexer-alt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ prometheus.workspace = true
reqwest.workspace = true
serde.workspace = true
telemetry-subscribers.workspace = true
tempfile.workspace = true
thiserror.workspace = true
tokio.workspace = true
tokio-stream.workspace = true
Expand All @@ -37,11 +36,13 @@ url.workspace = true

mysten-metrics.workspace = true
sui-field-count.workspace = true
sui-pg-temp-db.workspace = true
sui-storage.workspace = true
sui-types.workspace = true

[dev-dependencies]
rand.workspace = true
tempfile.workspace = true
wiremock.workspace = true

sui-types = { workspace = true, features = ["test-utils"] }
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use std::time::Duration;
use tracing::info;
use url::Url;

pub mod tempdb;

const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
const DEFAULT_POOL_SIZE: u32 = 100;
const DEFAULT_CONNECTION_TIMEOUT_SECS: u64 = 60;
Expand All @@ -34,7 +32,7 @@ pub struct DbConfig {
database_url: Url,

/// Number of connections to keep in the pool.
#[arg(long, default_value = stringify!(DEFAULT_POOL_SIZE))]
#[arg(long, default_value_t = DEFAULT_POOL_SIZE)]
connection_pool_size: u32,

/// Time spent waiting for a connection from the pool to become available.
Expand Down Expand Up @@ -156,8 +154,9 @@ impl DbConfig {
) -> Self {
Self {
database_url,
connection_pool_size: connection_pool_size.unwrap_or(DEFAULT_POOL_SIZE), // clap default
connection_timeout: connection_timeout.unwrap_or(Duration::from_secs(DEFAULT_CONNECTION_TIMEOUT_SECS)), // clap default
connection_pool_size: connection_pool_size.unwrap_or(DEFAULT_POOL_SIZE),
connection_timeout: connection_timeout
.unwrap_or(Duration::from_secs(DEFAULT_CONNECTION_TIMEOUT_SECS)),
}
}
}
Expand All @@ -177,9 +176,28 @@ pub async fn reset_database(
#[cfg(test)]
mod tests {
use super::*;
use crate::db::{Db, DbConfig};
use diesel::prelude::QueryableByName;
use diesel_async::RunQueryDsl;
use tempdb::TempDb;
use sui_pg_temp_db::TempDb;

#[tokio::test]
async fn temp_db_smoketest() {
telemetry_subscribers::init_for_testing();
let db = TempDb::new().unwrap();
let url = db.database().url();
println!("url: {}", url.as_str());
let db_config = DbConfig::new(url.clone(), None, None);
let db = Db::new(db_config).await.unwrap();
let mut connection = db.connect().await.unwrap();

// Run a simple query to verify the db can properly be queried
let resp = diesel::sql_query("SELECT datname FROM pg_database")
.execute(&mut connection)
.await
.unwrap();
println!("resp: {:?}", resp);
}

#[derive(QueryableByName)]
struct CountResult {
Expand Down
1 change: 1 addition & 0 deletions crates/sui-indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ sui-json-rpc.workspace = true
sui-json-rpc-api.workspace = true
sui-json-rpc-types.workspace = true
sui-open-rpc.workspace = true
sui-pg-temp-db.workspace = true
sui-sdk.workspace = true
sui-snapshot.workspace = true
sui-storage.workspace = true
Expand Down
23 changes: 22 additions & 1 deletion crates/sui-indexer/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ mod tests {
check_db_migration_consistency, check_db_migration_consistency_impl, reset_database,
ConnectionPoolConfig, MIGRATIONS,
};
use crate::tempdb::TempDb;
use diesel::migration::{Migration, MigrationSource};
use diesel::pg::Pg;
use diesel_migrations::MigrationHarness;
use sui_pg_temp_db::TempDb;

// Check that the migration records in the database created from the local schema
// pass the consistency check.
Expand Down Expand Up @@ -392,4 +392,25 @@ mod tests {
.await
.unwrap();
}

#[tokio::test]
async fn temp_db_smoketest() {
use crate::database::Connection;
use diesel_async::RunQueryDsl;
use sui_pg_temp_db::TempDb;

telemetry_subscribers::init_for_testing();

let db = TempDb::new().unwrap();
let url = db.database().url();
println!("url: {}", url.as_str());
let mut connection = Connection::dedicated(url).await.unwrap();

// Run a simple query to verify the db can properly be queried
let resp = diesel::sql_query("SELECT datname FROM pg_database")
.execute(&mut connection)
.await
.unwrap();
println!("resp: {:?}", resp);
}
}
1 change: 0 additions & 1 deletion crates/sui-indexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub mod restorer;
pub mod schema;
pub mod store;
pub mod system_package_task;
pub mod tempdb;
pub mod test_utils;
pub mod types;

Expand Down
Loading

0 comments on commit d4b5e07

Please sign in to comment.