feat(postgres): add cascade table drop option

This commit is contained in:
t8y2 committed 2026-07-06 15:45:47 +08:00
1 parent 8539011f25
commit 745f7f60b0
12 files changed
+155 -28

No files matched your search

+54 -1
View File
@@ -88,6 +88,8 @@ pub struct TableAdminSqlOptions {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub schema: Option<String>,
pub table_name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cascade: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -322,7 +324,29 @@ pub fn build_drop_table_sql(options: TableAdminSqlOptions) -> String {
} else if matches!(options.database_type, Some(DatabaseType::InfluxDb)) {
return format!("DROP MEASUREMENT {};", table);
}
format!("DROP TABLE {table};")
// CASCADE is valid for PostgreSQL-family dialects; keep default RESTRICT behavior elsewhere.
let cascade = if options.cascade.unwrap_or(false) && supports_drop_table_cascade(options.database_type) {
" CASCADE"
} else {
""
};
format!("DROP TABLE {table}{cascade};")
}
fn supports_drop_table_cascade(database_type: Option<DatabaseType>) -> bool {
matches!(
database_type,
Some(
DatabaseType::Postgres
| DatabaseType::Redshift
| DatabaseType::Gaussdb
| DatabaseType::Kwdb
| DatabaseType::Kingbase
| DatabaseType::Highgo
| DatabaseType::Vastbase
| DatabaseType::OpenGauss
)
)
}
pub fn build_drop_table_child_object_sql(options: DropTableChildObjectSqlOptions) -> Result<String, String> {
@@ -1018,8 +1042,27 @@ mod tests {
database_type: Some(DatabaseType::Postgres),
schema: Some("public".to_string()),
table_name: "events".to_string(),
cascade: None,
};
assert_eq!(build_drop_table_sql(options.clone()), "DROP TABLE \"public\".\"events\";");
assert_eq!(
build_drop_table_sql(TableAdminSqlOptions {
database_type: Some(DatabaseType::Postgres),
schema: Some("public".to_string()),
table_name: "events".to_string(),
cascade: Some(true),
}),
"DROP TABLE \"public\".\"events\" CASCADE;"
);
assert_eq!(
build_drop_table_sql(TableAdminSqlOptions {
database_type: Some(DatabaseType::Mysql),
schema: None,
table_name: "events".to_string(),
cascade: Some(true),
}),
"DROP TABLE `events`;"
);
assert_eq!(build_empty_table_sql(options.clone()), "DELETE FROM \"public\".\"events\";");
assert_eq!(build_truncate_table_sql(options), "TRUNCATE TABLE \"public\".\"events\";");
assert_eq!(
@@ -1027,6 +1070,7 @@ mod tests {
database_type: Some(DatabaseType::ClickHouse),
schema: None,
table_name: "PresetSubjectInfo".to_string(),
cascade: None,
}),
"ALTER TABLE `PresetSubjectInfo` DELETE WHERE 1 = 1;"
);
@@ -1035,6 +1079,7 @@ mod tests {
database_type: Some(DatabaseType::ClickHouse),
schema: None,
table_name: "PresetSubjectInfo".to_string(),
cascade: None,
}),
"TRUNCATE TABLE `PresetSubjectInfo`;"
);
@@ -1043,6 +1088,7 @@ mod tests {
database_type: Some(DatabaseType::Bigquery),
schema: None,
table_name: "events".to_string(),
cascade: None,
}),
"DELETE FROM `events` WHERE TRUE;"
);
@@ -1051,6 +1097,7 @@ mod tests {
database_type: Some(DatabaseType::Cassandra),
schema: None,
table_name: "events".to_string(),
cascade: None,
}),
"TRUNCATE TABLE \"events\";"
);
@@ -1059,6 +1106,7 @@ mod tests {
database_type: Some(DatabaseType::DuckDb),
schema: None,
table_name: "events".to_string(),
cascade: None,
}),
"DELETE FROM \"events\";"
);
@@ -1067,6 +1115,7 @@ mod tests {
database_type: Some(DatabaseType::Iotdb),
schema: Some("root.test".to_string()),
table_name: "DCU_101".to_string(),
cascade: None,
}),
"DELETE TIMESERIES root.test.DCU_101.*;"
);
@@ -1075,6 +1124,7 @@ mod tests {
database_type: Some(DatabaseType::Iotdb),
schema: Some("root.test".to_string()),
table_name: "root.test.DCU_101".to_string(),
cascade: None,
}),
"DELETE FROM root.test.DCU_101.*;"
);
@@ -1083,6 +1133,7 @@ mod tests {
database_type: Some(DatabaseType::Iotdb),
schema: Some("root.test".to_string()),
table_name: "DCU_101".to_string(),
cascade: None,
}),
"DELETE FROM root.test.DCU_101.*;"
);
@@ -1092,6 +1143,7 @@ mod tests {
database_type: Some(DatabaseType::Questdb),
schema: None,
table_name: "table_sample".to_string(),
cascade: None,
}),
"TRUNCATE TABLE `table_sample`;"
);
@@ -1100,6 +1152,7 @@ mod tests {
database_type: Some(DatabaseType::Questdb),
schema: None,
table_name: "table_sample".to_string(),
cascade: None,
}),
"TRUNCATE TABLE `table_sample`;"
);