Skip to content

Commit e4596b4

Browse files
committed
add bin for generating sql
When you add a column in PG on a partitioned table the new column will not be created on existing partitions. New partitions created will have the new column.
1 parent 112b3c3 commit e4596b4

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

be/src/bin/add_col.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use clap::Parser;
2+
use deadpool_postgres::Client;
3+
4+
async fn tables(pg: &Client, prefix: &str) -> Result<Vec<String>, tokio_postgres::Error> {
5+
let rows = pg
6+
.query(
7+
"select relname from pg_class
8+
where relkind = 'r'
9+
and relname like $1 || '%'
10+
order by relname",
11+
&[&prefix],
12+
)
13+
.await?;
14+
Ok(rows.into_iter().map(|r| r.get::<_, String>(0)).collect())
15+
}
16+
17+
async fn alter_table(table_name: &str, new_column: &str) -> String {
18+
format!("ALTER TABLE {table_name} ADD COLUMN {new_column};")
19+
}
20+
21+
#[derive(Clone, Debug, Parser)]
22+
struct Args {
23+
#[arg(
24+
long = "pg-be",
25+
env = "PG_URL",
26+
default_value = "postgres://localhost/be"
27+
)]
28+
pg_url: String,
29+
30+
#[arg(short = 't')]
31+
table_name: String,
32+
33+
#[arg(short = 'c')]
34+
column_desc: String,
35+
}
36+
37+
#[tokio::main]
38+
async fn main() {
39+
let args = Args::parse();
40+
let pool = shared::pg::new_pool(&args.pg_url, 2).expect("pg_be pool");
41+
let pg = pool.get().await.expect("backend pool");
42+
let tables = tables(&pg, &args.table_name).await.unwrap();
43+
for table in &tables {
44+
println!("{}", alter_table(table, &args.column_desc).await);
45+
}
46+
}

0 commit comments

Comments
 (0)