Page MenuHomeDevCentral

D2733.id6929.diff
No OneTemporary

D2733.id6929.diff

diff --git a/Cargo.toml b/Cargo.toml
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,6 +10,10 @@
[dependencies]
+[dependencies.async-scoped]
+version = "~0.7.1"
+features = ["use-tokio"]
+
[dependencies.clap]
version = "~4.0.32"
features = ["derive"]
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -1,3 +1,4 @@
//! Commands for the fantoir-datasource tool.
pub(crate) mod import;
+pub(crate) mod promote;
diff --git a/src/commands/promote/mod.rs b/src/commands/promote/mod.rs
new file mode 100644
--- /dev/null
+++ b/src/commands/promote/mod.rs
@@ -0,0 +1,27 @@
+//! Command to promote a table as the one to use.
+
+use sqlx::PgPool;
+use crate::db::{connect_to_db, run_multiple_queries_groups};
+
+/// Promotes a FANTOIR table as the relevant version to use
+pub async fn promote (fantoir_table: &str, database_url: &str) {
+ let pool = connect_to_db(database_url).await;
+ let queries_groups = get_queries_groups(&pool, fantoir_table);
+
+ run_multiple_queries_groups(&pool, &queries_groups);
+}
+
+/// Determines the groups of queries needed for promotion
+fn get_queries_groups (pool: &PgPool, fantoir_table: &str) -> Vec<String> {
+ let mut queries_groups = vec![
+ include_str!("../../schema/promote/config.sql"),
+ include_str!("../../schema/promote/fantoir_view.sql"),
+ ];
+
+ queries_groups
+ .into_iter()
+ .map(|queries| queries
+ .replace("/*table*/fantoir", fantoir_table)
+ )
+ .collect()
+}
diff --git a/src/db.rs b/src/db.rs
--- a/src/db.rs
+++ b/src/db.rs
@@ -3,6 +3,7 @@
//! This module provides helpers to interact with a PostgreSQL database.
//! Functions expect to work with an executor from sqlx crate.
+use async_scoped::TokioScope;
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;
@@ -69,3 +70,14 @@
.expect("Can't run SQL query.");
}
}
+
+pub fn run_multiple_queries_groups (pool: &PgPool, queries_groups: &Vec<String>) {
+ let n = queries_groups.len();
+ TokioScope::scope_and_block(|scope| {
+ for i in 0..n {
+ scope.spawn(
+ run_multiple_queries(pool, &queries_groups[i])
+ )
+ }
+ });
+}
diff --git a/src/main.rs b/src/main.rs
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,6 +3,7 @@
use clap::{Args, Parser};
use crate::commands::import::import;
+use crate::commands::promote::promote;
mod commands;
mod db;
@@ -15,6 +16,10 @@
/// Import from FANTOIR file generated by the DGFIP
#[command(arg_required_else_help = true)]
Import(ImportArgs),
+
+ /// Promote an imported FANTOIR table as the current FANTOIR table to use
+ #[command(arg_required_else_help = true)]
+ Promote(PromoteArgs),
}
#[derive(Debug, Args)]
@@ -35,6 +40,12 @@
fantoir_table: String,
}
+#[derive(Debug, Args)]
+pub struct PromoteArgs {
+ /// The name of the table to promote
+ fantoir_table: String,
+}
+
#[tokio::main]
async fn main() {
let command = FantoirCommand::parse(); // Will exit if argument is missing or --help/--version provided.
@@ -46,5 +57,8 @@
FantoirCommand::Import(args) => {
import(&args, &database_url).await;
},
+ FantoirCommand::Promote(args) => {
+ promote(&args.fantoir_table, &database_url).await;
+ },
};
}
diff --git a/src/schema/promote/config.sql b/src/schema/promote/config.sql
new file mode 100644
--- /dev/null
+++ b/src/schema/promote/config.sql
@@ -0,0 +1,22 @@
+-- If you provide several instructions, separate those with TWO blank lines.
+--
+-- This schema is compiled as part of the program, as such you need to rebuild
+-- (`cargo build`) the project after any schema modification.
+
+CREATE TABLE IF NOT EXISTS fantoir_config
+(
+ key VARCHAR(63) NOT NULL
+ CONSTRAINT fantoir_config_pk
+ PRIMARY KEY,
+ CONSTRAINT fantoir_config_key_format
+ CHECK ( key ~ '^[a-zA-Z][a-zA-Z0-9_]*$' ),
+ value VARCHAR(255)
+);
+
+
+INSERT INTO fantoir_config
+ (key, value)
+VALUES
+ ('fantoir_table', '/*table*/fantoir')
+ON CONFLICT (key) DO UPDATE
+ SET value = excluded.value;
diff --git a/src/schema/promote/fantoir_view.sql b/src/schema/promote/fantoir_view.sql
new file mode 100644
--- /dev/null
+++ b/src/schema/promote/fantoir_view.sql
@@ -0,0 +1,7 @@
+-- If you provide several instructions, separate those with TWO blank lines.
+--
+-- This schema is compiled as part of the program, as such you need to rebuild
+-- (`cargo build`) the project after any schema modification.
+
+CREATE OR REPLACE VIEW fantoir AS
+ SELECT * FROM /*table*/fantoir;

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 18, 11:40 (19 h, 2 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2250562
Default Alt Text
D2733.id6929.diff (4 KB)

Event Timeline