Skip to content

Commit a5521de

Browse files
committed
add doc about migration via sxlx
1 parent 591ab22 commit a5521de

File tree

6 files changed

+549
-49
lines changed

6 files changed

+549
-49
lines changed

configuration/src/configs/database.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ pub struct DatabaseConfig {
2222
pub shards_config:
2323
std::collections::HashMap<near_primitives::types::ShardId, DatabaseConnectUrl>,
2424
pub max_connections: u32,
25-
// Migrations cannot be applied to read-only replicas
26-
// We should run rpc-server only on read-only replicas
27-
pub read_only: bool,
2825
pub shard_layout: Option<near_primitives::shard_layout::ShardLayout>,
2926
}
3027

@@ -34,7 +31,6 @@ impl DatabaseConfig {
3431
database_url: self.database_url.clone(),
3532
shards_config: self.shards_config.clone(),
3633
max_connections: self.max_connections,
37-
read_only: true,
3834
shard_layout: self.shard_layout.clone(),
3935
}
4036
}
@@ -77,7 +73,6 @@ impl From<CommonDatabaseConfig> for DatabaseConfig {
7773
max_connections: database_config
7874
.max_connections
7975
.unwrap_or_else(CommonDatabaseConfig::default_max_connections),
80-
read_only: false,
8176
shard_layout: crate::shard_layout().ok(),
8277
}
8378
}

database/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@
33
This is a helper crate that provides db manager.
44

55
## [Postgres DB](src/postgres/README.md)
6+
7+
## [Database Migrations with SQLx](../docs/DATABASE_MIGRATIONS.md)
8+
9+
Comprehensive guide for managing database migrations using SQLx CLI. This covers:
10+
- Setting up and applying migrations for all database types (meta, shard, transaction details)
11+
- Creating new migrations
12+
- Migration best practices and troubleshooting
13+
- Automated migration scripts
14+
15+
## [Legacy Database Migration Scripts](database_migrations/README.md)
16+
17+
Scripts for migrating data from existing databases (only needed for upgrading existing deployments).

database/src/postgres/mod.rs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ mod rpc_server;
22
mod state_indexer;
33
mod tx_indexer;
44

5-
static META_DB_MIGRATOR: sqlx::migrate::Migrator =
6-
sqlx::migrate!("src/postgres/migrations/meta_db");
7-
static SHARD_DB_MIGRATOR: sqlx::migrate::Migrator =
8-
sqlx::migrate!("src/postgres/migrations/shard_db");
9-
105
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Clone, Debug)]
116
struct PageState {
127
pub last_data_key: Option<String>,
@@ -45,31 +40,23 @@ pub struct PostgresDBManager {
4540
impl PostgresDBManager {
4641
async fn create_meta_db_pool(
4742
database_url: &str,
48-
read_only: bool,
4943
max_connections: u32,
5044
) -> anyhow::Result<sqlx::Pool<sqlx::Postgres>> {
5145
let pool = sqlx::postgres::PgPoolOptions::new()
5246
.max_connections(max_connections)
5347
.connect(database_url)
5448
.await?;
55-
if !read_only {
56-
Self::run_migrations(&META_DB_MIGRATOR, &pool).await?;
57-
}
5849
Ok(pool)
5950
}
6051

6152
async fn create_shard_db_pool(
6253
database_url: &str,
63-
read_only: bool,
6454
max_connections: u32,
6555
) -> anyhow::Result<sqlx::Pool<sqlx::Postgres>> {
6656
let pool = sqlx::postgres::PgPoolOptions::new()
6757
.max_connections(max_connections)
6858
.connect(database_url)
6959
.await?;
70-
if !read_only {
71-
Self::run_migrations(&SHARD_DB_MIGRATOR, &pool).await?;
72-
}
7360
Ok(pool)
7461
}
7562

@@ -99,25 +86,13 @@ impl PostgresDBManager {
9986
))?,
10087
})
10188
}
102-
103-
async fn run_migrations(
104-
migrator: &sqlx::migrate::Migrator,
105-
pool: &sqlx::Pool<sqlx::Postgres>,
106-
) -> anyhow::Result<()> {
107-
migrator.run(pool).await?;
108-
Ok(())
109-
}
11089
}
11190

11291
#[async_trait::async_trait]
11392
impl crate::BaseDbManager for PostgresDBManager {
11493
async fn new(config: &configuration::DatabaseConfig) -> anyhow::Result<Box<Self>> {
115-
let meta_db_pool = Self::create_meta_db_pool(
116-
&config.database_url,
117-
config.read_only,
118-
config.max_connections,
119-
)
120-
.await?;
94+
let meta_db_pool =
95+
Self::create_meta_db_pool(&config.database_url, config.max_connections).await?;
12196
let mut shards_pool = std::collections::HashMap::new();
12297
let shard_layout = config
12398
.shard_layout
@@ -128,9 +103,7 @@ impl crate::BaseDbManager for PostgresDBManager {
128103
.shards_config
129104
.get(&shard_id)
130105
.unwrap_or_else(|| panic!("Shard_{shard_id} - database config not found"));
131-
let pool =
132-
Self::create_shard_db_pool(database_url, config.read_only, config.max_connections)
133-
.await?;
106+
let pool = Self::create_shard_db_pool(database_url, config.max_connections).await?;
134107
shards_pool.insert(shard_id, pool);
135108
}
136109
Ok(Box::new(Self {

database/src/postgres/tx_indexer.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,11 @@ use bigdecimal::num_traits::ToPrimitive;
44
use bigdecimal::BigDecimal;
55
use sqlx::QueryBuilder;
66

7-
static META_RECEIPTS_AND_OUTCOMES_MIGRATOR: sqlx::migrate::Migrator =
8-
sqlx::migrate!("src/postgres/migrations/tx_details/receipts_and_outcomes");
9-
static SHARDS_TRANSACTIONS_MIGRATOR: sqlx::migrate::Migrator =
10-
sqlx::migrate!("src/postgres/migrations/tx_details/transactions");
11-
127
#[async_trait]
138
impl crate::base::tx_indexer::TxIndexerDbManager for crate::postgres::PostgresDBManager {
149
async fn create_tx_tables(&self) -> Result<()> {
15-
// Transactions table and partitions on each shard
16-
for pool in self.shards_pool.values() {
17-
SHARDS_TRANSACTIONS_MIGRATOR.run(pool).await?;
18-
}
19-
20-
// Receipts and outcomes tables and partitions in meta_db_pool only
21-
META_RECEIPTS_AND_OUTCOMES_MIGRATOR
22-
.run(&self.meta_db_pool)
23-
.await?;
10+
// For POstgres please read the `README.md` in the `database/src/postgres` directory.
11+
// The tables are created by the migrations, so this method is not needed.
2412
Ok(())
2513
}
2614

0 commit comments

Comments
 (0)