Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(transfer): drop opengauss storage options for pg targets

dienaso25539c6e245d
diff --git a/crates/dbx-core/src/data/transfer.rs b/crates/dbx-core/src/data/transfer.rs
index 731f692331..4871456cce 100644
--- a/crates/dbx-core/src/data/transfer.rs
+++ b/crates/dbx-core/src/data/transfer.rs
@@ -5373,6 +5373,369 @@ fn strip_dameng_storage_clauses(sql: &str) -> String {
})
}
+/// Table storage options openGauss's `pg_get_tabledef()` appends but PostgreSQL and the
+/// other PG-compatible targets do not accept (#9037). The values are openGauss-specific
+/// as well — `compression` takes `no|low|middle|high` where PostgreSQL takes
+/// `pglz|lz4|none` — so the whole option goes, not just an unrecognized value. Kept as a
+/// deny-list: every option not listed here (`fillfactor`, `parallel_workers`,
+/// `autovacuum_*`, `toast.autovacuum_*`, ...) is a PostgreSQL table reloption and must
+/// survive verbatim.
+///
+/// The list follows openGauss 5.0's documented table storage parameters. It covers the
+/// row-store page-compression family (`compresstype`, `compress_level`,
+/// `compress_byte_convert`, `compress_diff_convert`), the UStore options (`init_td`,
+/// `hasuids`), and the column-store family (`compresslevel` is the column-store
+/// sub-level; `compress_level` is the row-store algorithm level). PostgreSQL 16 rejects
+/// every one of these names as a table storage parameter, so dropping them cannot remove
+/// a working option from a PG target.
+const OPENGAUSS_ONLY_TABLE_STORAGE_OPTIONS: &[&str] = &[
+ "orientation",
+ "compression",
+ "storage_type",
+ "segment",
+ "version",
+ "max_batchrow",
+ "partial_cluster_rows",
+ "enable_delta_store",
+ "compress_level",
+ "compresslevel",
+ "compress_chunk_size",
+ "compress_prealloc_chunks",
+ "compress_chunk_threshold",
+ "compresstype",
+ "compress_byte_convert",
+ "compress_diff_convert",
+ "deltarow_threshold",
+ "init_td",
+ "hasuids",
+];
+
+/// Whether a reused source DDL came from openGauss's `pg_get_tabledef()`. A plain
+/// PostgreSQL connection profiled as openGauss/GaussDB reads its table DDL through that
+/// same function (`schema::opengauss_table_ddl`), so the profile counts exactly like the
+/// database type — the same grouping `is_opengauss_family_config` uses.
+fn source_ddl_carries_opengauss_table_options(
+ source_db_type: &DatabaseType,
+ source_driver_profile: Option<&str>,
+) -> bool {
+ matches!(source_db_type, DatabaseType::OpenGauss | DatabaseType::Gaussdb)
+ || matches!(source_driver_profile, Some("opengauss" | "gaussdb"))
+}
+
+/// Targets that speak the openGauss dialect and therefore accept its table storage
+/// options. Vastbase is openGauss-derived like GaussDB (`table_structure_sql::partitions`
+/// groups the three for `pg_partition`), so the clause is kept for it too. Every other
+/// target rejects the options, and stripping is the safer default for one we have not
+/// verified: an unlisted openGauss-derived engine loses at most the storage kind, whereas
+/// leaving the clause in place fails the whole `CREATE TABLE`.
+///
+/// Not covered here: a PG-compatible target that takes no PostgreSQL-style `WITH (...)`
+/// table clause at all (Redshift), where even the surviving options are invalid. That gap
+/// is pre-existing for every reused DDL aimed at such a target, not something this strip
+/// introduces, so it is left to a separate fix.
+fn target_accepts_opengauss_table_options(target_db_type: &DatabaseType, target_driver_profile: Option<&str>) -> bool {
+ matches!(target_db_type, DatabaseType::OpenGauss | DatabaseType::Gaussdb | DatabaseType::Vastbase)
+ || matches!(target_driver_profile, Some("opengauss" | "gaussdb"))
+}
+
+fn strips_opengauss_table_storage_options(
+ source_db_type: &DatabaseType,
+ source_driver_profile: Option<&str>,
+ target_db_type: &DatabaseType,
+ target_driver_profile: Option<&str>,
+) -> bool {
+ // Only a PostgreSQL-family target can reach the reuse path that carries this DDL
+ // (`rewrite_transfer_source_table_ddl`'s first branch), so the gate is scoped to the
+ // family instead of claiming to rewrite MySQL- or Oracle-target DDL it never sees.
+ is_postgres_family_target(target_db_type)
+ && source_ddl_carries_opengauss_table_options(source_db_type, source_driver_profile)
+ && !target_accepts_opengauss_table_options(target_db_type, target_driver_profile)
+}
+
+fn is_sql_identifier_byte(byte: u8) -> bool {
+ byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'$' | b'#')
+}
+
+fn prefix_is_standalone(bytes: &[u8], start: usize) -> bool {
+ start == 0 || !is_sql_identifier_byte(bytes[start - 1])
+}
+
+fn scan_single_quoted(bytes: &[u8], quote: usize, backslash_escapes: bool) -> usize {
+ let mut index = quote + 1;
+ while index < bytes.len() {
+ if backslash_escapes && bytes[index] == b'\\' && index + 1 < bytes.len() {
+ index += 2;
+ continue;
+ }
+ if bytes[index] == b'\'' {
+ if bytes.get(index + 1) == Some(&b'\'') {
+ index += 2;
+ continue;
+ }
+ return index + 1;
+ }
+ index += 1;
+ }
+ bytes.len()
+}
+
+fn scan_double_quoted(bytes: &[u8], quote: usize) -> usize {
+ let mut index = quote + 1;
+ while index < bytes.len() {
+ if bytes[index] == b'"' {
+ if bytes.get(index + 1) == Some(&b'"') {
+ index += 2;
+ continue;
+ }
+ return index + 1;
+ }
+ index += 1;
+ }
+ bytes.len()
+}
+
+fn scan_dollar_quoted(bytes: &[u8], start: usize) -> Option<usize> {
+ let mut index = start + 1;
+ if bytes.get(index).is_some_and(|byte| byte.is_ascii_digit()) {
+ return None; // `$1` is a positional parameter, not a dollar quote
+ }
+ while bytes.get(index).is_some_and(|byte| byte.is_ascii_alphanumeric() || *byte == b'_') {
+ index += 1;
+ }
+ if bytes.get(index) != Some(&b'$') {
+ return None;
+ }
+ let tag = &bytes[start..=index];
+ let mut search = index + 1;
+ while search + tag.len() <= bytes.len() {
+ if &bytes[search..search + tag.len()] == tag {
+ return Some(search + tag.len());
+ }
+ search += 1;
+ }
+ Some(bytes.len()) // unterminated: keep the rest opaque rather than rewriting it
+}
+
+fn scan_line_comment(bytes: &[u8], start: usize) -> usize {
+ let mut index = start + 2;
+ while index < bytes.len() && bytes[index] != b'\n' {
+ index += 1;
+ }
+ index
+}
+
+fn scan_block_comment(bytes: &[u8], start: usize) -> usize {
+ let mut depth = 1usize;
+ let mut index = start + 2;
+ while index < bytes.len() {
+ if bytes[index] == b'/' && bytes.get(index + 1) == Some(&b'*') {
+ depth += 1;
+ index += 2;
+ } else if bytes[index] == b'*' && bytes.get(index + 1) == Some(&b'/') {
+ depth -= 1;
+ index += 2;
+ if depth == 0 {
+ return index;
+ }
+ } else {
+ index += 1;
+ }
+ }
+ bytes.len()
+}
+
+/// Returns the end (exclusive) of a PostgreSQL string literal, quoted identifier, dollar
+/// quote, or comment starting at `start`, or `None` when `start` is ordinary code.
+///
+/// openGauss reads its DDL with PostgreSQL's lexer rules, where `"tenant's"` is one quoted
+/// identifier and `'C:\'` is a complete string. The shared
+/// [`sql_non_code_spans_with_mysql_identifiers`] scanner assumes MySQL escapes and would
+/// treat the apostrophe inside the identifier as the start of a string, shifting every
+/// later span.
+fn pg_non_code_span_end(sql: &str, start: usize) -> Option<usize> {
+ let bytes = sql.as_bytes();
+ let byte = *bytes.get(start)?;
+ match byte {
+ b'E' | b'e' if bytes.get(start + 1) == Some(&b'\'') && prefix_is_standalone(bytes, start) => {
+ Some(scan_single_quoted(bytes, start + 1, true))
+ }
+ b'U' | b'u'
+ if bytes.get(start + 1) == Some(&b'&')
+ && bytes.get(start + 2) == Some(&b'\'')
+ && prefix_is_standalone(bytes, start) =>
+ {
+ Some(scan_single_quoted(bytes, start + 2, true))
+ }
+ b'B' | b'b' | b'X' | b'x' if bytes.get(start + 1) == Some(&b'\'') && prefix_is_standalone(bytes, start) => {
+ Some(scan_single_quoted(bytes, start + 1, false))
+ }
+ b'\'' => Some(scan_single_quoted(bytes, start, false)),
+ b'"' => Some(scan_double_quoted(bytes, start)),
+ b'$' => scan_dollar_quoted(bytes, start),
+ b'-' if bytes.get(start + 1) == Some(&b'-') => Some(scan_line_comment(bytes, start)),
+ b'/' if bytes.get(start + 1) == Some(&b'*') => Some(scan_block_comment(bytes, start)),
+ _ => None,
+ }
+}
+
+fn is_with_keyword_at(bytes: &[u8], start: usize) -> bool {
+ if start + 4 > bytes.len() || !bytes[start..start + 4].eq_ignore_ascii_case(b"WITH") {
+ return false;
+ }
+ let before_is_identifier = start > 0 && is_sql_identifier_byte(bytes[start - 1]);
+ let after_is_identifier = bytes.get(start + 4).is_some_and(|byte| is_sql_identifier_byte(*byte));
+ !before_is_identifier && !after_is_identifier
+}
+
+fn find_storage_clause_end(sql: &str, open: usize) -> Option<usize> {
+ let bytes = sql.as_bytes();
+ let mut depth = 1usize;
+ let mut index = open + 1;
+ while index < bytes.len() {
+ if let Some(end) = pg_non_code_span_end(sql, index) {
+ index = end;
+ continue;
+ }
+ match bytes[index] {
+ b'(' => depth += 1,
+ b')' => {
+ depth -= 1;
+ if depth == 0 {
+ return Some(index + 1);
+ }
+ }
+ _ => {}
+ }
+ index += 1;
+ }
+ None
+}
+
+/// Splits a `WITH (...)` clause body at top-level commas. Nested calls, quoted identifiers
+/// and string/dollar-quoted literals cannot be cut in half: their parentheses and commas
+/// are not structural. Each slice keeps the surrounding whitespace it had in `body`.
+fn split_table_storage_options(body: &str) -> Vec<&str> {
+ let bytes = body.as_bytes();
+ let mut options = Vec::new();
+ let mut depth = 0usize;
+ let mut start = 0usize;
+ let mut index = 0usize;
+ while index < bytes.len() {
+ if let Some(end) = pg_non_code_span_end(body, index) {
+ index = end;
+ continue;
+ }
+ match bytes[index] {
+ b'(' => depth += 1,
+ b')' => depth = depth.saturating_sub(1),
+ b',' if depth == 0 => {
+ options.push(&body[start..index]);
+ start = index + 1;
+ }
+ _ => {}
+ }
+ index += 1;
+ }
+ options.push(&body[start..]);
+ options
+}
+
+/// Whether one `WITH (...)` entry names an openGauss-only table storage option. The
+/// comparison covers the whole name up to `=`, so PostgreSQL's `toast.compression` is not
+/// mistaken for openGauss's `compression`, and a quoted name matches as well.
+fn is_opengauss_only_table_storage_option(option: &str) -> bool {
+ let name = option.split_once('=').map_or(option, |(name, _)| name).trim().trim_matches('"');
+ OPENGAUSS_ONLY_TABLE_STORAGE_OPTIONS.iter().any(|open| name.eq_ignore_ascii_case(open))
+}
+
+/// Drops the openGauss-only options from a reused table DDL's `WITH (...)` storage clause
+/// so a PostgreSQL-family target can parse it (#9037: PostgreSQL rejected
+/// `WITH (orientation=row, compression=no)` with `syntax error at or near "row"`). Options
+/// the target does accept are kept as written; a clause left with no options disappears
+/// entirely, because `WITH ()` is not valid SQL either.
+///
+/// The scan follows PostgreSQL's lexer rules: string literals (including `E'...'`),
+/// double-quoted identifiers, dollar-quoted strings, and `--`/`/* */` comments are copied
+/// verbatim and their parentheses/commas are not structural. This is what keeps a `WITH`
+/// inside a `COMMENT` literal or a quoted identifier such as `"tenant's"` from being
+/// rewritten; a MySQL-style scanner would shift every span after the apostrophe.
+fn strip_opengauss_only_table_storage_options(sql: &str) -> String {
+ let bytes = sql.as_bytes();
+ let mut output = String::with_capacity(sql.len());
+ let mut index = 0usize;
+ while index < bytes.len() {
+ if let Some(end) = pg_non_code_span_end(sql, index) {
+ output.push_str(&sql[index..end]);
+ index = end;
+ continue;
+ }
+ if !is_with_keyword_at(bytes, index) {
+ let char_len = sql[index..].chars().next().map_or(1, |ch| ch.len_utf8());
+ output.push_str(&sql[index..index + char_len]);
+ index += char_len;
+ continue;
+ }
+
+ let mut open = index + "WITH".len();
+ while open < bytes.len() && bytes[open].is_ascii_whitespace() {
+ open += 1;
+ }
+ let close = if open < bytes.len() && bytes[open] == b'(' { find_storage_clause_end(sql, open) } else { None };
+ let Some(close) = close else {
+ output.push_str(&sql[index..index + "WITH".len()]);
+ index += "WITH".len();
+ continue;
+ };
+
+ let options = split_table_storage_options(&sql[open + 1..close - 1]);
+ let kept: Vec<&str> =
+ options.iter().copied().filter(|option| !is_opengauss_only_table_storage_option(option)).collect();
+ if kept.len() == options.len() {
+ // Nothing openGauss-only here (an index's `fillfactor`, `WITH (oids=false)`,
+ // ...): keep the clause exactly as written.
+ output.push_str(&sql[index..close]);
+ } else {
+ let kept: Vec<&str> = kept.into_iter().filter(|option| !option.trim().is_empty()).collect();
+ if kept.is_empty() {
+ // `WITH ()` is not valid SQL either, so the keyword and the whitespace that
+ // separated it from the column list go too.
+ let trimmed_len = output.trim_end().len();
+ output.truncate(trimmed_len);
+ } else {
+ output.push_str(&sql[index..open + 1]);
+ output.push_str(kept.join(",").trim());
+ output.push(')');
+ }
+ }
+ index = close;
+ }
+ output
+}
+
+/// Normalizes a reused source table DDL for the target: openGauss's `pg_get_tabledef()`
+/// output is stripped of the storage options the target cannot parse, every other
+/// source/target pair gets the DDL unchanged. The gate lives here rather than inside
+/// [`rewrite_transfer_source_table_ddl`] because a plain PostgreSQL connection profiled as
+/// openGauss reads that same DDL, and only the transfer planner sees the driver profiles.
+fn normalize_reused_opengauss_table_ddl(
+ ddl: &str,
+ source_db_type: &DatabaseType,
+ source_driver_profile: Option<&str>,
+ target_db_type: &DatabaseType,
+ target_driver_profile: Option<&str>,
+) -> String {
+ if strips_opengauss_table_storage_options(
+ source_db_type,
+ source_driver_profile,
+ target_db_type,
+ target_driver_profile,
+ ) {
+ strip_opengauss_only_table_storage_options(ddl)
+ } else {
+ ddl.to_string()
+ }
+}
+
fn rewrite_transfer_source_table_ddl(
sql: &str,
source_schema: &str,
@@ -17411,6 +17774,339 @@ CREATE TABLE "Other"."prefix""Source"."NAME" ("ID" INT);"#;
));
}
+ #[test]
+ fn opengauss_reused_ddl_drops_only_the_open_gauss_storage_options() {
+ // #9037: openGauss's pg_get_tabledef() appends storage options PostgreSQL cannot
+ // parse, so the reused CREATE TABLE died with `syntax error at or near "row"`.
+ // PostgreSQL-valid options sitting in the same clause must survive untouched, and the
+ // rest of the script has to stay byte-identical — the result must be valid PostgreSQL.
+ let ddl = "SET search_path = public;\n\
+ CREATE TABLE \"public\".\"items\" (\n\
+ \"id\" integer,\n\
+ \"name\" character varying(20)\n\
+ )\n\
+ WITH (orientation=row, compression=no, storage_type=astore, fillfactor=70)\n\
+ TABLESPACE pg_default;";
+
+ let rewritten =
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None);
+
+ assert!(rewritten.contains("WITH (fillfactor=70)"), "storage clause: {rewritten}");
+ for dropped in ["orientation", "compression", "storage_type"] {
+ assert!(!rewritten.contains(dropped), "{dropped} must be dropped: {rewritten}");
+ }
+ // Only the WITH clause changes; the rest of the reused script is byte-identical.
+ assert!(rewritten.starts_with("SET search_path = public;\nCREATE TABLE \"public\".\"items\" ("), "{rewritten}");
+ assert!(rewritten.ends_with("TABLESPACE pg_default;"), "{rewritten}");
+ // The issue's exact pair has to reach the reuse path, otherwise the normalization
+ // above would never run for it.
+ assert!(can_reuse_source_table_ddl(&DatabaseType::OpenGauss, &DatabaseType::Postgres, None, None, true));
+ }
+
+ #[test]
+ fn opengauss_reused_ddl_removes_a_storage_clause_with_no_portable_option_left() {
+ // `WITH ()` is not valid SQL either, so an all-openGauss clause has to disappear
+ // whole — together with the newline that separated it from the column list.
+ let ddl = "CREATE TABLE \"public\".\"items\" (\n \"id\" integer\n)\nWITH (orientation=row, compression=no)\n;";
+
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None),
+ "CREATE TABLE \"public\".\"items\" (\n \"id\" integer\n)\n;"
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_strip_ignores_literals_comments_and_longer_identifiers() {
+ // Only a real `WITH (...)` clause may change: a literal or comment that spells one
+ // out has to survive, and `WITH` must match on a word boundary rather than inside a
+ // longer identifier.
+ let ddl = "CREATE TABLE \"public\".\"items\" (\n\
+ \"id\" integer,\n\
+ \"note\" text DEFAULT 'WITH (orientation=row)',\n\
+ \"note2\" text DEFAULT $$WITH (orientation=row)$$,\n\
+ withdrawal integer\n\
+ )\n\
+ -- WITH (orientation=row)\n\
+ WITH (orientation=row)\n\
+ ;";
+
+ let rewritten =
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None);
+
+ assert!(rewritten.contains("DEFAULT 'WITH (orientation=row)'"), "literal: {rewritten}");
+ assert!(rewritten.contains("DEFAULT $$WITH (orientation=row)$$"), "dollar quote: {rewritten}");
+ assert!(rewritten.contains("-- WITH (orientation=row)"), "comment: {rewritten}");
+ assert!(rewritten.contains("withdrawal integer"), "identifier: {rewritten}");
+ // The real clause is the only thing that goes: the literal, the comment and the
+ // column list are byte-identical, and the newline that separated the clause from the
+ // column list goes with it.
+ assert_eq!(
+ rewritten,
+ "CREATE TABLE \"public\".\"items\" (\n\
+ \"id\" integer,\n\
+ \"note\" text DEFAULT 'WITH (orientation=row)',\n\
+ \"note2\" text DEFAULT $$WITH (orientation=row)$$,\n\
+ withdrawal integer\n\
+ )\n\
+ -- WITH (orientation=row)\n\
+ ;"
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_strip_matches_whole_option_names_only() {
+ // The comparison covers the whole option name up to `=`, so a dotted name such as
+ // `toast.compression` is not mistaken for openGauss's table-level `compression`, a
+ // longer name is not cut short, and a quoted name still matches. (`toast.compression`
+ // is not a PostgreSQL 16 table reloption, but it is a legal name to keep untouched.)
+ let ddl = "CREATE TABLE \"public\".\"items\" (\"id\" integer) \
+ WITH (toast.compression=pglz, orientation_hint=keep, \"compression\"=no)";
+
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None),
+ "CREATE TABLE \"public\".\"items\" (\"id\" integer) WITH (toast.compression=pglz, orientation_hint=keep)"
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_strip_keeps_a_clause_it_has_nothing_to_drop_from() {
+ // Nothing openGauss-only in the clause: the DDL is returned exactly as it came in,
+ // including the formatting, so unrelated PostgreSQL options are never reformatted.
+ let ddl = "CREATE TABLE \"public\".\"items\" (\"id\" integer) \
+ WITH ( fillfactor = 70 , parallel_workers=2 )";
+
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None),
+ ddl
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_strip_covers_the_column_store_options() {
+ // openGauss column-store tables carry a longer reloption tail. `compresslevel` is
+ // the column-store sub-level (the row-store algorithm level is `compress_level`);
+ // PostgreSQL has no table reloption by any of these names, so every one goes and the
+ // clause disappears.
+ let ddl = "CREATE TABLE \"public\".\"items\" (\"id\" integer) \
+ WITH (orientation=column, compression=high, segment=32, max_batchrow=10000, \
+ partial_cluster_rows=10000, enable_delta_store=on, deltarow_threshold=100, \
+ compresslevel=2, compress_chunk_size=1024, compress_prealloc_chunks=1, \
+ compress_chunk_threshold=1024, version=0.12)";
+
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None),
+ "CREATE TABLE \"public\".\"items\" (\"id\" integer)"
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_strip_covers_the_row_store_compression_options() {
+ // Row-store page compression is a separate openGauss family from the column-store
+ // options; PostgreSQL rejects all four names.
+ let ddl = "CREATE TABLE \"public\".\"items\" (\"id\" integer) \
+ WITH (compresstype=2, compress_level=1, compress_byte_convert=on, \
+ compress_diff_convert=off, fillfactor=70)";
+
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None),
+ "CREATE TABLE \"public\".\"items\" (\"id\" integer) WITH (fillfactor=70)"
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_strip_covers_the_ustore_options() {
+ // UStore tables add their own reloptions; PostgreSQL does not know them.
+ let ddl = "CREATE TABLE \"public\".\"items\" (\"id\" integer) \
+ WITH (storage_type=ustore, init_td=8, hasuids=on)";
+
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None),
+ "CREATE TABLE \"public\".\"items\" (\"id\" integer)"
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_strip_keeps_a_trailing_backslash_literal() {
+ // PostgreSQL reads a backslash in a regular string literally, so `'path C:\'` ends
+ // at the quote. The storage clause still has to be stripped, and the literal has to
+ // survive byte-for-byte.
+ let ddl = "CREATE TABLE \"public\".\"items\" (\"id\" integer) WITH (orientation=row);\n\
+ COMMENT ON TABLE \"public\".\"items\" IS 'path C:\\';";
+
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None),
+ "CREATE TABLE \"public\".\"items\" (\"id\" integer);\n\
+ COMMENT ON TABLE \"public\".\"items\" IS 'path C:\\';"
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_strip_keeps_quoted_identifiers_and_comment_literals() {
+ // A quoted identifier may contain an apostrophe (`tenant's schema` is a real
+ // openGauss input; see schema::tests). A MySQL-style tokenizer would read the
+ // apostrophe as a string start, shift every later span, and delete the text inside
+ // the COMMENT literal. The storage clause must still be stripped.
+ let ddl = "SET search_path = \"tenant's\";\n\
+ CREATE TABLE \"tenant's\".\"items\" (\"id\" integer) WITH (orientation=row);\n\
+ COMMENT ON TABLE \"tenant's\".\"items\" IS 'keep WITH (orientation=row) text';";
+
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None),
+ "SET search_path = \"tenant's\";\n\
+ CREATE TABLE \"tenant's\".\"items\" (\"id\" integer);\n\
+ COMMENT ON TABLE \"tenant's\".\"items\" IS 'keep WITH (orientation=row) text';"
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_strip_keeps_e_string_literals() {
+ // In an `E'...'` literal a backslash escapes the next byte, so the `\'` inside the
+ // text does not end the literal; the `WITH` inside it must not be rewritten.
+ let ddl = "CREATE TABLE \"public\".\"items\" (\"id\" integer) WITH (orientation=row);\n\
+ COMMENT ON TABLE \"public\".\"items\" IS E'it\\'s WITH (orientation=row)';";
+
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, &DatabaseType::Postgres, None),
+ "CREATE TABLE \"public\".\"items\" (\"id\" integer);\n\
+ COMMENT ON TABLE \"public\".\"items\" IS E'it\\'s WITH (orientation=row)';"
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_strip_handles_quoted_parentheses() {
+ // Parentheses inside a quoted identifier or value are not structural; a scanner
+ // that counts them would cut the clause at the wrong `)` and emit invalid SQL.
+ let cases = [
+ ("WITH (\"orientation\"=\"row)\", fillfactor=70)", "WITH (fillfactor=70)"),
+ ("WITH (orientation=row, \"note\"=\"(\")", "WITH (\"note\"=\"(\")"),
+ ];
+ for (clause, expected) in cases {
+ let ddl = format!("CREATE TABLE \"public\".\"items\" (\"id\" integer) {clause}");
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(
+ &ddl,
+ &DatabaseType::OpenGauss,
+ None,
+ &DatabaseType::Postgres,
+ None
+ ),
+ format!("CREATE TABLE \"public\".\"items\" (\"id\" integer) {expected}"),
+ "input clause: {clause}"
+ );
+ }
+ }
+
+ #[test]
+ fn opengauss_storage_strip_keeps_the_surviving_options_well_formed() {
+ // Whatever the original spacing and separator placement, the surviving clause has to
+ // stay a valid comma-separated PostgreSQL option list — the real PostgreSQL 16 check
+ // for `WITH (fillfactor=70)` is in the PR notes.
+ let cases = [
+ ("WITH (orientation=row, fillfactor=70)", "WITH (fillfactor=70)"),
+ ("WITH (fillfactor=70, orientation=row)", "WITH (fillfactor=70)"),
+ ("WITH ( orientation=row , fillfactor = 70 )", "WITH (fillfactor = 70)"),
+ ("WITH (orientation=row, fillfactor=70, compression=no)", "WITH (fillfactor=70)"),
+ ("WITH (fillfactor=70, orientation=row,)", "WITH (fillfactor=70)"),
+ // A quoted value may contain a comma; it must not be cut in half and left behind.
+ ("WITH (\"orientation\"=\"row,column\", fillfactor=70)", "WITH (fillfactor=70)"),
+ ];
+ for (clause, expected) in cases {
+ let ddl = format!("CREATE TABLE \"public\".\"items\" (\"id\" integer) {clause}");
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(
+ &ddl,
+ &DatabaseType::OpenGauss,
+ None,
+ &DatabaseType::Postgres,
+ None
+ ),
+ format!("CREATE TABLE \"public\".\"items\" (\"id\" integer) {expected}"),
+ "input clause: {clause}"
+ );
+ }
+ // A clause left with nothing but a dangling separator goes away entirely rather than
+ // becoming the invalid `WITH ()`.
+ assert_eq!(
+ normalize_reused_opengauss_table_ddl(
+ "CREATE TABLE \"public\".\"items\" (\"id\" integer) WITH (orientation=row,)",
+ &DatabaseType::OpenGauss,
+ None,
+ &DatabaseType::Postgres,
+ None
+ ),
+ "CREATE TABLE \"public\".\"items\" (\"id\" integer)"
+ );
+ }
+
+ #[test]
+ fn opengauss_storage_options_are_stripped_for_every_target_that_cannot_parse_them() {
+ // #9037 polarity is locked against the whole closed enum rather than a hand-picked
+ // list, and the expected set is spelled out instead of derived from the predicate
+ // under test: a new PG-compatible target has to be classified here on purpose.
+ // Non-PostgreSQL targets never reach the reuse path, so they keep the DDL untouched.
+ let pg_family_that_rejects_the_clause = [
+ DatabaseType::Postgres,
+ DatabaseType::Redshift,
+ DatabaseType::Kingbase,
+ DatabaseType::Highgo,
+ DatabaseType::Uxdb,
+ DatabaseType::Kwdb,
+ ];
+ // The three openGauss-dialect targets understand the clause and must keep it, so a
+ // column-store source is not silently downgraded to a row-store table.
+ let open_gauss_dialect_targets = [DatabaseType::OpenGauss, DatabaseType::Gaussdb, DatabaseType::Vastbase];
+ let ddl = "CREATE TABLE \"public\".\"items\" (\"id\" integer) WITH (orientation=column, compression=low)";
+
+ for target in DatabaseType::ALL {
+ let rewritten = normalize_reused_opengauss_table_ddl(ddl, &DatabaseType::OpenGauss, None, target, None);
+ let expected_to_drop = pg_family_that_rejects_the_clause.contains(target);
+ assert_eq!(
+ !rewritten.contains("WITH ("),
+ expected_to_drop,
+ "openGauss -> {} must {} the storage clause: {rewritten}",
+ target.as_str(),
+ if expected_to_drop { "drop" } else { "keep" }
+ );
+ if open_gauss_dialect_targets.contains(target) {
+ assert_eq!(rewritten, ddl, "{} speaks openGauss and keeps the DDL", target.as_str());
+ }
+ }
+ }
+
+ #[test]
+ fn opengauss_storage_option_sources_are_the_open_gauss_dialect_and_its_profile() {
+ let ddl = "CREATE TABLE \"public\".\"items\" (\"id\" integer) WITH (orientation=column, compression=low)";
+
+ for source in DatabaseType::ALL {
+ let rewritten = normalize_reused_opengauss_table_ddl(ddl, source, None, &DatabaseType::Postgres, None);
+ // Only openGauss and GaussDB read table DDL through pg_get_tabledef() (see
+ // `is_opengauss_family_config`); every other source keeps its DDL as-is.
+ let expected_to_drop = matches!(source, DatabaseType::OpenGauss | DatabaseType::Gaussdb);
+ assert_eq!(
+ !rewritten.contains("WITH ("),
+ expected_to_drop,
+ "{} -> Postgres must {} the storage clause: {rewritten}",
+ source.as_str(),
+ if expected_to_drop { "drop" } else { "keep" }
+ );
+ }
+
+ // A plain PostgreSQL connection profiled as openGauss reads that same DDL, so the
+ // profile counts exactly like the database type — and only those two names, matching
+ // the case-sensitive comparison `is_opengauss_family_config` already uses.
+ let strip = |source: &DatabaseType, source_profile: Option<&str>, target_profile: Option<&str>| {
+ normalize_reused_opengauss_table_ddl(ddl, source, source_profile, &DatabaseType::Postgres, target_profile)
+ .contains("WITH (")
+ };
+ assert!(!strip(&DatabaseType::Postgres, Some("opengauss"), None), "openGauss-profiled source");
+ assert!(!strip(&DatabaseType::Postgres, Some("gaussdb"), None), "GaussDB-profiled source");
+ assert!(strip(&DatabaseType::Postgres, Some("postgres"), None), "unrelated profile");
+ assert!(strip(&DatabaseType::Postgres, None, None), "plain PostgreSQL source");
+ // A target profiled as openGauss understands the clause too.
+ assert!(strip(&DatabaseType::OpenGauss, None, Some("gaussdb")), "GaussDB-profiled target");
+ assert!(!strip(&DatabaseType::OpenGauss, None, Some("postgres")), "unrelated target profile");
+ }
+
#[test]
fn mysql_reused_ddl_renames_create_table_header_for_case_conversion() {
let ddl = "CREATE TABLE `orders_plain` (\n `id` int NOT NULL,\n PRIMARY KEY (`id`)\n) \
diff --git a/crates/dbx-core/src/data/transfer/ddl_plan.rs b/crates/dbx-core/src/data/transfer/ddl_plan.rs
index 9b4be43998..8670c53512 100644
--- a/crates/dbx-core/src/data/transfer/ddl_plan.rs
+++ b/crates/dbx-core/src/data/transfer/ddl_plan.rs
@@ -213,7 +213,17 @@ pub(super) async fn prepare_table_ddl(
target_table,
) {
reused_source_ddl = source_ddl_was_read;
- rewritten
+ // openGauss's `pg_get_tabledef()` DDL carries storage options the PG-compatible
+ // targets reject (`WITH (orientation=row, compression=no)` → #9037). The profiles
+ // are passed in because a plain PostgreSQL connection profiled as openGauss reads
+ // that same DDL.
+ normalize_reused_opengauss_table_ddl(
+ &rewritten,
+ source_db_type,
+ source_driver_profile.as_deref(),
+ target_db_type,
+ target_driver_profile.as_deref(),
+ )
} else {
// The reused DDL's CREATE TABLE head could not be renamed safely; fall back
// to the generated DDL rather than creating the table under the wrong name.