diff --git a/agents/common/src/main/java/com/dbx/agent/IndexInfo.java b/agents/common/src/main/java/com/dbx/agent/IndexInfo.java index 335775d9b6..ca8034b15c 100644 --- a/agents/common/src/main/java/com/dbx/agent/IndexInfo.java +++ b/agents/common/src/main/java/com/dbx/agent/IndexInfo.java @@ -18,6 +18,7 @@ public final class IndexInfo { // doesn't track this (provenance unknown for that dialect/path). Mirrors // crate::types::IndexInfo::key_is_expression on the Rust side (#6312 review). private List key_is_expression; + private List key_options = Collections.emptyList(); // True when this index is the object behind a table constraint (PRIMARY KEY / UNIQUE) // instead of a standalone index. Dameng lists both kinds in ALL_INDEXES but only accepts // index-level DDL for standalone ones, so the SQL builder needs to tell them apart @@ -104,6 +105,10 @@ public final class IndexInfo { return key_is_expression; } + public List getKey_options() { + return key_options; + } + public boolean getConstraint_backed() { return constraint_backed; } @@ -144,6 +149,10 @@ public final class IndexInfo { this.key_is_expression = key_is_expression; } + public void setKey_options(List key_options) { + this.key_options = key_options == null ? Collections.emptyList() : key_options; + } + public void setConstraint_backed(boolean constraint_backed) { this.constraint_backed = constraint_backed; } @@ -162,7 +171,8 @@ public final class IndexInfo { && Objects.equals(index_type, that.index_type) && Objects.equals(included_columns, that.included_columns) && Objects.equals(comment, that.comment) - && Objects.equals(key_is_expression, that.key_is_expression); + && Objects.equals(key_is_expression, that.key_is_expression) + && Objects.equals(key_options, that.key_options); } @Override @@ -177,6 +187,7 @@ public final class IndexInfo { included_columns, comment, key_is_expression, + key_options, constraint_backed ); } @@ -192,6 +203,7 @@ public final class IndexInfo { + ", included_columns=" + included_columns + ", comment=" + comment + ", key_is_expression=" + key_is_expression + + ", key_options=" + key_options + ", constraint_backed=" + constraint_backed + ")"; } diff --git a/agents/common/src/main/java/com/dbx/agent/PostgresLikeAgent.java b/agents/common/src/main/java/com/dbx/agent/PostgresLikeAgent.java index 24791ba596..6a6b764e8a 100644 --- a/agents/common/src/main/java/com/dbx/agent/PostgresLikeAgent.java +++ b/agents/common/src/main/java/com/dbx/agent/PostgresLikeAgent.java @@ -546,7 +546,9 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent { String sql = "SELECT i.relname AS index_name, am.amname AS index_type, " + "(ix.indisunique AND ix.indisvalid) AS is_unique, ix.indisprimary AS is_primary, " + "COALESCE(a.attname, " + profile.catalogPrefixedFunction("get_indexdef") + "(ix.indexrelid, k.n, true)) AS column_text, " + - "(a.attname IS NULL) AS is_expression " + + "(a.attname IS NULL) AS is_expression, " + + "array_length(ix.indoption, 1) AS nkeyatts, k.n AS key_position, " + + "ix.indoption[(k.n - 1)::int] AS key_option " + "FROM " + profile.catalogRelation("index") + " ix " + "JOIN " + profile.catalogRelation("class") + " t ON t.oid = ix.indrelid " + "JOIN " + profile.catalogRelation("class") + " i ON i.oid = ix.indexrelid " + @@ -569,24 +571,36 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent { indexName, name -> new IndexBuilder(name, indexType, isUnique, isPrimary) ); - builder.columns.add(rs.getString("column_text")); + String columnText = rs.getString("column_text"); + if (rs.getObject("nkeyatts") != null && rs.getObject("key_position") != null + && rs.getInt("key_position") > rs.getInt("nkeyatts")) { + builder.includedColumns.add(columnText); + continue; + } + builder.columns.add(columnText); builder.keyIsExpression.add(rs.getBoolean("is_expression")); + Object keyOption = rs.getObject("key_option"); + if (keyOption != null) { + builder.keyOptions.add(rs.getInt("key_option")); + } } } } List result = new ArrayList<>(); for (IndexBuilder builder : byName.values()) { - result.add(new IndexInfo( + IndexInfo index = new IndexInfo( builder.name, builder.columns, builder.isUnique, builder.isPrimary, null, builder.indexType, - null, + builder.includedColumns.isEmpty() ? null : builder.includedColumns, null, builder.keyIsExpression - )); + ); + index.setKey_options(builder.keyOptions); + result.add(index); } return result; }); @@ -598,8 +612,9 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent { private final boolean isUnique; private final boolean isPrimary; private final List columns = new ArrayList<>(); + private final List includedColumns = new ArrayList<>(); private final List keyIsExpression = new ArrayList<>(); - + private final List keyOptions = new ArrayList<>(); private IndexBuilder(String name, String indexType, boolean isUnique, boolean isPrimary) { this.name = name; this.indexType = indexType; diff --git a/agents/common/src/test/java/com/dbx/agent/PostgresLikeAgentTest.java b/agents/common/src/test/java/com/dbx/agent/PostgresLikeAgentTest.java index bb673e9a27..073593061b 100644 --- a/agents/common/src/test/java/com/dbx/agent/PostgresLikeAgentTest.java +++ b/agents/common/src/test/java/com/dbx/agent/PostgresLikeAgentTest.java @@ -157,6 +157,34 @@ class PostgresLikeAgentTest { assertTrue(index.getIs_unique()); assertFalse(index.getIs_primary()); assertEquals("btree", index.getIndex_type()); + assertTrue(index.getKey_options().isEmpty()); + } + + @Test + void listIndexesPreservesPerKeyOrderingAndSeparatesIncludedColumns() { + TestPostgresLikeAgent agent = new TestPostgresLikeAgent(preparedConnection(resultSet( + new String[]{ + "index_name", "index_type", "is_unique", "is_primary", "column_text", + "is_expression", "nkeyatts", "key_position", "key_option" + }, + new Object[][]{ + {"event_order_idx", "btree", false, false, "created_at", false, 4, 1, 1}, + {"event_order_idx", "btree", false, false, "tenant_id", false, 4, 2, 0}, + {"event_order_idx", "btree", false, false, "score", false, 4, 3, 2}, + {"event_order_idx", "btree", false, false, "lower(payload)", true, 4, 4, 3}, + {"event_order_idx", "btree", false, false, "payload", false, 4, 5, null} + } + ))); + agent.connect(new ConnectParams()); + + IndexInfo index = agent.listIndexes("public", "event_log").get(0); + + assertEquals(java.util.Arrays.asList("created_at", "tenant_id", "score", "lower(payload)"), index.getColumns()); + assertEquals(java.util.Arrays.asList(false, false, false, true), index.getKey_is_expression()); + assertEquals(java.util.Arrays.asList(1, 0, 2, 3), index.getKey_options()); + assertEquals(java.util.Collections.singletonList("payload"), index.getIncluded_columns()); + com.google.gson.Gson gson = new com.google.gson.Gson(); + assertEquals(index, gson.fromJson(gson.toJson(index), IndexInfo.class)); } @Test @@ -174,6 +202,8 @@ class PostgresLikeAgentTest { assertTrue(sql.contains("LEFT JOIN pg_catalog.pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum AND k.attnum > 0"), sql); assertTrue(sql.contains("pg_catalog.pg_get_indexdef(ix.indexrelid, k.n, true)"), sql); assertTrue(sql.contains("ix.indisunique AND ix.indisvalid"), sql); + assertTrue(sql.contains("ix.indoption[(k.n - 1)::int]"), sql); + assertTrue(sql.contains("array_length(ix.indoption, 1) AS nkeyatts"), sql); } @Test diff --git a/crates/dbx-core/src/db/clickhouse_driver.rs b/crates/dbx-core/src/db/clickhouse_driver.rs index 74988a641a..66e01bbcc0 100644 --- a/crates/dbx-core/src/db/clickhouse_driver.rs +++ b/crates/dbx-core/src/db/clickhouse_driver.rs @@ -542,6 +542,7 @@ fn clickhouse_index_from_skipping_row(row: &[serde_json::Value]) -> IndexInfo { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, } } @@ -795,6 +796,7 @@ pub async fn list_indexes(client: &ChClient, database: &str, table: &str) -> Res comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); } diff --git a/crates/dbx-core/src/db/cloudflare_d1/mod.rs b/crates/dbx-core/src/db/cloudflare_d1/mod.rs index 9040e33232..60470cdcc8 100644 --- a/crates/dbx-core/src/db/cloudflare_d1/mod.rs +++ b/crates/dbx-core/src/db/cloudflare_d1/mod.rs @@ -201,6 +201,7 @@ pub async fn list_indexes(client: &CloudflareD1Client, _schema: &str, table: &st comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); } diff --git a/crates/dbx-core/src/db/manticoresearch.rs b/crates/dbx-core/src/db/manticoresearch.rs index 6f0a57a135..dd1418c73a 100644 --- a/crates/dbx-core/src/db/manticoresearch.rs +++ b/crates/dbx-core/src/db/manticoresearch.rs @@ -211,6 +211,7 @@ fn index_info_from_row(row: &mysql_async::Row) -> IndexInfo { comment: (!comment_parts.is_empty()).then(|| comment_parts.join(", ")), key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, } } diff --git a/crates/dbx-core/src/db/mongo_driver.rs b/crates/dbx-core/src/db/mongo_driver.rs index 3e52cc09a8..c5c048bb46 100644 --- a/crates/dbx-core/src/db/mongo_driver.rs +++ b/crates/dbx-core/src/db/mongo_driver.rs @@ -905,6 +905,7 @@ fn index_info_from_model(model: IndexModel) -> IndexInfo { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, } } @@ -3640,6 +3641,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); @@ -3667,6 +3669,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); @@ -3912,6 +3915,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }, IndexInfo { @@ -3925,6 +3929,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }, IndexInfo { @@ -3938,6 +3943,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }, ]; diff --git a/crates/dbx-core/src/db/mysql.rs b/crates/dbx-core/src/db/mysql.rs index 05dccf130a..be3e9e6bb4 100644 --- a/crates/dbx-core/src/db/mysql.rs +++ b/crates/dbx-core/src/db/mysql.rs @@ -5776,6 +5776,7 @@ pub async fn list_indexes(pool: &MySqlPool, database: &str, table: &str) -> Resu comment: get_opt_str(&row, "INDEX_COMMENT").filter(|value| !value.is_empty()), key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); index_position diff --git a/crates/dbx-core/src/db/mysql_compatible.rs b/crates/dbx-core/src/db/mysql_compatible.rs index 5dce39ca5b..fa30f94f11 100644 --- a/crates/dbx-core/src/db/mysql_compatible.rs +++ b/crates/dbx-core/src/db/mysql_compatible.rs @@ -326,6 +326,7 @@ fn table_key_index(name: &str, line: &str, is_unique: bool, is_primary: bool, in comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }) } @@ -348,6 +349,7 @@ fn secondary_index(line: &str) -> Option { comment: mysql_quoted_string_argument(after_name, "COMMENT"), key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }) } diff --git a/crates/dbx-core/src/db/ob_oracle.rs b/crates/dbx-core/src/db/ob_oracle.rs index 3fc3e1e427..b185cafe51 100644 --- a/crates/dbx-core/src/db/ob_oracle.rs +++ b/crates/dbx-core/src/db/ob_oracle.rs @@ -243,6 +243,7 @@ pub async fn list_indexes(pool: &mysql_async::Pool, schema: &str, table: &str) - comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, } }) diff --git a/crates/dbx-core/src/db/postgres.rs b/crates/dbx-core/src/db/postgres.rs index 69c218e8d9..1f9ca863f7 100644 --- a/crates/dbx-core/src/db/postgres.rs +++ b/crates/dbx-core/src/db/postgres.rs @@ -4064,6 +4064,8 @@ async fn list_indexes_for_relations_with_sql( let all_is_expr: Vec = row.try_get::<_, Vec>(11).unwrap_or_default(); let key_is_expression = if all_is_expr.len() == all_cols.len() { all_is_expr[..split_at].to_vec() } else { Vec::new() }; + let all_key_options: Vec = row.try_get::<_, Vec>(12).unwrap_or_default(); + let key_options = if all_key_options.len() == split_at { all_key_options } else { Vec::new() }; result.entry(relid).or_default().push(IndexInfo { name: pg_row_try_string(row, 1), columns: key_cols, @@ -4075,6 +4077,7 @@ async fn list_indexes_for_relations_with_sql( comment: row.try_get::<_, Option>(10).ok().flatten(), key_is_expression, column_opclasses: key_opclasses, + key_options, constraint_backed: false, }); } @@ -4101,7 +4104,8 @@ fn postgres_indexes_for_relations_sql() -> &'static str { ix.indnkeyatts AS nkeyatts, \ ix.indkey AS indkey, \ obj_description(i.oid, 'pg_class') AS index_comment, \ - array_agg(a.attname IS NULL ORDER BY k.n) AS key_is_expression \ + array_agg(a.attname IS NULL ORDER BY k.n) AS key_is_expression, \ + array_agg(ix.indoption[(k.n - 1)::int] ORDER BY k.n) FILTER (WHERE k.n <= ix.indnkeyatts) AS key_options \ FROM pg_index ix \ JOIN pg_class t ON t.oid = ix.indrelid \ JOIN pg_class i ON i.oid = ix.indexrelid \ @@ -4145,7 +4149,7 @@ fn postgres_indexes_for_relations_compat_sql() -> &'static str { ix.indisprimary AS is_primary, \ pg_get_expr(ix.indpred, ix.indrelid) AS filter_expr, \ am.amname AS index_type, \ - NULL::smallint AS nkeyatts, \ + array_length(string_to_array(ix.indoption::text, ' '), 1)::smallint AS nkeyatts, \ ix.indkey AS indkey, \ obj_description(i.oid, 'pg_class') AS index_comment, \ ARRAY( \ @@ -4156,7 +4160,8 @@ fn postgres_indexes_for_relations_compat_sql() -> &'static str { AND a.attnum = (string_to_array(ix.indkey::text, ' '))[pos.n]::int2 \ AND a.attnum > 0 \ ORDER BY pos.n \ - ) AS key_is_expression \ + ) AS key_is_expression, \ + string_to_array(ix.indoption::text, ' ')::smallint[] AS key_options \ FROM pg_index ix \ JOIN pg_class t ON t.oid = ix.indrelid \ JOIN pg_class i ON i.oid = ix.indexrelid \ @@ -7634,7 +7639,8 @@ const POSTGRES_INDEXES_SQL: &str = "SELECT i.relname AS index_name, \ ix.indnkeyatts AS nkeyatts, \ ix.indkey AS indkey, \ obj_description(i.oid, 'pg_class') AS index_comment, \ - array_agg(a.attname IS NULL ORDER BY k.n) AS key_is_expression \ + array_agg(a.attname IS NULL ORDER BY k.n) AS key_is_expression, \ + array_agg(ix.indoption[(k.n - 1)::int] ORDER BY k.n) FILTER (WHERE k.n <= ix.indnkeyatts) AS key_options \ FROM pg_index ix \ JOIN pg_class t ON t.oid = ix.indrelid \ JOIN pg_class i ON i.oid = ix.indexrelid \ @@ -7677,7 +7683,7 @@ const POSTGRES_INDEXES_COMPAT_SQL: &str = "SELECT i.relname AS index_name, \ ix.indisprimary AS is_primary, \ pg_get_expr(ix.indpred, ix.indrelid) AS filter_expr, \ am.amname AS index_type, \ - NULL::smallint AS nkeyatts, \ + array_length(string_to_array(ix.indoption::text, ' '), 1)::smallint AS nkeyatts, \ ix.indkey AS indkey, \ obj_description(i.oid, 'pg_class') AS index_comment, \ ARRAY( \ @@ -7688,7 +7694,8 @@ const POSTGRES_INDEXES_COMPAT_SQL: &str = "SELECT i.relname AS index_name, \ AND a.attnum = (string_to_array(ix.indkey::text, ' '))[pos.n]::int2 \ AND a.attnum > 0 \ ORDER BY pos.n \ - ) AS key_is_expression \ + ) AS key_is_expression, \ + string_to_array(ix.indoption::text, ' ')::smallint[] AS key_options \ FROM pg_index ix \ JOIN pg_class t ON t.oid = ix.indrelid \ JOIN pg_class i ON i.oid = ix.indexrelid \ @@ -7808,6 +7815,8 @@ async fn list_indexes_with_sql( let all_is_expr: Vec = row.try_get::<_, Vec>(10).unwrap_or_default(); let key_is_expression = if all_is_expr.len() == all_cols.len() { all_is_expr[..split_at].to_vec() } else { Vec::new() }; + let all_key_options: Vec = row.try_get::<_, Vec>(11).unwrap_or_default(); + let key_options = if all_key_options.len() == split_at { all_key_options } else { Vec::new() }; IndexInfo { name: pg_row_try_string(row, 0), columns: key_cols, @@ -7819,6 +7828,7 @@ async fn list_indexes_with_sql( comment: row.try_get::<_, Option>(9).ok().flatten(), key_is_expression, column_opclasses: key_opclasses, + key_options, constraint_backed: false, } }) @@ -12899,7 +12909,8 @@ mod tests { fn postgres_index_metadata_has_legacy_catalog_fallback() { assert!(POSTGRES_INDEXES_SQL.contains("ix.indnkeyatts")); assert!(!POSTGRES_INDEXES_COMPAT_SQL.contains("ix.indnkeyatts")); - assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("NULL::smallint AS nkeyatts")); + assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("AS nkeyatts")); + assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("ix.indoption::text")); assert!(!POSTGRES_INDEXES_COMPAT_SQL.contains("LATERAL")); assert!(!POSTGRES_INDEXES_COMPAT_SQL.contains("WITH ORDINALITY")); assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("generate_series")); @@ -12923,6 +12934,17 @@ mod tests { assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("AS key_is_expression")); } + #[test] + fn postgres_index_metadata_tracks_per_key_ordering_without_include_columns() { + for sql in [POSTGRES_INDEXES_SQL, postgres_indexes_for_relations_sql()] { + assert!(sql.contains("ix.indoption[(k.n - 1)::int]")); + assert!(sql.contains("FILTER (WHERE k.n <= ix.indnkeyatts)")); + } + for sql in [POSTGRES_INDEXES_COMPAT_SQL, postgres_indexes_for_relations_compat_sql()] { + assert!(sql.contains("string_to_array(ix.indoption::text, ' ')") && sql.contains("AS key_options")); + } + } + #[test] fn postgres_index_metadata_schema_qualifies_opclass() { // The LATERAL (modern) index-introspection SQL returns each non-default diff --git a/crates/dbx-core/src/db/questdb.rs b/crates/dbx-core/src/db/questdb.rs index dfde97ea3c..c742e10471 100644 --- a/crates/dbx-core/src/db/questdb.rs +++ b/crates/dbx-core/src/db/questdb.rs @@ -178,6 +178,7 @@ pub async fn list_indexes(pool: &Pool, _schema: &str, table: &str) -> Result comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); } diff --git a/crates/dbx-core/src/db/sqlite.rs b/crates/dbx-core/src/db/sqlite.rs index 352935e903..66aa5eca27 100644 --- a/crates/dbx-core/src/db/sqlite.rs +++ b/crates/dbx-core/src/db/sqlite.rs @@ -2494,6 +2494,7 @@ pub async fn list_indexes(pool: &SqliteHandle, schema: &str, table: &str) -> Res comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); } @@ -2549,6 +2550,7 @@ pub async fn list_indexes(pool: &SqliteHandle, schema: &str, table: &str) -> Res comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); } diff --git a/crates/dbx-core/src/db/sqlserver.rs b/crates/dbx-core/src/db/sqlserver.rs index e88bfc4f1b..887b415086 100644 --- a/crates/dbx-core/src/db/sqlserver.rs +++ b/crates/dbx-core/src/db/sqlserver.rs @@ -2941,6 +2941,7 @@ pub async fn list_indexes(client: &mut SqlServerClient, schema: &str, table: &st comment: row.get::<&str, _>(7).filter(|s: &&str| !s.is_empty()).map(|s: &str| s.to_string()), key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, } }) diff --git a/crates/dbx-core/src/db/turso_driver.rs b/crates/dbx-core/src/db/turso_driver.rs index 09fa5d728f..b9c168d4a6 100644 --- a/crates/dbx-core/src/db/turso_driver.rs +++ b/crates/dbx-core/src/db/turso_driver.rs @@ -223,6 +223,7 @@ pub async fn list_indexes(client: &TursoClient, _schema: &str, table: &str) -> R comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); } diff --git a/crates/dbx-core/src/docs/dbml.rs b/crates/dbx-core/src/docs/dbml.rs index 23180244e0..6df6a0bed9 100644 --- a/crates/dbx-core/src/docs/dbml.rs +++ b/crates/dbx-core/src/docs/dbml.rs @@ -601,6 +601,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }; let table = doc_table("orders", vec![col("user_id", "integer")], vec![index]); @@ -626,6 +627,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }; let table = doc_table("orders", vec![col("reference", "text")], vec![index]); @@ -647,6 +649,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }; let mut id = col("id", "integer"); @@ -671,6 +674,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }; let table = doc_table("orders", vec![col("status", "text")], vec![index]); @@ -875,6 +879,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }; let out = diff --git a/crates/dbx-core/src/docs/relations.rs b/crates/dbx-core/src/docs/relations.rs index b69c915ed4..c23fd17b67 100644 --- a/crates/dbx-core/src/docs/relations.rs +++ b/crates/dbx-core/src/docs/relations.rs @@ -195,6 +195,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, } } diff --git a/crates/dbx-core/src/schema.rs b/crates/dbx-core/src/schema.rs index 2def70adef..11e73a9f50 100644 --- a/crates/dbx-core/src/schema.rs +++ b/crates/dbx-core/src/schema.rs @@ -3212,6 +3212,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }; let mut filtered = index("uq_active_code", &["active_code"], true, false); @@ -8345,6 +8346,7 @@ async fn external_driver_gaussdb_m_indexes( comment: current_comment.clone(), key_is_expression: current_is_expression.clone(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); } @@ -8416,6 +8418,7 @@ async fn external_driver_gaussdb_m_indexes( comment: current_comment, key_is_expression: current_is_expression, column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); } @@ -10477,6 +10480,7 @@ mod ddl_tests { comment: None, key_is_expression: vec![true], column_opclasses: vec![Some("gin_trgm_ops".to_string())], + key_options: Vec::new(), constraint_backed: false, }]; @@ -10503,6 +10507,7 @@ mod ddl_tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }]; let partition_info = db::postgres::PostgresTablePartitionInfo { @@ -10555,6 +10560,7 @@ mod ddl_tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }]; let partition_info = db::postgres::PostgresTablePartitionInfo { diff --git a/crates/dbx-core/src/schema_diff.rs b/crates/dbx-core/src/schema_diff.rs index 37138e84a8..a5758837af 100644 --- a/crates/dbx-core/src/schema_diff.rs +++ b/crates/dbx-core/src/schema_diff.rs @@ -6143,6 +6143,7 @@ mod tests { comment: overrides.comment, key_is_expression: overrides.key_is_expression, column_opclasses: overrides.column_opclasses, + key_options: overrides.key_options, constraint_backed: false, } } @@ -7137,6 +7138,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }; let diff = TableDiff { @@ -7201,6 +7203,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }; let unchanged_fk = ForeignKeyInfo { @@ -7540,6 +7543,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }; let btree_sql = create_index_sql("events", &btree, DatabaseType::SqlServer, Some("dbo")); @@ -7560,6 +7564,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }; assert_eq!( @@ -8265,6 +8270,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], &[index(IndexInfo { @@ -8278,6 +8284,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], ); @@ -8300,6 +8307,7 @@ mod tests { comment: None, key_is_expression: vec![false], column_opclasses: vec![Some("gin_trgm_ops".to_string())], + key_options: Vec::new(), constraint_backed: false, }); let target_index = index(IndexInfo { @@ -8313,6 +8321,7 @@ mod tests { comment: None, key_is_expression: vec![false], column_opclasses: vec![None], + key_options: Vec::new(), constraint_backed: false, }); @@ -8341,6 +8350,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); let target_index = index(IndexInfo { @@ -8354,6 +8364,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); @@ -8418,6 +8429,7 @@ mod tests { comment: None, key_is_expression: vec![false, false, false, true], column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }); @@ -8480,6 +8492,7 @@ mod tests { ], key_is_expression: vec![false, false, false, true], column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, is_unique: true, is_primary: false, @@ -8549,6 +8562,7 @@ mod tests { columns: vec!["order item".to_string(), "a(b)".to_string(), "a::b".to_string()], key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, is_unique: false, is_primary: false, @@ -8738,6 +8752,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })), target: None, @@ -9088,6 +9103,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })), target: None, @@ -9813,6 +9829,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], foreign_keys: vec![foreign_key(ForeignKeyInfo { @@ -9878,6 +9895,7 @@ mod tests { comment: Some("status lookup".to_string()), key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], foreign_keys: vec![foreign_key(ForeignKeyInfo { @@ -9933,6 +9951,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], foreign_keys: vec![foreign_key(ForeignKeyInfo { @@ -11052,6 +11071,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })), target: None, @@ -11096,6 +11116,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })), changes: vec![], @@ -11500,6 +11521,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], &[index(IndexInfo { @@ -11513,6 +11535,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], ); @@ -11534,6 +11557,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], &[index(IndexInfo { @@ -11547,6 +11571,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], ); @@ -11568,6 +11593,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], &[index(IndexInfo { @@ -11581,6 +11607,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], ); @@ -11603,6 +11630,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], &[index(IndexInfo { @@ -11616,6 +11644,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], ); @@ -11637,6 +11666,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], &[index(IndexInfo { @@ -11650,6 +11680,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], ); @@ -11671,6 +11702,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], &[index(IndexInfo { @@ -11684,6 +11716,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })], ); @@ -11707,6 +11740,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }), index(IndexInfo { @@ -11720,6 +11754,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }), ], @@ -11735,6 +11770,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }), index(IndexInfo { @@ -11748,6 +11784,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }), ], @@ -12120,6 +12157,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })), target: None, @@ -12140,6 +12178,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, })), changes: vec![], @@ -12540,6 +12579,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }), target: None, @@ -12795,6 +12835,7 @@ mod tests { comment: None, key_is_expression: Vec::new(), column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }), target: None, diff --git a/crates/dbx-core/src/transfer.rs b/crates/dbx-core/src/transfer.rs index 14b7ea449c..55048a94c0 100644 --- a/crates/dbx-core/src/transfer.rs +++ b/crates/dbx-core/src/transfer.rs @@ -2047,7 +2047,12 @@ fn sqlserver_row_number_page_sql( ) } -fn postgres_index_column_sql(column: &str, is_expression: bool, opclass: Option<&str>) -> String { +fn postgres_index_column_sql( + column: &str, + is_expression: bool, + opclass: Option<&str>, + key_options: Option, +) -> String { // The base key text: a real column is quoted as an identifier; an expression/functional // key part arrives as raw expression text (the per-column `pg_get_indexdef` omits the // opclass — see `crates/dbx-core/src/db/postgres.rs`), so quoting the whole thing as @@ -2056,9 +2061,17 @@ fn postgres_index_column_sql(column: &str, is_expression: bool, opclass: Option< // The opclass is read separately from `pg_index.indclass` for every key position // (including expression keys) and appended uniformly — it never lives inside the // expression text, so there is no duplication risk. - match opclass.filter(|o| !o.is_empty()) { + let with_opclass = match opclass.filter(|o| !o.is_empty()) { Some(opc) => format!("{base} {opc}"), None => base, + }; + match key_options { + Some(options) => format!( + "{with_opclass} {} NULLS {}", + if options & 1 != 0 { "DESC" } else { "ASC" }, + if options & 2 != 0 { "FIRST" } else { "LAST" } + ), + None => with_opclass, } } @@ -2084,7 +2097,12 @@ fn generate_postgres_index_ddl(indexes: &[db::IndexInfo], table: &str, schema: & .map(|(i, column)| { let is_expr = index.key_is_expression.get(i).copied().unwrap_or(false); let opclass = index.column_opclasses.get(i).and_then(|o| o.as_deref()); - postgres_index_column_sql(column, is_expr, opclass) + let key_options = index + .index_type + .as_deref() + .filter(|index_type| index_type.eq_ignore_ascii_case("btree")) + .and_then(|_| index.key_options.get(i).copied()); + postgres_index_column_sql(column, is_expr, opclass, key_options) }) .collect::>() .join(", "); @@ -12828,6 +12846,7 @@ mod tests { comment: Some("lookup index".to_string()), key_is_expression: vec![true], column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }]; let foreign_keys = vec![ @@ -12882,6 +12901,7 @@ mod tests { comment: None, key_is_expression: vec![false], column_opclasses: vec![Some("gin_trgm_ops".to_string())], + key_options: Vec::new(), constraint_backed: false, }]; @@ -12906,6 +12926,7 @@ mod tests { comment: None, key_is_expression: vec![false, false], column_opclasses: vec![None, None], + key_options: Vec::new(), constraint_backed: false, }]; @@ -12936,6 +12957,7 @@ mod tests { comment: None, key_is_expression: vec![true], column_opclasses: vec![Some("gin_trgm_ops".to_string())], + key_options: Vec::new(), constraint_backed: false, }]; @@ -12961,6 +12983,7 @@ mod tests { comment: None, key_is_expression: vec![false, false], column_opclasses: vec![Some("text_pattern_ops".to_string()), None], + key_options: Vec::new(), constraint_backed: false, }]; @@ -12972,6 +12995,62 @@ mod tests { ); } + #[test] + fn postgres_index_ddl_preserves_per_key_ordering_and_include_columns() { + let indexes = vec![db::IndexInfo { + name: "event_order_idx".to_string(), + columns: vec![ + "created_at".to_string(), + "tenant_id".to_string(), + "score".to_string(), + "lower(payload)".to_string(), + ], + is_unique: false, + is_primary: false, + filter: None, + index_type: Some("btree".to_string()), + included_columns: Some(vec!["payload".to_string()]), + comment: None, + key_is_expression: vec![false, false, false, true], + column_opclasses: vec![None, None, None, None], + key_options: vec![1, 0, 2, 3], + constraint_backed: false, + }]; + + let sql = generate_postgres_index_ddl(&indexes, "event_log", "public"); + + assert_eq!( + sql, + vec!["CREATE INDEX IF NOT EXISTS \"event_order_idx\" ON \"public\".\"event_log\" USING btree (\"created_at\" DESC NULLS LAST, \"tenant_id\" ASC NULLS LAST, \"score\" ASC NULLS FIRST, lower(payload) DESC NULLS FIRST) INCLUDE (\"payload\")".to_string()] + ); + } + + #[test] + fn postgres_index_ddl_ignores_access_method_options_for_non_btree_indexes() { + let indexes = vec![db::IndexInfo { + name: "events_payload_idx".to_string(), + columns: vec!["payload".to_string()], + is_unique: false, + is_primary: false, + filter: None, + index_type: Some("gin".to_string()), + included_columns: None, + comment: None, + key_is_expression: vec![false], + column_opclasses: vec![None], + key_options: vec![3], + constraint_backed: false, + }]; + + let sql = generate_postgres_index_ddl(&indexes, "events", "public"); + + assert_eq!( + sql, + vec!["CREATE INDEX IF NOT EXISTS \"events_payload_idx\" ON \"public\".\"events\" USING gin (\"payload\")" + .to_string()] + ); + } + #[test] fn postgres_index_ddl_empty_column_opclasses_vec_falls_back_to_no_opclass() { let indexes = vec![db::IndexInfo { @@ -12985,6 +13064,7 @@ mod tests { comment: None, key_is_expression: vec![false], column_opclasses: vec![], + key_options: Vec::new(), constraint_backed: false, }]; diff --git a/crates/dbx-core/src/types.rs b/crates/dbx-core/src/types.rs index a4d81a834e..a14d9627e4 100644 --- a/crates/dbx-core/src/types.rs +++ b/crates/dbx-core/src/types.rs @@ -591,6 +591,11 @@ pub struct IndexInfo { /// `None` means the default operator class is used (can be omitted in DDL). #[serde(default, skip_serializing_if = "Vec::is_empty")] pub column_opclasses: Vec>, + /// Parallel to `columns`: PostgreSQL `pg_index.indoption` flags for each key. + /// Bit 0 is DESC and bit 1 is NULLS FIRST. Empty when the introspection source + /// does not expose per-key ordering metadata. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub key_options: Vec, /// `true` when this index is the object *behind* a table constraint (PRIMARY KEY or /// UNIQUE) rather than a standalone index. Dameng lists both kinds in `ALL_INDEXES` /// but only a standalone ("real") index accepts index-level DDL: a constraint-backed diff --git a/crates/dbx-core/tests/live_postgres_transfer.rs b/crates/dbx-core/tests/live_postgres_transfer.rs index 203494e06b..e27e8294f1 100644 --- a/crates/dbx-core/tests/live_postgres_transfer.rs +++ b/crates/dbx-core/tests/live_postgres_transfer.rs @@ -98,7 +98,7 @@ async fn query_index_rows(pool: &deadpool_postgres::Pool, schema: &str) -> Vec<( postgres::execute_query( pool, &format!( - "SELECT indexname, indexdef FROM pg_indexes WHERE schemaname = '{}' AND tablename = 'index_transfer' ORDER BY indexname", + "SELECT c.relname, pg_get_indexdef(i.indexrelid) FROM pg_catalog.pg_index i JOIN pg_catalog.pg_class t ON t.oid = i.indrelid JOIN pg_catalog.pg_class c ON c.oid = i.indexrelid JOIN pg_catalog.pg_namespace n ON n.oid = t.relnamespace WHERE n.nspname = '{}' AND t.relname = 'index_transfer' ORDER BY c.relname", schema ), ) @@ -369,6 +369,10 @@ async fn live_postgres_structure_only_preserves_table_indexes() { "CREATE INDEX \"index_transfer_status_include_idx\" ON \"{}\".\"index_transfer\" (\"status\") INCLUDE (\"created_at\")", source_schema ), + format!( + "CREATE INDEX \"index_transfer_order_idx\" ON \"{}\".\"index_transfer\" (\"created_at\" DESC NULLS LAST, \"email\", \"status\" NULLS FIRST, lower(\"email\") DESC) INCLUDE (\"id\")", + source_schema + ), format!( "COMMENT ON INDEX \"{}\".\"index_transfer_status_idx\" IS 'status lookup'", source_schema @@ -381,6 +385,7 @@ async fn live_postgres_structure_only_preserves_table_indexes() { ) .await .unwrap(); + let source_index_rows = query_index_rows(&source_pool, &source_schema).await; let dir = std::env::temp_dir().join(format!("dbx-live-structure-only-transfer-{}", uuid::Uuid::new_v4())); std::fs::create_dir_all(&dir).unwrap(); @@ -455,6 +460,12 @@ async fn live_postgres_structure_only_preserves_table_indexes() { let structure_and_data_index_comment = query_index_comment(&target_pool, &target_schema).await; let _ = postgres::execute_batch(&target_pool, &[cleanup_sql[1].clone()]).await; + state + .update_connection_pools(|connections| { + connections.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone())); + connections.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone())); + }) + .await; request.content = dbx_core::transfer::TransferContent::StructureOnly; transfer_postgres_schema_dependencies(&state, &request, &source_pool_key, &target_pool_key, |_| {}).await.unwrap(); let structure_only_result = transfer_table( @@ -494,6 +505,7 @@ async fn live_postgres_structure_only_preserves_table_indexes() { "index_transfer_email_lower_idx", "index_transfer_created_at_partial_idx", "index_transfer_status_include_idx", + "index_transfer_order_idx", ] { assert!(names.contains(&expected), "missing {expected}; target indexes: {names:?}"); } @@ -512,9 +524,33 @@ async fn live_postgres_structure_only_preserves_table_indexes() { rows.iter().any(|(name, definition)| name == "index_transfer_status_include_idx" && definition.contains("INCLUDE")), "target indexes: {rows:?}" ); + assert!( + rows.iter().any(|(name, definition)| name == "index_transfer_order_idx" + && definition.contains("created_at DESC NULLS LAST") + && definition.contains("status NULLS FIRST") + && definition.contains("lower(email) DESC") + && definition.contains("INCLUDE (id)")), + "target indexes: {rows:?}" + ); }; assert_indexes(&structure_and_data_index_rows); assert_indexes(&structure_only_index_rows); + let source_order_definition = source_index_rows + .iter() + .find(|(name, _)| name == "index_transfer_order_idx") + .map(|(_, definition)| definition.as_str()) + .expect("source whole-index definition"); + for rows in [&structure_and_data_index_rows, &structure_only_index_rows] { + let target_order_definition = rows + .iter() + .find(|(name, _)| name == "index_transfer_order_idx") + .map(|(_, definition)| definition.as_str()) + .expect("target whole-index definition"); + assert_eq!( + source_order_definition.replace(&source_schema, "normalized_schema"), + target_order_definition.replace(&target_schema, "normalized_schema") + ); + } assert_eq!(structure_and_data_index_comment, Some(json!("status lookup"))); assert_eq!(structure_only_index_comment, Some(json!("status lookup"))); } diff --git a/crates/dbx-core/tests/schema_diff_case_options.rs b/crates/dbx-core/tests/schema_diff_case_options.rs index 9fa35238c8..8bc2308335 100644 --- a/crates/dbx-core/tests/schema_diff_case_options.rs +++ b/crates/dbx-core/tests/schema_diff_case_options.rs @@ -29,6 +29,7 @@ fn index(name: &str, columns: &[&str]) -> IndexInfo { comment: None, key_is_expression: Vec::new(), column_opclasses: Vec::new(), + key_options: Vec::new(), constraint_backed: false, } }