migration is now holding the target table information

This commit is contained in:
filipriec
2025-03-15 13:53:34 +01:00
parent 79f57ddaed
commit d87cc3b5b9
4 changed files with 13 additions and 20 deletions

View File

@@ -2,11 +2,10 @@
CREATE TABLE table_scripts ( CREATE TABLE table_scripts (
id BIGSERIAL PRIMARY KEY, id BIGSERIAL PRIMARY KEY,
table_definitions_id BIGINT NOT NULL REFERENCES table_definitions(id), table_definitions_id BIGINT NOT NULL REFERENCES table_definitions(id),
target_table TEXT NOT NULL,
target_column TEXT NOT NULL, target_column TEXT NOT NULL,
target_column_type TEXT NOT NULL, target_column_type TEXT NOT NULL,
script TEXT NOT NULL, script TEXT NOT NULL,
source_tables TEXT[],
source_columns TEXT[],
description TEXT, description TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
profile_id BIGINT NOT NULL REFERENCES profiles(id) DEFAULT 1, profile_id BIGINT NOT NULL REFERENCES profiles(id) DEFAULT 1,

View File

@@ -31,7 +31,7 @@ pub fn execute_script(
let mut vm = Engine::new(); let mut vm = Engine::new();
let data = current_data.clone(); let data = current_data.clone();
vm.register_fn("get_current_column", move |column: String| { vm.register_fn("steel_get_column", move |column: String| {
data.get(&column) data.get(&column)
.map(|s| SteelVal::StringV(s.clone().into())) .map(|s| SteelVal::StringV(s.clone().into()))
.ok_or_else(|| SteelVal::StringV(format!("Column {} not found", column).into())) .ok_or_else(|| SteelVal::StringV(format!("Column {} not found", column).into()))
@@ -67,14 +67,14 @@ pub fn execute_script_with_sql(
let handle = Handle::current(); let handle = Handle::current();
let data = current_data.clone(); let data = current_data.clone();
vm.register_fn("get_current_column", move |column: String| { vm.register_fn("steel_get_column", move |column: String| {
data.get(&column) data.get(&column)
.map(|s| SteelVal::StringV(s.clone().into())) .map(|s| SteelVal::StringV(s.clone().into()))
.ok_or_else(|| SteelVal::StringV(format!("Column {} not found", column).into())) .ok_or_else(|| SteelVal::StringV(format!("Column {} not found", column).into()))
}); });
let pool = db_pool.clone(); let pool = db_pool.clone();
vm.register_fn("query_sql_from_steel", move |query: String| { vm.register_fn("steel_query_sql", move |query: String| {
let pool = pool.clone(); let pool = pool.clone();
let query = query.clone(); let query = query.clone();
let handle = handle.clone(); let handle = handle.clone();

View File

@@ -35,12 +35,12 @@ impl SyntaxParser {
// Process basic column access // Process basic column access
transformed = self.column_access_re.replace_all(&transformed, |caps: &regex::Captures| { transformed = self.column_access_re.replace_all(&transformed, |caps: &regex::Captures| {
format!("(get_current_column \"{}\")", &caps[1]) format!("(steel_get_column \"{}\")", &caps[1])
}).to_string(); }).to_string();
// Process SQL integration // Process SQL integration
transformed = self.sql_integration_re.replace_all(&transformed, |caps: &regex::Captures| { transformed = self.sql_integration_re.replace_all(&transformed, |caps: &regex::Captures| {
format!("(query_sql_from_steel \"{}\")", &caps[2]) format!("(steel_query_sql \"{}\")", &caps[2])
}).to_string(); }).to_string();
transformed transformed

View File

@@ -19,7 +19,6 @@ fn validate_target_column(
let columns: Vec<String> = serde_json::from_value(table_columns.clone()) let columns: Vec<String> = serde_json::from_value(table_columns.clone())
.map_err(|e| format!("Invalid column data: {}", e))?; .map_err(|e| format!("Invalid column data: {}", e))?;
// Extract column name and type
let column_info: Vec<(&str, &str)> = columns let column_info: Vec<(&str, &str)> = columns
.iter() .iter()
.filter_map(|c| { .filter_map(|c| {
@@ -30,7 +29,6 @@ fn validate_target_column(
}) })
.collect(); .collect();
// Find the target column
let column_type = column_info let column_type = column_info
.iter() .iter()
.find(|(name, _)| *name == target) .find(|(name, _)| *name == target)
@@ -65,26 +63,22 @@ pub async fn post_table_script(
// Parse and transform the script // Parse and transform the script
let parser = SyntaxParser::new(); let parser = SyntaxParser::new();
let parsed_script = parser.parse(&request.script); let parsed_script = parser.parse(&request.script);
// Extract dependencies
let (source_tables, source_columns) = parser.extract_dependencies(&request.script);
// Store script in database with column type and profile_id // Store script in database with automatic target_table
let script_record = sqlx::query!( let script_record = sqlx::query!(
r#"INSERT INTO table_scripts r#"INSERT INTO table_scripts
(table_definitions_id, target_column, target_column_type, (table_definitions_id, target_table, target_column,
script, source_tables, source_columns, description, profile_id) target_column_type, script, description, profile_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id"#, RETURNING id"#,
request.table_definition_id, request.table_definition_id,
table_def.table_name, // Auto-populate target_table from table_def
request.target_column, request.target_column,
column_type, column_type,
parsed_script, // Store transformed script parsed_script,
&Vec::from_iter(source_tables),
&Vec::from_iter(source_columns),
request.description, request.description,
table_def.profile_id table_def.profile_id
) )
.fetch_one(db_pool) .fetch_one(db_pool)
.await .await
.map_err(|e| match e { .map_err(|e| match e {