fix(postgres): distinguish overloaded routines
This commit is contained in:
1 parent
823b4297d8
commit
a3f3cadb39
14 files changed
+146
-48
No files matched your search
@@ -1359,6 +1359,7 @@ pub async fn export_database_sql_core(
|
||||
&request.schema,
|
||||
view_name,
|
||||
crate::db::ObjectSourceKind::View,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -1402,6 +1403,7 @@ pub async fn export_database_sql_core(
|
||||
&request.schema,
|
||||
proc_name,
|
||||
crate::db::ObjectSourceKind::Procedure,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -1448,6 +1450,7 @@ pub async fn export_database_sql_core(
|
||||
&request.schema,
|
||||
func_name,
|
||||
crate::db::ObjectSourceKind::Function,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
|
||||
@@ -1808,7 +1808,7 @@ fn list_object_routines_sql(include_timestamps: bool, has_proc_prokind: bool, ha
|
||||
THEN pg_xact_commit_timestamp(p.xmin)::text END AS updated_at, \
|
||||
NULL::text AS parent_schema, \
|
||||
NULL::text AS parent_name, \
|
||||
pg_get_function_arguments(p.oid) AS signature, \
|
||||
pg_get_function_identity_arguments(p.oid) AS signature, \
|
||||
CASE WHEN p.prokind = 'p' OR p.prosp THEN 2 ELSE 3 END AS sort_order \
|
||||
FROM pg_catalog.pg_proc p \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
|
||||
@@ -1822,7 +1822,7 @@ fn list_object_routines_sql(include_timestamps: bool, has_proc_prokind: bool, ha
|
||||
NULL::text AS updated_at, \
|
||||
NULL::text AS parent_schema, \
|
||||
NULL::text AS parent_name, \
|
||||
pg_get_function_arguments(p.oid) AS signature, \
|
||||
pg_get_function_identity_arguments(p.oid) AS signature, \
|
||||
CASE WHEN p.prokind = 'p' OR p.prosp THEN 2 ELSE 3 END AS sort_order \
|
||||
FROM pg_catalog.pg_proc p \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
|
||||
@@ -1839,7 +1839,7 @@ fn list_object_routines_sql(include_timestamps: bool, has_proc_prokind: bool, ha
|
||||
THEN pg_xact_commit_timestamp(p.xmin)::text END AS updated_at, \
|
||||
NULL::text AS parent_schema, \
|
||||
NULL::text AS parent_name, \
|
||||
pg_get_function_arguments(p.oid) AS signature, \
|
||||
pg_get_function_identity_arguments(p.oid) AS signature, \
|
||||
CASE p.prokind WHEN 'p' THEN 2 ELSE 3 END AS sort_order \
|
||||
FROM pg_catalog.pg_proc p \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
|
||||
@@ -1853,7 +1853,7 @@ fn list_object_routines_sql(include_timestamps: bool, has_proc_prokind: bool, ha
|
||||
NULL::text AS updated_at, \
|
||||
NULL::text AS parent_schema, \
|
||||
NULL::text AS parent_name, \
|
||||
pg_get_function_arguments(p.oid) AS signature, \
|
||||
pg_get_function_identity_arguments(p.oid) AS signature, \
|
||||
CASE p.prokind WHEN 'p' THEN 2 ELSE 3 END AS sort_order \
|
||||
FROM pg_catalog.pg_proc p \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
|
||||
@@ -1870,7 +1870,7 @@ fn list_object_routines_sql(include_timestamps: bool, has_proc_prokind: bool, ha
|
||||
THEN pg_xact_commit_timestamp(p.xmin)::text END AS updated_at, \
|
||||
NULL::text AS parent_schema, \
|
||||
NULL::text AS parent_name, \
|
||||
pg_get_function_arguments(p.oid) AS signature, \
|
||||
pg_get_function_identity_arguments(p.oid) AS signature, \
|
||||
CASE WHEN p.prosp THEN 2 ELSE 3 END AS sort_order \
|
||||
FROM pg_catalog.pg_proc p \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
|
||||
@@ -1884,7 +1884,7 @@ fn list_object_routines_sql(include_timestamps: bool, has_proc_prokind: bool, ha
|
||||
NULL::text AS updated_at, \
|
||||
NULL::text AS parent_schema, \
|
||||
NULL::text AS parent_name, \
|
||||
pg_get_function_arguments(p.oid) AS signature, \
|
||||
pg_get_function_identity_arguments(p.oid) AS signature, \
|
||||
CASE WHEN p.prosp THEN 2 ELSE 3 END AS sort_order \
|
||||
FROM pg_catalog.pg_proc p \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
|
||||
@@ -1900,7 +1900,7 @@ fn list_object_routines_sql(include_timestamps: bool, has_proc_prokind: bool, ha
|
||||
THEN pg_xact_commit_timestamp(p.xmin)::text END AS updated_at, \
|
||||
NULL::text AS parent_schema, \
|
||||
NULL::text AS parent_name, \
|
||||
pg_get_function_arguments(p.oid) AS signature, \
|
||||
pg_get_function_identity_arguments(p.oid) AS signature, \
|
||||
3 AS sort_order \
|
||||
FROM pg_catalog.pg_proc p \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
|
||||
@@ -1914,7 +1914,7 @@ fn list_object_routines_sql(include_timestamps: bool, has_proc_prokind: bool, ha
|
||||
NULL::text AS updated_at, \
|
||||
NULL::text AS parent_schema, \
|
||||
NULL::text AS parent_name, \
|
||||
pg_get_function_arguments(p.oid) AS signature, \
|
||||
pg_get_function_identity_arguments(p.oid) AS signature, \
|
||||
3 AS sort_order \
|
||||
FROM pg_catalog.pg_proc p \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
|
||||
@@ -3899,7 +3899,7 @@ mod tests {
|
||||
assert!(sql.contains("parent_schema"));
|
||||
assert!(sql.contains("parent_name"));
|
||||
assert!(sql.contains("NULL::text AS signature"));
|
||||
assert!(sql.contains("pg_get_function_arguments(p.oid) AS signature"));
|
||||
assert!(sql.contains("pg_get_function_identity_arguments(p.oid) AS signature"));
|
||||
assert!(sql.contains("pc.relkind = 'p'"));
|
||||
assert!(sql.contains("pg_stat_file"));
|
||||
assert!(sql.contains("pg_xact_commit_timestamp"));
|
||||
@@ -3946,7 +3946,7 @@ mod tests {
|
||||
assert!(!sql.contains("p.prosp"));
|
||||
assert!(sql.contains("NOT p.proisagg"));
|
||||
assert!(sql.contains("NOT p.proiswindow"));
|
||||
assert!(sql.contains("pg_get_function_arguments(p.oid) AS signature"));
|
||||
assert!(sql.contains("pg_get_function_identity_arguments(p.oid) AS signature"));
|
||||
assert!(sql.contains("'FUNCTION' AS object_type"));
|
||||
assert!(!sql.contains("'PROCEDURE'"));
|
||||
}
|
||||
@@ -3959,7 +3959,7 @@ mod tests {
|
||||
assert!(sql.contains("CASE WHEN p.prosp THEN 2 ELSE 3 END AS sort_order"));
|
||||
assert!(sql.contains("NOT p.proisagg"));
|
||||
assert!(sql.contains("NOT p.proiswindow"));
|
||||
assert!(sql.contains("pg_get_function_arguments(p.oid) AS signature"));
|
||||
assert!(sql.contains("pg_get_function_identity_arguments(p.oid) AS signature"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -3970,7 +3970,7 @@ mod tests {
|
||||
);
|
||||
assert!(sql.contains("CASE WHEN p.prokind = 'p' OR p.prosp THEN 2 ELSE 3 END AS sort_order"));
|
||||
assert!(sql.contains("p.prokind IN ('p','f') OR p.prosp"));
|
||||
assert!(sql.contains("pg_get_function_arguments(p.oid) AS signature"));
|
||||
assert!(sql.contains("pg_get_function_identity_arguments(p.oid) AS signature"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -78,6 +78,8 @@ pub struct DropObjectSqlOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub schema: Option<String>,
|
||||
pub name: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub signature: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
@@ -311,10 +313,18 @@ pub fn build_create_user_sql(username: &str, password: &str, tablespace: &str) -
|
||||
}
|
||||
|
||||
pub fn build_drop_object_sql(options: DropObjectSqlOptions) -> String {
|
||||
let signature = if matches!(options.database_type, Some(DatabaseType::Postgres))
|
||||
&& matches!(options.object_type, DatabaseObjectType::Function | DatabaseObjectType::Procedure)
|
||||
{
|
||||
options.signature.as_deref().map(|value| format!("({value})")).unwrap_or_default()
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
format!(
|
||||
"DROP {} {};",
|
||||
"DROP {} {}{};",
|
||||
object_type_keyword(options.object_type),
|
||||
qualified_name(options.database_type, options.schema.as_deref(), &options.name)
|
||||
qualified_name(options.database_type, options.schema.as_deref(), &options.name),
|
||||
signature
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1206,9 +1216,20 @@ mod tests {
|
||||
object_type: DatabaseObjectType::Procedure,
|
||||
schema: Some("dbo".to_string()),
|
||||
name: "refresh_cache".to_string(),
|
||||
signature: None,
|
||||
}),
|
||||
"DROP PROCEDURE [dbo].[refresh_cache];"
|
||||
);
|
||||
assert_eq!(
|
||||
build_drop_object_sql(DropObjectSqlOptions {
|
||||
database_type: Some(DatabaseType::Postgres),
|
||||
object_type: DatabaseObjectType::Function,
|
||||
schema: Some("public".to_string()),
|
||||
name: "calc".to_string(),
|
||||
signature: Some("integer, integer".to_string()),
|
||||
}),
|
||||
"DROP FUNCTION \"public\".\"calc\"(integer, integer);"
|
||||
);
|
||||
assert_eq!(
|
||||
build_drop_database_sql(DatabaseNameSqlOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
|
||||
@@ -4526,7 +4526,8 @@ pub async fn get_table_ddl_core(
|
||||
}
|
||||
if matches!(object_type, Some(db::ObjectSourceKind::View)) {
|
||||
let source =
|
||||
get_object_source_core(state, connection_id, database, schema, table, db::ObjectSourceKind::View).await?;
|
||||
get_object_source_core(state, connection_id, database, schema, table, db::ObjectSourceKind::View, None)
|
||||
.await?;
|
||||
let database_type = connection_config(state, connection_id).await.map(|config| config.db_type);
|
||||
return Ok(crate::object_source_sql::build_view_ddl_sql(crate::object_source_sql::BuildViewDdlInput {
|
||||
database_type,
|
||||
@@ -4543,6 +4544,7 @@ pub async fn get_table_ddl_core(
|
||||
schema,
|
||||
table,
|
||||
db::ObjectSourceKind::MaterializedView,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
return Ok(source.source);
|
||||
@@ -4801,12 +4803,22 @@ pub fn sqlserver_object_source_sql(schema: &str, name: &str, kind: &db::ObjectSo
|
||||
)
|
||||
}
|
||||
|
||||
pub fn postgres_object_source_sql(schema: &str, name: &str, kind: &db::ObjectSourceKind) -> String {
|
||||
postgres_object_source_sql_inner(schema, name, kind, true)
|
||||
pub fn postgres_object_source_sql(
|
||||
schema: &str,
|
||||
name: &str,
|
||||
kind: &db::ObjectSourceKind,
|
||||
signature: Option<&str>,
|
||||
) -> String {
|
||||
postgres_object_source_sql_inner(schema, name, kind, signature, true)
|
||||
}
|
||||
|
||||
fn postgres_object_source_sql_without_relispopulated(schema: &str, name: &str, kind: &db::ObjectSourceKind) -> String {
|
||||
postgres_object_source_sql_inner(schema, name, kind, false)
|
||||
fn postgres_object_source_sql_without_relispopulated(
|
||||
schema: &str,
|
||||
name: &str,
|
||||
kind: &db::ObjectSourceKind,
|
||||
signature: Option<&str>,
|
||||
) -> String {
|
||||
postgres_object_source_sql_inner(schema, name, kind, signature, false)
|
||||
}
|
||||
|
||||
fn postgres_function_object_source_sql_without_prokind(schema: &str, name: &str) -> String {
|
||||
@@ -4825,6 +4837,7 @@ fn postgres_object_source_sql_inner(
|
||||
schema: &str,
|
||||
name: &str,
|
||||
kind: &db::ObjectSourceKind,
|
||||
signature: Option<&str>,
|
||||
include_relispopulated: bool,
|
||||
) -> String {
|
||||
match kind {
|
||||
@@ -4856,15 +4869,19 @@ fn postgres_object_source_sql_inner(
|
||||
}
|
||||
db::ObjectSourceKind::Procedure | db::ObjectSourceKind::Function => {
|
||||
let prokind = if matches!(kind, db::ObjectSourceKind::Procedure) { "p" } else { "f" };
|
||||
let signature_filter = signature
|
||||
.map(|value| format!(" AND pg_get_function_identity_arguments(p.oid) = {}", sql_string(value)))
|
||||
.unwrap_or_default();
|
||||
format!(
|
||||
"SELECT pg_get_functiondef(p.oid) \
|
||||
FROM pg_proc p \
|
||||
JOIN pg_namespace n ON n.oid = p.pronamespace \
|
||||
WHERE n.nspname = {} AND p.proname = {} AND p.prokind = '{}' \
|
||||
WHERE n.nspname = {} AND p.proname = {} AND p.prokind = '{}'{} \
|
||||
ORDER BY p.oid LIMIT 1",
|
||||
sql_string(schema),
|
||||
sql_string(name),
|
||||
prokind
|
||||
prokind,
|
||||
signature_filter
|
||||
)
|
||||
}
|
||||
db::ObjectSourceKind::Sequence => {
|
||||
@@ -4988,9 +5005,10 @@ pub async fn get_object_source_core(
|
||||
schema: &str,
|
||||
name: &str,
|
||||
object_type: db::ObjectSourceKind,
|
||||
signature: Option<&str>,
|
||||
) -> Result<db::ObjectSource, String> {
|
||||
retry_metadata_connection(state, connection_id, Some(database), || {
|
||||
get_object_source_once(state, connection_id, database, schema, name, object_type.clone())
|
||||
get_object_source_once(state, connection_id, database, schema, name, object_type.clone(), signature)
|
||||
})
|
||||
.await
|
||||
}
|
||||
@@ -5002,6 +5020,7 @@ async fn get_object_source_once(
|
||||
schema: &str,
|
||||
name: &str,
|
||||
object_type: db::ObjectSourceKind,
|
||||
signature: Option<&str>,
|
||||
) -> Result<db::ObjectSource, String> {
|
||||
let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
|
||||
let db_config = connection_config(state, connection_id).await;
|
||||
@@ -5069,7 +5088,7 @@ async fn get_object_source_once(
|
||||
// only view
|
||||
db::questdb::questdb_object_source(pool, name).await?
|
||||
}
|
||||
PoolKind::Postgres(pool) => postgres_object_source(pool, schema, name, &object_type).await?,
|
||||
PoolKind::Postgres(pool) => postgres_object_source(pool, schema, name, &object_type, signature).await?,
|
||||
PoolKind::Sqlite(pool) => first_string_cell(
|
||||
db::sqlite::execute_query(pool, &sqlite_object_source_sql(name, &object_type)).await?,
|
||||
)?,
|
||||
@@ -5399,15 +5418,16 @@ async fn postgres_object_source(
|
||||
schema: &str,
|
||||
name: &str,
|
||||
object_type: &db::ObjectSourceKind,
|
||||
signature: Option<&str>,
|
||||
) -> Result<String, String> {
|
||||
let sql = postgres_object_source_sql(schema, name, object_type);
|
||||
let sql = postgres_object_source_sql(schema, name, object_type, signature);
|
||||
match db::postgres::execute_query(pool, &sql).await.and_then(first_string_cell) {
|
||||
Ok(source) => Ok(source),
|
||||
Err(primary_err)
|
||||
if postgres_missing_relispopulated_error(&primary_err)
|
||||
&& matches!(object_type, db::ObjectSourceKind::View | db::ObjectSourceKind::MaterializedView) =>
|
||||
{
|
||||
let fallback_sql = postgres_object_source_sql_without_relispopulated(schema, name, object_type);
|
||||
let fallback_sql = postgres_object_source_sql_without_relispopulated(schema, name, object_type, signature);
|
||||
db::postgres::execute_query(pool, &fallback_sql)
|
||||
.await
|
||||
.and_then(first_string_cell)
|
||||
@@ -5465,7 +5485,7 @@ mod object_source_tests {
|
||||
|
||||
#[test]
|
||||
fn builds_postgres_object_source_sql_for_views_and_functions() {
|
||||
let view_sql = postgres_object_source_sql("public", "active_users", &ObjectSourceKind::View);
|
||||
let view_sql = postgres_object_source_sql("public", "active_users", &ObjectSourceKind::View, None);
|
||||
|
||||
assert!(view_sql.contains("CREATE MATERIALIZED VIEW"));
|
||||
assert!(view_sql.contains("CREATE OR REPLACE VIEW"));
|
||||
@@ -5474,9 +5494,14 @@ mod object_source_tests {
|
||||
assert!(view_sql.contains("c.relname = 'active_users'"));
|
||||
|
||||
assert_eq!(
|
||||
postgres_object_source_sql("public", "recalc_score", &ObjectSourceKind::Function),
|
||||
postgres_object_source_sql("public", "recalc_score", &ObjectSourceKind::Function, None),
|
||||
"SELECT pg_get_functiondef(p.oid) FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace WHERE n.nspname = 'public' AND p.proname = 'recalc_score' AND p.prokind = 'f' ORDER BY p.oid LIMIT 1"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
postgres_object_source_sql("public", "recalc_score", &ObjectSourceKind::Function, Some("integer, integer")),
|
||||
"SELECT pg_get_functiondef(p.oid) FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace WHERE n.nspname = 'public' AND p.proname = 'recalc_score' AND p.prokind = 'f' AND pg_get_function_identity_arguments(p.oid) = 'integer, integer' ORDER BY p.oid LIMIT 1"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -5485,6 +5510,7 @@ mod object_source_tests {
|
||||
"public",
|
||||
"active_users",
|
||||
&ObjectSourceKind::MaterializedView,
|
||||
None,
|
||||
);
|
||||
|
||||
assert!(sql.contains("CREATE MATERIALIZED VIEW"));
|
||||
@@ -5504,7 +5530,7 @@ mod object_source_tests {
|
||||
|
||||
#[test]
|
||||
fn keeps_legacy_materialized_viewdef_when_it_already_contains_create_statement() {
|
||||
let sql = postgres_object_source_sql("public", "active_users", &ObjectSourceKind::MaterializedView);
|
||||
let sql = postgres_object_source_sql("public", "active_users", &ObjectSourceKind::MaterializedView, None);
|
||||
|
||||
assert!(
|
||||
sql.contains(
|
||||
@@ -5531,7 +5557,7 @@ mod object_source_tests {
|
||||
|
||||
#[test]
|
||||
fn builds_postgres_view_source_sql_without_regclass_cast() {
|
||||
let sql = postgres_object_source_sql("tenant's schema", "active users", &ObjectSourceKind::View);
|
||||
let sql = postgres_object_source_sql("tenant's schema", "active users", &ObjectSourceKind::View, None);
|
||||
|
||||
assert!(!sql.contains("::regclass"));
|
||||
assert!(sql.contains("pg_get_viewdef(c.oid, 0)"));
|
||||
|
||||
Reference in new issue
Block a user