refactor(workspace): split SQL and database driver crates
This commit is contained in:
180 files changed
+1422
-538
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,10 @@
|
||||
#![recursion_limit = "256"]
|
||||
|
||||
pub use dbx_sql_core::sql;
|
||||
pub use dbx_sql_dialect::sql_dialect;
|
||||
pub use dbx_types::{database_manifest, models, types};
|
||||
|
||||
pub mod db_admin_sql;
|
||||
pub mod schema_diff;
|
||||
pub mod sql_parser;
|
||||
pub mod table_structure_sql;
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,547 @@
|
||||
//! Runtime scripting for SQL Server column dependencies.
|
||||
//!
|
||||
//! SQL Server refuses `ALTER COLUMN` while a number of catalog objects depend
|
||||
//! on the column. The schema diff model intentionally omits unchanged
|
||||
//! objects, so the only complete source for those dependencies is the target
|
||||
//! database itself. This helper snapshots the supported definitions before
|
||||
//! dropping anything, performs the caller-provided alteration, and restores
|
||||
//! the objects in dependency order.
|
||||
|
||||
const DEPENDENCY_CAPTURE_AND_DROP_SQL: &str = r#"
|
||||
DECLARE @dbx_qualified_table nvarchar(517) =
|
||||
QUOTENAME(OBJECT_SCHEMA_NAME(@dbx_object_id)) + N'.' + QUOTENAME(OBJECT_NAME(@dbx_object_id));
|
||||
|
||||
DECLARE @dbx_unsupported_index sysname;
|
||||
SELECT TOP (1) @dbx_unsupported_index = idx.name
|
||||
FROM sys.indexes AS idx
|
||||
WHERE idx.object_id = @dbx_object_id
|
||||
AND idx.index_id > 0
|
||||
AND idx.name IS NOT NULL
|
||||
AND idx.is_hypothetical = 0
|
||||
AND idx.type NOT IN (1, 2)
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.index_columns AS ic
|
||||
WHERE ic.object_id = idx.object_id
|
||||
AND ic.index_id = idx.index_id
|
||||
AND ic.column_id = @dbx_column_id
|
||||
);
|
||||
IF @dbx_unsupported_index IS NOT NULL
|
||||
BEGIN
|
||||
DECLARE @dbx_unsupported_index_message nvarchar(2048) =
|
||||
N'DBX cannot safely preserve affected SQL Server index ' + QUOTENAME(@dbx_unsupported_index) +
|
||||
N' because its index type is not a supported rowstore type.';
|
||||
;THROW 50001, @dbx_unsupported_index_message, 1;
|
||||
END;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.indexes AS idx
|
||||
JOIN sys.data_spaces AS data_space ON data_space.data_space_id = idx.data_space_id
|
||||
WHERE idx.object_id = @dbx_object_id
|
||||
AND idx.type IN (1, 2)
|
||||
AND idx.is_hypothetical = 0
|
||||
AND data_space.type = N'PS'
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.index_columns AS affected_ic
|
||||
WHERE affected_ic.object_id = idx.object_id
|
||||
AND affected_ic.index_id = idx.index_id
|
||||
AND affected_ic.column_id = @dbx_column_id
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.index_columns AS partition_ic
|
||||
WHERE partition_ic.object_id = idx.object_id
|
||||
AND partition_ic.index_id = idx.index_id
|
||||
AND partition_ic.partition_ordinal > 0
|
||||
)
|
||||
)
|
||||
BEGIN
|
||||
;THROW 50002, 'DBX could not resolve the partition column for an affected SQL Server index.', 1;
|
||||
END;
|
||||
|
||||
DECLARE @dbx_started_transaction bit = 0;
|
||||
DECLARE @dbx_has_savepoint bit = 0;
|
||||
|
||||
BEGIN TRY
|
||||
IF @@TRANCOUNT = 0
|
||||
BEGIN
|
||||
BEGIN TRANSACTION;
|
||||
SET @dbx_started_transaction = 1;
|
||||
END
|
||||
ELSE
|
||||
BEGIN
|
||||
SAVE TRANSACTION dbx_alter_column_dependencies;
|
||||
SET @dbx_has_savepoint = 1;
|
||||
END;
|
||||
|
||||
DECLARE @dbx_dependencies TABLE (
|
||||
dependency_id int IDENTITY(1, 1) NOT NULL,
|
||||
drop_order tinyint NOT NULL,
|
||||
create_order tinyint NOT NULL,
|
||||
drop_sql nvarchar(max) NOT NULL,
|
||||
create_sql nvarchar(max) NOT NULL
|
||||
);
|
||||
|
||||
-- Foreign keys are captured first and dropped before key constraints. The
|
||||
-- predicate covers outgoing, inbound, composite, and self-referencing FKs.
|
||||
INSERT INTO @dbx_dependencies (drop_order, create_order, drop_sql, create_sql)
|
||||
SELECT
|
||||
10,
|
||||
50,
|
||||
N'ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(fk.parent_object_id)) + N'.' +
|
||||
QUOTENAME(OBJECT_NAME(fk.parent_object_id)) + N' DROP CONSTRAINT ' + QUOTENAME(fk.name) + N';',
|
||||
N'ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(fk.parent_object_id)) + N'.' +
|
||||
QUOTENAME(OBJECT_NAME(fk.parent_object_id)) +
|
||||
CASE WHEN fk.is_not_trusted = 1 THEN N' WITH NOCHECK' ELSE N' WITH CHECK' END +
|
||||
N' ADD CONSTRAINT ' + QUOTENAME(fk.name) + N' FOREIGN KEY (' +
|
||||
STUFF((
|
||||
SELECT N', ' + QUOTENAME(parent_column.name)
|
||||
FROM sys.foreign_key_columns AS fkc
|
||||
JOIN sys.columns AS parent_column
|
||||
ON parent_column.object_id = fkc.parent_object_id
|
||||
AND parent_column.column_id = fkc.parent_column_id
|
||||
WHERE fkc.constraint_object_id = fk.object_id
|
||||
ORDER BY fkc.constraint_column_id
|
||||
FOR XML PATH(''), TYPE
|
||||
).value('.', 'nvarchar(max)'), 1, 2, N'') + N') REFERENCES ' +
|
||||
QUOTENAME(OBJECT_SCHEMA_NAME(fk.referenced_object_id)) + N'.' +
|
||||
QUOTENAME(OBJECT_NAME(fk.referenced_object_id)) + N' (' +
|
||||
STUFF((
|
||||
SELECT N', ' + QUOTENAME(referenced_column.name)
|
||||
FROM sys.foreign_key_columns AS fkc
|
||||
JOIN sys.columns AS referenced_column
|
||||
ON referenced_column.object_id = fkc.referenced_object_id
|
||||
AND referenced_column.column_id = fkc.referenced_column_id
|
||||
WHERE fkc.constraint_object_id = fk.object_id
|
||||
ORDER BY fkc.constraint_column_id
|
||||
FOR XML PATH(''), TYPE
|
||||
).value('.', 'nvarchar(max)'), 1, 2, N'') + N')' +
|
||||
CASE WHEN fk.delete_referential_action_desc = N'NO_ACTION' THEN N''
|
||||
ELSE N' ON DELETE ' + REPLACE(fk.delete_referential_action_desc, N'_', N' ') END +
|
||||
CASE WHEN fk.update_referential_action_desc = N'NO_ACTION' THEN N''
|
||||
ELSE N' ON UPDATE ' + REPLACE(fk.update_referential_action_desc, N'_', N' ') END +
|
||||
CASE WHEN fk.is_not_for_replication = 1 THEN N' NOT FOR REPLICATION' ELSE N'' END + N'; ' +
|
||||
N'ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(fk.parent_object_id)) + N'.' +
|
||||
QUOTENAME(OBJECT_NAME(fk.parent_object_id)) +
|
||||
CASE WHEN fk.is_disabled = 1 THEN N' NOCHECK' ELSE N' CHECK' END +
|
||||
N' CONSTRAINT ' + QUOTENAME(fk.name) + N';'
|
||||
FROM sys.foreign_keys AS fk
|
||||
WHERE EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.foreign_key_columns AS fkc
|
||||
WHERE fkc.constraint_object_id = fk.object_id
|
||||
AND ((fkc.parent_object_id = @dbx_object_id AND fkc.parent_column_id = @dbx_column_id)
|
||||
OR (fkc.referenced_object_id = @dbx_object_id AND fkc.referenced_column_id = @dbx_column_id))
|
||||
);
|
||||
|
||||
-- parent_column_id identifies column-level checks. Table-level checks use
|
||||
-- expression dependencies to avoid dropping unrelated constraints.
|
||||
INSERT INTO @dbx_dependencies (drop_order, create_order, drop_sql, create_sql)
|
||||
SELECT
|
||||
20,
|
||||
40,
|
||||
N'ALTER TABLE ' + @dbx_qualified_table + N' DROP CONSTRAINT ' + QUOTENAME(cc.name) + N';',
|
||||
N'ALTER TABLE ' + @dbx_qualified_table +
|
||||
CASE WHEN cc.is_not_trusted = 1 THEN N' WITH NOCHECK' ELSE N' WITH CHECK' END +
|
||||
N' ADD CONSTRAINT ' + QUOTENAME(cc.name) + N' CHECK ' +
|
||||
CASE WHEN cc.is_not_for_replication = 1 THEN N'NOT FOR REPLICATION ' ELSE N'' END +
|
||||
cc.definition + N'; ALTER TABLE ' + @dbx_qualified_table +
|
||||
CASE WHEN cc.is_disabled = 1 THEN N' NOCHECK' ELSE N' CHECK' END +
|
||||
N' CONSTRAINT ' + QUOTENAME(cc.name) + N';'
|
||||
FROM sys.check_constraints AS cc
|
||||
WHERE cc.parent_object_id = @dbx_object_id
|
||||
AND (cc.parent_column_id = @dbx_column_id OR EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.sql_expression_dependencies AS sed
|
||||
WHERE sed.referencing_id = cc.object_id
|
||||
AND sed.referenced_id = @dbx_object_id
|
||||
AND sed.referenced_minor_id = @dbx_column_id
|
||||
));
|
||||
|
||||
-- Index-owned statistics disappear with their index. Only independent,
|
||||
-- user-created statistics need an explicit drop and recreation.
|
||||
INSERT INTO @dbx_dependencies (drop_order, create_order, drop_sql, create_sql)
|
||||
SELECT
|
||||
30,
|
||||
30,
|
||||
N'DROP STATISTICS ' + @dbx_qualified_table + N'.' + QUOTENAME(stats.name) + N';',
|
||||
N'CREATE STATISTICS ' + QUOTENAME(stats.name) + N' ON ' + @dbx_qualified_table + N' (' +
|
||||
STUFF((
|
||||
SELECT N', ' + QUOTENAME(stats_column.name)
|
||||
FROM sys.stats_columns AS sc
|
||||
JOIN sys.columns AS stats_column
|
||||
ON stats_column.object_id = sc.object_id
|
||||
AND stats_column.column_id = sc.column_id
|
||||
WHERE sc.object_id = stats.object_id
|
||||
AND sc.stats_id = stats.stats_id
|
||||
ORDER BY sc.stats_column_id
|
||||
FOR XML PATH(''), TYPE
|
||||
).value('.', 'nvarchar(max)'), 1, 2, N'') + N')' +
|
||||
CASE WHEN stats.has_filter = 1 THEN N' WHERE ' + stats.filter_definition ELSE N'' END +
|
||||
CASE WHEN stats.no_recompute = 1 THEN N' WITH NORECOMPUTE' ELSE N'' END + N';'
|
||||
FROM sys.stats AS stats
|
||||
WHERE stats.object_id = @dbx_object_id
|
||||
AND stats.user_created = 1
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.stats_columns AS sc
|
||||
WHERE sc.object_id = stats.object_id
|
||||
AND sc.stats_id = stats.stats_id
|
||||
AND sc.column_id = @dbx_column_id
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.indexes AS matching_index
|
||||
WHERE matching_index.object_id = stats.object_id
|
||||
AND matching_index.index_id = stats.stats_id
|
||||
);
|
||||
|
||||
-- Ordinary clustered/nonclustered rowstore indexes. Explicit key order,
|
||||
-- INCLUDE columns, filters, durable catalog options, data space, and the
|
||||
-- disabled state are reconstructed from the live target catalog.
|
||||
INSERT INTO @dbx_dependencies (drop_order, create_order, drop_sql, create_sql)
|
||||
SELECT
|
||||
40,
|
||||
20,
|
||||
N'DROP INDEX ' + QUOTENAME(idx.name) + N' ON ' + @dbx_qualified_table + N';',
|
||||
N'CREATE ' + CASE WHEN idx.is_unique = 1 THEN N'UNIQUE ' ELSE N'' END + idx.type_desc +
|
||||
N' INDEX ' + QUOTENAME(idx.name) + N' ON ' + @dbx_qualified_table + N' (' +
|
||||
STUFF((
|
||||
SELECT N', ' + QUOTENAME(index_column.name) +
|
||||
CASE WHEN ic.is_descending_key = 1 THEN N' DESC' ELSE N' ASC' END
|
||||
FROM sys.index_columns AS ic
|
||||
JOIN sys.columns AS index_column
|
||||
ON index_column.object_id = ic.object_id
|
||||
AND index_column.column_id = ic.column_id
|
||||
WHERE ic.object_id = idx.object_id
|
||||
AND ic.index_id = idx.index_id
|
||||
AND ic.key_ordinal > 0
|
||||
ORDER BY ic.key_ordinal
|
||||
FOR XML PATH(''), TYPE
|
||||
).value('.', 'nvarchar(max)'), 1, 2, N'') + N')' +
|
||||
CASE WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.index_columns AS include_check
|
||||
WHERE include_check.object_id = idx.object_id
|
||||
AND include_check.index_id = idx.index_id
|
||||
AND include_check.is_included_column = 1
|
||||
) THEN N' INCLUDE (' + STUFF((
|
||||
SELECT N', ' + QUOTENAME(include_column.name)
|
||||
FROM sys.index_columns AS include_ic
|
||||
JOIN sys.columns AS include_column
|
||||
ON include_column.object_id = include_ic.object_id
|
||||
AND include_column.column_id = include_ic.column_id
|
||||
WHERE include_ic.object_id = idx.object_id
|
||||
AND include_ic.index_id = idx.index_id
|
||||
AND include_ic.is_included_column = 1
|
||||
ORDER BY include_ic.index_column_id
|
||||
FOR XML PATH(''), TYPE
|
||||
).value('.', 'nvarchar(max)'), 1, 2, N'') + N')' ELSE N'' END +
|
||||
CASE WHEN idx.has_filter = 1 THEN N' WHERE ' + idx.filter_definition ELSE N'' END +
|
||||
N' WITH (PAD_INDEX = ' + CASE WHEN idx.is_padded = 1 THEN N'ON' ELSE N'OFF' END +
|
||||
CASE WHEN idx.fill_factor > 0
|
||||
THEN N', FILLFACTOR = ' + CONVERT(nvarchar(3), idx.fill_factor)
|
||||
ELSE N'' END +
|
||||
N', IGNORE_DUP_KEY = ' + CASE WHEN idx.ignore_dup_key = 1 THEN N'ON' ELSE N'OFF' END +
|
||||
N', STATISTICS_NORECOMPUTE = ' + CASE WHEN index_stats.no_recompute = 1 THEN N'ON' ELSE N'OFF' END +
|
||||
N', ALLOW_ROW_LOCKS = ' + CASE WHEN idx.allow_row_locks = 1 THEN N'ON' ELSE N'OFF' END +
|
||||
N', ALLOW_PAGE_LOCKS = ' + CASE WHEN idx.allow_page_locks = 1 THEN N'ON' ELSE N'OFF' END + N')' +
|
||||
CASE WHEN data_space.type = N'FG' THEN N' ON ' + QUOTENAME(data_space.name)
|
||||
WHEN data_space.type = N'PS' THEN N' ON ' + QUOTENAME(data_space.name) + N' (' +
|
||||
QUOTENAME((
|
||||
SELECT TOP (1) partition_column.name
|
||||
FROM sys.index_columns AS partition_ic
|
||||
JOIN sys.columns AS partition_column
|
||||
ON partition_column.object_id = partition_ic.object_id
|
||||
AND partition_column.column_id = partition_ic.column_id
|
||||
WHERE partition_ic.object_id = idx.object_id
|
||||
AND partition_ic.index_id = idx.index_id
|
||||
AND partition_ic.partition_ordinal > 0
|
||||
ORDER BY partition_ic.partition_ordinal
|
||||
)) + N')'
|
||||
ELSE N'' END + N';' +
|
||||
CASE WHEN idx.is_disabled = 1 THEN N' ALTER INDEX ' + QUOTENAME(idx.name) +
|
||||
N' ON ' + @dbx_qualified_table + N' DISABLE;' ELSE N'' END
|
||||
FROM sys.indexes AS idx
|
||||
LEFT JOIN sys.stats AS index_stats
|
||||
ON index_stats.object_id = idx.object_id
|
||||
AND index_stats.stats_id = idx.index_id
|
||||
LEFT JOIN sys.data_spaces AS data_space ON data_space.data_space_id = idx.data_space_id
|
||||
WHERE idx.object_id = @dbx_object_id
|
||||
AND idx.index_id > 0
|
||||
AND idx.name IS NOT NULL
|
||||
AND idx.type IN (1, 2)
|
||||
AND idx.is_hypothetical = 0
|
||||
AND idx.is_primary_key = 0
|
||||
AND idx.is_unique_constraint = 0
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.index_columns AS affected_ic
|
||||
WHERE affected_ic.object_id = idx.object_id
|
||||
AND affected_ic.index_id = idx.index_id
|
||||
AND affected_ic.column_id = @dbx_column_id
|
||||
);
|
||||
|
||||
-- PRIMARY KEY and UNIQUE constraints must be dropped/created as constraints,
|
||||
-- never as ordinary unique indexes.
|
||||
INSERT INTO @dbx_dependencies (drop_order, create_order, drop_sql, create_sql)
|
||||
SELECT
|
||||
50,
|
||||
10,
|
||||
N'ALTER TABLE ' + @dbx_qualified_table + N' DROP CONSTRAINT ' + QUOTENAME(key_constraint.name) + N';',
|
||||
N'ALTER TABLE ' + @dbx_qualified_table + N' ADD CONSTRAINT ' + QUOTENAME(key_constraint.name) + N' ' +
|
||||
CASE WHEN key_constraint.type = N'PK' THEN N'PRIMARY KEY ' ELSE N'UNIQUE ' END +
|
||||
idx.type_desc + N' (' +
|
||||
STUFF((
|
||||
SELECT N', ' + QUOTENAME(key_column.name) +
|
||||
CASE WHEN ic.is_descending_key = 1 THEN N' DESC' ELSE N' ASC' END
|
||||
FROM sys.index_columns AS ic
|
||||
JOIN sys.columns AS key_column
|
||||
ON key_column.object_id = ic.object_id
|
||||
AND key_column.column_id = ic.column_id
|
||||
WHERE ic.object_id = idx.object_id
|
||||
AND ic.index_id = idx.index_id
|
||||
AND ic.key_ordinal > 0
|
||||
ORDER BY ic.key_ordinal
|
||||
FOR XML PATH(''), TYPE
|
||||
).value('.', 'nvarchar(max)'), 1, 2, N'') + N')' +
|
||||
N' WITH (PAD_INDEX = ' + CASE WHEN idx.is_padded = 1 THEN N'ON' ELSE N'OFF' END +
|
||||
CASE WHEN idx.fill_factor > 0
|
||||
THEN N', FILLFACTOR = ' + CONVERT(nvarchar(3), idx.fill_factor)
|
||||
ELSE N'' END +
|
||||
N', IGNORE_DUP_KEY = ' + CASE WHEN idx.ignore_dup_key = 1 THEN N'ON' ELSE N'OFF' END +
|
||||
N', STATISTICS_NORECOMPUTE = ' + CASE WHEN key_stats.no_recompute = 1 THEN N'ON' ELSE N'OFF' END +
|
||||
N', ALLOW_ROW_LOCKS = ' + CASE WHEN idx.allow_row_locks = 1 THEN N'ON' ELSE N'OFF' END +
|
||||
N', ALLOW_PAGE_LOCKS = ' + CASE WHEN idx.allow_page_locks = 1 THEN N'ON' ELSE N'OFF' END + N')' +
|
||||
CASE WHEN data_space.type = N'FG' THEN N' ON ' + QUOTENAME(data_space.name)
|
||||
WHEN data_space.type = N'PS' THEN N' ON ' + QUOTENAME(data_space.name) + N' (' +
|
||||
QUOTENAME((
|
||||
SELECT TOP (1) partition_column.name
|
||||
FROM sys.index_columns AS partition_ic
|
||||
JOIN sys.columns AS partition_column
|
||||
ON partition_column.object_id = partition_ic.object_id
|
||||
AND partition_column.column_id = partition_ic.column_id
|
||||
WHERE partition_ic.object_id = idx.object_id
|
||||
AND partition_ic.index_id = idx.index_id
|
||||
AND partition_ic.partition_ordinal > 0
|
||||
ORDER BY partition_ic.partition_ordinal
|
||||
)) + N')'
|
||||
ELSE N'' END + N';' +
|
||||
CASE WHEN idx.is_disabled = 1 THEN N' ALTER INDEX ' + QUOTENAME(idx.name) +
|
||||
N' ON ' + @dbx_qualified_table + N' DISABLE;' ELSE N'' END
|
||||
FROM sys.key_constraints AS key_constraint
|
||||
JOIN sys.indexes AS idx
|
||||
ON idx.object_id = key_constraint.parent_object_id
|
||||
AND idx.index_id = key_constraint.unique_index_id
|
||||
LEFT JOIN sys.stats AS key_stats
|
||||
ON key_stats.object_id = idx.object_id
|
||||
AND key_stats.stats_id = idx.index_id
|
||||
LEFT JOIN sys.data_spaces AS data_space ON data_space.data_space_id = idx.data_space_id
|
||||
WHERE key_constraint.parent_object_id = @dbx_object_id
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.index_columns AS affected_ic
|
||||
WHERE affected_ic.object_id = idx.object_id
|
||||
AND affected_ic.index_id = idx.index_id
|
||||
AND affected_ic.column_id = @dbx_column_id
|
||||
);
|
||||
|
||||
DECLARE @dbx_dependency_sql nvarchar(max);
|
||||
DECLARE dbx_dependency_drop_cursor CURSOR LOCAL FAST_FORWARD FOR
|
||||
SELECT drop_sql
|
||||
FROM @dbx_dependencies
|
||||
ORDER BY drop_order, dependency_id;
|
||||
OPEN dbx_dependency_drop_cursor;
|
||||
FETCH NEXT FROM dbx_dependency_drop_cursor INTO @dbx_dependency_sql;
|
||||
WHILE @@FETCH_STATUS = 0
|
||||
BEGIN
|
||||
EXEC sys.sp_executesql @dbx_dependency_sql;
|
||||
FETCH NEXT FROM dbx_dependency_drop_cursor INTO @dbx_dependency_sql;
|
||||
END;
|
||||
CLOSE dbx_dependency_drop_cursor;
|
||||
DEALLOCATE dbx_dependency_drop_cursor;
|
||||
"#;
|
||||
|
||||
const DEPENDENCY_RECREATE_AND_FINISH_SQL: &str = r#"
|
||||
DECLARE dbx_dependency_create_cursor CURSOR LOCAL FAST_FORWARD FOR
|
||||
SELECT create_sql
|
||||
FROM @dbx_dependencies
|
||||
ORDER BY create_order, dependency_id;
|
||||
OPEN dbx_dependency_create_cursor;
|
||||
FETCH NEXT FROM dbx_dependency_create_cursor INTO @dbx_dependency_sql;
|
||||
WHILE @@FETCH_STATUS = 0
|
||||
BEGIN
|
||||
EXEC sys.sp_executesql @dbx_dependency_sql;
|
||||
FETCH NEXT FROM dbx_dependency_create_cursor INTO @dbx_dependency_sql;
|
||||
END;
|
||||
CLOSE dbx_dependency_create_cursor;
|
||||
DEALLOCATE dbx_dependency_create_cursor;
|
||||
|
||||
IF @dbx_started_transaction = 1
|
||||
COMMIT TRANSACTION;
|
||||
END TRY
|
||||
BEGIN CATCH
|
||||
IF CURSOR_STATUS('local', 'dbx_dependency_drop_cursor') >= 0
|
||||
CLOSE dbx_dependency_drop_cursor;
|
||||
IF CURSOR_STATUS('local', 'dbx_dependency_drop_cursor') >= -1
|
||||
DEALLOCATE dbx_dependency_drop_cursor;
|
||||
IF CURSOR_STATUS('local', 'dbx_dependency_create_cursor') >= 0
|
||||
CLOSE dbx_dependency_create_cursor;
|
||||
IF CURSOR_STATUS('local', 'dbx_dependency_create_cursor') >= -1
|
||||
DEALLOCATE dbx_dependency_create_cursor;
|
||||
|
||||
IF @dbx_started_transaction = 1 AND XACT_STATE() <> 0
|
||||
ROLLBACK TRANSACTION;
|
||||
ELSE IF @dbx_has_savepoint = 1 AND XACT_STATE() = 1
|
||||
ROLLBACK TRANSACTION dbx_alter_column_dependencies;
|
||||
|
||||
THROW;
|
||||
END CATCH;
|
||||
"#;
|
||||
|
||||
fn escape_tsql_string_literal(value: &str) -> String {
|
||||
value.replace('\'', "''")
|
||||
}
|
||||
|
||||
/// Build one executable T-SQL batch that preserves live dependencies around an
|
||||
/// `ALTER COLUMN` operation.
|
||||
///
|
||||
/// `table` is the already-quoted target table name used by generated DDL (for
|
||||
/// example `[dbo].[orders]`). `column_name` is the unquoted catalog name.
|
||||
/// `alter_batch` may contain more than one statement, such as the existing
|
||||
/// default-constraint preservation batch followed by `ALTER COLUMN`.
|
||||
pub(super) fn build_dependency_aware_alter_column_batch(table: &str, column_name: &str, alter_batch: &str) -> String {
|
||||
let table_literal = escape_tsql_string_literal(table);
|
||||
let column_literal = escape_tsql_string_literal(column_name);
|
||||
let alter_batch = alter_batch.trim();
|
||||
|
||||
let mut batch = String::with_capacity(
|
||||
DEPENDENCY_CAPTURE_AND_DROP_SQL.len() + DEPENDENCY_RECREATE_AND_FINISH_SQL.len() + alter_batch.len() + 512,
|
||||
);
|
||||
batch.push_str(
|
||||
"SET ANSI_NULLS ON;\n\
|
||||
SET QUOTED_IDENTIFIER ON;\n\
|
||||
SET ANSI_PADDING ON;\n\
|
||||
SET ANSI_WARNINGS ON;\n\
|
||||
SET CONCAT_NULL_YIELDS_NULL ON;\n\
|
||||
SET ARITHABORT ON;\n\
|
||||
SET NUMERIC_ROUNDABORT OFF;\n\
|
||||
DECLARE @dbx_object_id int = OBJECT_ID(N'",
|
||||
);
|
||||
batch.push_str(&table_literal);
|
||||
batch.push_str("');\nDECLARE @dbx_column_id int = COLUMNPROPERTY(@dbx_object_id, N'");
|
||||
batch.push_str(&column_literal);
|
||||
batch.push_str("', 'ColumnId');\nIF @dbx_object_id IS NULL OR @dbx_column_id IS NULL\n");
|
||||
batch.push_str(" THROW 50000, 'DBX could not resolve the SQL Server column before ALTER COLUMN.', 1;\n");
|
||||
batch.push_str(DEPENDENCY_CAPTURE_AND_DROP_SQL);
|
||||
batch.push('\n');
|
||||
batch.push_str(" ");
|
||||
batch.push_str(alter_batch);
|
||||
if !alter_batch.ends_with(';') {
|
||||
batch.push(';');
|
||||
}
|
||||
batch.push('\n');
|
||||
batch.push_str(DEPENDENCY_RECREATE_AND_FINISH_SQL);
|
||||
batch
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn dependency_batch_escapes_catalog_literals_and_keeps_alter_sql() {
|
||||
let sql = build_dependency_aware_alter_column_batch(
|
||||
"[odd].[O'Brien]",
|
||||
"amount's value",
|
||||
"ALTER TABLE [odd].[O'Brien] ALTER COLUMN [amount's value] bigint NULL;",
|
||||
);
|
||||
|
||||
assert!(sql.contains("OBJECT_ID(N'[odd].[O''Brien]')"), "{sql}");
|
||||
assert!(sql.contains("COLUMNPROPERTY(@dbx_object_id, N'amount''s value', 'ColumnId')"), "{sql}");
|
||||
assert!(sql.contains("ALTER TABLE [odd].[O'Brien] ALTER COLUMN [amount's value] bigint NULL;"), "{sql}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dependency_batch_covers_every_supported_dependency_kind() {
|
||||
let sql = build_dependency_aware_alter_column_batch(
|
||||
"[dbo].[orders]",
|
||||
"amount",
|
||||
"ALTER TABLE [dbo].[orders] ALTER COLUMN [amount] bigint NOT NULL;",
|
||||
);
|
||||
|
||||
for catalog in [
|
||||
"sys.foreign_keys AS fk",
|
||||
"sys.foreign_key_columns AS fkc",
|
||||
"sys.check_constraints AS cc",
|
||||
"sys.sql_expression_dependencies AS sed",
|
||||
"sys.stats_columns AS sc",
|
||||
"sys.indexes AS idx",
|
||||
"sys.key_constraints AS key_constraint",
|
||||
] {
|
||||
assert!(sql.contains(catalog), "missing {catalog}: {sql}");
|
||||
}
|
||||
|
||||
assert!(sql.contains("fkc.referenced_object_id = @dbx_object_id"), "inbound FK predicate: {sql}");
|
||||
assert!(sql.contains("ORDER BY fkc.constraint_column_id"), "composite FK order: {sql}");
|
||||
assert!(sql.contains("fk.delete_referential_action_desc"), "delete action: {sql}");
|
||||
assert!(sql.contains("fk.update_referential_action_desc"), "update action: {sql}");
|
||||
assert!(sql.contains("fk.is_not_for_replication"), "NFR state: {sql}");
|
||||
assert!(sql.contains("fk.is_not_trusted"), "trust state: {sql}");
|
||||
assert!(sql.contains("fk.is_disabled"), "enabled state: {sql}");
|
||||
assert!(sql.contains("stats.user_created = 1"), "independent statistics: {sql}");
|
||||
assert!(sql.contains("stats.no_recompute"), "statistics options: {sql}");
|
||||
assert!(sql.contains("ic.is_descending_key"), "index key order: {sql}");
|
||||
assert!(sql.contains("include_ic.is_included_column = 1"), "included columns: {sql}");
|
||||
assert!(sql.contains("idx.filter_definition"), "filtered index: {sql}");
|
||||
assert!(sql.contains("idx.allow_row_locks"), "index options: {sql}");
|
||||
assert!(sql.contains("data_space.type = N'PS'"), "partition data space: {sql}");
|
||||
assert!(sql.contains("idx.is_disabled = 1"), "disabled index state: {sql}");
|
||||
assert!(sql.contains("key_constraint.type = N'PK'"), "primary key form: {sql}");
|
||||
assert!(sql.contains("THEN N'PRIMARY KEY ' ELSE N'UNIQUE ' END"), "PK/UQ distinction: {sql}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dependency_batch_rejects_unsupported_indexes_before_dropping_anything() {
|
||||
let sql = build_dependency_aware_alter_column_batch(
|
||||
"[dbo].[events]",
|
||||
"payload",
|
||||
"ALTER TABLE [dbo].[events] ALTER COLUMN [payload] varbinary(max) NULL;",
|
||||
);
|
||||
|
||||
let guard = sql.find("idx.type NOT IN (1, 2)").expect("unsupported-index guard");
|
||||
let rejection = sql.find("THROW 50001").expect("unsupported-index THROW");
|
||||
let drop_cursor = sql.find("dbx_dependency_drop_cursor CURSOR").expect("drop cursor");
|
||||
assert!(guard < rejection && rejection < drop_cursor, "guard must run before every dependency drop: {sql}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dependency_batch_is_atomic_and_recreates_after_the_alter() {
|
||||
let alter = "ALTER TABLE [dbo].[metrics] ALTER COLUMN [value] decimal(18,2) NOT NULL;";
|
||||
let sql = build_dependency_aware_alter_column_batch("[dbo].[metrics]", "value", alter);
|
||||
|
||||
let drop_cursor = sql.find("dbx_dependency_drop_cursor CURSOR").expect("drop cursor");
|
||||
let alter_position = sql.find(alter).expect("alter statement");
|
||||
let create_cursor = sql.find("dbx_dependency_create_cursor CURSOR").expect("create cursor");
|
||||
assert!(drop_cursor < alter_position && alter_position < create_cursor, "drop/alter/create order: {sql}");
|
||||
assert!(sql.contains("BEGIN TRY"), "{sql}");
|
||||
assert!(sql.starts_with("SET ANSI_NULLS ON;\nSET QUOTED_IDENTIFIER ON;"), "{sql}");
|
||||
assert!(sql.contains("SAVE TRANSACTION dbx_alter_column_dependencies"), "{sql}");
|
||||
assert!(sql.contains("ROLLBACK TRANSACTION dbx_alter_column_dependencies"), "{sql}");
|
||||
assert!(sql.contains("IF @dbx_started_transaction = 1\n COMMIT TRANSACTION"), "{sql}");
|
||||
assert!(sql.contains("THROW;\nEND CATCH"), "{sql}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dependency_batch_terminates_an_unterminated_alter_batch_once() {
|
||||
let sql = build_dependency_aware_alter_column_batch(
|
||||
"[dbo].[metrics]",
|
||||
"value",
|
||||
"ALTER TABLE [dbo].[metrics] ALTER COLUMN [value] bigint NULL",
|
||||
);
|
||||
assert!(sql.contains("ALTER TABLE [dbo].[metrics] ALTER COLUMN [value] bigint NULL;\n"), "{sql}");
|
||||
assert!(!sql.contains("bigint NULL;;"), "{sql}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlparser::ast::Statement;
|
||||
use sqlparser::dialect::GenericDialect;
|
||||
use sqlparser::parser::Parser;
|
||||
|
||||
use crate::schema_diff::SchemaDiffPreparationOptions;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum FilterAction {
|
||||
Allow,
|
||||
Deny(String),
|
||||
}
|
||||
|
||||
pub trait AstFilter {
|
||||
fn filter_statement(&self, stmt: &Statement) -> FilterAction;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FilterResult {
|
||||
pub allowed: Vec<String>,
|
||||
pub denied: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SandboxResult {
|
||||
pub allowed: Vec<String>,
|
||||
pub denied: Vec<String>,
|
||||
pub nesting_violations: Vec<String>,
|
||||
pub is_pure: bool,
|
||||
pub stats: SandboxStats,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum SandboxMode {
|
||||
Permissive,
|
||||
#[default]
|
||||
Strict,
|
||||
Isolation,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SandboxStats {
|
||||
pub total_statements: usize,
|
||||
pub allowed_count: usize,
|
||||
pub denied_count: usize,
|
||||
pub mode: String,
|
||||
pub max_nesting_depth: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AstSandbox {
|
||||
pub mode: SandboxMode,
|
||||
pub max_nesting_depth: usize,
|
||||
}
|
||||
|
||||
impl Default for AstSandbox {
|
||||
fn default() -> Self {
|
||||
Self { mode: SandboxMode::Strict, max_nesting_depth: 10 }
|
||||
}
|
||||
}
|
||||
|
||||
impl AstSandbox {
|
||||
pub fn new(mode: SandboxMode, max_nesting_depth: usize) -> Self {
|
||||
Self { mode, max_nesting_depth }
|
||||
}
|
||||
|
||||
pub fn filter_sql(&self, sql: &str, dialect_str: &str) -> Result<SandboxResult, String> {
|
||||
let base = AstTransmitFilter::filter_sql(sql, dialect_str)?;
|
||||
|
||||
let stats = SandboxStats {
|
||||
total_statements: base.allowed.len() + base.denied.len(),
|
||||
allowed_count: base.allowed.len(),
|
||||
denied_count: base.denied.len(),
|
||||
mode: match self.mode {
|
||||
SandboxMode::Permissive => "permissive".to_string(),
|
||||
SandboxMode::Strict => "strict".to_string(),
|
||||
SandboxMode::Isolation => "isolation".to_string(),
|
||||
},
|
||||
max_nesting_depth: self.max_nesting_depth,
|
||||
};
|
||||
|
||||
let (further_blocked, nest_violations) = match self.mode {
|
||||
SandboxMode::Permissive => (Vec::new(), Vec::new()),
|
||||
SandboxMode::Strict | SandboxMode::Isolation => self.check_nesting(&base.allowed, self.max_nesting_depth),
|
||||
};
|
||||
|
||||
let is_pure = match self.mode {
|
||||
SandboxMode::Permissive => base.denied.is_empty(),
|
||||
SandboxMode::Strict => base.denied.is_empty() && further_blocked.is_empty(),
|
||||
SandboxMode::Isolation => {
|
||||
base.denied.is_empty() && further_blocked.is_empty() && nest_violations.is_empty()
|
||||
}
|
||||
};
|
||||
|
||||
Ok(SandboxResult {
|
||||
allowed: base.allowed,
|
||||
denied: base.denied.iter().chain(further_blocked.iter()).cloned().collect(),
|
||||
nesting_violations: nest_violations,
|
||||
is_pure,
|
||||
stats,
|
||||
})
|
||||
}
|
||||
|
||||
fn check_nesting(&self, statements: &[String], max_depth: usize) -> (Vec<String>, Vec<String>) {
|
||||
let mut further_blocked = Vec::new();
|
||||
let mut violations = Vec::new();
|
||||
for stmt in statements {
|
||||
let depth = self.count_nesting(stmt);
|
||||
if depth > max_depth {
|
||||
further_blocked.push(stmt.clone());
|
||||
violations.push(format!("Nesting depth {depth} exceeds max {max_depth}: {stmt}"));
|
||||
}
|
||||
}
|
||||
(further_blocked, violations)
|
||||
}
|
||||
|
||||
fn count_nesting(&self, stmt: &str) -> usize {
|
||||
let mut max_depth = 0usize;
|
||||
let mut current = 0usize;
|
||||
for ch in stmt.chars() {
|
||||
match ch {
|
||||
'(' => {
|
||||
current += 1;
|
||||
max_depth = max_depth.max(current);
|
||||
}
|
||||
')' => {
|
||||
current = current.saturating_sub(1);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
max_depth
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AstTransmitFilter;
|
||||
|
||||
impl AstTransmitFilter {
|
||||
pub fn filter_sql(sql: &str, dialect_str: &str) -> Result<FilterResult, String> {
|
||||
let dialect = resolve_dialect(dialect_str);
|
||||
let stmts =
|
||||
Parser::parse_sql(dialect.as_ref(), sql).map_err(|e| format!("SQL parse error in AST filter: {e}"))?;
|
||||
|
||||
let filter = Self;
|
||||
let mut allowed = Vec::new();
|
||||
let mut denied: Vec<String> = Vec::new();
|
||||
|
||||
for stmt in &stmts {
|
||||
match filter.filter_statement(stmt) {
|
||||
FilterAction::Allow => {
|
||||
allowed.push(stmt.to_string());
|
||||
}
|
||||
FilterAction::Deny(reason) => {
|
||||
denied.push(format!("Blocked: {reason} — {stmt}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(FilterResult { allowed, denied })
|
||||
}
|
||||
|
||||
fn is_dangerous_body_node(&self, stmt: &Statement) -> bool {
|
||||
matches!(
|
||||
stmt,
|
||||
Statement::CreateFunction { .. } | Statement::CreateProcedure { .. } | Statement::CreateTrigger { .. }
|
||||
)
|
||||
}
|
||||
|
||||
/// Pass-through for schema-diff preparation options.
|
||||
///
|
||||
/// Routine definitions are compared as metadata and must be preserved here.
|
||||
/// Dangerous statement bodies are still blocked by `filter_sql` /
|
||||
/// `is_dangerous_body_node` when SQL is whitelisted for transmit/sync.
|
||||
pub fn filter_diff_preparation_options(
|
||||
options: SchemaDiffPreparationOptions,
|
||||
_dialect: &str,
|
||||
) -> SchemaDiffPreparationOptions {
|
||||
options
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_dialect(dialect: &str) -> Box<dyn sqlparser::dialect::Dialect> {
|
||||
match dialect.to_ascii_lowercase().as_str() {
|
||||
"postgres" | "postgresql" => Box::new(sqlparser::dialect::PostgreSqlDialect {}),
|
||||
"mysql" | "mariadb" | "tidb" => Box::new(sqlparser::dialect::MySqlDialect {}),
|
||||
"sqlite" => Box::new(sqlparser::dialect::SQLiteDialect {}),
|
||||
"sqlserver" | "mssql" => Box::new(sqlparser::dialect::MsSqlDialect {}),
|
||||
"clickhouse" => Box::new(sqlparser::dialect::ClickHouseDialect {}),
|
||||
"duckdb" => Box::new(sqlparser::dialect::DuckDbDialect {}),
|
||||
_ => Box::new(GenericDialect {}),
|
||||
}
|
||||
}
|
||||
|
||||
impl AstFilter for AstTransmitFilter {
|
||||
fn filter_statement(&self, stmt: &Statement) -> FilterAction {
|
||||
if self.is_dangerous_body_node(stmt) {
|
||||
return FilterAction::Deny("Function/procedure/trigger body not allowed in schema diff".into());
|
||||
}
|
||||
|
||||
match stmt {
|
||||
Statement::CreateTable { .. }
|
||||
| Statement::CreateView { .. }
|
||||
| Statement::CreateIndex { .. }
|
||||
| Statement::CreateSchema { .. }
|
||||
| Statement::CreateSequence { .. }
|
||||
| Statement::AlterTable { .. }
|
||||
| Statement::AlterIndex { .. }
|
||||
| Statement::AlterView { .. }
|
||||
| Statement::Drop { .. }
|
||||
| Statement::Truncate { .. } => FilterAction::Allow,
|
||||
|
||||
_ => FilterAction::Deny("Statement type not in whitelist for schema diff".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::types::FunctionInfo;
|
||||
|
||||
#[test]
|
||||
fn allows_create_table() {
|
||||
let result =
|
||||
AstTransmitFilter::filter_sql("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100))", "generic")
|
||||
.unwrap();
|
||||
assert!(!result.allowed.is_empty());
|
||||
assert!(result.allowed[0].contains("CREATE TABLE"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allows_create_index() {
|
||||
let result = AstTransmitFilter::filter_sql("CREATE INDEX idx_users_name ON users (name)", "generic").unwrap();
|
||||
assert!(!result.allowed.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allows_alter_table() {
|
||||
let result = AstTransmitFilter::filter_sql("ALTER TABLE users ADD COLUMN age INT", "generic").unwrap();
|
||||
assert!(!result.allowed.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allows_drop_table() {
|
||||
let result = AstTransmitFilter::filter_sql("DROP TABLE users", "generic").unwrap();
|
||||
assert!(!result.allowed.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocks_create_function() {
|
||||
let result = AstTransmitFilter::filter_sql(
|
||||
"CREATE FUNCTION add(a INT, b INT) RETURNS INT LANGUAGE SQL RETURN a + b",
|
||||
"generic",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(result.allowed.is_empty());
|
||||
assert!(!result.denied.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocks_create_procedure() {
|
||||
let result = AstTransmitFilter::filter_sql(
|
||||
"CREATE PROCEDURE test_proc() AS LANGUAGE SQL BEGIN ATOMIC SELECT 1; END",
|
||||
"postgres",
|
||||
);
|
||||
// parse failure on some platforms is acceptable
|
||||
if let Ok(r) = result {
|
||||
assert!(r.allowed.is_empty());
|
||||
assert!(!r.denied.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocks_create_trigger() {
|
||||
let result = AstTransmitFilter::filter_sql(
|
||||
"CREATE TRIGGER test_trigger BEFORE INSERT ON users FOR EACH ROW EXECUTE FUNCTION log_change()",
|
||||
"postgres",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(result.allowed.is_empty());
|
||||
assert!(!result.denied.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allows_multiple_ddl_statements() {
|
||||
let result = AstTransmitFilter::filter_sql(
|
||||
"CREATE TABLE a (id INT); CREATE TABLE b (id INT); ALTER TABLE a ADD COLUMN x INT;",
|
||||
"generic",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result.allowed.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocks_mixed_ddl_and_function() {
|
||||
let result = AstTransmitFilter::filter_sql(
|
||||
"CREATE TABLE t (id INT); CREATE FUNCTION f() RETURNS INT AS $$ SELECT 1 $$ LANGUAGE SQL",
|
||||
"postgres",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result.allowed.len(), 1);
|
||||
assert_eq!(result.denied.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocks_select_statement() {
|
||||
let result = AstTransmitFilter::filter_sql("SELECT * FROM users", "generic").unwrap();
|
||||
assert!(result.allowed.is_empty());
|
||||
assert!(!result.denied.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allows_create_view() {
|
||||
let result = AstTransmitFilter::filter_sql(
|
||||
"CREATE VIEW active_users AS SELECT * FROM users WHERE active = 1",
|
||||
"generic",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!result.allowed.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deny_insert_statement() {
|
||||
let result = AstTransmitFilter::filter_sql("INSERT INTO users VALUES (1, 'a')", "generic").unwrap();
|
||||
assert!(result.allowed.is_empty());
|
||||
assert!(!result.denied.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_options_preserves_routine_definitions() {
|
||||
let opts = SchemaDiffPreparationOptions {
|
||||
source_functions: vec![
|
||||
FunctionInfo {
|
||||
name: "f1".into(),
|
||||
definition: "CREATE FUNCTION f1() RETURNS INT BEGIN RETURN 1; END".into(),
|
||||
function_type: "FUNCTION".into(),
|
||||
data_type: "int".into(),
|
||||
arguments: "".into(),
|
||||
},
|
||||
FunctionInfo {
|
||||
name: "p1".into(),
|
||||
definition: "CREATE PROCEDURE p1() BEGIN SELECT 1; END".into(),
|
||||
function_type: "PROCEDURE".into(),
|
||||
data_type: "".into(),
|
||||
arguments: "".into(),
|
||||
},
|
||||
],
|
||||
source_tables: vec![],
|
||||
target_tables: vec![],
|
||||
source_details: vec![],
|
||||
target_details: vec![],
|
||||
target_functions: vec![],
|
||||
source_sequences: vec![],
|
||||
target_sequences: vec![],
|
||||
source_rules: vec![],
|
||||
target_rules: vec![],
|
||||
source_owners: vec![],
|
||||
target_owners: vec![],
|
||||
database_type: crate::models::connection::DatabaseType::Mysql,
|
||||
target_schema: None,
|
||||
ignore_comments: false,
|
||||
cascade_delete: false,
|
||||
compare_column_order: false,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let filtered = AstTransmitFilter::filter_diff_preparation_options(opts, "mysql");
|
||||
assert_eq!(filtered.source_functions.len(), 2);
|
||||
assert_eq!(filtered.source_functions[0].name, "f1");
|
||||
assert_eq!(filtered.source_functions[1].name, "p1");
|
||||
assert!(filtered.source_functions[0].definition.contains("BEGIN"));
|
||||
assert!(filtered.source_functions[1].definition.contains("PROCEDURE"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sandbox_strict_mode_blocks_dangerous_statement() {
|
||||
let sandbox = AstSandbox::new(SandboxMode::Strict, 10);
|
||||
let result = sandbox
|
||||
.filter_sql(
|
||||
"CREATE TABLE t (id INT); CREATE FUNCTION f() RETURNS INT AS $$ SELECT 1 $$ LANGUAGE SQL",
|
||||
"postgres",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result.allowed.len(), 1);
|
||||
assert!(!result.denied.is_empty());
|
||||
assert!(!result.is_pure);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sandbox_isolation_mode_checks_nesting() {
|
||||
let sandbox = AstSandbox::new(SandboxMode::Isolation, 4);
|
||||
let result = sandbox
|
||||
.filter_sql("CREATE TABLE t (a INT, b INT, c INT, d INT, e INT DEFAULT (((1))))", "generic")
|
||||
.unwrap();
|
||||
assert!(result.nesting_violations.is_empty(), "Should pass with nesting depth 4");
|
||||
assert!(result.is_pure);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sandbox_isolation_blocks_deeply_nested() {
|
||||
let sandbox = AstSandbox::new(SandboxMode::Isolation, 1);
|
||||
let result = sandbox.filter_sql("CREATE TABLE t (a INT DEFAULT ((((1)))))", "generic").unwrap();
|
||||
assert!(!result.nesting_violations.is_empty());
|
||||
assert!(!result.is_pure);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sandbox_permissive_accepts_everything_allowed() {
|
||||
let sandbox = AstSandbox::new(SandboxMode::Permissive, 10);
|
||||
let result = sandbox
|
||||
.filter_sql("CREATE TABLE t (id INT); CREATE FUNCTION f() RETURNS INT LANGUAGE SQL RETURN 1", "generic")
|
||||
.unwrap();
|
||||
assert_eq!(result.allowed.len(), 1, "Permissive allows CREATE TABLE but blocks CREATE FUNCTION");
|
||||
assert!(!result.denied.is_empty(), "Permissive denies function but not table");
|
||||
assert!(!result.is_pure, "Permissive is not pure when function is denied by base filter");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sandbox_permissive_no_nesting_check() {
|
||||
let sandbox = AstSandbox::new(SandboxMode::Permissive, 0);
|
||||
let result = sandbox.filter_sql("CREATE TABLE t (a INT DEFAULT ((((1)))))", "generic").unwrap();
|
||||
assert!(!result.allowed.is_empty());
|
||||
assert!(result.nesting_violations.is_empty());
|
||||
assert!(result.is_pure);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sandbox_stats_correct() {
|
||||
let sandbox = AstSandbox::new(SandboxMode::Strict, 10);
|
||||
let result = sandbox
|
||||
.filter_sql(
|
||||
"CREATE TABLE t1 (id INT); CREATE TABLE t2 (id INT); DROP TABLE t3; SELECT * FROM t1",
|
||||
"generic",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result.stats.total_statements, 4);
|
||||
assert_eq!(result.stats.allowed_count, 3);
|
||||
assert_eq!(result.stats.denied_count, 1);
|
||||
assert_eq!(result.stats.mode, "strict");
|
||||
assert_eq!(result.stats.max_nesting_depth, 10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
use std::collections::HashSet;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::schema_diff::SchemaDiffPreparationOptions;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DiffEntry {
|
||||
pub file_path: String,
|
||||
pub sql_content: String,
|
||||
pub change_type: ChangeType,
|
||||
pub old_path: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ChangeType {
|
||||
Added,
|
||||
Modified,
|
||||
Deleted,
|
||||
Renamed,
|
||||
}
|
||||
|
||||
pub struct GitDiffScanner {
|
||||
repo_path: String,
|
||||
base_commit: String,
|
||||
target_commit: String,
|
||||
}
|
||||
|
||||
impl GitDiffScanner {
|
||||
pub fn new(repo_path: &Path, base_commit: &str, target_commit: &str) -> Result<Self, String> {
|
||||
if base_commit.is_empty() || target_commit.is_empty() {
|
||||
return Err("Git commit refs cannot be empty".into());
|
||||
}
|
||||
let git_path = repo_path.join(".git");
|
||||
let is_git_repo =
|
||||
git_path.is_dir() || (git_path.is_file() && git_path.exists()) || repo_path.join("../.git").exists();
|
||||
if !is_git_repo {
|
||||
return Err(format!("Not a git repository: {}", repo_path.display()));
|
||||
}
|
||||
Ok(Self {
|
||||
repo_path: repo_path.to_string_lossy().to_string(),
|
||||
base_commit: base_commit.to_string(),
|
||||
target_commit: target_commit.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn scan(&self) -> Result<Vec<DiffEntry>, String> {
|
||||
let output = self.run_git_diff()?;
|
||||
|
||||
let sql_filter = self.build_sql_filter()?;
|
||||
|
||||
let raw_entries = self.parse_diff_output(&output, &sql_filter)?;
|
||||
|
||||
let entries = raw_entries
|
||||
.into_iter()
|
||||
.filter_map(|entry| {
|
||||
if entry.change_type == ChangeType::Deleted {
|
||||
return Some(entry);
|
||||
}
|
||||
let path = Path::new(&self.repo_path).join(&entry.file_path);
|
||||
let content = std::fs::read_to_string(&path).ok()?;
|
||||
Some(DiffEntry { sql_content: content, ..entry })
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
fn run_git_diff(&self) -> Result<String, String> {
|
||||
let output = std::process::Command::new("git")
|
||||
.arg("diff")
|
||||
.arg("--diff-filter=ACMR")
|
||||
.arg(&self.base_commit)
|
||||
.arg(&self.target_commit)
|
||||
.arg("--")
|
||||
.arg("*.sql")
|
||||
.arg("*.ddl")
|
||||
.current_dir(&self.repo_path)
|
||||
.output()
|
||||
.map_err(|e| format!("Failed to run git diff: {e}"))?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(format!("git diff failed: {stderr}"));
|
||||
}
|
||||
|
||||
Ok(String::from_utf8_lossy(&output.stdout).to_string())
|
||||
}
|
||||
|
||||
fn build_sql_filter(&self) -> Result<HashSet<String>, String> {
|
||||
let mut patterns = HashSet::new();
|
||||
let gitattributes_path = Path::new(&self.repo_path).join(".gitattributes");
|
||||
if gitattributes_path.exists() {
|
||||
let content = std::fs::read_to_string(&gitattributes_path)
|
||||
.map_err(|e| format!("Failed to read .gitattributes: {e}"))?;
|
||||
for line in content.lines() {
|
||||
let line = line.trim();
|
||||
if line.is_empty() || line.starts_with('#') {
|
||||
continue;
|
||||
}
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() >= 2 && parts[1] == "linguist-language=SQL" {
|
||||
let pattern = parts[0].trim_matches('*');
|
||||
if !pattern.is_empty() {
|
||||
patterns.insert(pattern.to_string());
|
||||
} else {
|
||||
patterns.insert("sql".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if patterns.is_empty() {
|
||||
patterns.insert("sql".to_string());
|
||||
}
|
||||
Ok(patterns)
|
||||
}
|
||||
|
||||
fn parse_diff_output(&self, output: &str, _filter: &HashSet<String>) -> Result<Vec<DiffEntry>, String> {
|
||||
let mut entries = Vec::new();
|
||||
let mut current_file: Option<String> = None;
|
||||
let mut current_status: Option<ChangeType> = None;
|
||||
let mut current_old_path: Option<String> = None;
|
||||
|
||||
for line in output.lines() {
|
||||
if line.starts_with("diff --git ") {
|
||||
Self::flush_raw_entry(&mut current_file, &mut current_status, &mut current_old_path, &mut entries);
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() >= 4 {
|
||||
let b_path = parts.get(3).copied().unwrap_or("");
|
||||
let b_path = b_path.trim_start_matches("b/");
|
||||
current_file = Some(b_path.to_string());
|
||||
current_status = Some(ChangeType::Modified);
|
||||
}
|
||||
} else if line.starts_with("--- ") {
|
||||
let path = line.trim_start_matches("--- ").trim_start_matches("a/");
|
||||
if path != "/dev/null" {
|
||||
current_old_path = Some(path.to_string());
|
||||
}
|
||||
} else if line.starts_with("+++ ") {
|
||||
let path = line.trim_start_matches("+++ ").trim_start_matches("b/");
|
||||
if path != "/dev/null" {
|
||||
current_file = Some(path.to_string());
|
||||
}
|
||||
} else if line.starts_with("new file mode") {
|
||||
current_status = Some(ChangeType::Added);
|
||||
} else if line.starts_with("deleted file mode") {
|
||||
current_status = Some(ChangeType::Deleted);
|
||||
} else if line.starts_with("rename from ") {
|
||||
current_old_path = Some(line.trim_start_matches("rename from ").to_string());
|
||||
} else if line.starts_with("rename to ") {
|
||||
current_file = Some(line.trim_start_matches("rename to ").to_string());
|
||||
current_status = Some(ChangeType::Renamed);
|
||||
}
|
||||
}
|
||||
|
||||
Self::flush_raw_entry(&mut current_file, &mut current_status, &mut current_old_path, &mut entries);
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
fn flush_raw_entry(
|
||||
file: &mut Option<String>,
|
||||
status: &mut Option<ChangeType>,
|
||||
old_path: &mut Option<String>,
|
||||
entries: &mut Vec<DiffEntry>,
|
||||
) {
|
||||
let file_path = file.take();
|
||||
let st = status.take().unwrap_or(ChangeType::Modified);
|
||||
let op = old_path.take();
|
||||
if let Some(path) = file_path {
|
||||
entries.push(DiffEntry { file_path: path, sql_content: String::new(), change_type: st, old_path: op });
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bind_to_commit(options: SchemaDiffPreparationOptions, _commit_id: &str) -> SchemaDiffPreparationOptions {
|
||||
options
|
||||
}
|
||||
|
||||
pub fn list_commits(&self, max_count: usize) -> Result<Vec<String>, String> {
|
||||
let output = std::process::Command::new("git")
|
||||
.arg("log")
|
||||
.arg("--oneline")
|
||||
.arg("-n")
|
||||
.arg(max_count.to_string())
|
||||
.arg("--")
|
||||
.arg("*.sql")
|
||||
.arg("*.ddl")
|
||||
.current_dir(&self.repo_path)
|
||||
.output()
|
||||
.map_err(|e| format!("Failed to run git log: {e}"))?;
|
||||
|
||||
if !output.status.success() {
|
||||
return Err(String::from_utf8_lossy(&output.stderr).to_string());
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
Ok(stdout.lines().map(|l| l.to_string()).collect())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rejects_non_git_directory() {
|
||||
let tmp = std::env::temp_dir().join("dbx_test_not_a_repo");
|
||||
let _ = std::fs::create_dir_all(&tmp);
|
||||
let result = GitDiffScanner::new(&tmp, "HEAD~1", "HEAD");
|
||||
assert!(result.is_err());
|
||||
let _ = std::fs::remove_dir_all(&tmp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_diff_output_with_added_file() {
|
||||
let scanner =
|
||||
GitDiffScanner { repo_path: "/tmp/repo".into(), base_commit: "abc".into(), target_commit: "def".into() };
|
||||
let output = "diff --git a/schema.sql b/schema.sql\nnew file mode 100644\n--- /dev/null\n+++ b/schema.sql\n@@ -0,0 +1 @@\n+CREATE TABLE t (id INT);\n";
|
||||
let mut filter = HashSet::new();
|
||||
filter.insert("sql".to_string());
|
||||
let entries = scanner.parse_diff_output(output, &filter).unwrap();
|
||||
assert_eq!(entries.len(), 1);
|
||||
assert_eq!(entries[0].file_path, "schema.sql");
|
||||
assert_eq!(entries[0].change_type, ChangeType::Added);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_diff_output_with_modified_file() {
|
||||
let scanner =
|
||||
GitDiffScanner { repo_path: "/tmp/repo".into(), base_commit: "abc".into(), target_commit: "def".into() };
|
||||
let output = "diff --git a/schema.sql b/schema.sql\n--- a/schema.sql\n+++ b/schema.sql\n@@ -1 +1 @@\n-CREATE TABLE t (id INT);\n+CREATE TABLE t (id BIGINT);\n";
|
||||
let mut filter = HashSet::new();
|
||||
filter.insert("sql".to_string());
|
||||
let entries = scanner.parse_diff_output(output, &filter).unwrap();
|
||||
assert_eq!(entries.len(), 1);
|
||||
assert_eq!(entries[0].change_type, ChangeType::Modified);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_diff_output_with_renamed_file() {
|
||||
let scanner =
|
||||
GitDiffScanner { repo_path: "/tmp/repo".into(), base_commit: "abc".into(), target_commit: "def".into() };
|
||||
let output = "diff --git a/old.sql b/new.sql\nrename from old.sql\nrename to new.sql\n";
|
||||
let mut filter = HashSet::new();
|
||||
filter.insert("sql".to_string());
|
||||
let entries = scanner.parse_diff_output(output, &filter).unwrap();
|
||||
assert_eq!(entries.len(), 1);
|
||||
assert_eq!(entries[0].change_type, ChangeType::Renamed);
|
||||
assert_eq!(entries[0].old_path, Some("old.sql".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_gitattributes_for_sql_patterns() {
|
||||
let tmp = std::env::temp_dir().join("dbx_test_gitattributes");
|
||||
let _ = std::fs::create_dir_all(&tmp);
|
||||
let gitattributes = tmp.join(".gitattributes");
|
||||
std::fs::write(&gitattributes, "*.sql linguist-language=SQL\n*.ddl linguist-language=SQL\n").unwrap();
|
||||
|
||||
let scanner = GitDiffScanner {
|
||||
repo_path: tmp.to_string_lossy().to_string(),
|
||||
base_commit: "abc".into(),
|
||||
target_commit: "def".into(),
|
||||
};
|
||||
let filter = scanner.build_sql_filter().unwrap();
|
||||
assert!(!filter.is_empty());
|
||||
|
||||
let _ = std::fs::remove_dir_all(&tmp);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::models::connection::ConnectionConfig;
|
||||
use crate::schema_diff::SchemaDiffPreparationOptions;
|
||||
use crate::sql_parser::meta::{validate_sql_meta_consistency, MetaData, MetaReader};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum InputSource {
|
||||
DdlFiles(Vec<PathBuf>),
|
||||
DatabaseConnection(ConnectionConfig),
|
||||
GitDiff { repo_path: PathBuf, base_commit: String, target_commit: String },
|
||||
MetaData(MetaData),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ResolvedInput {
|
||||
pub source: InputSource,
|
||||
pub meta: Option<MetaData>,
|
||||
pub ddl_sql: Option<String>,
|
||||
pub has_errors: bool,
|
||||
pub warnings: Vec<String>,
|
||||
}
|
||||
|
||||
pub struct InputResolver;
|
||||
|
||||
impl InputResolver {
|
||||
pub fn discover_ddl_files(dir: &Path) -> Vec<PathBuf> {
|
||||
let mut files = Vec::new();
|
||||
if !dir.is_dir() {
|
||||
return files;
|
||||
}
|
||||
let entries = match std::fs::read_dir(dir) {
|
||||
Ok(e) => e,
|
||||
Err(_) => return files,
|
||||
};
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
files.extend(Self::discover_ddl_files(&path));
|
||||
continue;
|
||||
}
|
||||
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
|
||||
if matches!(ext, "sql" | "ddl" | "txt") {
|
||||
let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||
if !file_name.starts_with('.') {
|
||||
files.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
files
|
||||
}
|
||||
|
||||
pub fn load_meta_for(sql_path: &Path) -> Option<MetaData> {
|
||||
let dir = sql_path.parent()?;
|
||||
let stem = sql_path.file_stem().and_then(|s| s.to_str())?;
|
||||
for ext in &["meta.json", "meta.yaml", "meta.yml"] {
|
||||
let meta_path = dir.join(format!("{}.{}", stem, ext));
|
||||
if meta_path.exists() {
|
||||
if let Ok(meta) = MetaReader::from_file(&meta_path) {
|
||||
return Some(meta);
|
||||
}
|
||||
}
|
||||
}
|
||||
let meta_path = dir.join(".dbmeta.json");
|
||||
if meta_path.exists() {
|
||||
if let Ok(meta) = MetaReader::from_file(&meta_path) {
|
||||
return Some(meta);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn resolve(inputs: Vec<InputSource>) -> ResolvedInput {
|
||||
let mut meta: Option<MetaData> = None;
|
||||
let mut sql_parts: Vec<String> = Vec::new();
|
||||
let mut warnings: Vec<String> = Vec::new();
|
||||
let mut live_db: Option<ConnectionConfig> = None;
|
||||
|
||||
for input in inputs {
|
||||
match input {
|
||||
InputSource::DdlFiles(files) => {
|
||||
for file in files {
|
||||
if let Ok(content) = std::fs::read_to_string(&file) {
|
||||
if let Some(file_meta) = Self::load_meta_for(&file) {
|
||||
let mw = validate_sql_meta_consistency(&content, &file_meta);
|
||||
warnings.extend(mw.into_iter().map(|w| format!("{}: {}", file.display(), w)));
|
||||
meta = Self::merge_meta(meta.take(), Some(file_meta));
|
||||
}
|
||||
sql_parts.push(content);
|
||||
} else {
|
||||
warnings.push(format!("Cannot read DDL file: {}", file.display()));
|
||||
}
|
||||
}
|
||||
}
|
||||
InputSource::DatabaseConnection(config) => {
|
||||
live_db = Some(config);
|
||||
}
|
||||
InputSource::GitDiff { repo_path, base_commit, target_commit } => {
|
||||
match crate::sql_parser::git::GitDiffScanner::new(&repo_path, &base_commit, &target_commit) {
|
||||
Ok(scanner) => match scanner.scan() {
|
||||
Ok(entries) => {
|
||||
for entry in entries {
|
||||
sql_parts.push(entry.sql_content);
|
||||
}
|
||||
}
|
||||
Err(e) => warnings.push(format!("Git diff scan error: {e}")),
|
||||
},
|
||||
Err(e) => warnings.push(format!("Git scanner init error: {e}")),
|
||||
}
|
||||
}
|
||||
InputSource::MetaData(m) => {
|
||||
meta = Self::merge_meta(meta.take(), Some(m));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref meta_data) = meta {
|
||||
let mw = MetaReader::validate_consistency(meta_data);
|
||||
warnings.extend(mw);
|
||||
}
|
||||
|
||||
let source = if let Some(config) = live_db {
|
||||
InputSource::DatabaseConnection(config)
|
||||
} else {
|
||||
InputSource::DdlFiles(Vec::new())
|
||||
};
|
||||
|
||||
ResolvedInput {
|
||||
source,
|
||||
meta,
|
||||
ddl_sql: if sql_parts.is_empty() { None } else { Some(sql_parts.join("\n\n")) },
|
||||
has_errors: warnings.iter().any(|w| w.starts_with("Cannot read") || w.contains("error")),
|
||||
warnings,
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_meta(existing: Option<MetaData>, incoming: Option<MetaData>) -> Option<MetaData> {
|
||||
match (existing, incoming) {
|
||||
(None, incoming) => incoming,
|
||||
(existing, None) => existing,
|
||||
(Some(mut e), Some(i)) => {
|
||||
e.dialect = e.dialect.or(i.dialect);
|
||||
e.version = e.version.or(i.version);
|
||||
e.charset = e.charset.or(i.charset);
|
||||
e.collation = e.collation.or(i.collation);
|
||||
e.sql_mode = e.sql_mode.or(i.sql_mode);
|
||||
e.explicit_dependencies.extend(i.explicit_dependencies);
|
||||
Some(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn normalize_to_diff_options(resolved: ResolvedInput) -> (SchemaDiffPreparationOptions, Vec<String>) {
|
||||
let mut options = SchemaDiffPreparationOptions {
|
||||
source_tables: Vec::new(),
|
||||
target_tables: Vec::new(),
|
||||
source_details: Vec::new(),
|
||||
target_details: Vec::new(),
|
||||
source_functions: Vec::new(),
|
||||
target_functions: Vec::new(),
|
||||
source_sequences: Vec::new(),
|
||||
target_sequences: Vec::new(),
|
||||
source_rules: Vec::new(),
|
||||
target_rules: Vec::new(),
|
||||
source_owners: Vec::new(),
|
||||
target_owners: Vec::new(),
|
||||
database_type: crate::models::connection::DatabaseType::Mysql,
|
||||
target_schema: None,
|
||||
ignore_comments: false,
|
||||
cascade_delete: false,
|
||||
compare_column_order: false,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if let Some(ref meta) = resolved.meta {
|
||||
if let Some(ref dialect) = meta.dialect {
|
||||
let db_type = dialect_to_database_type(dialect);
|
||||
options.database_type = db_type;
|
||||
}
|
||||
}
|
||||
|
||||
(options, resolved.warnings)
|
||||
}
|
||||
}
|
||||
|
||||
fn dialect_to_database_type(dialect: &str) -> crate::models::connection::DatabaseType {
|
||||
match dialect.to_ascii_lowercase().as_str() {
|
||||
"mysql" | "mariadb" | "tidb" => crate::models::connection::DatabaseType::Mysql,
|
||||
"postgres" | "postgresql" => crate::models::connection::DatabaseType::Postgres,
|
||||
"sqlite" => crate::models::connection::DatabaseType::Sqlite,
|
||||
"sqlserver" | "mssql" => crate::models::connection::DatabaseType::SqlServer,
|
||||
"clickhouse" => crate::models::connection::DatabaseType::ClickHouse,
|
||||
"duckdb" => crate::models::connection::DatabaseType::DuckDb,
|
||||
"oracle" => crate::models::connection::DatabaseType::Oracle,
|
||||
_ => crate::models::connection::DatabaseType::Mysql,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
|
||||
#[test]
|
||||
fn discovers_sql_files_in_directory() {
|
||||
let dir = std::env::temp_dir().join("dbx_test_ddl_discover");
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
let mut f1 = std::fs::File::create(dir.join("schema.sql")).unwrap();
|
||||
f1.write_all(b"CREATE TABLE t (id INT);").unwrap();
|
||||
let mut f2 = std::fs::File::create(dir.join("data.ddl")).unwrap();
|
||||
f2.write_all(b"INSERT INTO t VALUES (1);").unwrap();
|
||||
let _hidden = std::fs::File::create(dir.join(".ignore.sql")).unwrap();
|
||||
|
||||
let files = InputResolver::discover_ddl_files(&dir);
|
||||
let names: Vec<String> =
|
||||
files.iter().filter_map(|f| f.file_name().and_then(|n| n.to_str()).map(|s| s.to_string())).collect();
|
||||
assert!(names.contains(&"schema.sql".to_string()));
|
||||
assert!(names.contains(&"data.ddl".to_string()));
|
||||
assert!(!names.contains(&".ignore.sql".to_string()));
|
||||
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loads_meta_file_for_sql() {
|
||||
let dir = std::env::temp_dir().join("dbx_test_meta_load");
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
let sql_path = dir.join("test.sql");
|
||||
let meta_path = dir.join("test.meta.json");
|
||||
std::fs::write(&sql_path, "CREATE TABLE t (id INT);").unwrap();
|
||||
std::fs::write(&meta_path, r#"{"dialect": "postgres", "version": "15"}"#).unwrap();
|
||||
|
||||
let meta = InputResolver::load_meta_for(&sql_path);
|
||||
assert!(meta.is_some());
|
||||
assert_eq!(meta.unwrap().dialect.as_deref(), Some("postgres"));
|
||||
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_uses_meta_dialect() {
|
||||
let resolved = ResolvedInput {
|
||||
source: InputSource::DdlFiles(Vec::new()),
|
||||
meta: Some(MetaData {
|
||||
dialect: Some("postgres".into()),
|
||||
version: None,
|
||||
charset: None,
|
||||
collation: None,
|
||||
sql_mode: None,
|
||||
explicit_dependencies: std::collections::HashMap::new(),
|
||||
}),
|
||||
ddl_sql: Some("CREATE TABLE t (id INT);".into()),
|
||||
has_errors: false,
|
||||
warnings: Vec::new(),
|
||||
};
|
||||
let (options, _) = InputResolver::normalize_to_diff_options(resolved);
|
||||
assert_eq!(options.database_type, crate::models::connection::DatabaseType::Postgres);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_adds_has_errors_on_failed_read() {
|
||||
let resolved = InputResolver::resolve(vec![InputSource::DdlFiles(vec![PathBuf::from(
|
||||
r"C:\non_existent_dir\nonexistent.sql",
|
||||
)])]);
|
||||
assert!(resolved.has_errors);
|
||||
assert_eq!(resolved.warnings.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_merges_meta_from_multiple_sources() {
|
||||
let meta1 = InputSource::MetaData(MetaData {
|
||||
dialect: Some("mysql".into()),
|
||||
version: Some("8.0".into()),
|
||||
charset: None,
|
||||
collation: None,
|
||||
sql_mode: None,
|
||||
explicit_dependencies: std::collections::HashMap::new(),
|
||||
});
|
||||
let meta2 = InputSource::MetaData(MetaData {
|
||||
dialect: None,
|
||||
version: None,
|
||||
charset: Some("utf8mb4".into()),
|
||||
collation: None,
|
||||
sql_mode: None,
|
||||
explicit_dependencies: std::collections::HashMap::new(),
|
||||
});
|
||||
let resolved = InputResolver::resolve(vec![meta1, meta2]);
|
||||
assert_eq!(resolved.meta.as_ref().and_then(|m| m.dialect.as_deref()), Some("mysql"));
|
||||
assert_eq!(resolved.meta.as_ref().and_then(|m| m.charset.as_deref()), Some("utf8mb4"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_ddl_dir_returns_no_files() {
|
||||
let dir = std::env::temp_dir().join("dbx_test_empty_ddl");
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
let files = InputResolver::discover_ddl_files(&dir);
|
||||
assert!(files.is_empty());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct MetaData {
|
||||
#[serde(default)]
|
||||
pub dialect: Option<String>,
|
||||
#[serde(default)]
|
||||
pub version: Option<String>,
|
||||
#[serde(default)]
|
||||
pub charset: Option<String>,
|
||||
#[serde(default)]
|
||||
pub collation: Option<String>,
|
||||
#[serde(default)]
|
||||
pub sql_mode: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
|
||||
pub explicit_dependencies: HashMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
pub struct MetaReader;
|
||||
|
||||
impl MetaReader {
|
||||
pub fn from_file(path: &Path) -> Result<MetaData, String> {
|
||||
let content = std::fs::read_to_string(path).map_err(|e| format!("Failed to read meta file: {e}"))?;
|
||||
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
|
||||
match ext {
|
||||
"json" => Self::from_json(&content),
|
||||
"yaml" | "yml" => Self::from_yaml(&content),
|
||||
_ => Self::from_json(&content),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_json(content: &str) -> Result<MetaData, String> {
|
||||
serde_json::from_str(content).map_err(|e| format!("Invalid meta JSON: {e}"))
|
||||
}
|
||||
|
||||
pub fn from_yaml(content: &str) -> Result<MetaData, String> {
|
||||
serde_yaml::from_str(content).map_err(|e| format!("Invalid meta YAML: {e}"))
|
||||
}
|
||||
|
||||
pub fn to_json(meta: &MetaData) -> Result<String, String> {
|
||||
serde_json::to_string_pretty(meta).map_err(|e| format!("Serialize meta error: {e}"))
|
||||
}
|
||||
|
||||
pub fn validate_consistency(meta: &MetaData) -> Vec<String> {
|
||||
let mut warnings = Vec::new();
|
||||
if let Some(ref dialect) = meta.dialect {
|
||||
let lower = dialect.to_ascii_lowercase();
|
||||
let known = [
|
||||
"mysql",
|
||||
"postgres",
|
||||
"postgresql",
|
||||
"sqlite",
|
||||
"mssql",
|
||||
"sqlserver",
|
||||
"clickhouse",
|
||||
"duckdb",
|
||||
"oracle",
|
||||
"tidb",
|
||||
"mariadb",
|
||||
];
|
||||
if !known.contains(&lower.as_str()) {
|
||||
warnings.push(format!("Unknown dialect '{}' in metadata", meta.dialect.as_ref().unwrap()));
|
||||
}
|
||||
}
|
||||
if let Some(ref version) = meta.version {
|
||||
let cleaned = version.trim_start_matches(|c: char| c.is_ascii_alphabetic() || c == 'v' || c == 'V');
|
||||
let is_valid = !cleaned.is_empty()
|
||||
&& cleaned.chars().all(|c| c.is_ascii_digit() || c == '.' || c == '-')
|
||||
&& cleaned.chars().any(|c| c.is_ascii_digit());
|
||||
if !is_valid {
|
||||
warnings.push(format!("Unusual version format '{}'", version));
|
||||
}
|
||||
}
|
||||
warnings
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate_sql_meta_consistency(sql: &str, meta: &MetaData) -> Vec<String> {
|
||||
let mut warnings = Vec::new();
|
||||
if let Some(ref charset) = meta.charset {
|
||||
if !charset_mentions_match(sql, charset, meta.dialect.as_deref()) {
|
||||
warnings.push(format!("SQL content does not reference charset '{}' from metadata", charset));
|
||||
}
|
||||
}
|
||||
warnings
|
||||
}
|
||||
|
||||
fn charset_mentions_match(sql: &str, charset: &str, dialect: Option<&str>) -> bool {
|
||||
let sql_upper = sql.to_ascii_uppercase();
|
||||
let charset_upper = charset.to_ascii_uppercase();
|
||||
let is_pg = dialect.is_some_and(|d| matches!(d.to_ascii_lowercase().as_str(), "postgres" | "postgresql"));
|
||||
if is_pg {
|
||||
sql_upper.contains(&format!("ENCODING '{}'", charset_upper))
|
||||
|| sql_upper.contains(&format!("ENCODING '{}'", charset_upper.to_ascii_lowercase()))
|
||||
} else {
|
||||
let pattern = format!("CHARACTER SET {}", charset_upper);
|
||||
sql_upper.contains(&pattern) || sql_upper.contains(&format!("CHARSET {}", charset_upper))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn reads_valid_json_metadata() {
|
||||
let json = r#"{
|
||||
"dialect": "mysql",
|
||||
"version": "8.0",
|
||||
"charset": "utf8mb4",
|
||||
"explicitDependencies": {
|
||||
"view_orders": ["orders", "users"]
|
||||
}
|
||||
}"#;
|
||||
let meta = MetaReader::from_json(json).unwrap();
|
||||
assert_eq!(meta.dialect.as_deref(), Some("mysql"));
|
||||
assert_eq!(meta.version.as_deref(), Some("8.0"));
|
||||
assert_eq!(
|
||||
meta.explicit_dependencies.get("view_orders").map(|v| v.as_slice()),
|
||||
Some(&["orders".to_string(), "users".to_string()][..])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_valid_yaml_metadata() {
|
||||
let yaml = r#"
|
||||
dialect: postgres
|
||||
version: "15"
|
||||
charset: UTF8
|
||||
explicitDependencies:
|
||||
expensive_view: [table_a, table_b]
|
||||
"#;
|
||||
let meta = MetaReader::from_yaml(yaml).unwrap();
|
||||
assert_eq!(meta.dialect.as_deref(), Some("postgres"));
|
||||
assert_eq!(meta.explicit_dependencies.get("expensive_view").map(|v| v.len()), Some(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validates_known_dialect() {
|
||||
let meta = MetaData {
|
||||
dialect: Some("mysql".into()),
|
||||
version: Some("8.0".into()),
|
||||
charset: None,
|
||||
collation: None,
|
||||
sql_mode: None,
|
||||
explicit_dependencies: HashMap::new(),
|
||||
};
|
||||
assert!(MetaReader::validate_consistency(&meta).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn warns_unknown_dialect() {
|
||||
let meta = MetaData {
|
||||
dialect: Some("fakedb".into()),
|
||||
version: None,
|
||||
charset: None,
|
||||
collation: None,
|
||||
sql_mode: None,
|
||||
explicit_dependencies: HashMap::new(),
|
||||
};
|
||||
let warnings = MetaReader::validate_consistency(&meta);
|
||||
assert!(!warnings.is_empty());
|
||||
assert!(warnings[0].contains("fakedb"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn warns_unusual_version_format() {
|
||||
let meta = MetaData {
|
||||
dialect: Some("mysql".into()),
|
||||
version: Some("latest".into()),
|
||||
charset: None,
|
||||
collation: None,
|
||||
sql_mode: None,
|
||||
explicit_dependencies: HashMap::new(),
|
||||
};
|
||||
let warnings = MetaReader::validate_consistency(&meta);
|
||||
assert!(!warnings.is_empty());
|
||||
assert!(warnings[0].contains("latest"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn charset_consistency_check() {
|
||||
let meta = MetaData {
|
||||
dialect: Some("mysql".into()),
|
||||
version: Some("8.0".into()),
|
||||
charset: Some("utf8mb4".into()),
|
||||
collation: None,
|
||||
sql_mode: None,
|
||||
explicit_dependencies: HashMap::new(),
|
||||
};
|
||||
let sql = "CREATE TABLE t (c VARCHAR(100)) CHARACTER SET utf8mb4";
|
||||
assert!(validate_sql_meta_consistency(sql, &meta).is_empty());
|
||||
|
||||
let sql_no_charset = "CREATE TABLE t (c VARCHAR(100))";
|
||||
let warnings = validate_sql_meta_consistency(sql_no_charset, &meta);
|
||||
assert!(!warnings.is_empty());
|
||||
assert!(warnings[0].contains("utf8mb4"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_meta_has_no_warnings() {
|
||||
let meta = MetaData {
|
||||
dialect: None,
|
||||
version: None,
|
||||
charset: None,
|
||||
collation: None,
|
||||
sql_mode: None,
|
||||
explicit_dependencies: HashMap::new(),
|
||||
};
|
||||
assert!(MetaReader::validate_consistency(&meta).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serializes_and_deserializes_roundtrip() {
|
||||
let meta = MetaData {
|
||||
dialect: Some("postgres".into()),
|
||||
version: Some("16".into()),
|
||||
charset: Some("UTF8".into()),
|
||||
collation: Some("en_US.UTF-8".into()),
|
||||
sql_mode: None,
|
||||
explicit_dependencies: HashMap::from([("v".into(), vec!["a".into(), "b".into()])]),
|
||||
};
|
||||
let json = MetaReader::to_json(&meta).unwrap();
|
||||
let restored = MetaReader::from_json(&json).unwrap();
|
||||
assert_eq!(restored.dialect, meta.dialect);
|
||||
assert_eq!(restored.version, meta.version);
|
||||
assert_eq!(restored.explicit_dependencies.len(), 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
pub mod ast_filter;
|
||||
pub mod git;
|
||||
pub mod input;
|
||||
pub mod meta;
|
||||
@@ -0,0 +1,113 @@
|
||||
mod column_alter;
|
||||
mod column_format;
|
||||
mod columns;
|
||||
mod comments;
|
||||
mod create_table;
|
||||
mod dialect;
|
||||
mod foreign_keys;
|
||||
mod indexes;
|
||||
mod mysql_engine;
|
||||
mod owner;
|
||||
mod partitions;
|
||||
mod triggers;
|
||||
mod types;
|
||||
mod util;
|
||||
mod validation;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub use column_alter::build_single_column_alter_sql;
|
||||
pub use create_table::build_create_table_sql;
|
||||
pub use owner::build_table_owner_change_sql;
|
||||
pub use partitions::{build_create_partitioned_table_sql, build_table_partition_operation_sql};
|
||||
pub use types::*;
|
||||
|
||||
pub use column_alter::{
|
||||
build_sqlserver_alter_column_preserving_default_sql, build_sqlserver_drop_default_constraint_sql,
|
||||
};
|
||||
pub use comments::{build_sqlserver_column_comment_sql, build_sqlserver_table_comment_sql};
|
||||
pub use util::{oracle_new_object_reference, sqlserver_unicode_string_literal};
|
||||
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
use column_format::strip_inherited_mysql_column_charsets;
|
||||
use columns::{build_column_sql, validate_primary_key_change_scope};
|
||||
use comments::build_table_comment_sql;
|
||||
use foreign_keys::build_foreign_key_sql;
|
||||
use indexes::build_index_sql;
|
||||
use mysql_engine::{build_mysql_engine_change_sql, validate_mysql_engine};
|
||||
use triggers::build_trigger_sql;
|
||||
use validation::validate_concurrent_index_scope;
|
||||
pub use validation::validate_draft;
|
||||
|
||||
pub fn build_table_structure_change_sql(mut options: TableStructureSqlOptions) -> TableStructureSqlResult {
|
||||
let mysql_engine_errors = validate_mysql_engine(&options);
|
||||
if !mysql_engine_errors.is_empty() {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings: mysql_engine_errors };
|
||||
}
|
||||
// GaussDB M-mode uses MySQL-compatible SQL dialect with backtick quoting.
|
||||
// Map to StructureDialect::Mysql so DDL is generated correctly.
|
||||
if options.is_gaussdb_m_mode {
|
||||
options.database_type = Some(DatabaseType::Mysql);
|
||||
}
|
||||
strip_inherited_mysql_column_charsets(&mut options);
|
||||
let primary_key_errors = validate_primary_key_change_scope(&options);
|
||||
if !primary_key_errors.is_empty() {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings: primary_key_errors };
|
||||
}
|
||||
// Fail closed: an unsupported concurrent-index request (existing index, or
|
||||
// partitioned parent table) must never degrade into blocking index DDL
|
||||
// behind the caller's back, so the whole plan is refused up front.
|
||||
let concurrent_errors = validate_concurrent_index_scope(&options);
|
||||
if !concurrent_errors.is_empty() {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings: concurrent_errors };
|
||||
}
|
||||
let mut warnings = validate_draft(&options);
|
||||
let mut statements = build_column_sql(&options, &mut warnings);
|
||||
statements.extend(build_index_sql(&options, &mut warnings));
|
||||
statements.extend(build_foreign_key_sql(&options, &mut warnings));
|
||||
statements.extend(build_trigger_sql(&options, &mut warnings));
|
||||
statements.extend(build_table_comment_sql(&options, &mut warnings));
|
||||
statements.extend(build_mysql_engine_change_sql(&options));
|
||||
TableStructureSqlResult { statements, warnings }
|
||||
}
|
||||
|
||||
/// Whether this engine's structure editor can generate `COMMENT ON`/inline
|
||||
/// comment DDL for tables and columns — a DDL-generation capability, not an
|
||||
/// introspection one. The documentation collector uses it as a heuristic for
|
||||
/// "can this engine report comments at all", but the two questions can
|
||||
/// diverge: IRIS supports `%DESCRIPTION` while *defining* a table or column,
|
||||
/// but DBX's editor cannot ALTER an existing one, so this returns `false`
|
||||
/// for IRIS even though IRIS still reports descriptions on introspection.
|
||||
/// Callers using this as an introspection signal must corroborate it against
|
||||
/// what was actually collected rather than trust the flag alone.
|
||||
pub fn supports_comments(database_type: crate::models::connection::DatabaseType) -> bool {
|
||||
dialect::capabilities_for(Some(database_type), None).comment
|
||||
}
|
||||
|
||||
/// Whether this engine's structure editor can generate foreign key DDL — a
|
||||
/// DDL-generation capability, not an introspection one, used here as a
|
||||
/// heuristic for "does this engine report foreign key metadata at all".
|
||||
/// Engines like ClickHouse and Doris genuinely report none, so their ER
|
||||
/// diagrams have no edges by necessity rather than by accident, but a
|
||||
/// mismatch analogous to the IRIS comment case is possible for any future
|
||||
/// engine where DDL support and introspection support diverge — callers
|
||||
/// should corroborate against what was actually collected.
|
||||
pub fn supports_foreign_keys(database_type: crate::models::connection::DatabaseType) -> bool {
|
||||
dialect::capabilities_for(Some(database_type), None).foreign_key
|
||||
}
|
||||
|
||||
/// Canonical display label for a database engine (e.g. `postgres`,
|
||||
/// `sqlserver`, `mongodb`) — the same identifier already used throughout
|
||||
/// this module's own warning prose. The documentation collector uses it for
|
||||
/// `database_type` and its engine-capability warnings instead of the Rust
|
||||
/// `Debug` spelling (`Postgres`, `SqlServer`, `MongoDb`), which is an
|
||||
/// implementation detail, not something a consumer like dbdocs/dbdiagram
|
||||
/// should key off.
|
||||
pub fn database_type_label(database_type: crate::models::connection::DatabaseType) -> String {
|
||||
dialect::database_label(Some(database_type))
|
||||
}
|
||||
|
||||
pub use indexes::has_existing_index_change;
|
||||
pub use util::{clean, normalize_default, original_comment, original_default};
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,819 @@
|
||||
use super::dialect::{is_oracle_like, StructureDialect};
|
||||
use super::types::{EditableStructureColumn, TableStructureSqlOptions};
|
||||
use super::util::{clean, format_default_for_sql, normalize_default, quote_ident, quote_string};
|
||||
|
||||
/// Drops the `CHARACTER SET` / `COLLATE` inputs of MySQL columns that merely
|
||||
/// inherit the table's default collation, so the generated DDL does not spell
|
||||
/// out clauses the server would apply anyway.
|
||||
///
|
||||
/// MySQL reports the *effective* collation of every character column and never
|
||||
/// records whether it was written out explicitly, so "equals the table default"
|
||||
/// is the only signal available. Omitting the clauses is equivalent to keeping
|
||||
/// them: a column definition without `CHARACTER SET` takes the table default,
|
||||
/// which is exactly the value being dropped here. This runs on the DDL inputs
|
||||
/// only — introspection (`get_columns`) still reports the real values so the
|
||||
/// structure editor can show the column's current charset and collation.
|
||||
///
|
||||
/// The original snapshot is normalized alongside the draft so that an untouched
|
||||
/// column does not register as a charset change and trigger a needless `MODIFY`.
|
||||
pub(super) fn strip_inherited_mysql_column_charsets(options: &mut TableStructureSqlOptions) {
|
||||
let Some(table_collation) = options.table_collation.as_deref().map(str::trim).filter(|value| !value.is_empty())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let inherits = |collation: &str| collation.trim().eq_ignore_ascii_case(table_collation);
|
||||
for column in &mut options.columns {
|
||||
if inherits(&column.collation) {
|
||||
column.character_set = String::new();
|
||||
column.collation = String::new();
|
||||
}
|
||||
if let Some(original) = column.original.as_mut() {
|
||||
if original.collation.as_deref().is_some_and(inherits) {
|
||||
original.character_set = None;
|
||||
original.collation = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn column_definition(dialect: StructureDialect, column: &EditableStructureColumn) -> String {
|
||||
let data_type = column_data_type(dialect, column);
|
||||
let mut parts = vec![quote_ident(dialect, &column.name), data_type];
|
||||
// QuestDB SQL Syntax: ```ALTER TABLE tableName ADD COLUMN [IF NOT EXISTS] columnName typeDef```
|
||||
if dialect == StructureDialect::Questdb {
|
||||
parts.insert(0, "IF NOT EXISTS".to_string());
|
||||
return parts.join(" ");
|
||||
}
|
||||
// CHARACTER SET / COLLATE — MySQL character data types only
|
||||
if dialect == StructureDialect::Mysql && is_mysql_character_data_type(&column.data_type) {
|
||||
if !column.character_set.trim().is_empty() {
|
||||
parts.push(format!("CHARACTER SET {}", quote_ident(dialect, &column.character_set)));
|
||||
}
|
||||
if !column.collation.trim().is_empty() {
|
||||
parts.push(format!("COLLATE {}", quote_ident(dialect, &column.collation)));
|
||||
}
|
||||
}
|
||||
let mysql_generated_clause =
|
||||
(dialect == StructureDialect::Mysql).then(|| original_mysql_generated_clause(column)).flatten();
|
||||
if let Some(generated_clause) = mysql_generated_clause.as_ref() {
|
||||
parts.push(generated_clause.clone());
|
||||
}
|
||||
if !column.is_nullable && !is_oracle_like(dialect) && dialect != StructureDialect::ClickHouse {
|
||||
parts.push("NOT NULL".to_string());
|
||||
} else if column.is_nullable
|
||||
&& dialect == StructureDialect::Mysql
|
||||
&& mysql_generated_clause.is_none()
|
||||
&& is_mysql_timestamp_type(&column.data_type)
|
||||
{
|
||||
parts.push("NULL".to_string());
|
||||
}
|
||||
if mysql_generated_clause.is_none() {
|
||||
if let Some(extra_clause) = column_extra_clause(dialect, column) {
|
||||
parts.push(extra_clause);
|
||||
}
|
||||
}
|
||||
let default_value = normalize_default(Some(&column.default_value));
|
||||
if mysql_generated_clause.is_none() && !default_value.is_empty() {
|
||||
parts.push(format!("DEFAULT {}", format_default_for_sql(dialect, &column.data_type, &default_value)));
|
||||
}
|
||||
if mysql_generated_clause.is_none() {
|
||||
if let Some(on_update) = column.extra.as_ref().and_then(|e| e.on_update_current_timestamp).filter(|v| *v) {
|
||||
if on_update && dialect == StructureDialect::Mysql {
|
||||
parts.push(mysql_on_update_current_timestamp_clause(&column.data_type));
|
||||
}
|
||||
}
|
||||
}
|
||||
if matches!(dialect, StructureDialect::Mysql | StructureDialect::GaussdbM | StructureDialect::Doris)
|
||||
&& !clean(&column.comment).is_empty()
|
||||
{
|
||||
parts.push(format!("COMMENT {}", quote_string(&clean(&column.comment))));
|
||||
}
|
||||
parts.join(" ")
|
||||
}
|
||||
|
||||
pub(super) fn original_mysql_generated_clause(column: &EditableStructureColumn) -> Option<String> {
|
||||
let extra = column.original.as_ref()?.extra.as_deref()?.trim();
|
||||
let normalized = extra.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
(normalized.starts_with("generated ") && normalized.contains(" as (")).then(|| extra.to_string())
|
||||
}
|
||||
|
||||
pub(super) fn original_is_mysql_generated_column(column: &EditableStructureColumn) -> bool {
|
||||
let Some(extra) = column.original.as_ref().and_then(|original| original.extra.as_deref()) else {
|
||||
return false;
|
||||
};
|
||||
let mut tokens = extra.split_whitespace();
|
||||
let first = tokens.next();
|
||||
first.is_some_and(|token| token.eq_ignore_ascii_case("generated"))
|
||||
|| matches!(first, Some(token) if token.eq_ignore_ascii_case("virtual")
|
||||
|| token.eq_ignore_ascii_case("stored")
|
||||
|| token.eq_ignore_ascii_case("persistent"))
|
||||
&& tokens.next().is_some_and(|token| token.eq_ignore_ascii_case("generated"))
|
||||
}
|
||||
|
||||
pub(super) fn column_extra_clause(dialect: StructureDialect, column: &EditableStructureColumn) -> Option<String> {
|
||||
let extra = column.extra.as_ref()?;
|
||||
match dialect {
|
||||
StructureDialect::Sqlite => {
|
||||
if extra.auto_increment.unwrap_or(false) && column.is_primary_key {
|
||||
Some("AUTOINCREMENT".to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
StructureDialect::Mysql => {
|
||||
let mut clauses = Vec::new();
|
||||
if extra.auto_increment.unwrap_or(false) {
|
||||
clauses.push("AUTO_INCREMENT".to_string());
|
||||
}
|
||||
if clauses.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(clauses.join(" "))
|
||||
}
|
||||
}
|
||||
StructureDialect::Postgres | StructureDialect::H2 => {
|
||||
if let Some(identity) = &extra.identity {
|
||||
let generation = identity.generation.as_deref().unwrap_or("BY DEFAULT");
|
||||
let mut clause = format!("GENERATED {generation} AS IDENTITY");
|
||||
if identity.seed.is_some() || identity.increment.is_some() {
|
||||
let start = identity.seed.unwrap_or(1);
|
||||
let inc = identity.increment.unwrap_or(1);
|
||||
clause.push_str(&format!(" (START WITH {start} INCREMENT BY {inc})"));
|
||||
}
|
||||
Some(clause)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
StructureDialect::SqlServer => {
|
||||
if extra.auto_increment.unwrap_or(false) || extra.identity.is_some() {
|
||||
let seed = extra.identity.as_ref().and_then(|i| i.seed).unwrap_or(1);
|
||||
let increment = extra.identity.as_ref().and_then(|i| i.increment).unwrap_or(1);
|
||||
Some(format!("IDENTITY({seed}, {increment})"))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
StructureDialect::Dameng => {
|
||||
if extra.auto_increment.unwrap_or(false) || extra.identity.is_some() {
|
||||
let seed = extra.identity.as_ref().and_then(|i| i.seed).unwrap_or(1);
|
||||
let increment = extra.identity.as_ref().and_then(|i| i.increment).unwrap_or(1);
|
||||
Some(format!("IDENTITY({seed}, {increment})"))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn has_dameng_identity(column: &EditableStructureColumn) -> bool {
|
||||
column.extra.as_ref().is_some_and(|extra| extra.auto_increment.unwrap_or(false) || extra.identity.is_some())
|
||||
}
|
||||
|
||||
pub(super) fn is_dameng_identity_compatible_type(data_type: &str) -> bool {
|
||||
let trimmed = data_type.trim();
|
||||
let (base_type, params) = match trimmed.find('(') {
|
||||
Some(open_index) => {
|
||||
let close_index = trimmed.rfind(')').unwrap_or(trimmed.len());
|
||||
(&trimmed[..open_index], trimmed.get(open_index + 1..close_index).unwrap_or(""))
|
||||
}
|
||||
None => (trimmed, ""),
|
||||
};
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
if matches!(normalized.as_str(), "tinyint" | "smallint" | "int" | "integer" | "bigint") {
|
||||
return true;
|
||||
}
|
||||
if !matches!(normalized.as_str(), "number" | "numeric" | "decimal" | "dec") {
|
||||
return false;
|
||||
}
|
||||
let normalized_params = params.split_whitespace().collect::<String>();
|
||||
if normalized_params.is_empty() {
|
||||
return true;
|
||||
}
|
||||
let parts = normalized_params.split(',').collect::<Vec<_>>();
|
||||
match parts.as_slice() {
|
||||
[precision] => precision.parse::<u32>().is_ok(),
|
||||
[precision, scale] => precision.parse::<u32>().is_ok() && *scale == "0",
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn column_data_type(dialect: StructureDialect, column: &EditableStructureColumn) -> String {
|
||||
if dialect == StructureDialect::ClickHouse {
|
||||
return clickhouse_column_type(column);
|
||||
}
|
||||
if dialect == StructureDialect::ManticoreSearch {
|
||||
return manticore_column_type(column);
|
||||
}
|
||||
if dialect == StructureDialect::Questdb {
|
||||
return questdb_column_type(column);
|
||||
}
|
||||
let normalized = normalize_column_data_type(dialect, &column.data_type);
|
||||
// Dameng only recognizes its built-in type keywords in the canonical
|
||||
// upper-case form: a lower-case `varchar(50)` in DDL is stored as a
|
||||
// USER-DEFINED type instead of VARCHAR (issue #7343). Uppercase after the
|
||||
// dialect-specific normalization so its rewrites still apply.
|
||||
if dialect == StructureDialect::Dameng {
|
||||
return normalized.to_uppercase();
|
||||
}
|
||||
normalized
|
||||
}
|
||||
|
||||
fn manticore_column_type(column: &EditableStructureColumn) -> String {
|
||||
let data_type = normalize_column_data_type(StructureDialect::ManticoreSearch, &column.data_type);
|
||||
let normalized = data_type.trim().to_ascii_lowercase();
|
||||
if normalized == "json" {
|
||||
let Some(extra) = column.extra.as_ref() else {
|
||||
return data_type;
|
||||
};
|
||||
if extra.manticore_secondary_index.unwrap_or(false) {
|
||||
return format!("{data_type} secondary_index='1'");
|
||||
}
|
||||
return data_type;
|
||||
}
|
||||
if !matches!(normalized.as_str(), "text" | "string") {
|
||||
return data_type;
|
||||
}
|
||||
|
||||
let Some(extra) = column.extra.as_ref() else {
|
||||
return data_type;
|
||||
};
|
||||
let mut parts = vec![data_type];
|
||||
if extra.manticore_stored.unwrap_or(false) {
|
||||
parts.push("stored".to_string());
|
||||
}
|
||||
if extra.manticore_attribute.unwrap_or(false) {
|
||||
parts.push("attribute".to_string());
|
||||
}
|
||||
if extra.manticore_indexed.unwrap_or(false) {
|
||||
parts.push("indexed".to_string());
|
||||
}
|
||||
parts.join(" ")
|
||||
}
|
||||
|
||||
pub(super) fn normalize_column_data_type(dialect: StructureDialect, data_type: &str) -> String {
|
||||
let trimmed = data_type.trim();
|
||||
#[cfg(feature = "duckdb-sidecar")]
|
||||
if dialect == StructureDialect::DuckDb {
|
||||
if let Some(normalized) = normalize_duckdb_type_modifiers(trimmed) {
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
let Some(open_index) = trimmed.find('(') else {
|
||||
return trimmed.to_string();
|
||||
};
|
||||
if !trimmed.ends_with(')') {
|
||||
return trimmed.to_string();
|
||||
}
|
||||
|
||||
let base_type = trimmed[..open_index].trim();
|
||||
let params = trimmed[open_index + 1..trimmed.len() - 1].trim();
|
||||
if base_type.is_empty() || params.is_empty() {
|
||||
return trimmed.to_string();
|
||||
}
|
||||
|
||||
if dialect == StructureDialect::Mysql {
|
||||
if let Some(normalized) = normalize_mysql_numeric_attribute_type(base_type, params) {
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
|
||||
if is_oracle_like(dialect) && is_oracle_lengthless_type(base_type) {
|
||||
// Dameng/Oracle integer aliases do not accept MySQL-style display widths like INTEGER(11).
|
||||
return base_type.to_string();
|
||||
}
|
||||
|
||||
if dialect == StructureDialect::SqlServer && is_sqlserver_lengthless_type(base_type) {
|
||||
// SQL Server exact integer and legacy LOB types do not accept MySQL-style display widths.
|
||||
return base_type.to_string();
|
||||
}
|
||||
|
||||
if dialect == StructureDialect::SqlServer && is_sqlserver_float_with_scale(base_type, params) {
|
||||
// SQL Server float only accepts a single integer mantissa bit count (1–53),
|
||||
// not comma-separated precision/scale like float(10,2).
|
||||
return base_type.to_string();
|
||||
}
|
||||
|
||||
if is_temporal_precision_type(dialect, base_type) {
|
||||
return if is_valid_temporal_precision(params, dialect) {
|
||||
format!("{base_type}({params})")
|
||||
} else {
|
||||
base_type.to_string()
|
||||
};
|
||||
}
|
||||
|
||||
trimmed.to_string()
|
||||
}
|
||||
|
||||
#[cfg(feature = "duckdb-sidecar")]
|
||||
fn normalize_duckdb_type_modifiers(data_type: &str) -> Option<String> {
|
||||
let (raw_base, remainder) = data_type.split_once('(')?;
|
||||
let (raw_params, suffix) = remainder.split_once(')')?;
|
||||
let params = raw_params.trim();
|
||||
if params.is_empty() || params.contains('(') {
|
||||
return None;
|
||||
}
|
||||
let mut base_type = raw_base.trim().to_string();
|
||||
let suffix = suffix.split_whitespace().collect::<Vec<_>>().join(" ");
|
||||
if !suffix.is_empty() {
|
||||
if !matches!(base_type.to_ascii_lowercase().as_str(), "time" | "timestamp")
|
||||
|| !matches!(suffix.to_ascii_lowercase().as_str(), "with time zone" | "without time zone")
|
||||
{
|
||||
return None;
|
||||
}
|
||||
base_type.push(' ');
|
||||
base_type.push_str(&suffix);
|
||||
}
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
if matches!(
|
||||
normalized.as_str(),
|
||||
"tinyint"
|
||||
| "int1"
|
||||
| "smallint"
|
||||
| "int2"
|
||||
| "short"
|
||||
| "int16"
|
||||
| "integer"
|
||||
| "int"
|
||||
| "int4"
|
||||
| "signed"
|
||||
| "integral"
|
||||
| "int32"
|
||||
| "bigint"
|
||||
| "int8"
|
||||
| "long"
|
||||
| "oid"
|
||||
| "int64"
|
||||
| "hugeint"
|
||||
| "int128"
|
||||
| "utinyint"
|
||||
| "uint8"
|
||||
| "usmallint"
|
||||
| "uint16"
|
||||
| "uinteger"
|
||||
| "uint32"
|
||||
| "ubigint"
|
||||
| "uint64"
|
||||
| "uhugeint"
|
||||
| "uint128"
|
||||
| "real"
|
||||
| "float4"
|
||||
| "double"
|
||||
| "double precision"
|
||||
| "float8"
|
||||
| "boolean"
|
||||
| "bool"
|
||||
| "logical"
|
||||
| "blob"
|
||||
| "bytea"
|
||||
| "binary"
|
||||
| "varbinary"
|
||||
| "bit"
|
||||
| "bitstring"
|
||||
| "varint"
|
||||
| "bignum"
|
||||
| "date"
|
||||
| "time"
|
||||
| "time without time zone"
|
||||
| "time with time zone"
|
||||
| "timetz"
|
||||
| "timestamptz"
|
||||
| "timestamp with time zone"
|
||||
| "timestamp_s"
|
||||
| "timestamp_ms"
|
||||
| "timestamp_ns"
|
||||
| "uuid"
|
||||
| "guid"
|
||||
| "json"
|
||||
| "interval"
|
||||
) {
|
||||
return Some(base_type);
|
||||
}
|
||||
if normalized == "float"
|
||||
&& (!params.bytes().all(|byte| byte.is_ascii_digit())
|
||||
|| !params.parse::<u64>().is_ok_and(|precision| (1..=53).contains(&precision)))
|
||||
{
|
||||
return Some(base_type);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn is_oracle_lengthless_type(base_type: &str) -> bool {
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
matches!(
|
||||
normalized.as_str(),
|
||||
"binary_double"
|
||||
| "binary_float"
|
||||
| "bigint"
|
||||
| "boolean"
|
||||
| "bool"
|
||||
| "byte"
|
||||
| "date"
|
||||
| "double"
|
||||
| "double precision"
|
||||
| "float"
|
||||
| "integer"
|
||||
| "int"
|
||||
| "long"
|
||||
| "long raw"
|
||||
| "nclob"
|
||||
| "real"
|
||||
| "smallint"
|
||||
| "text"
|
||||
| "tinyint"
|
||||
)
|
||||
}
|
||||
|
||||
fn is_sqlserver_lengthless_type(base_type: &str) -> bool {
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
matches!(
|
||||
normalized.as_str(),
|
||||
"bigint"
|
||||
| "bit"
|
||||
| "date"
|
||||
| "datetime"
|
||||
| "image"
|
||||
| "int"
|
||||
| "integer"
|
||||
| "money"
|
||||
| "ntext"
|
||||
| "real"
|
||||
| "smalldatetime"
|
||||
| "smallint"
|
||||
| "smallmoney"
|
||||
| "sql_variant"
|
||||
| "text"
|
||||
| "timestamp"
|
||||
| "tinyint"
|
||||
| "uniqueidentifier"
|
||||
| "xml"
|
||||
)
|
||||
}
|
||||
|
||||
/// SQL Server `float` accepts a single integer mantissa bit count (1–53)
|
||||
/// but rejects comma-separated precision/scale like `float(10,2)`.
|
||||
fn is_sqlserver_float_with_scale(base_type: &str, params: &str) -> bool {
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
normalized == "float" && params.contains(',')
|
||||
}
|
||||
|
||||
fn normalize_mysql_numeric_attribute_type(base_type: &str, params: &str) -> Option<String> {
|
||||
let mut parts: Vec<&str> = base_type.split_whitespace().collect();
|
||||
let type_name = parts.first().copied()?.to_ascii_lowercase();
|
||||
if !matches!(
|
||||
type_name.as_str(),
|
||||
"tinyint"
|
||||
| "smallint"
|
||||
| "mediumint"
|
||||
| "int"
|
||||
| "integer"
|
||||
| "bigint"
|
||||
| "real"
|
||||
| "double"
|
||||
| "float"
|
||||
| "decimal"
|
||||
| "numeric"
|
||||
) {
|
||||
return None;
|
||||
}
|
||||
let split_index = parts.iter().position(|part| {
|
||||
let normalized = part.to_ascii_lowercase();
|
||||
matches!(normalized.as_str(), "signed" | "unsigned" | "zerofill")
|
||||
})?;
|
||||
if !parts[split_index..].iter().all(|part| {
|
||||
let normalized = part.to_ascii_lowercase();
|
||||
matches!(normalized.as_str(), "signed" | "unsigned" | "zerofill")
|
||||
}) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let attrs = parts.split_off(split_index).join(" ");
|
||||
let base = parts.join(" ");
|
||||
Some(format!("{base}({params}) {attrs}"))
|
||||
}
|
||||
|
||||
pub(super) fn is_temporal_precision_type(dialect: StructureDialect, base_type: &str) -> bool {
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
match dialect {
|
||||
StructureDialect::Mysql => matches!(normalized.as_str(), "time" | "datetime" | "timestamp"),
|
||||
StructureDialect::Postgres => matches!(
|
||||
normalized.as_str(),
|
||||
"time"
|
||||
| "time without time zone"
|
||||
| "time with time zone"
|
||||
| "timestamp"
|
||||
| "timestamp without time zone"
|
||||
| "timestamp with time zone"
|
||||
),
|
||||
StructureDialect::SqlServer => matches!(normalized.as_str(), "time" | "datetime2" | "datetimeoffset"),
|
||||
StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::Oscar => {
|
||||
matches!(normalized.as_str(), "timestamp" | "timestamp with time zone" | "timestamp with local time zone")
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn is_valid_temporal_precision(params: &str, dialect: StructureDialect) -> bool {
|
||||
let Ok(value) = params.parse::<u8>() else {
|
||||
return false;
|
||||
};
|
||||
let max = if matches!(dialect, StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::Oscar) {
|
||||
9
|
||||
} else {
|
||||
6
|
||||
};
|
||||
value <= max && params == value.to_string()
|
||||
}
|
||||
|
||||
/// Fractional-seconds precision of a MySQL temporal column type, e.g. `3` for
|
||||
/// `datetime(3)`. MySQL requires every `CURRENT_TIMESTAMP` in a column
|
||||
/// definition to carry the same precision the column type declares: a
|
||||
/// `datetime(3) ... DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`
|
||||
/// definition is rejected with `ERROR 1067 (42000): Invalid default value`
|
||||
/// while the `(3)`-suffixed clauses succeed. The editor model keeps only an
|
||||
/// on-update flag, so the precision has to be recovered from the data type.
|
||||
/// Non-temporal types and invalid precisions fall back to `None` (no suffix).
|
||||
pub(super) fn mysql_temporal_precision(data_type: &str) -> Option<String> {
|
||||
let trimmed = data_type.trim();
|
||||
let open_index = trimmed.find('(')?;
|
||||
if !trimmed.ends_with(')') {
|
||||
return None;
|
||||
}
|
||||
let base_type = trimmed[..open_index].trim();
|
||||
let params = trimmed[open_index + 1..trimmed.len() - 1].trim();
|
||||
if !is_temporal_precision_type(StructureDialect::Mysql, base_type)
|
||||
|| !is_valid_temporal_precision(params, StructureDialect::Mysql)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
Some(params.to_string())
|
||||
}
|
||||
|
||||
/// `ON UPDATE CURRENT_TIMESTAMP` clause for a MySQL column, carrying the
|
||||
/// column's temporal precision (if any) so the clause matches the type.
|
||||
pub(super) fn mysql_on_update_current_timestamp_clause(data_type: &str) -> String {
|
||||
match mysql_temporal_precision(data_type) {
|
||||
Some(fsp) => format!("ON UPDATE CURRENT_TIMESTAMP({fsp})"),
|
||||
None => "ON UPDATE CURRENT_TIMESTAMP".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn clickhouse_column_type(column: &EditableStructureColumn) -> String {
|
||||
let data_type = column.data_type.trim();
|
||||
if column.is_nullable {
|
||||
if data_type.to_ascii_lowercase().starts_with("nullable") {
|
||||
data_type.to_string()
|
||||
} else {
|
||||
format!("Nullable({data_type})")
|
||||
}
|
||||
} else {
|
||||
unwrap_clickhouse_nullable_type(data_type)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn unwrap_clickhouse_nullable_type(data_type: &str) -> String {
|
||||
let trimmed = data_type.trim();
|
||||
let lower = trimmed.to_ascii_lowercase();
|
||||
if lower.starts_with("nullable(") && trimmed.ends_with(')') {
|
||||
trimmed[trimmed.find('(').unwrap_or(0) + 1..trimmed.len() - 1].trim().to_string()
|
||||
} else {
|
||||
trimmed.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// QuestDB 类型处理
|
||||
/// geohash, decimal 这2种类型才能带()
|
||||
pub(super) fn questdb_column_type(column: &EditableStructureColumn) -> String {
|
||||
let data_type = column.data_type.trim().to_ascii_lowercase();
|
||||
let length_types: [&str; 2] = ["geohash", "decimal"];
|
||||
match data_type.find('(') {
|
||||
Some(pos) => {
|
||||
let base_type = &data_type[..pos];
|
||||
let params = &data_type[pos..];
|
||||
if length_types.contains(&base_type) {
|
||||
format!("{}{}", base_type, params)
|
||||
} else {
|
||||
base_type.to_string()
|
||||
}
|
||||
}
|
||||
None => data_type,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` when the data type is a MySQL character/string type that accepts
|
||||
/// `CHARACTER SET` and `COLLATE` clauses in column definitions.
|
||||
///
|
||||
/// Character types that support per-column charset/collation:
|
||||
/// `char`, `varchar`, `tinytext`, `text`, `mediumtext`, `longtext`, `enum`, `set`
|
||||
///
|
||||
/// Numeric, temporal, spatial, binary/blob, and JSON types do NOT support these clauses.
|
||||
pub(super) fn is_mysql_character_data_type(data_type: &str) -> bool {
|
||||
let trimmed = data_type.trim();
|
||||
let base_type = match trimmed.find('(') {
|
||||
Some(open_index) => trimmed[..open_index].trim(),
|
||||
None => trimmed,
|
||||
};
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
matches!(normalized.as_str(), "char" | "varchar" | "tinytext" | "text" | "mediumtext" | "longtext" | "enum" | "set")
|
||||
}
|
||||
|
||||
/// MySQL silently rewrites a nullable `TIMESTAMP` column to `NOT NULL` when the
|
||||
/// generated DDL omits an explicit `NULL` keyword — regardless of whether a
|
||||
/// `DEFAULT` is present. With the (still common) `explicit_defaults_for_timestamp`
|
||||
/// server default of `OFF`, this can outright fail with `ERROR 1067 (42000):
|
||||
/// Invalid default value` for any non-first TIMESTAMP column. `DATETIME` is not
|
||||
/// affected and must not be touched.
|
||||
pub(super) fn is_mysql_timestamp_type(data_type: &str) -> bool {
|
||||
let trimmed = data_type.trim();
|
||||
let base_type = match trimmed.find('(') {
|
||||
Some(open_index) => trimmed[..open_index].trim(),
|
||||
None => trimmed,
|
||||
};
|
||||
base_type.eq_ignore_ascii_case("timestamp")
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "duckdb-sidecar"))]
|
||||
mod duckdb_type_parameter_tests {
|
||||
use super::*;
|
||||
use crate::table_structure_sql::{build_create_table_sql, build_table_structure_change_sql};
|
||||
|
||||
#[test]
|
||||
fn removes_unsupported_scalar_modifiers() {
|
||||
for base_type in [
|
||||
"TINYINT",
|
||||
"INT1",
|
||||
"SMALLINT",
|
||||
"INT2",
|
||||
"SHORT",
|
||||
"INT16",
|
||||
"INTEGER",
|
||||
"INT",
|
||||
"INT4",
|
||||
"SIGNED",
|
||||
"INTEGRAL",
|
||||
"INT32",
|
||||
"BIGINT",
|
||||
"INT8",
|
||||
"LONG",
|
||||
"OID",
|
||||
"INT64",
|
||||
"HUGEINT",
|
||||
"INT128",
|
||||
"UTINYINT",
|
||||
"UINT8",
|
||||
"USMALLINT",
|
||||
"UINT16",
|
||||
"UINTEGER",
|
||||
"UINT32",
|
||||
"UBIGINT",
|
||||
"UINT64",
|
||||
"UHUGEINT",
|
||||
"UINT128",
|
||||
"REAL",
|
||||
"FLOAT4",
|
||||
"DOUBLE",
|
||||
"DOUBLE PRECISION",
|
||||
"FLOAT8",
|
||||
"BOOLEAN",
|
||||
"BOOL",
|
||||
"LOGICAL",
|
||||
"BLOB",
|
||||
"BYTEA",
|
||||
"BINARY",
|
||||
"VARBINARY",
|
||||
"BIT",
|
||||
"BITSTRING",
|
||||
"VARINT",
|
||||
"BIGNUM",
|
||||
"DATE",
|
||||
"TIME",
|
||||
"TIME WITHOUT TIME ZONE",
|
||||
"TIME WITH TIME ZONE",
|
||||
"TIMETZ",
|
||||
"TIMESTAMPTZ",
|
||||
"TIMESTAMP WITH TIME ZONE",
|
||||
"TIMESTAMP_S",
|
||||
"TIMESTAMP_MS",
|
||||
"TIMESTAMP_NS",
|
||||
"UUID",
|
||||
"GUID",
|
||||
"JSON",
|
||||
"INTERVAL",
|
||||
] {
|
||||
for params in ["11", "10,2"] {
|
||||
let data_type = format!("{base_type}({params})");
|
||||
assert_eq!(normalize_column_data_type(StructureDialect::DuckDb, &data_type), base_type);
|
||||
}
|
||||
}
|
||||
assert_eq!(normalize_column_data_type(StructureDialect::DuckDb, " integer (11) "), "integer");
|
||||
assert_eq!(
|
||||
normalize_column_data_type(StructureDialect::DuckDb, "TIMESTAMP(6) WITH TIME ZONE"),
|
||||
"TIMESTAMP WITH TIME ZONE"
|
||||
);
|
||||
assert_eq!(
|
||||
normalize_column_data_type(StructureDialect::DuckDb, "TIME(6) WITHOUT TIME ZONE"),
|
||||
"TIME WITHOUT TIME ZONE"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_float_mantissa_bits_and_drops_invalid_float_parameters() {
|
||||
for precision in ["1", "24", "25", "53"] {
|
||||
let data_type = format!("FLOAT({precision})");
|
||||
assert_eq!(normalize_column_data_type(StructureDialect::DuckDb, &data_type), data_type);
|
||||
}
|
||||
for invalid in ["0", "54", "-1", "1.5", "10,2", "abc", "999999999999999999999999"] {
|
||||
assert_eq!(normalize_column_data_type(StructureDialect::DuckDb, &format!("FLOAT({invalid})")), "FLOAT");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_supported_compound_unknown_and_other_dialect_types() {
|
||||
for data_type in [
|
||||
"INTEGER",
|
||||
"DECIMAL(10,2)",
|
||||
"NUMERIC(38,0)",
|
||||
"DEC(12,3)",
|
||||
"VARCHAR(255)",
|
||||
"CHAR(1)",
|
||||
"BPCHAR(20)",
|
||||
"CHARACTER VARYING(20)",
|
||||
"CHARACTER(20)",
|
||||
"TEXT(20)",
|
||||
"STRING(20)",
|
||||
"NVARCHAR(20)",
|
||||
"TIMESTAMP(9)",
|
||||
"DATETIME(3)",
|
||||
"TIMESTAMP_US(3)",
|
||||
"TIMESTAMP(3) WITHOUT TIME ZONE",
|
||||
"DECIMAL(10,2)[]",
|
||||
"INTEGER[3]",
|
||||
"INTEGER(11)[]",
|
||||
"STRUCT(id INTEGER, price DECIMAL(10,2))",
|
||||
"MAP(VARCHAR, DECIMAL(10,2))",
|
||||
"UNION(id INTEGER, name VARCHAR)",
|
||||
"ENUM('a', 'b')",
|
||||
"custom_type(10)",
|
||||
"main.INTEGER(11)",
|
||||
"\"INTEGER\"(11)",
|
||||
"INTEGER(",
|
||||
"INTEGER()",
|
||||
"INTEGER(11) trailing",
|
||||
"DECIMAL(39,0)",
|
||||
"VARCHAR(-1)",
|
||||
] {
|
||||
assert_eq!(normalize_column_data_type(StructureDialect::DuckDb, data_type), data_type);
|
||||
}
|
||||
for dialect in [StructureDialect::Mysql, StructureDialect::Postgres, StructureDialect::Sqlite] {
|
||||
for data_type in ["INTEGER(11)", "FLOAT(10,2)", "DECIMAL(10,2)", "VARCHAR(255)"] {
|
||||
assert_eq!(normalize_column_data_type(dialect, data_type), data_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalizes_create_and_add_column_save_paths() {
|
||||
let options: TableStructureSqlOptions = serde_json::from_value(serde_json::json!({
|
||||
"databaseType": "duckdb", "tableName": "issue_9980",
|
||||
"columns": [
|
||||
{"id": "new:id", "name": "id", "dataType": "INTEGER(11)", "isNullable": true},
|
||||
{"id": "new:ratio", "name": "ratio", "dataType": "FLOAT(10,2)", "isNullable": true},
|
||||
{"id": "new:amount", "name": "amount", "dataType": "DECIMAL(10,2)", "isNullable": true},
|
||||
{"id": "new:label", "name": "label", "dataType": "VARCHAR(255)", "isNullable": true}
|
||||
]
|
||||
}))
|
||||
.unwrap();
|
||||
let created = build_create_table_sql(options.clone());
|
||||
assert!(created.warnings.is_empty(), "{:?}", created.warnings);
|
||||
assert_eq!(created.statements, vec![
|
||||
"CREATE TABLE \"issue_9980\" (\n \"id\" INTEGER,\n \"ratio\" FLOAT,\n \"amount\" DECIMAL(10,2),\n \"label\" VARCHAR(255)\n);"
|
||||
]);
|
||||
let altered = build_table_structure_change_sql(options);
|
||||
assert!(altered.warnings.is_empty(), "{:?}", altered.warnings);
|
||||
assert_eq!(
|
||||
altered.statements,
|
||||
vec![
|
||||
"ALTER TABLE \"issue_9980\" ADD COLUMN \"id\" INTEGER;",
|
||||
"ALTER TABLE \"issue_9980\" ADD COLUMN \"ratio\" FLOAT;",
|
||||
"ALTER TABLE \"issue_9980\" ADD COLUMN \"amount\" DECIMAL(10,2);",
|
||||
"ALTER TABLE \"issue_9980\" ADD COLUMN \"label\" VARCHAR(255);",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retains_existing_column_edit_boundary() {
|
||||
let options = serde_json::from_value(serde_json::json!({
|
||||
"databaseType": "duckdb", "tableName": "issue_9980",
|
||||
"column": {
|
||||
"id": "existing:id", "name": "id", "dataType": "INTEGER(11)", "isNullable": true,
|
||||
"original": {"name": "id", "data_type": "SMALLINT", "is_nullable": true, "column_default": null}
|
||||
}
|
||||
}))
|
||||
.unwrap();
|
||||
let altered = crate::table_structure_sql::build_single_column_alter_sql(options);
|
||||
assert!(altered.statements.is_empty());
|
||||
assert_eq!(altered.warnings, vec!["Editing existing columns is not supported for duckdb yet."]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,774 @@
|
||||
use super::column_alter::{
|
||||
build_clickhouse_existing_column_sql, build_dameng_existing_column_sql, build_doris_existing_column_sql,
|
||||
build_h2_existing_column_sql, build_informix_existing_column_sql, build_iris_existing_column_sql,
|
||||
build_mysql_existing_column_clause, build_oracle_like_existing_column_sql, build_oscar_existing_column_sql,
|
||||
build_postgres_existing_column_sql, build_questdb_existing_column_sql, build_sqlite_existing_column_sql,
|
||||
build_sqlserver_existing_column_sql, build_xugu_existing_column_sql, dameng_drops_identity,
|
||||
has_column_extra_change, has_existing_column_attribute_change, validate_dameng_existing_identity_change,
|
||||
};
|
||||
use super::column_format::{
|
||||
column_definition, has_dameng_identity, is_dameng_identity_compatible_type, is_mysql_character_data_type,
|
||||
original_is_mysql_generated_column, original_mysql_generated_clause,
|
||||
};
|
||||
use super::comments::build_sqlserver_column_comment_sql_for_profile;
|
||||
use super::dialect::{capabilities_for, database_label, is_oracle_like, StructureDialect};
|
||||
use super::indexes::has_existing_index_change;
|
||||
use super::types::{EditableStructureColumn, TableStructureSqlOptions};
|
||||
use super::util::{
|
||||
clean, is_protected_manticore_id_column, normalize_default, original_comment, original_default, qualified_table,
|
||||
quote_ident, quote_string,
|
||||
};
|
||||
use crate::models::connection::DatabaseType;
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub(super) fn build_column_sql(options: &TableStructureSqlOptions, warnings: &mut Vec<String>) -> Vec<String> {
|
||||
let capabilities = capabilities_for(options.database_type, options.driver_profile.as_deref());
|
||||
let dialect = capabilities.dialect;
|
||||
let table = qualified_table(dialect, options.schema.as_deref(), &options.table_name);
|
||||
let database_label = database_label(options.database_type);
|
||||
let active_columns: Vec<_> = options.columns.iter().filter(|column| !column.marked_for_drop).collect();
|
||||
if dialect == StructureDialect::Dameng {
|
||||
let identity_columns: Vec<_> = active_columns.iter().filter(|column| has_dameng_identity(column)).collect();
|
||||
if identity_columns.len() > 1
|
||||
|| identity_columns.iter().any(|column| {
|
||||
column.extra.as_ref().and_then(|extra| extra.identity.as_ref()).and_then(|identity| identity.increment)
|
||||
== Some(0)
|
||||
})
|
||||
{
|
||||
return Vec::new();
|
||||
}
|
||||
let mut valid_identity_changes = true;
|
||||
for column in &active_columns {
|
||||
if has_dameng_identity(column) && !is_dameng_identity_compatible_type(&column.data_type) {
|
||||
warnings.push(format!(
|
||||
"Dameng identity column \"{}\" must use tinyint, smallint, int, integer, bigint, number, numeric, or decimal/dec with scale 0.",
|
||||
column.name
|
||||
));
|
||||
valid_identity_changes = false;
|
||||
} else if column.original.is_some() && !validate_dameng_existing_identity_change(column, warnings) {
|
||||
valid_identity_changes = false;
|
||||
}
|
||||
}
|
||||
if !valid_identity_changes {
|
||||
return Vec::new();
|
||||
}
|
||||
}
|
||||
if is_oracle_like(dialect)
|
||||
&& active_columns.is_empty()
|
||||
&& options.columns.iter().any(|column| column.marked_for_drop)
|
||||
{
|
||||
warnings.push("Oracle does not allow dropping all columns from a table. Keep at least one column or drop the table instead.".to_string());
|
||||
return Vec::new();
|
||||
}
|
||||
let has_original_column_positions = active_columns.iter().any(|column| column.original_position.is_some());
|
||||
let mut simulated_column_order =
|
||||
if has_original_column_positions { original_active_column_order(&active_columns) } else { Vec::new() };
|
||||
// Pre-compute the minimal set of existing columns that really need an explicit move.
|
||||
// For MySQL/ClickHouse we keep the largest already-ordered subset in place and only
|
||||
// emit FIRST/AFTER SQL for columns outside that subset.
|
||||
let reordered_existing_column_ids =
|
||||
if has_original_column_positions && matches!(dialect, StructureDialect::Mysql | StructureDialect::ClickHouse) {
|
||||
planned_existing_column_move_ids(&active_columns)
|
||||
} else {
|
||||
HashSet::new()
|
||||
};
|
||||
let mysql_primary_key_change = if options.database_type == Some(DatabaseType::Mysql) && !options.is_gaussdb_m_mode {
|
||||
primary_key_change(options)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// MySQL validates AUTO_INCREMENT against the statement's final index layout. Keep the
|
||||
// dependent column and key clauses together so no implicit commit exposes an invalid middle state.
|
||||
let mysql_coalesced_primary_key_change = mysql_primary_key_change
|
||||
.as_ref()
|
||||
.filter(|change| options.columns.iter().any(|column| mysql_auto_increment_touches_primary_key(column, change)));
|
||||
let mut mysql_primary_key_column_clauses = Vec::new();
|
||||
let mut statements = Vec::new();
|
||||
// DM8 owns identity at table level, so remove the old identity before any per-column ADD,
|
||||
// even when the target column appears earlier in the submitted draft.
|
||||
if dialect == StructureDialect::Dameng
|
||||
&& options.columns.iter().any(|column| !column.marked_for_drop && dameng_drops_identity(column))
|
||||
{
|
||||
statements.push(format!("ALTER TABLE {table} DROP IDENTITY;"));
|
||||
}
|
||||
|
||||
for column in &options.columns {
|
||||
if column.marked_for_drop {
|
||||
let Some(original) = &column.original else {
|
||||
continue;
|
||||
};
|
||||
if !capabilities.drop_column {
|
||||
warnings.push(format!("Dropping columns is not supported for {database_label} from this editor."));
|
||||
continue;
|
||||
}
|
||||
if original.is_primary_key {
|
||||
warnings.push(format!("Primary key column \"{}\" cannot be dropped from this editor.", original.name));
|
||||
continue;
|
||||
}
|
||||
if is_protected_manticore_id_column(dialect, &original.name) {
|
||||
warnings.push("Manticore Search id column cannot be dropped from this editor.".to_string());
|
||||
continue;
|
||||
}
|
||||
statements.push(build_drop_column_sql(dialect, &table, &original.name));
|
||||
continue;
|
||||
}
|
||||
|
||||
let active_index = active_columns.iter().position(|active| active.id == column.id).unwrap_or(0);
|
||||
let position_clause = if has_original_column_positions {
|
||||
column_position_clause(dialect, &active_columns, active_index)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let desired_previous_column_id = active_previous_column_id(&active_columns, active_index);
|
||||
// A position change only matters when this column is part of the planned move set
|
||||
// and its predecessor still differs in the simulated order.
|
||||
let has_position_change = has_original_column_positions
|
||||
&& matches!(dialect, StructureDialect::Mysql | StructureDialect::ClickHouse)
|
||||
&& reordered_existing_column_ids.contains(&column.id)
|
||||
&& column.original.is_some()
|
||||
&& column.original_position.is_some()
|
||||
&& simulated_column_position_changed(&simulated_column_order, &column.id, desired_previous_column_id);
|
||||
|
||||
if column.original.is_none() {
|
||||
if !capabilities.add_column {
|
||||
warnings.push(format!("Adding columns is not supported for {database_label} from this editor."));
|
||||
continue;
|
||||
}
|
||||
if dialect == StructureDialect::SqlServer
|
||||
&& has_sqlserver_identity(column)
|
||||
&& !is_sqlserver_identity_compatible_type(&column.data_type)
|
||||
{
|
||||
warnings.push(format!(
|
||||
"SQL Server identity column \"{}\" must use tinyint, smallint, int, bigint, or decimal/numeric with scale 0.",
|
||||
column.name
|
||||
));
|
||||
continue;
|
||||
}
|
||||
if !capabilities.comment && !clean(&column.comment).is_empty() {
|
||||
warnings.push(format!(
|
||||
"Column comments are not supported for {database_label} from this editor; the comment for \"{}\" was ignored.",
|
||||
column.name
|
||||
));
|
||||
}
|
||||
if mysql_coalesced_primary_key_change
|
||||
.is_some_and(|change| mysql_auto_increment_touches_primary_key(column, change))
|
||||
{
|
||||
mysql_primary_key_column_clauses.push(format!(
|
||||
"ADD COLUMN {}{position_clause}",
|
||||
column_definition(StructureDialect::Mysql, column)
|
||||
));
|
||||
} else {
|
||||
statements.extend(build_add_column_sql(
|
||||
dialect,
|
||||
options.database_type,
|
||||
capabilities.comment,
|
||||
&table,
|
||||
column,
|
||||
&position_clause,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
options.driver_profile.as_deref(),
|
||||
));
|
||||
}
|
||||
if has_original_column_positions
|
||||
&& matches!(dialect, StructureDialect::Mysql | StructureDialect::ClickHouse)
|
||||
{
|
||||
apply_simulated_column_position(&mut simulated_column_order, &column.id, desired_previous_column_id);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// A column handing its primary key membership over to another column keeps its
|
||||
// AUTO_INCREMENT flag in the submitted draft, which MySQL rejects (ERROR 1075) once
|
||||
// the column is no longer keyed. Rewrite it inside the same coalesced ALTER even
|
||||
// though the draft reports no change on the column itself.
|
||||
let clears_orphaned_auto_increment = mysql_coalesced_primary_key_change
|
||||
.is_some_and(|change| mysql_orphans_auto_increment_column(options, column, change));
|
||||
|
||||
if !has_existing_column_attribute_change(column)
|
||||
&& !has_column_extra_change(column)
|
||||
&& !has_position_change
|
||||
&& !clears_orphaned_auto_increment
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let original = column.original.as_ref().unwrap();
|
||||
let has_rename = column.name != original.name;
|
||||
let has_comment_change = clean(&column.comment) != original_comment(column);
|
||||
let has_attribute_change = column.data_type.trim() != original.data_type.trim()
|
||||
|| column.is_nullable != original.is_nullable
|
||||
|| normalize_default(Some(&column.default_value)) != original_default(column)
|
||||
|| (has_comment_change && capabilities.comment)
|
||||
|| (is_mysql_character_data_type(&column.data_type)
|
||||
&& (column.character_set.trim() != original.character_set.as_deref().unwrap_or("")
|
||||
|| column.collation.trim() != original.collation.as_deref().unwrap_or("")))
|
||||
|| has_column_extra_change(column);
|
||||
if has_comment_change && !capabilities.comment {
|
||||
warnings.push(format!(
|
||||
"Column comments are not supported for {database_label} from this editor; the comment change for \"{}\" was ignored.",
|
||||
original.name
|
||||
));
|
||||
}
|
||||
if has_position_change && !capabilities.reorder_column {
|
||||
warnings.push(format!("Reordering columns is not supported for {database_label} from this editor."));
|
||||
}
|
||||
if has_rename && !capabilities.rename_column {
|
||||
warnings.push(format!("Renaming columns is not supported for {database_label} from this editor."));
|
||||
}
|
||||
if has_attribute_change && !capabilities.alter_existing_column && dialect != StructureDialect::Sqlite {
|
||||
warnings.push(format!("Editing existing columns is not supported for {database_label} yet."));
|
||||
}
|
||||
if (has_position_change && !capabilities.reorder_column)
|
||||
|| (has_rename && !capabilities.rename_column)
|
||||
|| (has_attribute_change && !capabilities.alter_existing_column && dialect != StructureDialect::Sqlite)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if dialect == StructureDialect::Mysql
|
||||
&& original_is_mysql_generated_column(column)
|
||||
&& original_mysql_generated_clause(column).is_none()
|
||||
{
|
||||
warnings.push(format!(
|
||||
"Column \"{}\" is generated, but its generation expression could not be loaded; no ALTER statement was generated to avoid removing the generated-column definition.",
|
||||
original.name
|
||||
));
|
||||
continue;
|
||||
}
|
||||
if !has_rename && !has_attribute_change && !has_position_change && !clears_orphaned_auto_increment {
|
||||
continue;
|
||||
}
|
||||
|
||||
match dialect {
|
||||
StructureDialect::Mysql => {
|
||||
let rewritten;
|
||||
let effective_column = if clears_orphaned_auto_increment {
|
||||
rewritten = without_auto_increment(column);
|
||||
&rewritten
|
||||
} else {
|
||||
column
|
||||
};
|
||||
let clause = build_mysql_existing_column_clause(
|
||||
effective_column,
|
||||
if has_position_change { &position_clause } else { "" },
|
||||
);
|
||||
if mysql_coalesced_primary_key_change
|
||||
.is_some_and(|change| mysql_auto_increment_touches_primary_key(column, change))
|
||||
{
|
||||
mysql_primary_key_column_clauses.push(clause);
|
||||
} else {
|
||||
statements.push(format!("ALTER TABLE {table} {clause};"));
|
||||
}
|
||||
}
|
||||
StructureDialect::Doris => statements.extend(build_doris_existing_column_sql(&table, column, "")),
|
||||
StructureDialect::Postgres => statements.extend(build_postgres_existing_column_sql(&table, column)),
|
||||
StructureDialect::Oracle => {
|
||||
if options.database_type == Some(crate::models::connection::DatabaseType::Iris) {
|
||||
statements.extend(build_iris_existing_column_sql(&table, column));
|
||||
} else if options.database_type == Some(crate::models::connection::DatabaseType::Xugu) {
|
||||
statements.extend(build_xugu_existing_column_sql(&table, column));
|
||||
} else {
|
||||
statements.extend(build_oracle_like_existing_column_sql(dialect, &table, column))
|
||||
}
|
||||
}
|
||||
StructureDialect::Dameng => {
|
||||
statements.extend(build_dameng_existing_column_sql(&table, column, false, warnings))
|
||||
}
|
||||
// 神通 MODIFY 语法与 Oracle 有差异(NULL/NOT NULL 须单独一条),用专属实现。
|
||||
StructureDialect::Oscar => statements.extend(build_oscar_existing_column_sql(dialect, &table, column)),
|
||||
StructureDialect::H2 => statements.extend(build_h2_existing_column_sql(&table, column)),
|
||||
StructureDialect::ClickHouse => statements.extend(build_clickhouse_existing_column_sql(
|
||||
&table,
|
||||
column,
|
||||
if has_position_change { &position_clause } else { "" },
|
||||
)),
|
||||
StructureDialect::Informix => statements.extend(build_informix_existing_column_sql(&table, column)),
|
||||
StructureDialect::SqlServer => statements.extend(build_sqlserver_existing_column_sql(
|
||||
&table,
|
||||
column,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
options.driver_profile.as_deref(),
|
||||
warnings,
|
||||
)),
|
||||
StructureDialect::Sqlite => statements.extend(build_sqlite_existing_column_sql(&table, column, warnings)),
|
||||
StructureDialect::Questdb => statements.extend(build_questdb_existing_column_sql(&table, column)),
|
||||
_ => warnings.push(format!("Editing existing columns is not supported for {database_label} yet.")),
|
||||
}
|
||||
if has_position_change {
|
||||
apply_simulated_column_position(&mut simulated_column_order, &column.id, desired_previous_column_id);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(change) = mysql_coalesced_primary_key_change {
|
||||
let mut clauses = Vec::new();
|
||||
if !change.old_ids.is_empty() {
|
||||
clauses.push("DROP PRIMARY KEY".to_string());
|
||||
}
|
||||
clauses.append(&mut mysql_primary_key_column_clauses);
|
||||
if !change.new_names.is_empty() {
|
||||
let pk_list = change
|
||||
.new_names
|
||||
.iter()
|
||||
.map(|name| quote_ident(StructureDialect::Mysql, name))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
clauses.push(format!("ADD PRIMARY KEY ({pk_list})"));
|
||||
}
|
||||
statements.push(format!("ALTER TABLE {table} {};", clauses.join(", ")));
|
||||
} else {
|
||||
// Keep the existing key while column DDL validates. This avoids leaving a table
|
||||
// without a key when an incoming key column cannot be made valid.
|
||||
statements.extend(build_primary_key_sql(options, dialect, &table, warnings));
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
fn was_primary_key_column(column: &EditableStructureColumn) -> bool {
|
||||
column.original.as_ref().is_some_and(|original| original.is_primary_key)
|
||||
}
|
||||
|
||||
/// Columns that should appear in `ADD PRIMARY KEY (...)` (must remain on the table).
|
||||
fn appears_in_add_primary_key(column: &EditableStructureColumn) -> bool {
|
||||
column.is_primary_key && !column.marked_for_drop
|
||||
}
|
||||
|
||||
struct PrimaryKeyChange<'a> {
|
||||
old_ids: HashSet<&'a str>,
|
||||
new_ids: HashSet<&'a str>,
|
||||
new_names: Vec<&'a str>,
|
||||
}
|
||||
|
||||
fn primary_key_change(options: &TableStructureSqlOptions) -> Option<PrimaryKeyChange<'_>> {
|
||||
if options.columns.iter().any(|column| column.marked_for_drop && was_primary_key_column(column)) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let old_ids: HashSet<&str> = options
|
||||
.columns
|
||||
.iter()
|
||||
.filter(|column| was_primary_key_column(column))
|
||||
.map(|column| column.id.as_str())
|
||||
.collect();
|
||||
let new_ids: HashSet<&str> = options
|
||||
.columns
|
||||
.iter()
|
||||
.filter(|column| appears_in_add_primary_key(column))
|
||||
.map(|column| column.id.as_str())
|
||||
.collect();
|
||||
if old_ids == new_ids {
|
||||
return None;
|
||||
}
|
||||
|
||||
let new_names = options
|
||||
.columns
|
||||
.iter()
|
||||
.filter(|column| appears_in_add_primary_key(column))
|
||||
.map(|column| column.name.as_str())
|
||||
.collect();
|
||||
Some(PrimaryKeyChange { old_ids, new_ids, new_names })
|
||||
}
|
||||
|
||||
fn unsupported_primary_key_change_warning(options: &TableStructureSqlOptions, change: &PrimaryKeyChange<'_>) -> String {
|
||||
let action = if change.old_ids.is_empty() { "Adding" } else { "Changing" };
|
||||
format!("{action} primary keys is not supported for {} from this editor.", database_label(options.database_type))
|
||||
}
|
||||
|
||||
pub(super) fn validate_primary_key_change_scope(options: &TableStructureSqlOptions) -> Vec<String> {
|
||||
if let Some(original) = options
|
||||
.columns
|
||||
.iter()
|
||||
.find(|column| column.marked_for_drop && was_primary_key_column(column))
|
||||
.and_then(|column| column.original.as_ref())
|
||||
{
|
||||
return vec![format!("Primary key column \"{}\" cannot be dropped from this editor.", original.name)];
|
||||
}
|
||||
|
||||
let Some(change) = primary_key_change(options) else { return Vec::new() };
|
||||
let capabilities = capabilities_for(options.database_type, options.driver_profile.as_deref());
|
||||
let supported =
|
||||
if change.old_ids.is_empty() { capabilities.add_primary_key } else { capabilities.alter_primary_key };
|
||||
if supported {
|
||||
Vec::new()
|
||||
} else {
|
||||
vec![unsupported_primary_key_change_warning(options, &change)]
|
||||
}
|
||||
}
|
||||
|
||||
fn mysql_auto_increment_touches_primary_key(column: &EditableStructureColumn, change: &PrimaryKeyChange<'_>) -> bool {
|
||||
column.extra.as_ref().is_some_and(|extra| extra.auto_increment.unwrap_or(false))
|
||||
&& (change.old_ids.contains(column.id.as_str()) || change.new_ids.contains(column.id.as_str()))
|
||||
}
|
||||
|
||||
/// MySQL allows AUTO_INCREMENT only on a column that leads some key, so a column handing the
|
||||
/// primary key over to another column must lose the flag in the same statement — unless an
|
||||
/// already existing index that survives this change still keys it.
|
||||
fn mysql_orphans_auto_increment_column(
|
||||
options: &TableStructureSqlOptions,
|
||||
column: &EditableStructureColumn,
|
||||
change: &PrimaryKeyChange<'_>,
|
||||
) -> bool {
|
||||
column.original.is_some()
|
||||
&& column.extra.as_ref().is_some_and(|extra| extra.auto_increment.unwrap_or(false))
|
||||
&& change.old_ids.contains(column.id.as_str())
|
||||
&& !change.new_ids.contains(column.id.as_str())
|
||||
&& !mysql_column_leads_kept_index(options, column)
|
||||
}
|
||||
|
||||
/// Whether a persisted, non-primary index keys this column for the whole change.
|
||||
///
|
||||
/// Only an index the draft leaves completely untouched counts. One created by this draft is
|
||||
/// added after the column DDL, and an edited one is rebuilt as DROP + CREATE after it, so
|
||||
/// neither covers the column while the coalesced ALTER runs — and the DROP would hit the very
|
||||
/// same ERROR 1075. Matching is against the persisted names on both sides, so a draft that
|
||||
/// swaps two column names cannot credit one column's index to the other.
|
||||
///
|
||||
/// This applies the InnoDB rule that the auto column must *lead* an index. MyISAM also accepts
|
||||
/// it in a later position of a multi-column index; DBX has no engine information here, so such
|
||||
/// a table loses AUTO_INCREMENT rather than every InnoDB table keeping an invalid one.
|
||||
fn mysql_column_leads_kept_index(options: &TableStructureSqlOptions, column: &EditableStructureColumn) -> bool {
|
||||
let Some(original_name) = column.original.as_ref().map(|original| original.name.as_str()) else {
|
||||
return false;
|
||||
};
|
||||
options.indexes.iter().any(|index| {
|
||||
!index.marked_for_drop
|
||||
&& !has_existing_index_change(index)
|
||||
&& index.original.as_ref().is_some_and(|original| {
|
||||
!original.is_primary
|
||||
&& original.columns.first().is_some_and(|first| clean(first).eq_ignore_ascii_case(original_name))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn without_auto_increment(column: &EditableStructureColumn) -> EditableStructureColumn {
|
||||
let mut cleared = column.clone();
|
||||
if let Some(extra) = cleared.extra.as_mut() {
|
||||
extra.auto_increment = Some(false);
|
||||
}
|
||||
cleared
|
||||
}
|
||||
|
||||
pub(super) fn build_primary_key_sql(
|
||||
options: &TableStructureSqlOptions,
|
||||
dialect: StructureDialect,
|
||||
table: &str,
|
||||
warnings: &mut Vec<String>,
|
||||
) -> Vec<String> {
|
||||
let capabilities = capabilities_for(options.database_type, options.driver_profile.as_deref());
|
||||
|
||||
// Membership by draft id (set equality): pure rename / local reorder of the same key
|
||||
// columns is not a PK change. A draft that drops a PK column is also rejected here.
|
||||
let Some(change) = primary_key_change(options) else { return Vec::new() };
|
||||
|
||||
let supported =
|
||||
if change.old_ids.is_empty() { capabilities.add_primary_key } else { capabilities.alter_primary_key };
|
||||
if !supported {
|
||||
warnings.push(unsupported_primary_key_change_warning(options, &change));
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let persisted_postgres_primary_key_name = if options.database_type == Some(DatabaseType::Postgres)
|
||||
&& !change.old_ids.is_empty()
|
||||
{
|
||||
let mut primary_indexes =
|
||||
options.indexes.iter().filter_map(|index| index.original.as_ref().filter(|original| original.is_primary));
|
||||
match (primary_indexes.next(), primary_indexes.next()) {
|
||||
(Some(primary_index), None) if !primary_index.name.is_empty() => Some(primary_index.name.as_str()),
|
||||
_ => {
|
||||
warnings.push(
|
||||
"Could not determine the existing PostgreSQL primary key constraint name. Refresh the table structure and try again."
|
||||
.to_string(),
|
||||
);
|
||||
return Vec::new();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut statements = Vec::new();
|
||||
if !change.old_ids.is_empty() {
|
||||
let Some(drop_sql) = drop_primary_key_statement(dialect, table, options, persisted_postgres_primary_key_name)
|
||||
else {
|
||||
warnings.push(format!(
|
||||
"Changing primary keys is not supported for {} from this editor.",
|
||||
database_label(options.database_type)
|
||||
));
|
||||
return Vec::new();
|
||||
};
|
||||
statements.push(drop_sql);
|
||||
}
|
||||
|
||||
if !change.new_names.is_empty() {
|
||||
let pk_list = change.new_names.iter().map(|name| quote_ident(dialect, name)).collect::<Vec<_>>().join(", ");
|
||||
// DM8: ADD [CONSTRAINT name] PRIMARY KEY; anonymous form matches Navicat/DBeaver/MySQL editors.
|
||||
let constraint = persisted_postgres_primary_key_name
|
||||
.map(|name| format!("CONSTRAINT {} ", quote_ident(dialect, name)))
|
||||
.unwrap_or_default();
|
||||
statements.push(format!("ALTER TABLE {table} ADD {constraint}PRIMARY KEY ({pk_list});"));
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
/// Dialect-specific DROP for an existing primary key.
|
||||
///
|
||||
/// - MySQL: `DROP PRIMARY KEY`
|
||||
/// - Dameng (DM8): official `DROP PRIMARY KEY [RESTRICT|CASCADE]`; default RESTRICT
|
||||
/// (no CASCADE — dependent FKs should not be silently removed).
|
||||
/// System names (`CONS…`) are not stable; name-based DROP is avoided.
|
||||
/// Cluster primary keys cannot use this path (DM8 restriction) — left to the server.
|
||||
/// - Postgres: `DROP CONSTRAINT <persisted primary index name>` for PostgreSQL;
|
||||
/// other Postgres-compatible engines retain the existing default-name behavior.
|
||||
fn drop_primary_key_statement(
|
||||
dialect: StructureDialect,
|
||||
table: &str,
|
||||
options: &TableStructureSqlOptions,
|
||||
persisted_postgres_primary_key_name: Option<&str>,
|
||||
) -> Option<String> {
|
||||
match dialect {
|
||||
StructureDialect::Postgres => {
|
||||
let fallback_name;
|
||||
let pk_name = if let Some(name) = persisted_postgres_primary_key_name {
|
||||
name
|
||||
} else {
|
||||
let raw_table = options.table_name.split('.').next_back().unwrap_or(&options.table_name);
|
||||
fallback_name = format!("{}_pkey", clean(raw_table));
|
||||
&fallback_name
|
||||
};
|
||||
Some(format!("ALTER TABLE {table} DROP CONSTRAINT {};", quote_ident(dialect, pk_name)))
|
||||
}
|
||||
// 神通 Oscar 实测支持 `ALTER TABLE ... DROP PRIMARY KEY`(与 Dameng/MySQL 一致)。
|
||||
StructureDialect::Mysql | StructureDialect::Dameng | StructureDialect::Oscar => {
|
||||
Some(format!("ALTER TABLE {table} DROP PRIMARY KEY;"))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn has_sqlserver_identity(column: &EditableStructureColumn) -> bool {
|
||||
column.extra.as_ref().is_some_and(|extra| extra.auto_increment.unwrap_or(false) || extra.identity.is_some())
|
||||
}
|
||||
|
||||
fn is_sqlserver_identity_compatible_type(data_type: &str) -> bool {
|
||||
let trimmed = data_type.trim();
|
||||
let (base_type, params) = match trimmed.find('(') {
|
||||
Some(open_index) => {
|
||||
let close_index = trimmed.rfind(')').unwrap_or(trimmed.len());
|
||||
(&trimmed[..open_index], trimmed.get(open_index + 1..close_index).unwrap_or(""))
|
||||
}
|
||||
None => (trimmed, ""),
|
||||
};
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
if matches!(normalized.as_str(), "tinyint" | "smallint" | "int" | "integer" | "bigint") {
|
||||
return true;
|
||||
}
|
||||
if !matches!(normalized.as_str(), "decimal" | "numeric") {
|
||||
return false;
|
||||
}
|
||||
let normalized_params = params.split_whitespace().collect::<String>();
|
||||
if normalized_params.is_empty() {
|
||||
return true;
|
||||
}
|
||||
let parts = normalized_params.split(',').collect::<Vec<_>>();
|
||||
match parts.as_slice() {
|
||||
[precision] => precision.parse::<u32>().is_ok(),
|
||||
[precision, scale] => precision.parse::<u32>().is_ok() && *scale == "0",
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn build_add_column_sql(
|
||||
dialect: StructureDialect,
|
||||
database_type: Option<crate::models::connection::DatabaseType>,
|
||||
supports_comments: bool,
|
||||
table: &str,
|
||||
column: &EditableStructureColumn,
|
||||
position_clause: &str,
|
||||
schema: Option<&str>,
|
||||
table_name: &str,
|
||||
driver_profile: Option<&str>,
|
||||
) -> Vec<String> {
|
||||
let definition = column_definition(dialect, column);
|
||||
let mut statements = if is_oracle_like(dialect) || dialect == StructureDialect::Informix {
|
||||
vec![format!("ALTER TABLE {table} ADD ({definition});")]
|
||||
} else {
|
||||
let add_keyword = if dialect == StructureDialect::SqlServer
|
||||
|| database_type == Some(crate::models::connection::DatabaseType::Kingbase)
|
||||
{
|
||||
"ADD"
|
||||
} else {
|
||||
"ADD COLUMN"
|
||||
};
|
||||
vec![format!("ALTER TABLE {table} {add_keyword} {definition}{position_clause};")]
|
||||
};
|
||||
if supports_comments
|
||||
&& matches!(
|
||||
dialect,
|
||||
StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::Oscar
|
||||
)
|
||||
&& !clean(&column.comment).is_empty()
|
||||
{
|
||||
statements.push(format!(
|
||||
"COMMENT ON COLUMN {table}.{} IS {};",
|
||||
quote_ident(dialect, &column.name),
|
||||
quote_string(&clean(&column.comment))
|
||||
));
|
||||
}
|
||||
if dialect == StructureDialect::ClickHouse && !clean(&column.comment).is_empty() {
|
||||
statements.push(format!(
|
||||
"ALTER TABLE {table} COMMENT COLUMN {} {};",
|
||||
quote_ident(dialect, &column.name),
|
||||
quote_string(&clean(&column.comment))
|
||||
));
|
||||
}
|
||||
if dialect == StructureDialect::SqlServer && !clean(&column.comment).is_empty() {
|
||||
statements.extend(build_sqlserver_column_comment_sql_for_profile(
|
||||
table,
|
||||
schema,
|
||||
table_name,
|
||||
&column.name,
|
||||
&column.comment,
|
||||
driver_profile,
|
||||
));
|
||||
}
|
||||
statements
|
||||
}
|
||||
|
||||
pub(super) fn build_drop_column_sql(dialect: StructureDialect, table: &str, column_name: &str) -> String {
|
||||
if dialect == StructureDialect::Informix {
|
||||
return format!("ALTER TABLE {table} DROP ({});", quote_ident(dialect, column_name));
|
||||
}
|
||||
format!("ALTER TABLE {table} DROP COLUMN {};", quote_ident(dialect, column_name))
|
||||
}
|
||||
|
||||
pub(super) fn column_position_clause(
|
||||
dialect: StructureDialect,
|
||||
columns: &[&EditableStructureColumn],
|
||||
index: usize,
|
||||
) -> String {
|
||||
if !matches!(dialect, StructureDialect::Mysql | StructureDialect::ClickHouse) {
|
||||
return String::new();
|
||||
}
|
||||
if index == 0 {
|
||||
return " FIRST".to_string();
|
||||
}
|
||||
format!(" AFTER {}", quote_ident(dialect, columns.get(index - 1).map(|column| column.name.as_str()).unwrap_or("")))
|
||||
}
|
||||
|
||||
pub(super) fn original_active_column_order(columns: &[&EditableStructureColumn]) -> Vec<String> {
|
||||
let mut original_columns: Vec<_> = columns
|
||||
.iter()
|
||||
.filter(|column| column.original.is_some() && column.original_position.is_some())
|
||||
.copied()
|
||||
.collect();
|
||||
original_columns.sort_by_key(|column| column.original_position.unwrap_or(0));
|
||||
original_columns.into_iter().map(|column| column.id.clone()).collect()
|
||||
}
|
||||
|
||||
/// Returns the ids of existing columns that must be explicitly moved to reach the target order.
|
||||
///
|
||||
/// The function keeps the longest subsequence of existing columns whose relative order is already
|
||||
/// correct, and marks only the remaining columns for FIRST/AFTER reordering SQL.
|
||||
pub(super) fn planned_existing_column_move_ids(columns: &[&EditableStructureColumn]) -> HashSet<String> {
|
||||
// Only existing columns with an original position participate in move planning.
|
||||
// Newly added columns are positioned directly from the target order.
|
||||
let reorderable_columns: Vec<_> = columns
|
||||
.iter()
|
||||
.filter_map(|column| {
|
||||
column
|
||||
.original
|
||||
.as_ref()
|
||||
.zip(column.original_position)
|
||||
.map(|_| (column.id.as_str(), column.original_position.unwrap_or(0)))
|
||||
})
|
||||
.collect();
|
||||
if reorderable_columns.len() < 2 {
|
||||
return HashSet::new();
|
||||
}
|
||||
|
||||
// Map the target order back to original positions, then keep the largest increasing subsequence.
|
||||
let original_positions: Vec<_> = reorderable_columns.iter().map(|(_, position)| *position).collect();
|
||||
// Columns inside the LIS can stay where they are; everything else needs an explicit move.
|
||||
let kept_indices: HashSet<_> = longest_increasing_subsequence_indices(&original_positions).into_iter().collect();
|
||||
|
||||
reorderable_columns
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter(|(index, _)| !kept_indices.contains(index))
|
||||
.map(|(_, (column_id, _))| column_id.to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Returns the indices of one longest increasing subsequence within `values`.
|
||||
///
|
||||
/// In the reorder planner, an increasing subsequence represents existing columns whose relative
|
||||
/// order still matches the original table layout, so they can remain untouched.
|
||||
fn longest_increasing_subsequence_indices(values: &[usize]) -> Vec<usize> {
|
||||
if values.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
// O(n^2) is sufficient here because table editors deal with relatively small column counts
|
||||
// and the simpler implementation is easier to maintain.
|
||||
let mut lengths = vec![1; values.len()];
|
||||
let mut previous = vec![None; values.len()];
|
||||
let mut best_end_index = 0;
|
||||
|
||||
for current_index in 0..values.len() {
|
||||
for previous_index in 0..current_index {
|
||||
if values[previous_index] < values[current_index] && lengths[previous_index] + 1 > lengths[current_index] {
|
||||
lengths[current_index] = lengths[previous_index] + 1;
|
||||
previous[current_index] = Some(previous_index);
|
||||
}
|
||||
}
|
||||
|
||||
if lengths[current_index] > lengths[best_end_index] {
|
||||
best_end_index = current_index;
|
||||
}
|
||||
}
|
||||
|
||||
// Reconstruct the subsequence by following the predecessor chain backwards.
|
||||
let mut indices = Vec::new();
|
||||
let mut cursor = Some(best_end_index);
|
||||
while let Some(index) = cursor {
|
||||
indices.push(index);
|
||||
cursor = previous[index];
|
||||
}
|
||||
indices.reverse();
|
||||
indices
|
||||
}
|
||||
|
||||
pub(super) fn active_previous_column_id<'a>(columns: &[&'a EditableStructureColumn], index: usize) -> Option<&'a str> {
|
||||
if index == 0 {
|
||||
None
|
||||
} else {
|
||||
columns.get(index - 1).map(|column| column.id.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn simulated_column_position_changed(
|
||||
simulated_column_order: &[String],
|
||||
column_id: &str,
|
||||
desired_previous_column_id: Option<&str>,
|
||||
) -> bool {
|
||||
let Some(index) = simulated_column_order.iter().position(|id| id == column_id) else {
|
||||
return false;
|
||||
};
|
||||
let current_previous_column_id = if index == 0 { None } else { Some(simulated_column_order[index - 1].as_str()) };
|
||||
current_previous_column_id != desired_previous_column_id
|
||||
}
|
||||
|
||||
pub(super) fn apply_simulated_column_position(
|
||||
simulated_column_order: &mut Vec<String>,
|
||||
column_id: &str,
|
||||
desired_previous_column_id: Option<&str>,
|
||||
) {
|
||||
if let Some(index) = simulated_column_order.iter().position(|id| id == column_id) {
|
||||
simulated_column_order.remove(index);
|
||||
}
|
||||
let index = desired_previous_column_id
|
||||
.and_then(|previous_id| simulated_column_order.iter().position(|id| id == previous_id).map(|index| index + 1))
|
||||
.unwrap_or(0);
|
||||
simulated_column_order.insert(index, column_id.to_string());
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
use super::dialect::{capabilities_for, database_label, dialect_label, StructureDialect};
|
||||
use super::types::TableStructureSqlOptions;
|
||||
use super::util::{clean, qualified_table, quote_string};
|
||||
|
||||
pub(super) fn build_table_comment_sql(options: &TableStructureSqlOptions, warnings: &mut Vec<String>) -> Vec<String> {
|
||||
let capabilities = capabilities_for(options.database_type, options.driver_profile.as_deref());
|
||||
let new_comment = options.table_comment.as_deref().unwrap_or("");
|
||||
let original_comment = options.original_table_comment.as_deref().unwrap_or("");
|
||||
if clean(new_comment) == clean(original_comment) {
|
||||
return Vec::new();
|
||||
}
|
||||
if !capabilities.comment {
|
||||
warnings.push(format!(
|
||||
"Table comments are not supported for {} from this editor; the comment change was ignored.",
|
||||
database_label(options.database_type)
|
||||
));
|
||||
return Vec::new();
|
||||
}
|
||||
let dialect = capabilities.dialect;
|
||||
let table = qualified_table(dialect, options.schema.as_deref(), &options.table_name);
|
||||
let quoted = quote_string(&clean(new_comment));
|
||||
match dialect {
|
||||
StructureDialect::Mysql | StructureDialect::GaussdbM => {
|
||||
vec![format!("ALTER TABLE {table} COMMENT = {quoted};")]
|
||||
}
|
||||
StructureDialect::Postgres
|
||||
| StructureDialect::Oracle
|
||||
| StructureDialect::Dameng
|
||||
| StructureDialect::Oscar
|
||||
| StructureDialect::H2 => {
|
||||
vec![format!("COMMENT ON TABLE {table} IS {quoted};")]
|
||||
}
|
||||
StructureDialect::ClickHouse => {
|
||||
vec![format!("ALTER TABLE {table} MODIFY COMMENT {quoted};")]
|
||||
}
|
||||
StructureDialect::SqlServer => build_sqlserver_table_comment_sql_for_profile(
|
||||
&table,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
new_comment,
|
||||
options.driver_profile.as_deref(),
|
||||
),
|
||||
_ => {
|
||||
if !clean(new_comment).is_empty() {
|
||||
warnings
|
||||
.push(format!("Table comments are not supported for {} from this editor.", dialect_label(dialect)));
|
||||
}
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn sqlserver_schema_name(schema: Option<&str>) -> String {
|
||||
schema.filter(|s| !s.trim().is_empty()).map(|s| s.trim().to_string()).unwrap_or_else(|| "dbo".to_string())
|
||||
}
|
||||
|
||||
fn build_sqlserver_extended_property_comment_sql(
|
||||
exists: &str,
|
||||
levels: &str,
|
||||
new_comment: &str,
|
||||
procedure_prefix: &str,
|
||||
) -> Vec<String> {
|
||||
let new_comment = clean(new_comment);
|
||||
if new_comment.is_empty() {
|
||||
return vec![format!(
|
||||
"IF {exists} EXEC {procedure_prefix}sp_dropextendedproperty @name=N'MS_Description', {levels};"
|
||||
)];
|
||||
}
|
||||
|
||||
let escaped_comment = new_comment.replace('\'', "''");
|
||||
vec![format!(
|
||||
"IF {exists} EXEC {procedure_prefix}sp_updateextendedproperty @name=N'MS_Description', @value=N'{escaped_comment}', {levels} ELSE EXEC {procedure_prefix}sp_addextendedproperty @name=N'MS_Description', @value=N'{escaped_comment}', {levels};"
|
||||
)]
|
||||
}
|
||||
|
||||
pub fn build_sqlserver_table_comment_sql(
|
||||
qualified_table: &str,
|
||||
schema: Option<&str>,
|
||||
table_name: &str,
|
||||
new_comment: &str,
|
||||
) -> Vec<String> {
|
||||
build_sqlserver_table_comment_sql_for_profile(qualified_table, schema, table_name, new_comment, None)
|
||||
}
|
||||
|
||||
pub fn build_sqlserver_table_comment_sql_for_profile(
|
||||
qualified_table: &str,
|
||||
schema: Option<&str>,
|
||||
table_name: &str,
|
||||
new_comment: &str,
|
||||
driver_profile: Option<&str>,
|
||||
) -> Vec<String> {
|
||||
let schema_name = sqlserver_schema_name(schema);
|
||||
let escaped_qualified = qualified_table.replace('\'', "''");
|
||||
let escaped_schema = schema_name.replace('\'', "''");
|
||||
let escaped_table = table_name.replace('\'', "''");
|
||||
let (exists, levels, procedure_prefix) = if is_sqlserver_legacy_profile(driver_profile) {
|
||||
(
|
||||
format!(
|
||||
"EXISTS (SELECT 1 FROM ::fn_listextendedproperty(N'MS_Description', N'USER', N'{escaped_schema}', N'TABLE', N'{escaped_table}', NULL, NULL))"
|
||||
),
|
||||
format!(
|
||||
"@level0type=N'USER', @level0name=N'{escaped_schema}', @level1type=N'TABLE', @level1name=N'{escaped_table}'"
|
||||
),
|
||||
"",
|
||||
)
|
||||
} else {
|
||||
(
|
||||
format!(
|
||||
"EXISTS (SELECT 1 FROM sys.extended_properties AS ep WHERE ep.class = 1 AND ep.major_id = OBJECT_ID(N'{escaped_qualified}') AND ep.minor_id = 0 AND ep.name = N'MS_Description')"
|
||||
),
|
||||
format!(
|
||||
"@level0type=N'SCHEMA', @level0name=N'{escaped_schema}', @level1type=N'TABLE', @level1name=N'{escaped_table}'"
|
||||
),
|
||||
"sys.",
|
||||
)
|
||||
};
|
||||
|
||||
build_sqlserver_extended_property_comment_sql(&exists, &levels, new_comment, procedure_prefix)
|
||||
}
|
||||
|
||||
pub(super) fn build_sqlserver_index_comment_sql_for_profile(
|
||||
qualified_table: &str,
|
||||
schema: Option<&str>,
|
||||
table_name: &str,
|
||||
index_name: &str,
|
||||
new_comment: &str,
|
||||
driver_profile: Option<&str>,
|
||||
) -> Vec<String> {
|
||||
let schema_name = sqlserver_schema_name(schema);
|
||||
let escaped_qualified = qualified_table.replace('\'', "''");
|
||||
let escaped_schema = schema_name.replace('\'', "''");
|
||||
let escaped_table = table_name.replace('\'', "''");
|
||||
let escaped_idx = index_name.replace('\'', "''");
|
||||
let (exists, levels, procedure_prefix) = if is_sqlserver_legacy_profile(driver_profile) {
|
||||
(
|
||||
format!(
|
||||
"EXISTS (SELECT 1 FROM ::fn_listextendedproperty(N'MS_Description', N'USER', N'{escaped_schema}', N'TABLE', N'{escaped_table}', N'INDEX', N'{escaped_idx}'))"
|
||||
),
|
||||
format!(
|
||||
"@level0type=N'USER', @level0name=N'{escaped_schema}', @level1type=N'TABLE', @level1name=N'{escaped_table}', @level2type=N'INDEX', @level2name=N'{escaped_idx}'"
|
||||
),
|
||||
"",
|
||||
)
|
||||
} else {
|
||||
(
|
||||
format!(
|
||||
"EXISTS (SELECT 1 FROM sys.extended_properties AS ep INNER JOIN sys.indexes AS i ON i.object_id = ep.major_id AND i.index_id = ep.minor_id WHERE ep.class = 7 AND ep.major_id = OBJECT_ID(N'{escaped_qualified}') AND i.name = N'{escaped_idx}' AND ep.name = N'MS_Description')"
|
||||
),
|
||||
format!(
|
||||
"@level0type=N'SCHEMA', @level0name=N'{escaped_schema}', @level1type=N'TABLE', @level1name=N'{escaped_table}', @level2type=N'INDEX', @level2name=N'{escaped_idx}'"
|
||||
),
|
||||
"sys.",
|
||||
)
|
||||
};
|
||||
|
||||
build_sqlserver_extended_property_comment_sql(&exists, &levels, new_comment, procedure_prefix)
|
||||
}
|
||||
|
||||
pub fn build_sqlserver_column_comment_sql(
|
||||
qualified_table: &str,
|
||||
schema: Option<&str>,
|
||||
table_name: &str,
|
||||
column_name: &str,
|
||||
new_comment: &str,
|
||||
) -> Vec<String> {
|
||||
build_sqlserver_column_comment_sql_for_profile(qualified_table, schema, table_name, column_name, new_comment, None)
|
||||
}
|
||||
|
||||
pub fn build_sqlserver_column_comment_sql_for_profile(
|
||||
qualified_table: &str,
|
||||
schema: Option<&str>,
|
||||
table_name: &str,
|
||||
column_name: &str,
|
||||
new_comment: &str,
|
||||
driver_profile: Option<&str>,
|
||||
) -> Vec<String> {
|
||||
let schema_name = sqlserver_schema_name(schema);
|
||||
let escaped_qualified = qualified_table.replace('\'', "''");
|
||||
let escaped_schema = schema_name.replace('\'', "''");
|
||||
let escaped_table = table_name.replace('\'', "''");
|
||||
let escaped_col = column_name.replace('\'', "''");
|
||||
let (exists, levels, procedure_prefix) = if is_sqlserver_legacy_profile(driver_profile) {
|
||||
(
|
||||
format!(
|
||||
"EXISTS (SELECT 1 FROM ::fn_listextendedproperty(N'MS_Description', N'USER', N'{escaped_schema}', N'TABLE', N'{escaped_table}', N'COLUMN', N'{escaped_col}'))"
|
||||
),
|
||||
format!(
|
||||
"@level0type=N'USER', @level0name=N'{escaped_schema}', @level1type=N'TABLE', @level1name=N'{escaped_table}', @level2type=N'COLUMN', @level2name=N'{escaped_col}'"
|
||||
),
|
||||
"",
|
||||
)
|
||||
} else {
|
||||
(
|
||||
format!(
|
||||
"EXISTS (SELECT 1 FROM sys.extended_properties AS ep WHERE ep.class = 1 AND ep.major_id = OBJECT_ID(N'{escaped_qualified}') AND ep.minor_id = COLUMNPROPERTY(OBJECT_ID(N'{escaped_qualified}'), N'{escaped_col}', 'ColumnId') AND ep.name = N'MS_Description')"
|
||||
),
|
||||
format!(
|
||||
"@level0type=N'SCHEMA', @level0name=N'{escaped_schema}', @level1type=N'TABLE', @level1name=N'{escaped_table}', @level2type=N'COLUMN', @level2name=N'{escaped_col}'"
|
||||
),
|
||||
"sys.",
|
||||
)
|
||||
};
|
||||
|
||||
build_sqlserver_extended_property_comment_sql(&exists, &levels, new_comment, procedure_prefix)
|
||||
}
|
||||
|
||||
fn is_sqlserver_legacy_profile(driver_profile: Option<&str>) -> bool {
|
||||
driver_profile.is_some_and(|profile| profile.trim().eq_ignore_ascii_case("sqlserver-legacy"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sqlserver_table_comment_updates_or_adds_without_dropping() {
|
||||
let statements = build_sqlserver_table_comment_sql("[app's].[user's]", Some("app's"), "user's", "owner's 新值");
|
||||
|
||||
assert_eq!(statements.len(), 1);
|
||||
let sql = &statements[0];
|
||||
assert!(sql.contains("ep.class = 1"), "table extended-property class: {sql}");
|
||||
assert!(sql.contains("sys.sp_updateextendedproperty"), "update existing comment: {sql}");
|
||||
assert!(sql.contains("ELSE EXEC sys.sp_addextendedproperty"), "add missing comment: {sql}");
|
||||
assert!(!sql.contains("sys.sp_dropextendedproperty"), "non-empty comments are not dropped first: {sql}");
|
||||
assert!(sql.contains("OBJECT_ID(N'[app''s].[user''s]')"), "object name escaping: {sql}");
|
||||
assert!(sql.contains("@value=N'owner''s 新值'"), "Unicode comment escaping: {sql}");
|
||||
assert_eq!(
|
||||
crate::sql::split_sql_statements_for_database(sql, crate::models::connection::DatabaseType::SqlServer)
|
||||
.len(),
|
||||
1,
|
||||
"IF/ELSE comment upsert must remain one executable statement: {sql}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_empty_column_comment_drops_only_when_present() {
|
||||
let statements = build_sqlserver_column_comment_sql("[dbo].[orders]", None, "orders", "owner'id", " ");
|
||||
|
||||
assert_eq!(statements.len(), 1);
|
||||
let sql = &statements[0];
|
||||
assert!(sql.contains("COLUMNPROPERTY(OBJECT_ID(N'[dbo].[orders]'), N'owner''id', 'ColumnId')"));
|
||||
assert!(sql.contains("sys.sp_dropextendedproperty"), "drop existing comment: {sql}");
|
||||
assert!(!sql.contains("sys.sp_updateextendedproperty"), "empty comment must not update: {sql}");
|
||||
assert!(!sql.contains("sys.sp_addextendedproperty"), "empty comment must not add: {sql}");
|
||||
assert!(sql.contains("@level0name=N'dbo'"), "default schema: {sql}");
|
||||
assert_eq!(
|
||||
crate::sql::split_sql_statements_for_database(sql, crate::models::connection::DatabaseType::SqlServer)
|
||||
.len(),
|
||||
1,
|
||||
"conditional comment drop must remain one executable statement: {sql}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_index_comment_uses_index_extended_property_identity() {
|
||||
let statements = build_sqlserver_index_comment_sql_for_profile(
|
||||
"[dbo].[orders]",
|
||||
None,
|
||||
"orders",
|
||||
"ix_owner's",
|
||||
"index comment",
|
||||
None,
|
||||
);
|
||||
|
||||
assert_eq!(statements.len(), 1);
|
||||
let sql = &statements[0];
|
||||
assert!(sql.contains("INNER JOIN sys.indexes AS i"), "index lookup: {sql}");
|
||||
assert!(sql.contains("ep.class = 7"), "index extended-property class: {sql}");
|
||||
assert!(sql.contains("i.name = N'ix_owner''s'"), "index name escaping: {sql}");
|
||||
assert!(sql.contains("sys.sp_updateextendedproperty"), "update existing comment: {sql}");
|
||||
assert!(sql.contains("ELSE EXEC sys.sp_addextendedproperty"), "add missing comment: {sql}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_legacy_column_comment_uses_sql_server_2000_compatibility_syntax() {
|
||||
let statements = build_sqlserver_column_comment_sql_for_profile(
|
||||
"[dbo].[Categories]",
|
||||
Some("dbo"),
|
||||
"Categories",
|
||||
"CategoryID",
|
||||
"test",
|
||||
Some("sqlserver-legacy"),
|
||||
);
|
||||
|
||||
assert_eq!(statements.len(), 1);
|
||||
let sql = &statements[0];
|
||||
assert!(sql.contains("::fn_listextendedproperty"), "legacy property lookup: {sql}");
|
||||
assert!(sql.contains("@level0type=N'USER'"), "legacy hierarchy: {sql}");
|
||||
assert!(sql.contains("EXEC sp_updateextendedproperty"), "legacy update procedure: {sql}");
|
||||
assert!(sql.contains("ELSE EXEC sp_addextendedproperty"), "legacy add procedure: {sql}");
|
||||
assert!(
|
||||
!sql.contains("sys.extended_properties"),
|
||||
"SQL Server 2005+ catalog view leaked into legacy SQL: {sql}"
|
||||
);
|
||||
assert!(!sql.contains("EXEC sys."), "SQL Server 2005+ procedure qualification leaked into legacy SQL: {sql}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
use super::column_format::{
|
||||
column_data_type, column_extra_clause, has_dameng_identity, is_dameng_identity_compatible_type,
|
||||
is_mysql_character_data_type, is_mysql_timestamp_type, mysql_on_update_current_timestamp_clause,
|
||||
strip_inherited_mysql_column_charsets,
|
||||
};
|
||||
use super::comments::{build_sqlserver_column_comment_sql_for_profile, build_sqlserver_table_comment_sql_for_profile};
|
||||
use super::dialect::{capabilities_for, database_label, StructureDialect};
|
||||
use super::foreign_keys::build_foreign_key_sql_for_new_table;
|
||||
use super::indexes::build_create_index_statements;
|
||||
use super::mysql_engine::{append_mysql_table_option, validate_mysql_engine};
|
||||
use super::triggers::build_trigger_sql_for_new_table;
|
||||
use super::types::{TableStructureSqlOptions, TableStructureSqlResult};
|
||||
use super::util::{
|
||||
clean, format_default_for_sql, normalize_default, qualified_new_table, quote_ident, quote_new_ident, quote_string,
|
||||
};
|
||||
use super::validation::{validate_columns, validate_concurrent_index_scope, validate_dameng_identity};
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
fn is_sqlite_integer_family_type(data_type: &str) -> bool {
|
||||
let normalized = data_type.trim().to_ascii_lowercase();
|
||||
["int", "integer", "tinyint", "smallint", "mediumint", "bigint"]
|
||||
.iter()
|
||||
.any(|candidate| normalized == *candidate || normalized.starts_with(&format!("{candidate}(")))
|
||||
}
|
||||
|
||||
pub fn build_create_table_sql(options: TableStructureSqlOptions) -> TableStructureSqlResult {
|
||||
build_create_table_sql_with_partition_clause(options, None)
|
||||
}
|
||||
|
||||
/// Shared implementation for plain and partitioned `CREATE TABLE`.
|
||||
///
|
||||
/// `partition_clause` is a ready-made `PARTITION BY ...` body (no leading
|
||||
/// keyword), appended to the `CREATE TABLE` statement. Keeping it as a separate
|
||||
/// entry point means `build_create_table_sql`'s signature — used from ~160 call
|
||||
/// sites and integration tests — stays unchanged.
|
||||
pub(super) fn build_create_table_sql_with_partition_clause(
|
||||
mut options: TableStructureSqlOptions,
|
||||
partition_clause: Option<String>,
|
||||
) -> TableStructureSqlResult {
|
||||
let capabilities;
|
||||
let dialect;
|
||||
if options.is_gaussdb_m_mode {
|
||||
capabilities = super::dialect::gaussdb_m_capabilities();
|
||||
dialect = StructureDialect::GaussdbM;
|
||||
} else {
|
||||
capabilities = capabilities_for(options.database_type, options.driver_profile.as_deref());
|
||||
dialect = capabilities.dialect;
|
||||
}
|
||||
options.table_name = clean(&options.table_name);
|
||||
strip_inherited_mysql_column_charsets(&mut options);
|
||||
let mut warnings = Vec::new();
|
||||
warnings.extend(validate_mysql_engine(&options));
|
||||
// Fail closed: a concurrent-index request on a partitioned parent (or on an
|
||||
// existing index in a hand-built draft) is refused up front instead of
|
||||
// degrading into blocking index DDL.
|
||||
warnings.extend(validate_concurrent_index_scope(&options));
|
||||
if options.table_name.is_empty() {
|
||||
warnings.push("Table name is required.".to_string());
|
||||
}
|
||||
let active_columns: Vec<_> = options.columns.iter().filter(|column| !column.marked_for_drop).collect();
|
||||
if active_columns.is_empty() {
|
||||
warnings.push("At least one column is required.".to_string());
|
||||
}
|
||||
validate_columns(&active_columns, &mut warnings);
|
||||
validate_dameng_identity(&options, &active_columns, &mut warnings);
|
||||
if !warnings.is_empty() {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings };
|
||||
}
|
||||
let table = qualified_new_table(options.database_type, dialect, options.schema.as_deref(), &options.table_name);
|
||||
if dialect == StructureDialect::Dameng {
|
||||
for column in &active_columns {
|
||||
if has_dameng_identity(column) && !is_dameng_identity_compatible_type(&column.data_type) {
|
||||
warnings.push(format!(
|
||||
"Dameng identity column \"{}\" must use tinyint, smallint, int, integer, bigint, number, numeric, or decimal/dec with scale 0.",
|
||||
column.name
|
||||
));
|
||||
}
|
||||
}
|
||||
if !warnings.is_empty() {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings };
|
||||
}
|
||||
}
|
||||
if dialect == StructureDialect::Sqlite {
|
||||
let primary_key_count = active_columns.iter().filter(|column| column.is_primary_key).count();
|
||||
for column in &active_columns {
|
||||
if !column.is_primary_key || !column.extra.as_ref().is_some_and(|e| e.auto_increment.unwrap_or(false)) {
|
||||
continue;
|
||||
}
|
||||
if primary_key_count != 1 {
|
||||
warnings.push(
|
||||
"SQLite AUTOINCREMENT requires a single INTEGER PRIMARY KEY column; disable auto-increment on composite primary keys.".to_string(),
|
||||
);
|
||||
} else if !is_sqlite_integer_family_type(&column.data_type) {
|
||||
warnings.push(format!(
|
||||
"SQLite auto-increment column \"{}\" must use an integer type (normalized to INTEGER).",
|
||||
column.name
|
||||
));
|
||||
}
|
||||
}
|
||||
if !warnings.is_empty() {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings };
|
||||
}
|
||||
}
|
||||
let mut statements = Vec::new();
|
||||
let mut column_definitions = Vec::new();
|
||||
|
||||
for column in &active_columns {
|
||||
let mut data_type = column_data_type(dialect, column);
|
||||
// SQLite accepts AUTOINCREMENT only on an exact INTEGER PRIMARY KEY,
|
||||
// so integer-family aliases are normalized when auto-increment is on.
|
||||
if dialect == StructureDialect::Sqlite
|
||||
&& column.is_primary_key
|
||||
&& column.extra.as_ref().is_some_and(|e| e.auto_increment.unwrap_or(false))
|
||||
{
|
||||
data_type = "INTEGER".to_string();
|
||||
}
|
||||
let mut parts = vec![quote_new_ident(options.database_type, dialect, &column.name), data_type];
|
||||
if options.database_type == Some(DatabaseType::Mysql) && is_mysql_character_data_type(&column.data_type) {
|
||||
if !column.character_set.trim().is_empty() {
|
||||
parts.push(format!("CHARACTER SET {}", quote_ident(dialect, &column.character_set)));
|
||||
}
|
||||
if !column.collation.trim().is_empty() {
|
||||
parts.push(format!("COLLATE {}", quote_ident(dialect, &column.collation)));
|
||||
}
|
||||
}
|
||||
// Oracle's column grammar is `col type [DEFAULT expr] [inline constraint ...]`, so the
|
||||
// DEFAULT clause has to precede NOT NULL. Emitting `NOT NULL DEFAULT ...` leaves the
|
||||
// parser looking for the closing parenthesis of the column list and fails with
|
||||
// ORA-00907 (t8y2/dbx#9477). Scoped to Oracle: every other engine keeps the historical
|
||||
// NOT NULL-then-DEFAULT order.
|
||||
let default_value = normalize_default(Some(&column.default_value));
|
||||
let default_clause = (!default_value.is_empty() && dialect != StructureDialect::ManticoreSearch)
|
||||
.then(|| format!("DEFAULT {}", format_default_for_sql(dialect, &column.data_type, &default_value)));
|
||||
if dialect == StructureDialect::Oracle {
|
||||
if let Some(clause) = default_clause.clone() {
|
||||
parts.push(clause);
|
||||
}
|
||||
}
|
||||
if dialect == StructureDialect::Sqlite
|
||||
&& column.is_primary_key
|
||||
&& column.extra.as_ref().is_some_and(|e| e.auto_increment.unwrap_or(false))
|
||||
{
|
||||
parts.push("PRIMARY KEY".to_string());
|
||||
} else if !column.is_nullable
|
||||
&& !column.is_primary_key
|
||||
&& !matches!(dialect, StructureDialect::ClickHouse | StructureDialect::ManticoreSearch)
|
||||
{
|
||||
parts.push("NOT NULL".to_string());
|
||||
} else if column.is_nullable
|
||||
&& !column.is_primary_key
|
||||
&& dialect == StructureDialect::Mysql
|
||||
&& is_mysql_timestamp_type(&column.data_type)
|
||||
{
|
||||
parts.push("NULL".to_string());
|
||||
}
|
||||
if let Some(extra_clause) = column_extra_clause(dialect, column) {
|
||||
parts.push(extra_clause);
|
||||
}
|
||||
if dialect != StructureDialect::Oracle {
|
||||
if let Some(clause) = default_clause {
|
||||
parts.push(clause);
|
||||
}
|
||||
}
|
||||
if let Some(on_update) = column.extra.as_ref().and_then(|e| e.on_update_current_timestamp).filter(|v| *v) {
|
||||
if on_update && dialect == StructureDialect::Mysql {
|
||||
parts.push(mysql_on_update_current_timestamp_clause(&column.data_type));
|
||||
}
|
||||
}
|
||||
if dialect == StructureDialect::Mysql && capabilities.comment && !clean(&column.comment).is_empty() {
|
||||
parts.push(format!("COMMENT {}", quote_string(&clean(&column.comment))));
|
||||
}
|
||||
column_definitions.push(parts.join(" "));
|
||||
}
|
||||
|
||||
let pk_columns: Vec<_> = active_columns
|
||||
.iter()
|
||||
.filter(|column| {
|
||||
column.is_primary_key
|
||||
&& dialect != StructureDialect::ManticoreSearch
|
||||
&& !(dialect == StructureDialect::Sqlite
|
||||
&& column.extra.as_ref().is_some_and(|e| e.auto_increment.unwrap_or(false)))
|
||||
})
|
||||
.collect();
|
||||
if !pk_columns.is_empty() {
|
||||
let pk_list = pk_columns
|
||||
.iter()
|
||||
.map(|column| quote_new_ident(options.database_type, dialect, &column.name))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
column_definitions.push(format!("PRIMARY KEY ({pk_list})"));
|
||||
}
|
||||
|
||||
let create_table = format!("CREATE TABLE {table} (\n {}\n)", column_definitions.join(",\n "));
|
||||
let create_table = match partition_clause {
|
||||
Some(clause) => format!("{create_table} {clause}"),
|
||||
None => create_table,
|
||||
};
|
||||
statements.push(format!("{create_table};"));
|
||||
|
||||
if let Some(engine) = options.mysql_engine.as_deref().map(str::trim).filter(|engine| !engine.is_empty()) {
|
||||
if let Some(statement) = statements.last_mut() {
|
||||
append_mysql_table_option(statement, &format!("ENGINE = {engine}"));
|
||||
}
|
||||
}
|
||||
|
||||
if capabilities.comment {
|
||||
let table_comment = clean(options.table_comment.as_deref().unwrap_or(""));
|
||||
if !table_comment.is_empty() {
|
||||
if matches!(dialect, StructureDialect::Mysql | StructureDialect::GaussdbM) {
|
||||
if let Some(last) = statements.last_mut() {
|
||||
append_mysql_table_option(last, &format!("COMMENT = {}", quote_string(&table_comment)));
|
||||
}
|
||||
} else if matches!(
|
||||
dialect,
|
||||
StructureDialect::Postgres
|
||||
| StructureDialect::Oracle
|
||||
| StructureDialect::Dameng
|
||||
| StructureDialect::Oscar
|
||||
| StructureDialect::H2
|
||||
) {
|
||||
statements.push(format!("COMMENT ON TABLE {table} IS {};", quote_string(&table_comment)));
|
||||
} else if dialect == StructureDialect::ClickHouse {
|
||||
statements.push(format!("ALTER TABLE {table} MODIFY COMMENT {};", quote_string(&table_comment)));
|
||||
} else if dialect == StructureDialect::SqlServer {
|
||||
statements.extend(build_sqlserver_table_comment_sql_for_profile(
|
||||
&table,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
&table_comment,
|
||||
options.driver_profile.as_deref(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if capabilities.comment
|
||||
&& matches!(
|
||||
dialect,
|
||||
StructureDialect::Postgres
|
||||
| StructureDialect::Oracle
|
||||
| StructureDialect::Dameng
|
||||
| StructureDialect::Oscar
|
||||
| StructureDialect::H2
|
||||
)
|
||||
{
|
||||
for column in &active_columns {
|
||||
if !clean(&column.comment).is_empty() {
|
||||
statements.push(format!(
|
||||
"COMMENT ON COLUMN {table}.{} IS {};",
|
||||
quote_new_ident(options.database_type, dialect, &column.name),
|
||||
quote_string(&clean(&column.comment))
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
if capabilities.comment && dialect == StructureDialect::ClickHouse {
|
||||
for column in &active_columns {
|
||||
if !clean(&column.comment).is_empty() {
|
||||
statements.push(format!(
|
||||
"ALTER TABLE {table} COMMENT COLUMN {} {};",
|
||||
quote_new_ident(options.database_type, dialect, &column.name),
|
||||
quote_string(&clean(&column.comment))
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
if capabilities.comment && dialect == StructureDialect::SqlServer {
|
||||
for column in &active_columns {
|
||||
if !clean(&column.comment).is_empty() {
|
||||
statements.extend(build_sqlserver_column_comment_sql_for_profile(
|
||||
&table,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
&column.name,
|
||||
&column.comment,
|
||||
options.driver_profile.as_deref(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for index in options.indexes.iter().filter(|index| !index.marked_for_drop && !index.is_primary) {
|
||||
if !capabilities.create_index {
|
||||
warnings.push(format!(
|
||||
"Creating indexes is not supported for {} from this editor.",
|
||||
database_label(options.database_type)
|
||||
));
|
||||
continue;
|
||||
}
|
||||
statements.extend(build_create_index_statements(
|
||||
options.database_type,
|
||||
dialect,
|
||||
&table,
|
||||
index,
|
||||
&mut warnings,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
false,
|
||||
capabilities.index_concurrent,
|
||||
true,
|
||||
options.driver_profile.as_deref(),
|
||||
));
|
||||
}
|
||||
|
||||
statements.extend(build_foreign_key_sql_for_new_table(&options, &mut warnings));
|
||||
statements.extend(build_trigger_sql_for_new_table(&options, &mut warnings));
|
||||
|
||||
TableStructureSqlResult { statements, warnings }
|
||||
}
|
||||
@@ -0,0 +1,444 @@
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum StructureDialect {
|
||||
Mysql,
|
||||
Doris,
|
||||
Postgres,
|
||||
Sqlite,
|
||||
#[cfg(feature = "duckdb-sidecar")]
|
||||
DuckDb,
|
||||
SqlServer,
|
||||
Oracle,
|
||||
Dameng,
|
||||
// 神通 Oscar:Oracle 兼容方言,且支持 ALTER TABLE DROP/ADD PRIMARY KEY(与 Dameng 一致,
|
||||
// 不同于 Oracle)。DDL 生成行为与 Dameng 共享分支;单独成 dialect 以保证 label/反向映射准确。
|
||||
Oscar,
|
||||
H2,
|
||||
ClickHouse,
|
||||
ManticoreSearch,
|
||||
Informix,
|
||||
Questdb,
|
||||
// GaussDB M 模式:MySQL 兼容方言,使用反引号引用标识符。DDL 行为接近 MySQL,
|
||||
// 但使用 UBTREE 索引类型(而非 BTREE),不支持 INCLUDE/FILTER 等。
|
||||
// DROP INDEX 使用 PostgreSQL 风格(DROP INDEX name),而非 MySQL 的 DROP INDEX name ON table。
|
||||
GaussdbM,
|
||||
Unsupported,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(super) struct TableStructureCapabilities {
|
||||
pub(super) dialect: StructureDialect,
|
||||
pub(super) add_column: bool,
|
||||
pub(super) drop_column: bool,
|
||||
pub(super) rename_column: bool,
|
||||
pub(super) alter_existing_column: bool,
|
||||
pub(super) reorder_column: bool,
|
||||
pub(super) comment: bool,
|
||||
pub(super) create_index: bool,
|
||||
pub(super) drop_index: bool,
|
||||
pub(super) rebuild_index: bool,
|
||||
pub(super) index_type: bool,
|
||||
pub(super) index_include: bool,
|
||||
pub(super) index_filter: bool,
|
||||
pub(super) index_comment: bool,
|
||||
pub(super) index_concurrent: bool,
|
||||
pub(super) add_primary_key: bool,
|
||||
pub(super) alter_primary_key: bool,
|
||||
pub(super) foreign_key: bool,
|
||||
}
|
||||
|
||||
impl Default for TableStructureCapabilities {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
dialect: StructureDialect::Unsupported,
|
||||
add_column: false,
|
||||
drop_column: false,
|
||||
rename_column: false,
|
||||
alter_existing_column: false,
|
||||
reorder_column: false,
|
||||
comment: false,
|
||||
create_index: false,
|
||||
drop_index: false,
|
||||
rebuild_index: false,
|
||||
index_type: false,
|
||||
index_include: false,
|
||||
index_filter: false,
|
||||
index_comment: false,
|
||||
index_concurrent: false,
|
||||
add_primary_key: false,
|
||||
alter_primary_key: false,
|
||||
foreign_key: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn gaussdb_m_capabilities() -> TableStructureCapabilities {
|
||||
TableStructureCapabilities {
|
||||
dialect: StructureDialect::GaussdbM,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
reorder_column: true,
|
||||
comment: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_type: true,
|
||||
index_comment: true,
|
||||
add_primary_key: true,
|
||||
alter_primary_key: true,
|
||||
..TableStructureCapabilities::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn capabilities_for(
|
||||
database_type: Option<DatabaseType>,
|
||||
driver_profile: Option<&str>,
|
||||
) -> TableStructureCapabilities {
|
||||
let base = TableStructureCapabilities::default();
|
||||
match database_type {
|
||||
Some(
|
||||
DatabaseType::Mysql
|
||||
| DatabaseType::StarRocks
|
||||
| DatabaseType::Goldendb
|
||||
| DatabaseType::Sundb
|
||||
| DatabaseType::Databend,
|
||||
) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Mysql,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
reorder_column: true,
|
||||
comment: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_type: true,
|
||||
index_comment: true,
|
||||
add_primary_key: true,
|
||||
alter_primary_key: true,
|
||||
foreign_key: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::Doris) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Doris,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
..base
|
||||
},
|
||||
// GBase 8s is Informix-compatible, unlike the rest of the `Gbase`
|
||||
// family (GBase 8a and friends) which speak MySQL wire protocol and
|
||||
// SQL dialect. `driver_profile` is the only signal that distinguishes
|
||||
// them (see the same check in `table_export.rs` / `table_select.rs`).
|
||||
Some(DatabaseType::Gbase) if driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case("gbase8s")) => {
|
||||
TableStructureCapabilities {
|
||||
dialect: StructureDialect::Informix,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
..base
|
||||
}
|
||||
}
|
||||
Some(DatabaseType::Gbase) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Mysql,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
reorder_column: true,
|
||||
..base
|
||||
},
|
||||
Some(
|
||||
DatabaseType::Postgres
|
||||
| DatabaseType::Gaussdb
|
||||
| DatabaseType::Kwdb
|
||||
| DatabaseType::OpenGauss
|
||||
| DatabaseType::Highgo
|
||||
| DatabaseType::Uxdb
|
||||
| DatabaseType::Vastbase
|
||||
| DatabaseType::Kingbase
|
||||
| DatabaseType::Firebird,
|
||||
) => {
|
||||
let mut caps = TableStructureCapabilities {
|
||||
dialect: StructureDialect::Postgres,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_type: true,
|
||||
index_include: true,
|
||||
index_filter: true,
|
||||
index_comment: true,
|
||||
add_primary_key: true,
|
||||
alter_primary_key: true,
|
||||
foreign_key: true,
|
||||
..base
|
||||
};
|
||||
// CREATE INDEX CONCURRENTLY is PostgreSQL-specific. Other PostgreSQL-family
|
||||
// engines (Kingbase, GaussDB, OpenGauss, HighGo, UXDB, Vastbase, KWDB,
|
||||
// Firebird) share the Postgres dialect but have not been verified against a
|
||||
// real environment, so they keep this capability off until individually
|
||||
// validated.
|
||||
if database_type == Some(DatabaseType::Postgres) {
|
||||
caps.index_concurrent = true;
|
||||
}
|
||||
caps
|
||||
}
|
||||
Some(DatabaseType::Questdb) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Questdb,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
reorder_column: false,
|
||||
comment: false,
|
||||
create_index: false,
|
||||
drop_index: false,
|
||||
rebuild_index: false,
|
||||
index_type: false,
|
||||
index_include: false,
|
||||
index_filter: false,
|
||||
index_comment: false,
|
||||
index_concurrent: false,
|
||||
add_primary_key: false,
|
||||
alter_primary_key: false,
|
||||
foreign_key: false,
|
||||
},
|
||||
Some(DatabaseType::Redshift | DatabaseType::Vertica) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Postgres,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::Sqlite | DatabaseType::Rqlite) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Sqlite,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_filter: true,
|
||||
..base
|
||||
},
|
||||
#[cfg(feature = "duckdb-sidecar")]
|
||||
Some(DatabaseType::DuckDb) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::DuckDb,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::SqlServer) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::SqlServer,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_type: true,
|
||||
index_include: true,
|
||||
index_filter: true,
|
||||
index_comment: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::Dameng) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Dameng,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_type: true,
|
||||
add_primary_key: true,
|
||||
alter_primary_key: true,
|
||||
..base
|
||||
},
|
||||
// 神通 Oscar v7:Oracle 兼容,实测支持 ALTER TABLE ADD/MODIFY/DROP/RENAME COLUMN、
|
||||
// DROP/ADD PRIMARY KEY、COMMENT ON、CREATE/DROP INDEX(见 issue #5505 探测)。
|
||||
Some(DatabaseType::Oscar) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Oscar,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_type: true,
|
||||
add_primary_key: true,
|
||||
alter_primary_key: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::Iris) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Oracle,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
// IRIS supports %DESCRIPTION while defining tables/columns, but cannot alter existing descriptions.
|
||||
comment: false,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_type: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::Oracle) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Oracle,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_type: true,
|
||||
add_primary_key: true,
|
||||
foreign_key: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::OceanbaseOracle | DatabaseType::Yashandb | DatabaseType::Xugu) => {
|
||||
TableStructureCapabilities {
|
||||
dialect: StructureDialect::Oracle,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_type: true,
|
||||
..base
|
||||
}
|
||||
}
|
||||
Some(DatabaseType::H2) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::H2,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::ClickHouse) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::ClickHouse,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
reorder_column: true,
|
||||
comment: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::ManticoreSearch) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::ManticoreSearch,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::Informix) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Informix,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
..base
|
||||
},
|
||||
_ => base,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn is_oracle_like(dialect: StructureDialect) -> bool {
|
||||
matches!(dialect, StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::Oscar)
|
||||
}
|
||||
|
||||
pub(super) fn database_label(database_type: Option<DatabaseType>) -> String {
|
||||
database_type
|
||||
.map(|database_type| {
|
||||
serde_json::to_value(database_type)
|
||||
.ok()
|
||||
.and_then(|value| value.as_str().map(str::to_string))
|
||||
.unwrap_or_else(|| "this database".to_string())
|
||||
})
|
||||
.unwrap_or_else(|| "this database".to_string())
|
||||
}
|
||||
|
||||
pub(super) fn dialect_label(dialect: StructureDialect) -> String {
|
||||
match dialect {
|
||||
StructureDialect::Mysql => "mysql",
|
||||
StructureDialect::Doris => "doris",
|
||||
StructureDialect::Postgres => "postgres",
|
||||
StructureDialect::Sqlite => "sqlite",
|
||||
#[cfg(feature = "duckdb-sidecar")]
|
||||
StructureDialect::DuckDb => "duckdb",
|
||||
StructureDialect::SqlServer => "sqlserver",
|
||||
StructureDialect::Oracle => "oracle",
|
||||
StructureDialect::Dameng => "dameng",
|
||||
StructureDialect::Oscar => "oscar",
|
||||
StructureDialect::H2 => "h2",
|
||||
StructureDialect::ClickHouse => "clickhouse",
|
||||
StructureDialect::ManticoreSearch => "manticoresearch",
|
||||
StructureDialect::Informix => "informix",
|
||||
StructureDialect::GaussdbM => "gaussdb-m",
|
||||
StructureDialect::Questdb => "questdb",
|
||||
StructureDialect::Unsupported => "this database",
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub(super) fn database_type_for_dialect(dialect: StructureDialect) -> Option<DatabaseType> {
|
||||
match dialect {
|
||||
StructureDialect::Mysql => Some(DatabaseType::Mysql),
|
||||
StructureDialect::Doris => Some(DatabaseType::Doris),
|
||||
StructureDialect::Postgres => Some(DatabaseType::Postgres),
|
||||
StructureDialect::Sqlite => Some(DatabaseType::Sqlite),
|
||||
#[cfg(feature = "duckdb-sidecar")]
|
||||
StructureDialect::DuckDb => Some(DatabaseType::DuckDb),
|
||||
StructureDialect::SqlServer => Some(DatabaseType::SqlServer),
|
||||
StructureDialect::Oracle => Some(DatabaseType::Oracle),
|
||||
StructureDialect::Dameng => Some(DatabaseType::Dameng),
|
||||
StructureDialect::Oscar => Some(DatabaseType::Oscar),
|
||||
StructureDialect::H2 => Some(DatabaseType::H2),
|
||||
StructureDialect::ClickHouse => Some(DatabaseType::ClickHouse),
|
||||
StructureDialect::ManticoreSearch => Some(DatabaseType::ManticoreSearch),
|
||||
StructureDialect::Informix => Some(DatabaseType::Informix),
|
||||
StructureDialect::Questdb => Some(DatabaseType::Questdb),
|
||||
StructureDialect::GaussdbM => Some(DatabaseType::Mysql),
|
||||
StructureDialect::Unsupported => None,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
use super::dialect::{database_label, StructureDialect};
|
||||
use super::types::{EditableStructureForeignKey, ForeignKeyInfo, TableStructureSqlOptions};
|
||||
use super::util::{clean, qualified_new_table, qualified_table, quote_ident, quote_new_ident};
|
||||
|
||||
pub(super) fn build_foreign_key_sql(options: &TableStructureSqlOptions, warnings: &mut Vec<String>) -> Vec<String> {
|
||||
build_foreign_key_sql_with_mode(options, warnings, false)
|
||||
}
|
||||
|
||||
pub(super) fn build_foreign_key_sql_for_new_table(
|
||||
options: &TableStructureSqlOptions,
|
||||
warnings: &mut Vec<String>,
|
||||
) -> Vec<String> {
|
||||
build_foreign_key_sql_with_mode(options, warnings, true)
|
||||
}
|
||||
|
||||
fn build_foreign_key_sql_with_mode(
|
||||
options: &TableStructureSqlOptions,
|
||||
warnings: &mut Vec<String>,
|
||||
for_new_table: bool,
|
||||
) -> Vec<String> {
|
||||
if options.foreign_keys.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let database_label = database_label(options.database_type);
|
||||
let capabilities = super::dialect::capabilities_for(options.database_type, options.driver_profile.as_deref());
|
||||
let dialect = capabilities.dialect;
|
||||
if !capabilities.foreign_key {
|
||||
if has_foreign_key_edits(&options.foreign_keys) {
|
||||
warnings.push(format!("Editing foreign keys is not supported for {database_label} from this editor."));
|
||||
}
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let table = if for_new_table {
|
||||
qualified_new_table(options.database_type, dialect, options.schema.as_deref(), &options.table_name)
|
||||
} else {
|
||||
qualified_table(dialect, options.schema.as_deref(), &options.table_name)
|
||||
};
|
||||
let mut statements = Vec::new();
|
||||
|
||||
for foreign_key in &options.foreign_keys {
|
||||
if foreign_key.marked_for_drop {
|
||||
if let Some(original) = &foreign_key.original {
|
||||
statements.push(drop_foreign_key_sql(dialect, &table, &original.name));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(original) = &foreign_key.original {
|
||||
if !has_foreign_key_change(foreign_key, original) {
|
||||
continue;
|
||||
}
|
||||
statements.push(drop_foreign_key_sql(dialect, &table, &original.name));
|
||||
}
|
||||
|
||||
if let Some(sql) =
|
||||
create_foreign_key_sql(options.database_type, dialect, &table, foreign_key, warnings, for_new_table)
|
||||
{
|
||||
statements.push(sql);
|
||||
}
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
fn has_foreign_key_edits(foreign_keys: &[EditableStructureForeignKey]) -> bool {
|
||||
foreign_keys.iter().any(|foreign_key| {
|
||||
if foreign_key.marked_for_drop {
|
||||
return foreign_key.original.is_some();
|
||||
}
|
||||
|
||||
match &foreign_key.original {
|
||||
Some(original) => has_foreign_key_change(foreign_key, original),
|
||||
None => has_foreign_key_definition(foreign_key),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn has_foreign_key_definition(foreign_key: &EditableStructureForeignKey) -> bool {
|
||||
!clean(&foreign_key.name).is_empty()
|
||||
|| !clean(&foreign_key.column).is_empty()
|
||||
|| !clean(&foreign_key.ref_schema).is_empty()
|
||||
|| !clean(&foreign_key.ref_table).is_empty()
|
||||
|| !clean(&foreign_key.ref_column).is_empty()
|
||||
|| !clean(&foreign_key.on_update).is_empty()
|
||||
|| !clean(&foreign_key.on_delete).is_empty()
|
||||
}
|
||||
|
||||
fn has_foreign_key_change(foreign_key: &EditableStructureForeignKey, original: &ForeignKeyInfo) -> bool {
|
||||
clean(&foreign_key.name) != clean(&original.name)
|
||||
|| clean(&foreign_key.column) != clean(&original.column)
|
||||
|| clean(&foreign_key.ref_schema) != clean(original.ref_schema.as_deref().unwrap_or(""))
|
||||
|| clean(&foreign_key.ref_table) != clean(&original.ref_table)
|
||||
|| clean(&foreign_key.ref_column) != clean(&original.ref_column)
|
||||
|| normalize_action(&foreign_key.on_update) != normalize_action(original.on_update.as_deref().unwrap_or(""))
|
||||
|| normalize_action(&foreign_key.on_delete) != normalize_action(original.on_delete.as_deref().unwrap_or(""))
|
||||
}
|
||||
|
||||
fn drop_foreign_key_sql(dialect: StructureDialect, table: &str, name: &str) -> String {
|
||||
match dialect {
|
||||
StructureDialect::Mysql => format!("ALTER TABLE {table} DROP FOREIGN KEY {};", quote_ident(dialect, name)),
|
||||
StructureDialect::Postgres | StructureDialect::Oracle => {
|
||||
format!("ALTER TABLE {table} DROP CONSTRAINT {};", quote_ident(dialect, name))
|
||||
}
|
||||
_ => unreachable!("foreign key SQL requested for unsupported dialect"),
|
||||
}
|
||||
}
|
||||
|
||||
fn create_foreign_key_sql(
|
||||
database_type: Option<crate::models::connection::DatabaseType>,
|
||||
dialect: StructureDialect,
|
||||
table: &str,
|
||||
foreign_key: &EditableStructureForeignKey,
|
||||
warnings: &mut Vec<String>,
|
||||
for_new_table: bool,
|
||||
) -> Option<String> {
|
||||
let name = clean(&foreign_key.name);
|
||||
let columns = identifier_list(&foreign_key.column);
|
||||
let ref_table = clean(&foreign_key.ref_table);
|
||||
let ref_columns = identifier_list(&foreign_key.ref_column);
|
||||
if name.is_empty() || columns.is_empty() || ref_table.is_empty() || ref_columns.is_empty() {
|
||||
warnings.push("Foreign key name, column, referenced table, and referenced column are required.".to_string());
|
||||
return None;
|
||||
}
|
||||
if columns.len() != ref_columns.len() {
|
||||
warnings.push(format!("Foreign key \"{name}\" must have the same number of local and referenced columns."));
|
||||
return None;
|
||||
}
|
||||
|
||||
let ref_target = if clean(&foreign_key.ref_schema).is_empty() {
|
||||
quote_ident(dialect, &ref_table)
|
||||
} else {
|
||||
format!("{}.{}", quote_ident(dialect, &foreign_key.ref_schema), quote_ident(dialect, &ref_table))
|
||||
};
|
||||
|
||||
let constraint_name =
|
||||
if for_new_table { quote_new_ident(database_type, dialect, &name) } else { quote_ident(dialect, &name) };
|
||||
let local_columns =
|
||||
columns
|
||||
.iter()
|
||||
.map(|column| {
|
||||
if for_new_table {
|
||||
quote_new_ident(database_type, dialect, column)
|
||||
} else {
|
||||
quote_ident(dialect, column)
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let referenced_columns =
|
||||
ref_columns.iter().map(|column| quote_ident(dialect, column)).collect::<Vec<_>>().join(", ");
|
||||
let mut sql = format!(
|
||||
"ALTER TABLE {table} ADD CONSTRAINT {constraint_name} FOREIGN KEY ({local_columns}) REFERENCES {ref_target} ({referenced_columns})"
|
||||
);
|
||||
|
||||
if let Some(action) = action_clause(dialect, "ON DELETE", &foreign_key.on_delete, warnings) {
|
||||
sql.push(' ');
|
||||
sql.push_str(&action);
|
||||
}
|
||||
if let Some(action) = action_clause(dialect, "ON UPDATE", &foreign_key.on_update, warnings) {
|
||||
sql.push(' ');
|
||||
sql.push_str(&action);
|
||||
}
|
||||
sql.push(';');
|
||||
Some(sql)
|
||||
}
|
||||
|
||||
fn action_clause(dialect: StructureDialect, prefix: &str, value: &str, warnings: &mut Vec<String>) -> Option<String> {
|
||||
let action = normalize_action(value);
|
||||
if action.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if dialect == StructureDialect::Oracle {
|
||||
if prefix == "ON UPDATE" {
|
||||
if action != "NO ACTION" {
|
||||
warnings.push(format!("Oracle does not support {prefix} {action}."));
|
||||
}
|
||||
return None;
|
||||
}
|
||||
return match action.as_str() {
|
||||
"CASCADE" | "SET NULL" => Some(format!("{prefix} {action}")),
|
||||
"NO ACTION" => None,
|
||||
_ => {
|
||||
warnings.push(format!("Unsupported Oracle foreign key action \"{}\".", clean(value)));
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
match action.as_str() {
|
||||
"CASCADE" | "SET NULL" | "RESTRICT" | "NO ACTION" => Some(format!("{prefix} {action}")),
|
||||
_ => {
|
||||
warnings.push(format!("Unsupported foreign key action \"{}\".", clean(value)));
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_action(value: &str) -> String {
|
||||
clean(value).split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_uppercase()
|
||||
}
|
||||
|
||||
fn identifier_list(value: &str) -> Vec<String> {
|
||||
value.split(',').map(clean).filter(|value| !value.is_empty()).collect()
|
||||
}
|
||||
@@ -0,0 +1,586 @@
|
||||
use super::comments::build_sqlserver_index_comment_sql_for_profile;
|
||||
use super::dialect::{capabilities_for, database_label, database_type_for_dialect, dialect_label, StructureDialect};
|
||||
use super::types::{EditableStructureIndex, IndexInfo, TableStructureSqlOptions};
|
||||
use super::util::{clean, qualified_table, quote_ident, quote_new_ident, quote_string};
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
pub(super) fn build_index_sql(options: &TableStructureSqlOptions, warnings: &mut Vec<String>) -> Vec<String> {
|
||||
let capabilities;
|
||||
let dialect;
|
||||
if options.is_gaussdb_m_mode {
|
||||
let caps = super::dialect::gaussdb_m_capabilities();
|
||||
capabilities = caps;
|
||||
dialect = StructureDialect::GaussdbM;
|
||||
} else {
|
||||
let caps = capabilities_for(options.database_type, options.driver_profile.as_deref());
|
||||
capabilities = caps;
|
||||
dialect = caps.dialect;
|
||||
}
|
||||
let table = qualified_table(dialect, options.schema.as_deref(), &options.table_name);
|
||||
let database_label = database_label(options.database_type);
|
||||
let mut statements = Vec::new();
|
||||
|
||||
for index in &options.indexes {
|
||||
if index.marked_for_drop {
|
||||
let Some(original) = &index.original else {
|
||||
continue;
|
||||
};
|
||||
if !capabilities.drop_index {
|
||||
warnings.push(format!("Dropping indexes is not supported for {database_label} from this editor."));
|
||||
continue;
|
||||
}
|
||||
if original.is_primary {
|
||||
warnings.push(format!("Primary index \"{}\" cannot be dropped from this editor.", original.name));
|
||||
continue;
|
||||
}
|
||||
if is_dameng_constraint_backed(options.database_type, original) {
|
||||
statements.push(build_dameng_drop_constraint_sql(dialect, &table, &original.name));
|
||||
continue;
|
||||
}
|
||||
statements.push(build_drop_index_sql(
|
||||
options.database_type,
|
||||
dialect,
|
||||
&table,
|
||||
options.schema.as_deref(),
|
||||
&original.name,
|
||||
));
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(original) = &index.original {
|
||||
if !has_existing_index_change(index) {
|
||||
continue;
|
||||
}
|
||||
if !capabilities.rebuild_index || !capabilities.drop_index || !capabilities.create_index {
|
||||
warnings
|
||||
.push(format!("Editing existing indexes is not supported for {database_label} from this editor."));
|
||||
continue;
|
||||
}
|
||||
if original.is_primary {
|
||||
warnings.push(format!("Primary index \"{}\" cannot be edited from this editor.", original.name));
|
||||
continue;
|
||||
}
|
||||
if is_dameng_constraint_backed(options.database_type, original) {
|
||||
statements.extend(build_dameng_constraint_index_edit(
|
||||
options,
|
||||
dialect,
|
||||
&table,
|
||||
index,
|
||||
original,
|
||||
capabilities.index_concurrent,
|
||||
warnings,
|
||||
));
|
||||
continue;
|
||||
}
|
||||
let or_replace =
|
||||
options.database_type == Some(DatabaseType::Dameng) && clean(&index.name) == clean(&original.name);
|
||||
if !or_replace {
|
||||
statements.push(build_drop_index_sql(
|
||||
options.database_type,
|
||||
dialect,
|
||||
&table,
|
||||
options.schema.as_deref(),
|
||||
&original.name,
|
||||
));
|
||||
}
|
||||
statements.extend(build_create_index_statements(
|
||||
options.database_type,
|
||||
dialect,
|
||||
&table,
|
||||
index,
|
||||
warnings,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
or_replace,
|
||||
capabilities.index_concurrent,
|
||||
false,
|
||||
options.driver_profile.as_deref(),
|
||||
));
|
||||
continue;
|
||||
}
|
||||
|
||||
if !capabilities.create_index {
|
||||
warnings.push(format!("Creating indexes is not supported for {database_label} from this editor."));
|
||||
continue;
|
||||
}
|
||||
statements.extend(build_create_index_statements(
|
||||
options.database_type,
|
||||
dialect,
|
||||
&table,
|
||||
index,
|
||||
warnings,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
false,
|
||||
capabilities.index_concurrent,
|
||||
false,
|
||||
options.driver_profile.as_deref(),
|
||||
));
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
/// Dameng builds the index behind a PRIMARY KEY / UNIQUE constraint as a "virtual" index
|
||||
/// owned by that constraint. Index-level DDL against such an index — `DROP INDEX` as much as
|
||||
/// `CREATE OR REPLACE INDEX` — is rejected with a misleading "no permission to drop index"
|
||||
/// error even for the owning user (#7959); it can only be changed through
|
||||
/// `ALTER TABLE ... ADD/DROP CONSTRAINT`, the same way Dameng primary keys are already
|
||||
/// handled in `columns.rs`.
|
||||
///
|
||||
/// A "real" unique index (`CREATE UNIQUE INDEX`, which is also what this editor emits for a
|
||||
/// newly created unique index) has no constraint behind it, is not reported as
|
||||
/// constraint-backed by introspection, and keeps the index-level path — constraint DDL would
|
||||
/// fail on it with "constraint does not exist".
|
||||
fn is_dameng_constraint_backed(database_type: Option<DatabaseType>, original: &IndexInfo) -> bool {
|
||||
database_type == Some(DatabaseType::Dameng) && original.constraint_backed
|
||||
}
|
||||
|
||||
fn build_dameng_drop_constraint_sql(dialect: StructureDialect, table: &str, constraint_name: &str) -> String {
|
||||
format!("ALTER TABLE {table} DROP CONSTRAINT {};", quote_ident(dialect, constraint_name))
|
||||
}
|
||||
|
||||
/// Rewrites an edit of a constraint-backed Dameng index as constraint DDL: drop the
|
||||
/// constraint, then re-add it as `UNIQUE` (still unique) or replace it with an ordinary
|
||||
/// index (downgraded to a plain index, where index-level DDL is fine again because the
|
||||
/// constraint is gone).
|
||||
fn build_dameng_constraint_index_edit(
|
||||
options: &TableStructureSqlOptions,
|
||||
dialect: StructureDialect,
|
||||
table: &str,
|
||||
index: &EditableStructureIndex,
|
||||
original: &IndexInfo,
|
||||
concurrently_supported: bool,
|
||||
warnings: &mut Vec<String>,
|
||||
) -> Vec<String> {
|
||||
let name = clean(&index.name);
|
||||
let columns: Vec<String> =
|
||||
index.columns.iter().map(|column| clean(column)).filter(|column| !column.is_empty()).collect();
|
||||
// Same guard as `build_create_index_statements`: an empty name or an empty column list
|
||||
// cannot produce a valid replacement (`validate_draft` already reports it as a warning).
|
||||
// Emitting only the DROP would silently delete the constraint, so skip the edit entirely.
|
||||
if name.is_empty() || columns.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let mut statements = vec![build_dameng_drop_constraint_sql(dialect, table, &original.name)];
|
||||
if !index.is_unique {
|
||||
statements.extend(build_create_index_statements(
|
||||
options.database_type,
|
||||
dialect,
|
||||
table,
|
||||
index,
|
||||
warnings,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
false,
|
||||
concurrently_supported,
|
||||
false,
|
||||
options.driver_profile.as_deref(),
|
||||
));
|
||||
return statements;
|
||||
}
|
||||
|
||||
// `build_create_index_statements` would honor BITMAP for Dameng, but a unique constraint
|
||||
// always builds a normal index behind itself, so the type cannot be carried over.
|
||||
if normalized_index_type(index) == "BITMAP" {
|
||||
warnings.push(format!(
|
||||
"Index type BITMAP is ignored for unique index \"{name}\": Dameng enforces it with a unique constraint, whose index cannot be a bitmap index."
|
||||
));
|
||||
}
|
||||
let cols = columns.iter().map(|column| quote_ident(dialect, column)).collect::<Vec<_>>().join(", ");
|
||||
statements.push(format!("ALTER TABLE {table} ADD CONSTRAINT {} UNIQUE ({cols});", quote_ident(dialect, &name)));
|
||||
statements
|
||||
}
|
||||
|
||||
pub fn has_existing_index_change(index: &EditableStructureIndex) -> bool {
|
||||
let Some(original) = &index.original else {
|
||||
return false;
|
||||
};
|
||||
clean(&index.name) != clean(&original.name)
|
||||
|| index_list_changed(&index.columns, Some(&original.columns))
|
||||
|| index.is_unique != original.is_unique
|
||||
|| normalized_index_type(index) != clean(original.index_type.as_deref().unwrap_or("")).to_ascii_uppercase()
|
||||
|| index_list_changed(&index.included_columns, original.included_columns.as_ref())
|
||||
|| clean(&index.filter) != clean(original.filter.as_deref().unwrap_or(""))
|
||||
|| clean(&index.comment) != clean(original.comment.as_deref().unwrap_or(""))
|
||||
|| index_opclasses_changed(
|
||||
&index.column_opclasses,
|
||||
&original.column_opclasses,
|
||||
&index.columns,
|
||||
&original.columns,
|
||||
)
|
||||
}
|
||||
|
||||
/// Compare opclass arrays positionally, skipping expression columns.
|
||||
/// When the column list has changed we already detect that above; here we only
|
||||
/// compare opclasses for columns that exist in both the edited and original lists.
|
||||
fn index_opclasses_changed(
|
||||
next: &[Option<String>],
|
||||
previous: &[Option<String>],
|
||||
next_cols: &[String],
|
||||
prev_cols: &[String],
|
||||
) -> bool {
|
||||
// If the edited side has explicit opclasses, compare positionally.
|
||||
if !next.is_empty() {
|
||||
// Length mismatch (shouldn't happen when columns are in lockstep, but
|
||||
// defend against it): any difference in length is a change.
|
||||
if next.len() != previous.len() {
|
||||
return true;
|
||||
}
|
||||
return next.iter().zip(previous.iter()).any(|(n, p)| {
|
||||
let n_flat = n.as_deref().map(clean).filter(|v| !v.is_empty());
|
||||
let p_flat = p.as_deref().map(clean).filter(|v| !v.is_empty());
|
||||
n_flat != p_flat
|
||||
});
|
||||
}
|
||||
// If the edited side has no opclasses, check whether the original side
|
||||
// had any non-default opclasses for columns still present.
|
||||
if previous.iter().any(|o| o.as_deref().map(|v| !clean(v).is_empty()).unwrap_or(false)) {
|
||||
let prev_set: std::collections::HashSet<_> = prev_cols.iter().map(|c| clean(c)).collect();
|
||||
return next_cols.iter().zip(previous.iter()).any(|(nc, po)| {
|
||||
po.as_deref().map(|v| !clean(v).is_empty()).unwrap_or(false) && prev_set.contains(&clean(nc))
|
||||
});
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub(super) fn index_list_changed(next: &[String], previous: Option<&Vec<String>>) -> bool {
|
||||
let next_clean: Vec<_> = next.iter().map(|value| clean(value)).filter(|value| !value.is_empty()).collect();
|
||||
let previous_clean: Vec<_> =
|
||||
previous.unwrap_or(&Vec::new()).iter().map(|value| clean(value)).filter(|value| !value.is_empty()).collect();
|
||||
next_clean.len() != previous_clean.len()
|
||||
|| next_clean.iter().enumerate().any(|(index, value)| previous_clean.get(index) != Some(value))
|
||||
}
|
||||
|
||||
pub(super) fn normalized_index_type(index: &EditableStructureIndex) -> String {
|
||||
clean(&index.index_type).to_ascii_uppercase()
|
||||
}
|
||||
|
||||
pub(super) fn mysql_index_parts(index_type: &str) -> (String, String) {
|
||||
match index_type.to_ascii_uppercase().as_str() {
|
||||
"FULLTEXT" | "SPATIAL" => (format!("{} ", index_type.to_ascii_uppercase()), String::new()),
|
||||
"RTREE" => ("SPATIAL ".to_string(), String::new()),
|
||||
"BTREE" | "HASH" => (String::new(), format!(" USING {}", index_type.to_ascii_uppercase())),
|
||||
_ => (String::new(), String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
fn gaussdbm_index_parts(index_type: &str) -> (String, String) {
|
||||
match index_type.to_ascii_uppercase().as_str() {
|
||||
"BTREE" | "UBTREE" => (String::new(), " USING UBTREE".to_string()),
|
||||
"HASH" => (String::new(), " USING HASH".to_string()),
|
||||
_ => (String::new(), String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
fn gaussdbm_index_column_sql(column: &str, is_expression: bool) -> String {
|
||||
let trimmed = column.trim();
|
||||
if is_expression {
|
||||
return trimmed.to_string();
|
||||
}
|
||||
if let Some((name, suffix)) = trimmed.rsplit_once('(') {
|
||||
if let Some(length) = suffix.strip_suffix(')') {
|
||||
if !name.trim().is_empty() && !length.is_empty() && length.chars().all(|ch| ch.is_ascii_digit()) {
|
||||
return format!("{}({length})", quote_ident(StructureDialect::GaussdbM, name.trim()));
|
||||
}
|
||||
}
|
||||
}
|
||||
quote_ident(StructureDialect::GaussdbM, trimmed)
|
||||
}
|
||||
|
||||
fn mysql_index_column_sql(column: &str) -> String {
|
||||
let trimmed = column.trim();
|
||||
// Keep the wrapped expression from MySQL metadata instead of quoting it as a column identifier.
|
||||
if trimmed.starts_with("((") && trimmed.ends_with("))") {
|
||||
trimmed.to_string()
|
||||
} else {
|
||||
quote_ident(StructureDialect::Mysql, column)
|
||||
}
|
||||
}
|
||||
|
||||
fn postgres_index_column_sql(column: &str, is_expression: bool, opclass: Option<&str>) -> String {
|
||||
// The base key text: a real column is quoted as an identifier; an expression/functional
|
||||
// key part arrives as raw expression text (from the per-column `pg_get_indexdef`, which
|
||||
// omits the opclass — see `list_indexes_with_sql`), so quoting the whole thing as an
|
||||
// identifier would turn it into a nonexistent column reference (#6295).
|
||||
let base =
|
||||
if is_expression { column.trim().to_string() } else { quote_ident(StructureDialect::Postgres, column.trim()) };
|
||||
// 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()) {
|
||||
Some(opc) => format!("{} {}", base, opc),
|
||||
None => base,
|
||||
}
|
||||
}
|
||||
|
||||
/// Per-key expression provenance for `columns` (the edited/current key list), computed once for
|
||||
/// the whole index. The editor only lets users toggle real table columns onto an index (see
|
||||
/// TableStructureEditor.vue's column checkbox list) — there is no free-text expression input —
|
||||
/// so an edited key part is only ever an expression when it corresponds to one from the original
|
||||
/// introspected snapshot.
|
||||
///
|
||||
/// Matches each edited key part against the first *not yet claimed* original key part with the
|
||||
/// same text, rather than the first textual match overall: a real column whose name happens to
|
||||
/// equal another key's expression text (or two key parts with identical text) can otherwise steal
|
||||
/// the wrong original's provenance. Consuming each original slot at most once keeps provenance
|
||||
/// tied to its true ordinal position instead of being re-derived by scanning for a text match
|
||||
/// (#6312 review). A key without explicit provenance is treated as a real column: the editor can
|
||||
/// only add table columns, and guessing from the identifier text breaks valid quoted names.
|
||||
fn key_expression_flags(index: &EditableStructureIndex, columns: &[String]) -> Vec<bool> {
|
||||
let original = match &index.original {
|
||||
Some(original) if !original.key_is_expression.is_empty() => original,
|
||||
_ => return vec![false; columns.len()],
|
||||
};
|
||||
let mut consumed = vec![false; original.columns.len()];
|
||||
columns
|
||||
.iter()
|
||||
.map(|column| {
|
||||
let claimed = original
|
||||
.columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(i, original_column)| !consumed[*i] && *original_column == column);
|
||||
match claimed {
|
||||
Some((i, _)) => {
|
||||
consumed[i] = true;
|
||||
original.key_is_expression.get(i).copied().unwrap_or(false)
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Per-key operator class provenance for `columns` (the edited/current key list).
|
||||
///
|
||||
/// Honors explicit per-key opclasses (`index.column_opclasses`) positionally when
|
||||
/// they are aligned to `columns` (non-empty, same length) and represent a genuine
|
||||
/// edit: either there is no `original` (a newly created index, where the UI
|
||||
/// authors `column_opclasses` in lockstep with `columns`), or the edited
|
||||
/// opclasses differ from the round-tripped `original.column_opclasses`. A stale
|
||||
/// copy left behind by a column toggle/reorder — still equal to the original —
|
||||
/// falls through to the order-independent text-matching below, so opclasses are
|
||||
/// never misassigned positionally before the editor maintains `column_opclasses`
|
||||
/// in lockstep with `columns`. Once that lockstep is in place the guard always
|
||||
/// passes and the honor branch is authoritative.
|
||||
fn key_opclasses(index: &EditableStructureIndex, columns: &[String]) -> Vec<Option<String>> {
|
||||
if !index.column_opclasses.is_empty()
|
||||
&& index.column_opclasses.len() == columns.len()
|
||||
&& index.original.as_ref().is_none_or(|original| original.column_opclasses != index.column_opclasses)
|
||||
{
|
||||
return index.column_opclasses.to_vec();
|
||||
}
|
||||
let original = match &index.original {
|
||||
Some(original) if !original.column_opclasses.is_empty() => original,
|
||||
_ => return vec![None; columns.len()],
|
||||
};
|
||||
let mut consumed = vec![false; original.columns.len()];
|
||||
columns
|
||||
.iter()
|
||||
.map(|column| {
|
||||
let claimed = original
|
||||
.columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(i, original_column)| !consumed[*i] && *original_column == column);
|
||||
match claimed {
|
||||
Some((i, _)) => {
|
||||
consumed[i] = true;
|
||||
original.column_opclasses.get(i).cloned().flatten()
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(super) fn build_drop_index_sql(
|
||||
database_type: Option<DatabaseType>,
|
||||
dialect: StructureDialect,
|
||||
table: &str,
|
||||
schema: Option<&str>,
|
||||
index_name: &str,
|
||||
) -> String {
|
||||
if database_type == Some(DatabaseType::Iris) {
|
||||
return format!("DROP INDEX {} ON TABLE {table};", quote_ident(dialect, index_name));
|
||||
}
|
||||
if matches!(dialect, StructureDialect::Mysql | StructureDialect::SqlServer) {
|
||||
return format!("DROP INDEX {} ON {table};", quote_ident(dialect, index_name));
|
||||
}
|
||||
if matches!(
|
||||
dialect,
|
||||
StructureDialect::Postgres
|
||||
| StructureDialect::Oracle
|
||||
| StructureDialect::Dameng
|
||||
| StructureDialect::Oscar
|
||||
| StructureDialect::Informix
|
||||
| StructureDialect::Sqlite
|
||||
) && schema.is_some_and(|schema| !schema.trim().is_empty())
|
||||
{
|
||||
return format!("DROP INDEX {}.{};", quote_ident(dialect, schema.unwrap()), quote_ident(dialect, index_name));
|
||||
}
|
||||
format!("DROP INDEX {};", quote_ident(dialect, index_name))
|
||||
}
|
||||
|
||||
#[expect(
|
||||
clippy::too_many_arguments,
|
||||
reason = "The existing index builder API passes independent structure-edit options"
|
||||
)]
|
||||
pub(super) fn build_create_index_statements(
|
||||
database_type: Option<DatabaseType>,
|
||||
dialect: StructureDialect,
|
||||
table: &str,
|
||||
index: &EditableStructureIndex,
|
||||
warnings: &mut Vec<String>,
|
||||
schema: Option<&str>,
|
||||
table_name: &str,
|
||||
or_replace: bool,
|
||||
concurrently_supported: bool,
|
||||
for_new_table: bool,
|
||||
driver_profile: Option<&str>,
|
||||
) -> Vec<String> {
|
||||
let capabilities = capabilities_for(database_type_for_dialect(dialect), None);
|
||||
let name = clean(&index.name);
|
||||
let columns: Vec<String> =
|
||||
index.columns.iter().map(|column| clean(column)).filter(|column| !column.is_empty()).collect();
|
||||
if name.is_empty() || columns.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
// `CREATE INDEX CONCURRENTLY` is PostgreSQL-only: the flag is honored only when
|
||||
// the caller's real database type advertises the capability, so a stale or
|
||||
// forged `concurrently: true` from another engine is ignored and the original
|
||||
// SQL is generated unchanged. Unsupported concurrent requests (existing index,
|
||||
// partitioned parent) are rejected up front by `validate_concurrent_index_scope`
|
||||
// before any statement is built — this function never downgrades them.
|
||||
let concurrently = index.concurrently && concurrently_supported && dialect == StructureDialect::Postgres;
|
||||
|
||||
let unique = if index.is_unique { "UNIQUE " } else { "" };
|
||||
let replace = if or_replace { "OR REPLACE " } else { "" };
|
||||
let key_is_expression = key_expression_flags(index, &columns);
|
||||
let key_opclasses = key_opclasses(index, &columns);
|
||||
let cols = columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, column)| {
|
||||
if dialect == StructureDialect::Mysql {
|
||||
mysql_index_column_sql(column)
|
||||
} else if dialect == StructureDialect::GaussdbM {
|
||||
gaussdbm_index_column_sql(column, key_is_expression[i])
|
||||
} else if dialect == StructureDialect::Postgres {
|
||||
postgres_index_column_sql(column, key_is_expression[i], key_opclasses[i].as_deref())
|
||||
} else if for_new_table {
|
||||
quote_new_ident(database_type, dialect, column)
|
||||
} else {
|
||||
quote_ident(dialect, column)
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let idx_type = normalized_index_type(index);
|
||||
let mut type_prefix = String::new();
|
||||
let mut using_clause = String::new();
|
||||
|
||||
if !idx_type.is_empty() && capabilities.index_type {
|
||||
match dialect {
|
||||
StructureDialect::Postgres => using_clause = format!(" USING {idx_type}"),
|
||||
StructureDialect::SqlServer => type_prefix = format!("{idx_type} "),
|
||||
StructureDialect::Mysql => {
|
||||
let (prefix, using) = mysql_index_parts(&idx_type);
|
||||
type_prefix = prefix;
|
||||
using_clause = using;
|
||||
}
|
||||
StructureDialect::Oracle | StructureDialect::Dameng if idx_type == "BITMAP" => {
|
||||
type_prefix = "BITMAP ".to_string()
|
||||
}
|
||||
StructureDialect::GaussdbM => {
|
||||
let (prefix, using) = gaussdbm_index_parts(&idx_type);
|
||||
type_prefix = prefix;
|
||||
using_clause = using;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let included_columns: Vec<String> =
|
||||
index.included_columns.iter().map(|column| clean(column)).filter(|column| !column.is_empty()).collect();
|
||||
let include_clause = if !included_columns.is_empty()
|
||||
&& capabilities.index_include
|
||||
&& matches!(dialect, StructureDialect::Postgres | StructureDialect::SqlServer)
|
||||
{
|
||||
format!(
|
||||
" INCLUDE ({})",
|
||||
included_columns
|
||||
.iter()
|
||||
.map(|column| {
|
||||
if for_new_table {
|
||||
quote_new_ident(database_type, dialect, column)
|
||||
} else {
|
||||
quote_ident(dialect, column)
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let comment = clean(&index.comment);
|
||||
let comment_clause = if !comment.is_empty()
|
||||
&& capabilities.index_comment
|
||||
&& matches!(dialect, StructureDialect::Mysql | StructureDialect::GaussdbM)
|
||||
{
|
||||
format!(" COMMENT {}", quote_string(&comment))
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let filter = clean(&index.filter);
|
||||
let supports_where = capabilities.index_filter
|
||||
&& matches!(dialect, StructureDialect::Postgres | StructureDialect::SqlServer | StructureDialect::Sqlite);
|
||||
let where_clause = if !filter.is_empty() && supports_where { format!(" WHERE {filter}") } else { String::new() };
|
||||
let quoted_index_name =
|
||||
if for_new_table { quote_new_ident(database_type, dialect, &name) } else { quote_ident(dialect, &name) };
|
||||
let index_name = if dialect == StructureDialect::Sqlite && schema.is_some_and(|schema| !schema.trim().is_empty()) {
|
||||
format!("{}.{}", quote_ident(dialect, schema.unwrap()), quoted_index_name)
|
||||
} else {
|
||||
quoted_index_name
|
||||
};
|
||||
// SQLite assigns an index to the database named by the qualified index.
|
||||
// Its CREATE INDEX grammar does not allow a qualified table name.
|
||||
let create_table =
|
||||
if dialect == StructureDialect::Sqlite { quote_ident(dialect, table_name) } else { table.to_string() };
|
||||
let create_sql = if dialect == StructureDialect::Postgres {
|
||||
let concurrent_clause = if concurrently { "CONCURRENTLY " } else { "" };
|
||||
format!(
|
||||
"CREATE {replace}{unique}INDEX {concurrent_clause}{index_name} ON {create_table}{using_clause} ({cols}){include_clause}{where_clause};"
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"CREATE {replace}{unique}{type_prefix}INDEX {index_name}{using_clause} ON {create_table} ({cols}){include_clause}{where_clause}{comment_clause};"
|
||||
)
|
||||
};
|
||||
let mut statements = vec![create_sql];
|
||||
|
||||
if !comment.is_empty() && capabilities.index_comment && dialect == StructureDialect::Postgres {
|
||||
statements.push(format!("COMMENT ON INDEX {} IS {};", quote_ident(dialect, &name), quote_string(&comment)));
|
||||
} else if !comment.is_empty() && capabilities.index_comment && dialect == StructureDialect::SqlServer {
|
||||
statements.extend(build_sqlserver_index_comment_sql_for_profile(
|
||||
table,
|
||||
schema,
|
||||
table_name,
|
||||
&name,
|
||||
&comment,
|
||||
driver_profile,
|
||||
));
|
||||
} else if !comment.is_empty()
|
||||
&& capabilities.index_comment
|
||||
&& matches!(dialect, StructureDialect::Mysql | StructureDialect::GaussdbM)
|
||||
{
|
||||
// Comment is embedded inline in the CREATE INDEX statement above
|
||||
} else if !comment.is_empty() && capabilities.index_comment {
|
||||
warnings.push(format!("Index comments are not supported for {} from this editor.", dialect_label(dialect)));
|
||||
}
|
||||
statements
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
use super::dialect::StructureDialect;
|
||||
use super::types::TableStructureSqlOptions;
|
||||
use super::util::qualified_table;
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
pub(super) fn validate_mysql_engine(options: &TableStructureSqlOptions) -> Vec<String> {
|
||||
let Some(engine) = options.mysql_engine.as_deref() else {
|
||||
return Vec::new();
|
||||
};
|
||||
let engine = engine.trim();
|
||||
if options.database_type != Some(DatabaseType::Mysql) || options.is_gaussdb_m_mode {
|
||||
return vec!["Changing the table engine is supported only for native MySQL connections.".to_string()];
|
||||
}
|
||||
if engine.is_empty() {
|
||||
return vec!["MySQL table engine is required.".to_string()];
|
||||
}
|
||||
if engine.len() > 64 || !engine.chars().all(|character| character.is_ascii_alphanumeric() || character == '_') {
|
||||
return vec!["MySQL table engine contains invalid characters.".to_string()];
|
||||
}
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
pub(super) fn build_mysql_engine_change_sql(options: &TableStructureSqlOptions) -> Vec<String> {
|
||||
let Some(engine) = options.mysql_engine.as_deref().map(str::trim).filter(|engine| !engine.is_empty()) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let table = qualified_table(StructureDialect::Mysql, options.schema.as_deref(), &options.table_name);
|
||||
vec![format!("ALTER TABLE {table} ENGINE = {engine};")]
|
||||
}
|
||||
|
||||
pub(super) fn append_mysql_table_option(statement: &mut String, option: &str) {
|
||||
if statement.ends_with(';') {
|
||||
statement.pop();
|
||||
}
|
||||
statement.push(' ');
|
||||
statement.push_str(option);
|
||||
statement.push(';');
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
use super::dialect::StructureDialect;
|
||||
use super::types::{TableOwnerChangeSqlOptions, TableStructureSqlResult};
|
||||
use super::util::{qualified_table, quote_ident};
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
pub fn build_table_owner_change_sql(options: TableOwnerChangeSqlOptions) -> TableStructureSqlResult {
|
||||
let owner = &options.owner;
|
||||
if owner == &options.original_owner {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings: Vec::new() };
|
||||
}
|
||||
if owner.is_empty() {
|
||||
return TableStructureSqlResult {
|
||||
statements: Vec::new(),
|
||||
warnings: vec!["Table owner cannot be empty.".to_string()],
|
||||
};
|
||||
}
|
||||
if options.database_type != Some(DatabaseType::Postgres) {
|
||||
return TableStructureSqlResult {
|
||||
statements: Vec::new(),
|
||||
warnings: vec!["Changing the table owner is currently supported only for PostgreSQL.".to_string()],
|
||||
};
|
||||
}
|
||||
|
||||
let table = qualified_table(StructureDialect::Postgres, options.schema.as_deref(), &options.table_name);
|
||||
let owner = quote_ident(StructureDialect::Postgres, owner);
|
||||
TableStructureSqlResult { statements: vec![format!("ALTER TABLE {table} OWNER TO {owner};")], warnings: Vec::new() }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn options(owner: &str, original_owner: &str) -> TableOwnerChangeSqlOptions {
|
||||
TableOwnerChangeSqlOptions {
|
||||
database_type: Some(DatabaseType::Postgres),
|
||||
schema: Some("app schema".to_string()),
|
||||
table_name: "order\"items".to_string(),
|
||||
owner: owner.to_string(),
|
||||
original_owner: original_owner.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_quoted_postgres_owner_change() {
|
||||
let result = build_table_owner_change_sql(options("reporting\"role", "app_user"));
|
||||
assert!(result.warnings.is_empty());
|
||||
assert_eq!(
|
||||
result.statements,
|
||||
vec!["ALTER TABLE \"app schema\".\"order\"\"items\" OWNER TO \"reporting\"\"role\";".to_string()]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_whitespace_in_postgres_role_names() {
|
||||
let changed = build_table_owner_change_sql(options(" app_user ", "app_user"));
|
||||
assert!(changed.warnings.is_empty());
|
||||
assert_eq!(
|
||||
changed.statements,
|
||||
vec!["ALTER TABLE \"app schema\".\"order\"\"items\" OWNER TO \" app_user \";".to_string()]
|
||||
);
|
||||
|
||||
let unchanged = build_table_owner_change_sql(options(" app_user ", " app_user "));
|
||||
assert!(unchanged.statements.is_empty());
|
||||
assert!(unchanged.warnings.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_empty_or_unsupported_owner_changes() {
|
||||
let empty = build_table_owner_change_sql(options("", "app_user"));
|
||||
assert!(empty.statements.is_empty());
|
||||
assert_eq!(empty.warnings, vec!["Table owner cannot be empty."]);
|
||||
|
||||
let mut unsupported = options("reporting", "app_user");
|
||||
unsupported.database_type = Some(DatabaseType::Mysql);
|
||||
let unsupported = build_table_owner_change_sql(unsupported);
|
||||
assert!(unsupported.statements.is_empty());
|
||||
assert_eq!(unsupported.warnings, vec!["Changing the table owner is currently supported only for PostgreSQL."]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
use super::create_table::build_create_table_sql_with_partition_clause;
|
||||
use super::dialect::{capabilities_for, StructureDialect};
|
||||
use super::types::{
|
||||
TablePartitionBoundDraft, TablePartitionDefinition, TablePartitionOperation, TablePartitionOperationKind,
|
||||
TablePartitionSqlOptions, TableStructureSqlOptions, TableStructureSqlResult,
|
||||
};
|
||||
use super::util::quote_ident;
|
||||
use crate::models::connection::DatabaseType;
|
||||
use crate::types::PgPartitionKind;
|
||||
|
||||
/// Engines whose declarative-partition DDL has been verified. Only PostgreSQL
|
||||
/// and KingbaseES expose the same `pg_partitioned_table` catalog and
|
||||
/// `CREATE TABLE ... PARTITION OF` / `ATTACH` / `DETACH` syntax. openGauss-based
|
||||
/// engines (openGauss, Vastbase, GaussDB) use a different `pg_partition`
|
||||
/// catalog and are intentionally excluded until validated.
|
||||
fn supports_partition_ddl(database_type: Option<DatabaseType>) -> bool {
|
||||
matches!(database_type, Some(DatabaseType::Postgres | DatabaseType::Kingbase))
|
||||
}
|
||||
|
||||
/// Builds `CREATE TABLE ... PARTITION BY ...` for a table being created.
|
||||
///
|
||||
/// Kept separate from `build_create_table_sql` so the widely-used plain builder
|
||||
/// keeps its signature; shares its implementation through
|
||||
/// `build_create_table_sql_with_partition_clause`.
|
||||
pub fn build_create_partitioned_table_sql(
|
||||
options: TableStructureSqlOptions,
|
||||
definition: TablePartitionDefinition,
|
||||
) -> TableStructureSqlResult {
|
||||
let dialect = capabilities_for(options.database_type, options.driver_profile.as_deref()).dialect;
|
||||
if dialect != StructureDialect::Postgres || !supports_partition_ddl(options.database_type) {
|
||||
return TableStructureSqlResult {
|
||||
statements: Vec::new(),
|
||||
warnings: vec!["Partitioning is not supported for this database engine.".to_string()],
|
||||
};
|
||||
}
|
||||
let mut warnings = Vec::new();
|
||||
let Some(clause) = format_partition_clause(&definition, &mut warnings) else {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings };
|
||||
};
|
||||
build_create_table_sql_with_partition_clause(options, Some(clause))
|
||||
}
|
||||
|
||||
/// Renders the body of a `PARTITION BY` clause (without the leading keyword),
|
||||
/// e.g. `RANGE (region, sold_on)`.
|
||||
fn format_partition_clause(definition: &TablePartitionDefinition, warnings: &mut Vec<String>) -> Option<String> {
|
||||
let keyword = match definition.kind {
|
||||
PgPartitionKind::Range => "RANGE",
|
||||
PgPartitionKind::List => "LIST",
|
||||
PgPartitionKind::Hash => "HASH",
|
||||
};
|
||||
let expression = definition.expression.trim();
|
||||
let key = if !expression.is_empty() {
|
||||
expression.to_string()
|
||||
} else {
|
||||
let columns = definition
|
||||
.columns
|
||||
.iter()
|
||||
.map(|column| column.trim())
|
||||
.filter(|column| !column.is_empty())
|
||||
.collect::<Vec<_>>();
|
||||
if columns.is_empty() {
|
||||
warnings.push("A partition key needs at least one column or an expression.".to_string());
|
||||
return None;
|
||||
}
|
||||
columns.iter().map(|column| quote_ident(StructureDialect::Postgres, column)).collect::<Vec<_>>().join(", ")
|
||||
};
|
||||
Some(format!("PARTITION BY {keyword} ({key})"))
|
||||
}
|
||||
|
||||
/// Builds the DDL for explicit partition maintenance operations
|
||||
/// (create / attach / detach / drop).
|
||||
///
|
||||
/// Gated to the engines in [`supports_partition_ddl`]; a refusal empties the
|
||||
/// statement list and pushes one warning. Callers surface the warning and block
|
||||
/// the save, so an unsupported request never silently degrades.
|
||||
pub fn build_table_partition_operation_sql(options: TablePartitionSqlOptions) -> TableStructureSqlResult {
|
||||
if options.operations.is_empty() {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings: Vec::new() };
|
||||
}
|
||||
let dialect = capabilities_for(options.database_type, options.driver_profile.as_deref()).dialect;
|
||||
if dialect != StructureDialect::Postgres || !supports_partition_ddl(options.database_type) {
|
||||
return TableStructureSqlResult {
|
||||
statements: Vec::new(),
|
||||
warnings: vec!["Partition operations are not supported for this database engine.".to_string()],
|
||||
};
|
||||
}
|
||||
|
||||
let default_schema = options.schema.as_deref().unwrap_or("");
|
||||
let mut warnings = Vec::new();
|
||||
let mut statements = Vec::new();
|
||||
for operation in &options.operations {
|
||||
if let Some(statement) = build_operation_sql(operation, default_schema, &options.table_name, &mut warnings) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
TableStructureSqlResult { statements, warnings }
|
||||
}
|
||||
|
||||
fn build_operation_sql(
|
||||
operation: &TablePartitionOperation,
|
||||
default_schema: &str,
|
||||
default_parent_table: &str,
|
||||
warnings: &mut Vec<String>,
|
||||
) -> Option<String> {
|
||||
let dialect = StructureDialect::Postgres;
|
||||
let parent_schema = pick(&operation.parent_schema, default_schema);
|
||||
let parent_table = pick(&operation.parent_table, default_parent_table);
|
||||
let child_schema = pick(&operation.schema, &parent_schema);
|
||||
let name = operation.name.trim();
|
||||
if parent_table.is_empty() {
|
||||
warnings.push("A partition operation requires a parent table.".to_string());
|
||||
return None;
|
||||
}
|
||||
if name.is_empty() {
|
||||
warnings.push("A partition operation requires a partition name.".to_string());
|
||||
return None;
|
||||
}
|
||||
|
||||
let parent = qualify(dialect, &parent_schema, &parent_table);
|
||||
let child = qualify(dialect, &child_schema, name);
|
||||
match operation.kind {
|
||||
TablePartitionOperationKind::Create => {
|
||||
let bound = require_bound(operation, "create", warnings)?;
|
||||
Some(format!("CREATE TABLE {child} PARTITION OF {parent} {bound};"))
|
||||
}
|
||||
TablePartitionOperationKind::Attach => {
|
||||
let bound = require_bound(operation, "attach", warnings)?;
|
||||
Some(format!("ALTER TABLE {parent} ATTACH PARTITION {child} {bound};"))
|
||||
}
|
||||
TablePartitionOperationKind::Detach => {
|
||||
reject_bound(operation, "detach", warnings)?;
|
||||
let concurrently = if operation.concurrently { " CONCURRENTLY" } else { "" };
|
||||
Some(format!("ALTER TABLE {parent} DETACH PARTITION {child}{concurrently};"))
|
||||
}
|
||||
TablePartitionOperationKind::Drop => {
|
||||
reject_bound(operation, "drop", warnings)?;
|
||||
Some(format!("DROP TABLE {child};"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn require_bound(operation: &TablePartitionOperation, action: &str, warnings: &mut Vec<String>) -> Option<String> {
|
||||
let Some(bound) = operation.bound.as_ref() else {
|
||||
warnings.push(format!("A partition {action} operation requires a bound definition."));
|
||||
return None;
|
||||
};
|
||||
format_bound(bound, warnings)
|
||||
}
|
||||
|
||||
fn reject_bound(operation: &TablePartitionOperation, action: &str, warnings: &mut Vec<String>) -> Option<()> {
|
||||
if operation.bound.is_some() {
|
||||
warnings.push(format!("A partition {action} operation must not carry a bound definition."));
|
||||
return None;
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// Renders a bound as the `FOR VALUES ...` clause PostgreSQL expects (or a bare
|
||||
/// `DEFAULT`). Returns `None` and records a warning when the bound is
|
||||
/// malformed, so the caller can drop just that statement.
|
||||
fn format_bound(bound: &TablePartitionBoundDraft, warnings: &mut Vec<String>) -> Option<String> {
|
||||
match bound {
|
||||
TablePartitionBoundDraft::Default => Some("DEFAULT".to_string()),
|
||||
TablePartitionBoundDraft::Range { from, to } => {
|
||||
if from.is_empty() || from.len() != to.len() {
|
||||
warnings
|
||||
.push("A RANGE partition bound needs a FROM and TO tuple of equal, non-zero length.".to_string());
|
||||
return None;
|
||||
}
|
||||
let from = from.iter().map(|value| value.trim()).collect::<Vec<_>>().join(", ");
|
||||
let to = to.iter().map(|value| value.trim()).collect::<Vec<_>>().join(", ");
|
||||
Some(format!("FOR VALUES FROM ({from}) TO ({to})"))
|
||||
}
|
||||
TablePartitionBoundDraft::List { values } => {
|
||||
if values.is_empty() {
|
||||
warnings.push("A LIST partition bound needs at least one value.".to_string());
|
||||
return None;
|
||||
}
|
||||
let values = values.iter().map(|value| value.trim()).collect::<Vec<_>>().join(", ");
|
||||
Some(format!("FOR VALUES IN ({values})"))
|
||||
}
|
||||
TablePartitionBoundDraft::Hash { modulus, remainder } => {
|
||||
if *modulus <= 0 || *remainder < 0 || remainder >= modulus {
|
||||
warnings.push("A HASH partition bound needs MODULUS > 0 and 0 <= REMAINDER < MODULUS.".to_string());
|
||||
return None;
|
||||
}
|
||||
Some(format!("FOR VALUES WITH (MODULUS {modulus}, REMAINDER {remainder})"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn pick(value: &str, fallback: &str) -> String {
|
||||
let value = value.trim();
|
||||
if value.is_empty() {
|
||||
fallback.trim().to_string()
|
||||
} else {
|
||||
value.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn qualify(dialect: StructureDialect, schema: &str, table: &str) -> String {
|
||||
if schema.is_empty() {
|
||||
quote_ident(dialect, table)
|
||||
} else {
|
||||
format!("{}.{}", quote_ident(dialect, schema), quote_ident(dialect, table))
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,525 @@
|
||||
use super::dialect::{database_label, StructureDialect};
|
||||
use super::types::{EditableStructureTrigger, TableStructureSqlOptions, TriggerInfo};
|
||||
use super::util::{clean, qualified_new_table, qualified_table, quote_ident, quote_new_ident};
|
||||
|
||||
pub(super) fn build_trigger_sql(options: &TableStructureSqlOptions, warnings: &mut Vec<String>) -> Vec<String> {
|
||||
build_trigger_sql_with_mode(options, warnings, false)
|
||||
}
|
||||
|
||||
pub(super) fn build_trigger_sql_for_new_table(
|
||||
options: &TableStructureSqlOptions,
|
||||
warnings: &mut Vec<String>,
|
||||
) -> Vec<String> {
|
||||
build_trigger_sql_with_mode(options, warnings, true)
|
||||
}
|
||||
|
||||
fn build_trigger_sql_with_mode(
|
||||
options: &TableStructureSqlOptions,
|
||||
warnings: &mut Vec<String>,
|
||||
for_new_table: bool,
|
||||
) -> Vec<String> {
|
||||
if options.triggers.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let dialect = super::dialect::capabilities_for(options.database_type, options.driver_profile.as_deref()).dialect;
|
||||
let database_label = database_label(options.database_type);
|
||||
if !matches!(dialect, StructureDialect::Mysql | StructureDialect::Oracle | StructureDialect::SqlServer) {
|
||||
if options.triggers.iter().any(has_trigger_edit) {
|
||||
warnings.push(format!("Editing triggers is not supported for {database_label} from this editor."));
|
||||
}
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let table = if for_new_table {
|
||||
qualified_new_table(options.database_type, dialect, options.schema.as_deref(), &options.table_name)
|
||||
} else {
|
||||
qualified_table(dialect, options.schema.as_deref(), &options.table_name)
|
||||
};
|
||||
let mut statements = Vec::new();
|
||||
|
||||
for trigger in &options.triggers {
|
||||
if trigger.marked_for_drop {
|
||||
if let Some(original) = &trigger.original {
|
||||
statements.push(drop_trigger_sql(dialect, options.schema.as_deref(), &original.name));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(original) = &trigger.original {
|
||||
if dialect == StructureDialect::Oracle {
|
||||
if has_trigger_change(trigger, original) {
|
||||
warnings.push(format!(
|
||||
"Editing existing Oracle trigger \"{}\" requires its complete source definition.",
|
||||
original.name
|
||||
));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if !has_trigger_change(trigger, original) {
|
||||
continue;
|
||||
}
|
||||
// Oracle can replace a trigger in place; renames still require dropping the old object.
|
||||
if dialect != StructureDialect::Oracle || clean(&trigger.name) != clean(&original.name) {
|
||||
statements.push(drop_trigger_sql(dialect, options.schema.as_deref(), &original.name));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(sql) = create_trigger_sql(
|
||||
options.database_type,
|
||||
dialect,
|
||||
options.schema.as_deref(),
|
||||
&table,
|
||||
trigger,
|
||||
warnings,
|
||||
for_new_table,
|
||||
) {
|
||||
statements.push(sql);
|
||||
// SQL Server rebuilds via DROP + CREATE, which resets is_disabled to
|
||||
// enabled; restore the catalog-reported disabled state explicitly.
|
||||
// `table` is already qualified the same way the CREATE's ON clause is.
|
||||
if dialect == StructureDialect::SqlServer
|
||||
&& trigger.original.as_ref().is_some_and(|original| original.enabled == Some(false))
|
||||
{
|
||||
let schema = options.schema.as_deref();
|
||||
let trigger_name = qualified_trigger_object_name(dialect, schema, &trigger.name);
|
||||
statements.push(format!("DISABLE TRIGGER {trigger_name} ON {table};"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
fn qualified_trigger_object_name(dialect: StructureDialect, schema: Option<&str>, name: &str) -> String {
|
||||
if schema.is_some_and(|schema| !schema.trim().is_empty()) {
|
||||
format!("{}.{}", quote_ident(dialect, schema.unwrap()), quote_ident(dialect, name))
|
||||
} else {
|
||||
quote_ident(dialect, name)
|
||||
}
|
||||
}
|
||||
|
||||
fn has_trigger_edit(trigger: &EditableStructureTrigger) -> bool {
|
||||
trigger.marked_for_drop || trigger.original.as_ref().is_none_or(|original| has_trigger_change(trigger, original))
|
||||
}
|
||||
|
||||
fn has_trigger_change(trigger: &EditableStructureTrigger, original: &TriggerInfo) -> bool {
|
||||
clean(&trigger.name) != clean(&original.name)
|
||||
|| normalize_keyword(&trigger.timing) != normalize_keyword(&original.timing)
|
||||
|| normalize_keyword(&trigger.event) != normalize_keyword(&original.event)
|
||||
|| normalize_statement(&trigger.statement) != normalize_statement(original.statement.as_deref().unwrap_or(""))
|
||||
}
|
||||
|
||||
fn drop_trigger_sql(dialect: StructureDialect, schema: Option<&str>, name: &str) -> String {
|
||||
let qualified_name = if schema.is_some_and(|schema| !schema.trim().is_empty()) {
|
||||
format!("{}.{}", quote_ident(dialect, schema.unwrap()), quote_ident(dialect, name))
|
||||
} else {
|
||||
quote_ident(dialect, name)
|
||||
};
|
||||
format!("DROP TRIGGER {qualified_name};")
|
||||
}
|
||||
|
||||
fn create_trigger_sql(
|
||||
database_type: Option<crate::models::connection::DatabaseType>,
|
||||
dialect: StructureDialect,
|
||||
schema: Option<&str>,
|
||||
table: &str,
|
||||
trigger: &EditableStructureTrigger,
|
||||
warnings: &mut Vec<String>,
|
||||
for_new_table: bool,
|
||||
) -> Option<String> {
|
||||
let name = clean(&trigger.name);
|
||||
let timing = normalize_keyword(&trigger.timing);
|
||||
let event = clean(&trigger.event);
|
||||
let statement = clean(&trigger.statement);
|
||||
|
||||
if name.is_empty() || timing.is_empty() || event.is_empty() || statement.is_empty() {
|
||||
warnings.push("Trigger name, timing, event, and statement are required.".to_string());
|
||||
return None;
|
||||
}
|
||||
match dialect {
|
||||
StructureDialect::Mysql => create_mysql_trigger_sql(table, &name, &timing, &event, &statement, warnings),
|
||||
StructureDialect::SqlServer => {
|
||||
create_sqlserver_trigger_sql(schema, table, &name, &timing, &event, &statement, warnings)
|
||||
}
|
||||
StructureDialect::Oracle => create_oracle_trigger_sql(
|
||||
database_type,
|
||||
schema,
|
||||
table,
|
||||
&name,
|
||||
&timing,
|
||||
&event,
|
||||
&statement,
|
||||
warnings,
|
||||
for_new_table,
|
||||
),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn create_sqlserver_trigger_sql(
|
||||
schema: Option<&str>,
|
||||
table: &str,
|
||||
name: &str,
|
||||
timing: &str,
|
||||
event: &str,
|
||||
statement: &str,
|
||||
warnings: &mut Vec<String>,
|
||||
) -> Option<String> {
|
||||
if !matches!(timing, "AFTER" | "INSTEAD OF") {
|
||||
warnings.push(format!("Unsupported SQL Server trigger timing \"{timing}\"."));
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut events = Vec::new();
|
||||
for item in event.split([',', '\n']) {
|
||||
let item = item.trim();
|
||||
if item.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let normalized_item = item.to_ascii_uppercase();
|
||||
for event in normalized_item.split(" OR ") {
|
||||
let event = event.trim().to_string();
|
||||
if !matches!(event.as_str(), "INSERT" | "UPDATE" | "DELETE") {
|
||||
warnings.push(format!("Unsupported SQL Server trigger event \"{}\".", item));
|
||||
return None;
|
||||
}
|
||||
if !events.contains(&event) {
|
||||
events.push(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
if events.is_empty() {
|
||||
warnings.push("SQL Server trigger event is required.".to_string());
|
||||
return None;
|
||||
}
|
||||
|
||||
let trigger_name = if schema.is_some_and(|schema| !schema.trim().is_empty()) {
|
||||
format!(
|
||||
"{}.{}",
|
||||
quote_ident(StructureDialect::SqlServer, schema.unwrap()),
|
||||
quote_ident(StructureDialect::SqlServer, name)
|
||||
)
|
||||
} else {
|
||||
quote_ident(StructureDialect::SqlServer, name)
|
||||
};
|
||||
let body = sqlserver_trigger_body(statement);
|
||||
if body.is_empty() {
|
||||
warnings.push("SQL Server trigger statement is required.".to_string());
|
||||
return None;
|
||||
}
|
||||
Some(format!(
|
||||
"CREATE TRIGGER {trigger_name} ON {table} {timing} {} AS\n{};",
|
||||
events.join(", "),
|
||||
body.trim_end_matches(';').trim_end()
|
||||
))
|
||||
}
|
||||
|
||||
fn sqlserver_trigger_body(statement: &str) -> &str {
|
||||
let statement = statement.trim();
|
||||
if statement.get(..2).is_some_and(|prefix| prefix.eq_ignore_ascii_case("AS")) {
|
||||
return statement[2..].trim_start();
|
||||
}
|
||||
if !statement.to_ascii_uppercase().starts_with("CREATE TRIGGER") {
|
||||
return statement;
|
||||
}
|
||||
|
||||
// The body separator is the standalone AS that follows the event list
|
||||
// (`... AFTER INSERT, UPDATE AS <body>`); earlier AS tokens can appear in
|
||||
// trigger options such as `WITH EXECUTE AS OWNER`, so the first standalone
|
||||
// AS alone is not a reliable split point.
|
||||
let mut seen_event = false;
|
||||
let mut token = String::new();
|
||||
let mut char_indices = statement.char_indices().peekable();
|
||||
while let Some((index, ch)) = char_indices.next() {
|
||||
if ch.is_ascii_alphanumeric() || ch == '_' {
|
||||
token.push(ch);
|
||||
if !char_indices.peek().is_some_and(|(_, next)| next.is_ascii_alphanumeric() || *next == '_') {
|
||||
if token.eq_ignore_ascii_case("AS") && seen_event {
|
||||
return statement[index + 1..].trim_start();
|
||||
}
|
||||
if matches!(token.to_ascii_uppercase().as_str(), "INSERT" | "UPDATE" | "DELETE" | "LOGON") {
|
||||
seen_event = true;
|
||||
}
|
||||
token.clear();
|
||||
}
|
||||
} else {
|
||||
token.clear();
|
||||
}
|
||||
}
|
||||
statement
|
||||
}
|
||||
|
||||
fn create_mysql_trigger_sql(
|
||||
table: &str,
|
||||
name: &str,
|
||||
timing: &str,
|
||||
event: &str,
|
||||
statement: &str,
|
||||
warnings: &mut Vec<String>,
|
||||
) -> Option<String> {
|
||||
let event = normalize_keyword(event);
|
||||
if !matches!(timing, "BEFORE" | "AFTER") {
|
||||
warnings.push(format!("Unsupported trigger timing \"{timing}\"."));
|
||||
return None;
|
||||
}
|
||||
if !matches!(event.as_str(), "INSERT" | "UPDATE" | "DELETE") {
|
||||
warnings.push(format!("Unsupported trigger event \"{}\".", clean(&event)));
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(format!(
|
||||
"CREATE TRIGGER {} {timing} {event} ON {table} FOR EACH ROW\n{};",
|
||||
quote_ident(StructureDialect::Mysql, name),
|
||||
statement.trim_end_matches(';').trim_end()
|
||||
))
|
||||
}
|
||||
|
||||
fn create_oracle_trigger_sql(
|
||||
database_type: Option<crate::models::connection::DatabaseType>,
|
||||
schema: Option<&str>,
|
||||
table: &str,
|
||||
name: &str,
|
||||
timing: &str,
|
||||
event: &str,
|
||||
statement: &str,
|
||||
warnings: &mut Vec<String>,
|
||||
for_new_table: bool,
|
||||
) -> Option<String> {
|
||||
let Some((timing_clause, row_level)) = oracle_trigger_timing(timing) else {
|
||||
warnings.push(format!("Unsupported Oracle trigger timing \"{timing}\"."));
|
||||
return None;
|
||||
};
|
||||
if !is_supported_oracle_trigger_event(event) {
|
||||
warnings.push(format!("Unsupported Oracle trigger event \"{}\".", clean(event)));
|
||||
return None;
|
||||
}
|
||||
|
||||
let trigger_identifier = if for_new_table {
|
||||
quote_new_ident(database_type, StructureDialect::Oracle, name)
|
||||
} else {
|
||||
quote_ident(StructureDialect::Oracle, name)
|
||||
};
|
||||
let trigger_name = if schema.is_some_and(|schema| !schema.trim().is_empty()) {
|
||||
format!("{}.{}", quote_ident(StructureDialect::Oracle, schema.unwrap()), trigger_identifier)
|
||||
} else {
|
||||
trigger_identifier
|
||||
};
|
||||
let row_clause = if row_level { "\nFOR EACH ROW" } else { "" };
|
||||
let statement = oracle_trigger_body(statement);
|
||||
if starts_with_trigger_declaration(statement) {
|
||||
// Oracle triggers are edited through their complete source definition, so a declaration we
|
||||
// could not strip means dumping the raw statement after our own `FOR EACH ROW` would send
|
||||
// invalid DDL to the server (ORA-04079) and abort the clone half way.
|
||||
warnings.push(format!(
|
||||
"Trigger \"{name}\" could not be cloned automatically; its stored source is not a plain PL/SQL body. Recreate it manually on {table}."
|
||||
));
|
||||
return None;
|
||||
}
|
||||
let statement = statement.trim_end().trim_end_matches('/').trim_end().trim_end_matches(';').trim_end();
|
||||
if statement.is_empty() {
|
||||
warnings.push(format!("Trigger \"{name}\" has an empty body and was skipped."));
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(format!(
|
||||
"CREATE OR REPLACE TRIGGER {trigger_name} {timing_clause} {event} ON {table}{row_clause}\n{statement};"
|
||||
))
|
||||
}
|
||||
|
||||
/// Oracle triggers come back from the driver as `ALL_SOURCE` text. When the dictionary declaration
|
||||
/// and the stored source cannot be aligned line by line — a trigger written on a single line — the
|
||||
/// raw source is returned instead of the body, and appending it after our own `FOR EACH ROW`
|
||||
/// produces invalid DDL (`ORA-04079`). Strip the declaration the same way `sqlserver_trigger_body`
|
||||
/// strips `CREATE TRIGGER ... AS`.
|
||||
fn oracle_trigger_body(statement: &str) -> &str {
|
||||
let statement = statement.trim();
|
||||
if !starts_with_trigger_declaration(statement) {
|
||||
return statement;
|
||||
}
|
||||
match oracle_trigger_body_start(statement) {
|
||||
Some(index) => statement[index..].trim_end(),
|
||||
None => statement,
|
||||
}
|
||||
}
|
||||
|
||||
fn starts_with_trigger_declaration(statement: &str) -> bool {
|
||||
let keywords = leading_keywords(statement, 4);
|
||||
match keywords.first().map(String::as_str) {
|
||||
Some("TRIGGER") => true,
|
||||
Some("CREATE") => keywords.iter().any(|keyword| keyword == "TRIGGER"),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn leading_keywords(statement: &str, limit: usize) -> Vec<String> {
|
||||
let mut keywords = Vec::new();
|
||||
let mut current = String::new();
|
||||
for ch in statement.chars() {
|
||||
if ch.is_ascii_alphanumeric() || ch == '_' {
|
||||
current.push(ch.to_ascii_uppercase());
|
||||
continue;
|
||||
}
|
||||
if !current.is_empty() {
|
||||
keywords.push(std::mem::take(&mut current));
|
||||
if keywords.len() == limit {
|
||||
return keywords;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !current.is_empty() && keywords.len() < limit {
|
||||
keywords.push(current);
|
||||
}
|
||||
keywords
|
||||
}
|
||||
|
||||
/// Returns the offset where the PL/SQL block of an Oracle trigger declaration starts, skipping
|
||||
/// string literals, quoted identifiers, and comments so a `WHEN` condition cannot be mistaken for
|
||||
/// the body. The declaration always closes with the `ON <table>` clause before the body begins.
|
||||
fn oracle_trigger_body_start(statement: &str) -> Option<usize> {
|
||||
let bytes = statement.as_bytes();
|
||||
let mut index = 0;
|
||||
let mut seen_on = false;
|
||||
let mut word_start: Option<usize> = None;
|
||||
while index < bytes.len() {
|
||||
let ch = bytes[index] as char;
|
||||
if ch == '\'' || ch == '"' {
|
||||
index += 1;
|
||||
while index < bytes.len() {
|
||||
if bytes[index] as char == ch {
|
||||
index += 1;
|
||||
break;
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
word_start = None;
|
||||
continue;
|
||||
}
|
||||
if ch == '-' && bytes.get(index + 1) == Some(&b'-') {
|
||||
while index < bytes.len() && bytes[index] != b'\n' {
|
||||
index += 1;
|
||||
}
|
||||
word_start = None;
|
||||
continue;
|
||||
}
|
||||
if ch == '/' && bytes.get(index + 1) == Some(&b'*') {
|
||||
index += 2;
|
||||
while index + 1 < bytes.len() && !(bytes[index] == b'*' && bytes[index + 1] == b'/') {
|
||||
index += 1;
|
||||
}
|
||||
index = (index + 2).min(bytes.len());
|
||||
word_start = None;
|
||||
continue;
|
||||
}
|
||||
if ch.is_ascii_alphanumeric() || ch == '_' {
|
||||
word_start.get_or_insert(index);
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if let Some(start) = word_start.take() {
|
||||
let word = &statement[start..index];
|
||||
if is_oracle_trigger_body_keyword(word, seen_on) {
|
||||
return Some(start);
|
||||
}
|
||||
if word.eq_ignore_ascii_case("ON") {
|
||||
seen_on = true;
|
||||
}
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
if let Some(start) = word_start {
|
||||
let word = &statement[start..];
|
||||
if is_oracle_trigger_body_keyword(word, seen_on) {
|
||||
return Some(start);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn is_oracle_trigger_body_keyword(word: &str, seen_on: bool) -> bool {
|
||||
seen_on
|
||||
&& (word.eq_ignore_ascii_case("DECLARE")
|
||||
|| word.eq_ignore_ascii_case("BEGIN")
|
||||
|| word.eq_ignore_ascii_case("CALL"))
|
||||
}
|
||||
|
||||
fn oracle_trigger_timing(timing: &str) -> Option<(&'static str, bool)> {
|
||||
match timing {
|
||||
"BEFORE" | "BEFORE EACH ROW" => Some(("BEFORE", true)),
|
||||
"AFTER" | "AFTER EACH ROW" => Some(("AFTER", true)),
|
||||
"INSTEAD OF" | "INSTEAD OF EACH ROW" => Some(("INSTEAD OF", true)),
|
||||
"BEFORE STATEMENT" => Some(("BEFORE", false)),
|
||||
"AFTER STATEMENT" => Some(("AFTER", false)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_supported_oracle_trigger_event(event: &str) -> bool {
|
||||
if event.contains([';', '\n', '\r']) || event.contains("--") || event.contains("/*") {
|
||||
return false;
|
||||
}
|
||||
let upper = event.to_ascii_uppercase();
|
||||
let clauses: Vec<&str> = upper.split(" OR ").map(str::trim).collect();
|
||||
!clauses.is_empty()
|
||||
&& clauses.iter().all(|clause| {
|
||||
matches!(*clause, "INSERT" | "DELETE")
|
||||
|| *clause == "UPDATE"
|
||||
|| clause.strip_prefix("UPDATE OF ").is_some_and(|columns| !columns.trim().is_empty())
|
||||
})
|
||||
}
|
||||
|
||||
fn normalize_keyword(value: &str) -> String {
|
||||
clean(value).to_ascii_uppercase()
|
||||
}
|
||||
|
||||
fn normalize_statement(value: &str) -> String {
|
||||
clean(value).trim_end_matches(';').trim().to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sqlserver_trigger_body_splits_after_event_list_not_execute_as() {
|
||||
// The AS inside `WITH EXECUTE AS OWNER` must not be treated as the body separator.
|
||||
let source = "CREATE TRIGGER dbo.t ON dbo.tbl WITH EXECUTE AS OWNER AFTER INSERT AS PRINT 'x'";
|
||||
assert_eq!(sqlserver_trigger_body(source), "PRINT 'x'");
|
||||
let no_options = "CREATE TRIGGER dbo.t ON dbo.tbl AFTER UPDATE AS SELECT 1 AS a";
|
||||
assert_eq!(sqlserver_trigger_body(no_options), "SELECT 1 AS a");
|
||||
let leading_as = "AS SELECT 2";
|
||||
assert_eq!(sqlserver_trigger_body(leading_as), "SELECT 2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oracle_trigger_body_strips_single_line_dictionary_source() {
|
||||
// Oracle stores a trigger written on one line with its declaration and body on the same
|
||||
// line, so the driver cannot align ALL_SOURCE with the dictionary DESCRIPTION and returns
|
||||
// the whole statement. t8y2/dbx#9731.
|
||||
let source = "trigger dbx_v1_trg_bi before insert on dbx_v1_trg for each row begin null; end;";
|
||||
assert_eq!(oracle_trigger_body(source), "begin null; end;");
|
||||
|
||||
let with_when = "TRIGGER audit_trg BEFORE INSERT ON \"HR\".\"ORDERS\" FOR EACH ROW WHEN (NEW.STATUS <> 'BEGIN') BEGIN NULL; END;";
|
||||
assert_eq!(oracle_trigger_body(with_when), "BEGIN NULL; END;");
|
||||
|
||||
let full_ddl = "CREATE OR REPLACE TRIGGER \"HR\".\"AUDIT_TRG\" BEFORE INSERT ON \"HR\".\"ORDERS\" FOR EACH ROW\nDECLARE\n v NUMBER;\nBEGIN\n NULL;\nEND;";
|
||||
assert_eq!(oracle_trigger_body(full_ddl), "DECLARE\n v NUMBER;\nBEGIN\n NULL;\nEND;");
|
||||
|
||||
let call_trigger = "TRIGGER call_trg BEFORE INSERT ON HR.ORDERS FOR EACH ROW CALL log_insert()";
|
||||
assert_eq!(oracle_trigger_body(call_trigger), "CALL log_insert()");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oracle_trigger_body_keeps_plain_bodies_untouched() {
|
||||
let body = "DECLARE\n v NUMBER;\nBEGIN\n v := 1; -- ON BEGIN\nEND;";
|
||||
assert_eq!(oracle_trigger_body(body), body);
|
||||
|
||||
// A body that merely mentions the keywords must not be truncated.
|
||||
let literal = "BEGIN\n INSERT INTO log_v2 VALUES ('on', 'begin');\nEND;";
|
||||
assert_eq!(oracle_trigger_body(literal), literal);
|
||||
|
||||
// Without the ON clause the declaration cannot be split safely, so the source is kept.
|
||||
let unsplittable = "TRIGGER odd_trg COMPOUND TRIGGER";
|
||||
assert_eq!(oracle_trigger_body(unsplittable), unsplittable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::models::connection::DatabaseType;
|
||||
use crate::types::PgPartitionKind;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EditableStructureColumn {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub data_type: String,
|
||||
pub is_nullable: bool,
|
||||
#[serde(default)]
|
||||
pub default_value: String,
|
||||
#[serde(default)]
|
||||
pub comment: String,
|
||||
#[serde(default)]
|
||||
pub is_primary_key: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub extra: Option<ColumnExtra>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub original: Option<ColumnInfo>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub original_position: Option<usize>,
|
||||
#[serde(default)]
|
||||
pub marked_for_drop: bool,
|
||||
#[serde(default)]
|
||||
pub character_set: String,
|
||||
#[serde(default)]
|
||||
pub collation: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ColumnExtra {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub auto_increment: Option<bool>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub on_update_current_timestamp: Option<bool>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub identity: Option<ColumnIdentity>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub manticore_indexed: Option<bool>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub manticore_stored: Option<bool>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub manticore_attribute: Option<bool>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub manticore_secondary_index: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ColumnIdentity {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub generation: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub seed: Option<i64>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub increment: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
|
||||
pub struct ColumnInfo {
|
||||
pub name: String,
|
||||
pub data_type: String,
|
||||
pub is_nullable: bool,
|
||||
pub column_default: Option<String>,
|
||||
#[serde(default)]
|
||||
pub is_primary_key: bool,
|
||||
#[serde(default)]
|
||||
pub extra: Option<String>,
|
||||
#[serde(default)]
|
||||
pub comment: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub character_set: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub collation: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EditableStructureIndex {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub columns: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub is_unique: bool,
|
||||
#[serde(default)]
|
||||
pub is_primary: bool,
|
||||
#[serde(default)]
|
||||
pub filter: String,
|
||||
#[serde(default)]
|
||||
pub index_type: String,
|
||||
#[serde(default)]
|
||||
pub included_columns: Vec<String>,
|
||||
/// Parallel to `columns`: operator class for each key column (PostgreSQL).
|
||||
/// `None` means default operator class. The UI keeps this array in lockstep
|
||||
/// with `columns`; when empty, opclasses fall back to `original` matching.
|
||||
#[serde(default)]
|
||||
pub column_opclasses: Vec<Option<String>>,
|
||||
#[serde(default)]
|
||||
pub comment: String,
|
||||
#[serde(default)]
|
||||
pub concurrently: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub original: Option<IndexInfo>,
|
||||
#[serde(default)]
|
||||
pub marked_for_drop: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct IndexInfo {
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub columns: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub is_unique: bool,
|
||||
#[serde(default)]
|
||||
pub is_primary: bool,
|
||||
#[serde(default)]
|
||||
pub filter: Option<String>,
|
||||
#[serde(default)]
|
||||
pub index_type: Option<String>,
|
||||
#[serde(default)]
|
||||
pub included_columns: Option<Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub comment: Option<String>,
|
||||
/// Parallel to `columns`: `true` at index `i` means `columns[i]` is a raw expression
|
||||
/// (e.g. sourced from `pg_get_indexdef`), not a plain column name.
|
||||
#[serde(default)]
|
||||
pub key_is_expression: Vec<bool>,
|
||||
/// Parallel to `columns`: operator class name for each key column, if non-default.
|
||||
#[serde(default)]
|
||||
pub column_opclasses: Vec<Option<String>>,
|
||||
/// Round-tripped from `crate::types::IndexInfo`: `true` when the introspected index is
|
||||
/// the object behind a PRIMARY KEY / UNIQUE constraint. Dameng only accepts constraint
|
||||
/// level DDL for those (#7959); a standalone unique index keeps the index-level path.
|
||||
/// Defaults to `false`, so a payload from an older client (or a source that does not
|
||||
/// report it) behaves exactly as before.
|
||||
#[serde(default)]
|
||||
pub constraint_backed: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EditableStructureForeignKey {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub column: String,
|
||||
#[serde(default)]
|
||||
pub ref_schema: String,
|
||||
#[serde(default)]
|
||||
pub ref_table: String,
|
||||
#[serde(default)]
|
||||
pub ref_column: String,
|
||||
#[serde(default)]
|
||||
pub on_update: String,
|
||||
#[serde(default)]
|
||||
pub on_delete: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub original: Option<ForeignKeyInfo>,
|
||||
#[serde(default)]
|
||||
pub marked_for_drop: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ForeignKeyInfo {
|
||||
pub name: String,
|
||||
pub column: String,
|
||||
#[serde(default)]
|
||||
pub ref_schema: Option<String>,
|
||||
pub ref_table: String,
|
||||
pub ref_column: String,
|
||||
#[serde(default)]
|
||||
pub on_update: Option<String>,
|
||||
#[serde(default)]
|
||||
pub on_delete: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EditableStructureTrigger {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub timing: String,
|
||||
#[serde(default)]
|
||||
pub event: String,
|
||||
#[serde(default)]
|
||||
pub statement: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub original: Option<TriggerInfo>,
|
||||
#[serde(default)]
|
||||
pub marked_for_drop: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct TriggerInfo {
|
||||
pub name: String,
|
||||
pub event: String,
|
||||
pub timing: String,
|
||||
#[serde(default)]
|
||||
pub statement: Option<String>,
|
||||
/// Carries the catalog-reported enabled state so SQL Server edits can
|
||||
/// restore it after the DROP + CREATE rebuild (`DISABLE TRIGGER`).
|
||||
#[serde(default)]
|
||||
pub enabled: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TableStructureSqlOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
/// Driver profile reported by the connection (e.g. `"gbase8s"`). GBase 8s
|
||||
/// is Informix-compatible rather than MySQL-compatible like the rest of
|
||||
/// the `Gbase` family, so this disambiguates which dialect to generate.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub driver_profile: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub schema: Option<String>,
|
||||
pub table_name: String,
|
||||
#[serde(default)]
|
||||
pub columns: Vec<EditableStructureColumn>,
|
||||
#[serde(default)]
|
||||
pub indexes: Vec<EditableStructureIndex>,
|
||||
#[serde(default)]
|
||||
pub foreign_keys: Vec<EditableStructureForeignKey>,
|
||||
#[serde(default)]
|
||||
pub triggers: Vec<EditableStructureTrigger>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub table_comment: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub original_table_comment: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub mysql_engine: Option<String>,
|
||||
/// MySQL only: the table's current default collation
|
||||
/// (`information_schema.TABLES.TABLE_COLLATION`). A column whose collation
|
||||
/// merely matches it inherits the table default, so its `CHARACTER SET` /
|
||||
/// `COLLATE` clauses are redundant and are dropped from the generated DDL.
|
||||
/// Introspection keeps reporting the column's real values, which is what
|
||||
/// the structure editor renders in its charset/collation pickers.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub table_collation: Option<String>,
|
||||
/// Whether the target table is a partitioned parent table (PostgreSQL
|
||||
/// `relkind = 'p'`). PostgreSQL rejects `CREATE INDEX CONCURRENTLY` on
|
||||
/// partitioned parents, so the builder refuses such a request up front
|
||||
/// (`validate_concurrent_index_scope`) instead of emitting SQL the server
|
||||
/// will reject or downgrading to a blocking `CREATE INDEX`.
|
||||
#[serde(default)]
|
||||
pub partitioned: bool,
|
||||
/// When true, the connection is GaussDB M-mode which uses MySQL-compatible
|
||||
/// SQL dialect with backtick quoting. The structure editor maps this to
|
||||
/// `StructureDialect::Mysql` so that DDL is generated with MySQL syntax.
|
||||
#[serde(default)]
|
||||
pub is_gaussdb_m_mode: bool,
|
||||
}
|
||||
|
||||
/// Options for `build_table_partition_operation_sql`.
|
||||
///
|
||||
/// Partition maintenance is deliberately kept out of `TableStructureSqlOptions`:
|
||||
/// the operations are explicit user actions (never diffed against introspected
|
||||
/// state), and keeping them separate means a column/index draft cannot
|
||||
/// accidentally trigger or suppress partition DDL.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TablePartitionSqlOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub driver_profile: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub schema: Option<String>,
|
||||
/// The edited table; also the default parent of every operation.
|
||||
pub table_name: String,
|
||||
#[serde(default)]
|
||||
pub operations: Vec<TablePartitionOperation>,
|
||||
}
|
||||
|
||||
/// Declarative partitioning for a table being created (`CREATE TABLE ...
|
||||
/// PARTITION BY ...`). PostgreSQL only.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TablePartitionDefinition {
|
||||
pub kind: PgPartitionKind,
|
||||
/// Partition-key columns, used when `expression` is empty.
|
||||
#[serde(default)]
|
||||
pub columns: Vec<String>,
|
||||
/// Raw partition-key expression (e.g. `date_trunc('month', ts)`), used
|
||||
/// instead of `columns` when non-empty.
|
||||
#[serde(default)]
|
||||
pub expression: String,
|
||||
}
|
||||
|
||||
/// One partition maintenance operation. The parent is the edited table unless
|
||||
/// `parent_table` names another relation (used when detaching the partition
|
||||
/// currently being edited).
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TablePartitionOperation {
|
||||
#[serde(default)]
|
||||
pub id: String,
|
||||
pub kind: TablePartitionOperationKind,
|
||||
/// Partitioned parent's schema; empty falls back to the edited table's schema.
|
||||
#[serde(default)]
|
||||
pub parent_schema: String,
|
||||
/// Partitioned parent; empty falls back to the edited table.
|
||||
#[serde(default)]
|
||||
pub parent_table: String,
|
||||
/// Schema of the partition relation; empty falls back to the parent's schema.
|
||||
#[serde(default)]
|
||||
pub schema: String,
|
||||
pub name: String,
|
||||
/// Required for `create`/`attach`; must be absent for `detach`/`drop`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub bound: Option<TablePartitionBoundDraft>,
|
||||
/// Emit `DETACH PARTITION ... CONCURRENTLY` (PostgreSQL 14+).
|
||||
#[serde(default)]
|
||||
pub concurrently: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum TablePartitionOperationKind {
|
||||
Create,
|
||||
Attach,
|
||||
Detach,
|
||||
Drop,
|
||||
}
|
||||
|
||||
/// Partition bound as entered in the UI. Values are SQL literal text; the
|
||||
/// builder only wraps them in the PostgreSQL `FOR VALUES` grammar, so `1`,
|
||||
/// `'2024-01-01'`, `MINVALUE`, and `MAXVALUE` all round-trip unchanged.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", tag = "kind")]
|
||||
pub enum TablePartitionBoundDraft {
|
||||
Range { from: Vec<String>, to: Vec<String> },
|
||||
List { values: Vec<String> },
|
||||
Hash { modulus: i32, remainder: i32 },
|
||||
Default,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TableStructureSqlResult {
|
||||
pub statements: Vec<String>,
|
||||
pub warnings: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TableOwnerChangeSqlOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub schema: Option<String>,
|
||||
pub table_name: String,
|
||||
#[serde(default)]
|
||||
pub owner: String,
|
||||
#[serde(default)]
|
||||
pub original_owner: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SqliteTableStructurePreview {
|
||||
pub statements: Vec<String>,
|
||||
pub warnings: Vec<String>,
|
||||
pub schema_revision: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SingleColumnAlterSqlOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub driver_profile: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub schema: Option<String>,
|
||||
pub table_name: String,
|
||||
pub column: EditableStructureColumn,
|
||||
}
|
||||
@@ -0,0 +1,463 @@
|
||||
use super::column_format::mysql_temporal_precision;
|
||||
use super::dialect::StructureDialect;
|
||||
use super::types::EditableStructureColumn;
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
/// Dialects whose qualified names use `schema.table` when a non-empty schema is present.
|
||||
fn is_schema_qualifying_dialect(dialect: StructureDialect) -> bool {
|
||||
matches!(
|
||||
dialect,
|
||||
StructureDialect::Postgres
|
||||
| StructureDialect::Oracle
|
||||
| StructureDialect::Dameng
|
||||
| StructureDialect::Oscar
|
||||
| StructureDialect::SqlServer
|
||||
| StructureDialect::H2
|
||||
| StructureDialect::Informix
|
||||
| StructureDialect::Sqlite
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn qualified_table(dialect: StructureDialect, schema: Option<&str>, table_name: &str) -> String {
|
||||
if is_schema_qualifying_dialect(dialect) && schema.is_some_and(|schema| !schema.trim().is_empty()) {
|
||||
return format!("{}.{}", quote_ident(dialect, schema.unwrap()), quote_ident(dialect, table_name));
|
||||
}
|
||||
quote_ident(dialect, table_name)
|
||||
}
|
||||
|
||||
/// Qualify a table being created while preserving the schema as an existing object.
|
||||
///
|
||||
/// Oracle's ordinary identifiers are case-insensitive and are folded to uppercase. The
|
||||
/// regular `qualified_table`/`quote_ident` pair must remain exact because it is also used
|
||||
/// for tables already loaded from metadata, including quoted mixed-case names.
|
||||
pub(super) fn qualified_new_table(
|
||||
database_type: Option<DatabaseType>,
|
||||
dialect: StructureDialect,
|
||||
schema: Option<&str>,
|
||||
table_name: &str,
|
||||
) -> String {
|
||||
if is_schema_qualifying_dialect(dialect) && schema.is_some_and(|schema| !schema.trim().is_empty()) {
|
||||
return format!(
|
||||
"{}.{}",
|
||||
quote_ident(dialect, schema.unwrap()),
|
||||
quote_new_ident(database_type, dialect, table_name)
|
||||
);
|
||||
}
|
||||
quote_new_ident(database_type, dialect, table_name)
|
||||
}
|
||||
|
||||
pub(super) fn quote_ident(dialect: StructureDialect, name: &str) -> String {
|
||||
match dialect {
|
||||
StructureDialect::Mysql
|
||||
| StructureDialect::Doris
|
||||
| StructureDialect::GaussdbM
|
||||
| StructureDialect::ManticoreSearch
|
||||
| StructureDialect::Questdb => {
|
||||
format!("`{}`", name.replace('`', "``"))
|
||||
}
|
||||
StructureDialect::SqlServer => format!("[{}]", name.replace(']', "]]")),
|
||||
StructureDialect::Informix if is_simple_informix_identifier(name) => name.to_string(),
|
||||
_ => format!("\"{}\"", name.replace('"', "\"\"")),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_simple_informix_identifier(name: &str) -> bool {
|
||||
let mut chars = name.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return false;
|
||||
};
|
||||
(first.is_ascii_alphabetic() || first == '_')
|
||||
&& chars.all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '$')
|
||||
}
|
||||
|
||||
/// Format an identifier that will be created by a CREATE/ADD statement.
|
||||
///
|
||||
/// This is deliberately separate from `quote_ident`: metadata-backed DDL must preserve the
|
||||
/// exact spelling of an existing quoted Oracle object, while a newly entered ordinary name
|
||||
/// should use Oracle's normal case-insensitive identifier semantics.
|
||||
pub(super) fn quote_new_ident(database_type: Option<DatabaseType>, dialect: StructureDialect, name: &str) -> String {
|
||||
if database_type == Some(DatabaseType::Oracle) && dialect == StructureDialect::Oracle {
|
||||
oracle_new_object_reference(name)
|
||||
} else {
|
||||
quote_ident(dialect, name)
|
||||
}
|
||||
}
|
||||
|
||||
/// Reference spelling that resolves to an object created through [`quote_new_ident`]'s Oracle path.
|
||||
///
|
||||
/// Plain Oracle identifiers are created unquoted and are therefore stored uppercase-folded; a
|
||||
/// later reference with the same unquoted spelling folds to the same object. Names that had to
|
||||
/// stay quoted at creation (special characters, reserved words) keep exact quoting here too, so
|
||||
/// generated references always line up with the created object.
|
||||
pub fn oracle_new_object_reference(name: &str) -> String {
|
||||
if is_simple_oracle_identifier(name) && !is_oracle_reserved_identifier(name) {
|
||||
name.to_string()
|
||||
} else {
|
||||
quote_ident(StructureDialect::Oracle, name)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_simple_oracle_identifier(name: &str) -> bool {
|
||||
let mut chars = name.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return false;
|
||||
};
|
||||
first.is_ascii_alphabetic() && chars.all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '$' || ch == '#')
|
||||
}
|
||||
|
||||
// sqlparser maintains the shared SQL keyword vocabulary used by this crate. It intentionally
|
||||
// includes more than Oracle's reserved words, so the conservative check is supplemented only
|
||||
// with Oracle-specific legacy words missing from that maintained list instead of copying a
|
||||
// separate several-hundred-entry keyword table here.
|
||||
const ORACLE_RESERVED_WORDS_MISSING_FROM_SQLPARSER: &[&str] =
|
||||
&["MAXEXTENTS", "NOAUDIT", "ROWID", "ROWNUM", "SUCCESSFUL", "UID"];
|
||||
|
||||
fn is_oracle_reserved_identifier(name: &str) -> bool {
|
||||
sqlparser::keywords::ALL_KEYWORDS.iter().any(|keyword| keyword.eq_ignore_ascii_case(name))
|
||||
|| ORACLE_RESERVED_WORDS_MISSING_FROM_SQLPARSER.iter().any(|keyword| keyword.eq_ignore_ascii_case(name))
|
||||
}
|
||||
|
||||
pub(super) fn quote_string(value: &str) -> String {
|
||||
format!("'{}'", value.replace('\'', "''"))
|
||||
}
|
||||
|
||||
fn is_sql_string_literal(value: &str) -> bool {
|
||||
let trimmed = value.trim();
|
||||
let Some(inner) = trimmed.strip_prefix('\'').and_then(|value| value.strip_suffix('\'')) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
let mut chars = inner.chars().peekable();
|
||||
while let Some(ch) = chars.next() {
|
||||
if ch == '\'' && chars.next_if_eq(&'\'').is_none() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn sqlserver_unicode_string_literal(value: &str) -> Option<String> {
|
||||
let mut candidate = value.trim();
|
||||
let mut parenthesis_depth = 0usize;
|
||||
|
||||
loop {
|
||||
if let Some(literal) = candidate.strip_prefix('N').or_else(|| candidate.strip_prefix('n')) {
|
||||
if is_sql_string_literal(literal) {
|
||||
return Some(format!("{}N{}{}", "(".repeat(parenthesis_depth), literal, ")".repeat(parenthesis_depth)));
|
||||
}
|
||||
}
|
||||
if is_sql_string_literal(candidate) {
|
||||
return Some(format!("{}N{}{}", "(".repeat(parenthesis_depth), candidate, ")".repeat(parenthesis_depth)));
|
||||
}
|
||||
|
||||
let inner = candidate.strip_prefix('(')?.strip_suffix(')')?;
|
||||
parenthesis_depth += 1;
|
||||
candidate = inner.trim();
|
||||
}
|
||||
}
|
||||
|
||||
fn postgres_string_default_literal(value: &str) -> Option<&str> {
|
||||
let trimmed = value.trim();
|
||||
let inner = trimmed.strip_prefix('\'')?;
|
||||
let mut literal_end = 1;
|
||||
let mut chars = inner.char_indices().peekable();
|
||||
while let Some((index, ch)) = chars.next() {
|
||||
if ch == '\'' {
|
||||
if chars.next_if(|(_, next)| *next == '\'').is_some() {
|
||||
continue;
|
||||
}
|
||||
literal_end += index + ch.len_utf8();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if literal_end == 1 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let literal = &trimmed[..literal_end];
|
||||
if !is_sql_string_literal(literal) {
|
||||
return None;
|
||||
}
|
||||
let cast_type = trimmed[literal_end..].trim().strip_prefix("::")?.trim();
|
||||
if is_postgres_textual_cast_type(cast_type) {
|
||||
Some(literal)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn is_postgres_textual_cast_type(value: &str) -> bool {
|
||||
let normalized =
|
||||
value.trim().trim_matches('"').split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
let base_type = normalized.split('(').next().unwrap_or(&normalized).trim();
|
||||
matches!(
|
||||
base_type,
|
||||
"char"
|
||||
| "character"
|
||||
| "varchar"
|
||||
| "character varying"
|
||||
| "text"
|
||||
| "bpchar"
|
||||
| "name"
|
||||
| "json"
|
||||
| "jsonb"
|
||||
| "xml"
|
||||
| "bytea"
|
||||
| "uuid"
|
||||
)
|
||||
}
|
||||
|
||||
pub fn clean(value: &str) -> String {
|
||||
value.trim().to_string()
|
||||
}
|
||||
|
||||
pub(super) fn is_protected_manticore_id_column(dialect: StructureDialect, column_name: &str) -> bool {
|
||||
dialect == StructureDialect::ManticoreSearch && column_name.trim().eq_ignore_ascii_case("id")
|
||||
}
|
||||
|
||||
pub(super) fn is_temporal_type_for_default(dialect: StructureDialect, base_type: &str) -> bool {
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
match dialect {
|
||||
StructureDialect::Mysql | StructureDialect::Doris => {
|
||||
matches!(normalized.as_str(), "date" | "datetime" | "timestamp" | "time" | "year")
|
||||
}
|
||||
StructureDialect::Postgres => {
|
||||
matches!(
|
||||
normalized.as_str(),
|
||||
"date"
|
||||
| "time"
|
||||
| "time without time zone"
|
||||
| "time with time zone"
|
||||
| "timestamp"
|
||||
| "timestamp without time zone"
|
||||
| "timestamp with time zone"
|
||||
| "interval"
|
||||
| "timetz"
|
||||
| "timestamptz"
|
||||
) || normalized.starts_with("interval ")
|
||||
}
|
||||
StructureDialect::SqlServer => matches!(
|
||||
normalized.as_str(),
|
||||
"date" | "time" | "datetime" | "datetime2" | "smalldatetime" | "datetimeoffset"
|
||||
),
|
||||
StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::Oscar => matches!(
|
||||
normalized.as_str(),
|
||||
"date"
|
||||
| "timestamp"
|
||||
| "timestamp with time zone"
|
||||
| "timestamp with local time zone"
|
||||
| "interval year to month"
|
||||
| "interval day to second"
|
||||
),
|
||||
StructureDialect::H2 => {
|
||||
matches!(
|
||||
normalized.as_str(),
|
||||
"date"
|
||||
| "time"
|
||||
| "time without time zone"
|
||||
| "time with time zone"
|
||||
| "timestamp"
|
||||
| "timestamp without time zone"
|
||||
| "timestamp with time zone"
|
||||
) || normalized.starts_with("interval ")
|
||||
}
|
||||
StructureDialect::ClickHouse => {
|
||||
matches!(normalized.as_str(), "date" | "date32" | "datetime" | "datetime64")
|
||||
}
|
||||
StructureDialect::Sqlite => {
|
||||
matches!(normalized.as_str(), "date" | "datetime" | "timestamp" | "time")
|
||||
}
|
||||
StructureDialect::Informix => matches!(normalized.as_str(), "date" | "datetime"),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn is_temporal_expression(value: &str) -> bool {
|
||||
let trimmed = value.trim();
|
||||
if trimmed.is_empty() {
|
||||
return false;
|
||||
}
|
||||
if trimmed.contains('(') || trimmed.contains(')') {
|
||||
return true;
|
||||
}
|
||||
trimmed.chars().all(|c| c.is_ascii_alphabetic() || c == '_')
|
||||
}
|
||||
|
||||
pub(super) fn is_string_type_for_default(dialect: StructureDialect, base_type: &str) -> bool {
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
match dialect {
|
||||
StructureDialect::Mysql | StructureDialect::Doris => matches!(
|
||||
normalized.as_str(),
|
||||
"char"
|
||||
| "varchar"
|
||||
| "tinytext"
|
||||
| "text"
|
||||
| "mediumtext"
|
||||
| "longtext"
|
||||
| "binary"
|
||||
| "varbinary"
|
||||
| "tinyblob"
|
||||
| "blob"
|
||||
| "mediumblob"
|
||||
| "longblob"
|
||||
| "enum"
|
||||
| "set"
|
||||
| "json"
|
||||
| "nvarchar"
|
||||
| "nchar"
|
||||
| "long"
|
||||
),
|
||||
StructureDialect::Postgres => matches!(
|
||||
normalized.as_str(),
|
||||
"char"
|
||||
| "character"
|
||||
| "varchar"
|
||||
| "character varying"
|
||||
| "text"
|
||||
| "bpchar"
|
||||
| "name"
|
||||
| "json"
|
||||
| "jsonb"
|
||||
| "xml"
|
||||
| "bytea"
|
||||
| "uuid"
|
||||
),
|
||||
StructureDialect::SqlServer => matches!(
|
||||
normalized.as_str(),
|
||||
"char" | "varchar" | "nchar" | "nvarchar" | "text" | "ntext" | "xml" | "uniqueidentifier" | "sysname"
|
||||
),
|
||||
StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::Oscar => matches!(
|
||||
normalized.as_str(),
|
||||
"char" | "nchar" | "varchar2" | "nvarchar2" | "clob" | "nclob" | "long" | "raw" | "long raw" | "bfile"
|
||||
),
|
||||
StructureDialect::H2 => matches!(
|
||||
normalized.as_str(),
|
||||
"char"
|
||||
| "character"
|
||||
| "varchar"
|
||||
| "character varying"
|
||||
| "text"
|
||||
| "clob"
|
||||
| "binary"
|
||||
| "varbinary"
|
||||
| "blob"
|
||||
| "uuid"
|
||||
| "json"
|
||||
),
|
||||
StructureDialect::ClickHouse => matches!(normalized.as_str(), "string" | "fixedstring" | "uuid" | "json"),
|
||||
StructureDialect::Sqlite => {
|
||||
matches!(normalized.as_str(), "text" | "varchar" | "char" | "character" | "clob" | "nvarchar" | "nchar")
|
||||
}
|
||||
StructureDialect::Informix => matches!(
|
||||
normalized.as_str(),
|
||||
"char"
|
||||
| "character"
|
||||
| "varchar"
|
||||
| "character varying"
|
||||
| "nvarchar"
|
||||
| "nchar"
|
||||
| "text"
|
||||
| "clob"
|
||||
| "lvarchar"
|
||||
| "byte"
|
||||
| "blob"
|
||||
),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn format_default_for_sql(dialect: StructureDialect, data_type: &str, default_value: &str) -> String {
|
||||
if default_value.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
let base_type = data_type.split('(').next().unwrap_or(data_type).trim();
|
||||
if is_temporal_type_for_default(dialect, base_type) {
|
||||
if is_temporal_expression(default_value) {
|
||||
return mysql_temporal_default_with_column_precision(dialect, data_type, default_value);
|
||||
}
|
||||
return quote_string(default_value);
|
||||
}
|
||||
if is_string_type_for_default(dialect, base_type) {
|
||||
if dialect == StructureDialect::SqlServer
|
||||
&& matches!(base_type.to_ascii_lowercase().as_str(), "nchar" | "nvarchar" | "ntext" | "sysname")
|
||||
{
|
||||
if let Some(literal) = sqlserver_unicode_string_literal(default_value) {
|
||||
return literal;
|
||||
}
|
||||
if default_value.contains('(') || default_value.contains(')') {
|
||||
return default_value.to_string();
|
||||
}
|
||||
return format!("N{}", quote_string(default_value));
|
||||
}
|
||||
// Only skip quoting for function-call expressions like `gen_random_uuid()`.
|
||||
// Simple identifiers like `CURRENT_TIMESTAMP` are not valid defaults for string columns.
|
||||
if is_sql_string_literal(default_value)
|
||||
|| (dialect == StructureDialect::Postgres && postgres_string_default_literal(default_value).is_some())
|
||||
|| default_value.contains('(')
|
||||
|| default_value.contains(')')
|
||||
{
|
||||
return default_value.to_string();
|
||||
}
|
||||
return quote_string(default_value);
|
||||
}
|
||||
default_value.to_string()
|
||||
}
|
||||
|
||||
/// MySQL requires a `CURRENT_TIMESTAMP` default on a fractional-precision
|
||||
/// temporal column to spell out the column's precision (`datetime(3)` needs
|
||||
/// `DEFAULT CURRENT_TIMESTAMP(3)`), otherwise the statement fails with
|
||||
/// `ERROR 1067 (42000): Invalid default value`. Complete a bare
|
||||
/// `CURRENT_TIMESTAMP` with the precision parsed from the data type; a value
|
||||
/// that already carries parentheses is passed through unchanged.
|
||||
fn mysql_temporal_default_with_column_precision(
|
||||
dialect: StructureDialect,
|
||||
data_type: &str,
|
||||
default_value: &str,
|
||||
) -> String {
|
||||
let trimmed = default_value.trim();
|
||||
if dialect != StructureDialect::Mysql || !trimmed.eq_ignore_ascii_case("current_timestamp") {
|
||||
return default_value.to_string();
|
||||
}
|
||||
match mysql_temporal_precision(data_type) {
|
||||
Some(fsp) => format!("{trimmed}({fsp})"),
|
||||
None => default_value.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn normalize_default(value: Option<&String>) -> String {
|
||||
let trimmed = value.map(|value| value.trim()).unwrap_or("");
|
||||
if trimmed.eq_ignore_ascii_case("null") {
|
||||
String::new()
|
||||
} else if let Some(literal) = postgres_string_default_literal(trimmed) {
|
||||
literal.to_string()
|
||||
} else {
|
||||
trimmed.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn original_default(column: &EditableStructureColumn) -> String {
|
||||
normalize_default(column.original.as_ref().and_then(|original| original.column_default.as_ref()))
|
||||
}
|
||||
|
||||
pub fn original_comment(column: &EditableStructureColumn) -> String {
|
||||
clean(column.original.as_ref().and_then(|original| original.comment.as_deref()).unwrap_or(""))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sqlserver_unicode_defaults_use_unicode_literals_without_double_quoting() {
|
||||
assert_eq!(format_default_for_sql(StructureDialect::SqlServer, "nvarchar(50)", "中文"), "N'中文'");
|
||||
assert_eq!(format_default_for_sql(StructureDialect::SqlServer, "nchar(5)", "'中文'"), "N'中文'");
|
||||
assert_eq!(format_default_for_sql(StructureDialect::SqlServer, "sysname", "n'guest'"), "N'guest'");
|
||||
assert_eq!(format_default_for_sql(StructureDialect::SqlServer, "nvarchar(50)", "('中文')"), "(N'中文')");
|
||||
assert_eq!(format_default_for_sql(StructureDialect::SqlServer, "nvarchar(50)", "((N'中文'))"), "((N'中文'))");
|
||||
assert_eq!(
|
||||
format_default_for_sql(StructureDialect::SqlServer, "nvarchar(max)", "CONCAT(N'a', N'b')"),
|
||||
"CONCAT(N'a', N'b')"
|
||||
);
|
||||
assert_eq!(format_default_for_sql(StructureDialect::SqlServer, "varchar(50)", "中文"), "'中文'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
use super::column_format::has_dameng_identity;
|
||||
use super::dialect::{capabilities_for, StructureDialect};
|
||||
use super::indexes::has_existing_index_change;
|
||||
use super::types::{EditableStructureColumn, TableStructureSqlOptions};
|
||||
use super::util::clean;
|
||||
|
||||
/// Hard errors for `CREATE INDEX CONCURRENTLY` requests the editor cannot
|
||||
/// honor safely. Unlike the advisory warnings in [`validate_draft`], these
|
||||
/// reject the whole save plan: the caller asked for a non-blocking build and
|
||||
/// the only way to honor that is the requested concurrent DDL, so falling back
|
||||
/// to blocking index DDL must never happen silently.
|
||||
///
|
||||
/// Only real PostgreSQL advertises `index_concurrent`, so stale/forged
|
||||
/// `concurrently` flags on other engines are ignored here (they cannot express
|
||||
/// a concurrent request in the first place).
|
||||
pub(super) fn validate_concurrent_index_scope(options: &TableStructureSqlOptions) -> Vec<String> {
|
||||
let mut errors = Vec::new();
|
||||
let capabilities = capabilities_for(options.database_type, options.driver_profile.as_deref());
|
||||
if !capabilities.index_concurrent || capabilities.dialect != StructureDialect::Postgres {
|
||||
return errors;
|
||||
}
|
||||
for index in options.indexes.iter().filter(|index| !index.marked_for_drop) {
|
||||
if !index.concurrently {
|
||||
continue;
|
||||
}
|
||||
if index.original.is_some() {
|
||||
errors.push(format!(
|
||||
"CREATE INDEX CONCURRENTLY is only supported for newly created indexes. Editing an existing index \"{}\" with Concurrent enabled is not supported.",
|
||||
index.name
|
||||
));
|
||||
} else if options.partitioned {
|
||||
errors.push(
|
||||
"CREATE INDEX CONCURRENTLY is not supported on PostgreSQL partitioned parent tables. Create indexes concurrently on individual partitions and attach them separately."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
errors
|
||||
}
|
||||
|
||||
pub fn validate_draft(options: &TableStructureSqlOptions) -> Vec<String> {
|
||||
let mut warnings = Vec::new();
|
||||
let active_columns: Vec<_> = options.columns.iter().filter(|column| !column.marked_for_drop).collect();
|
||||
validate_columns(&active_columns, &mut warnings);
|
||||
validate_dameng_identity(options, &active_columns, &mut warnings);
|
||||
validate_gin_opclass_warnings(options, &mut warnings);
|
||||
for index in options
|
||||
.indexes
|
||||
.iter()
|
||||
.filter(|index| !index.marked_for_drop && (index.original.is_none() || has_existing_index_change(index)))
|
||||
{
|
||||
if clean(&index.name).is_empty() {
|
||||
warnings.push("Index name cannot be empty.".to_string());
|
||||
}
|
||||
if index.columns.iter().map(|column| clean(column)).filter(|column| !column.is_empty()).count() == 0 {
|
||||
warnings.push(format!(
|
||||
"Index \"{}\" needs at least one column.",
|
||||
if index.name.is_empty() { "(new)" } else { &index.name }
|
||||
));
|
||||
}
|
||||
}
|
||||
warnings
|
||||
}
|
||||
|
||||
pub(super) fn validate_dameng_identity(
|
||||
options: &TableStructureSqlOptions,
|
||||
columns: &[&EditableStructureColumn],
|
||||
warnings: &mut Vec<String>,
|
||||
) {
|
||||
if capabilities_for(options.database_type, options.driver_profile.as_deref()).dialect != StructureDialect::Dameng {
|
||||
return;
|
||||
}
|
||||
|
||||
let identity_columns: Vec<_> = columns.iter().filter(|column| has_dameng_identity(column)).collect();
|
||||
if identity_columns.len() > 1 {
|
||||
warnings.push("Dameng tables can have only one identity column.".to_string());
|
||||
}
|
||||
for column in identity_columns {
|
||||
if column.extra.as_ref().and_then(|extra| extra.identity.as_ref()).and_then(|identity| identity.increment)
|
||||
== Some(0)
|
||||
{
|
||||
warnings.push(format!("Dameng identity column \"{}\" increment cannot be 0.", column.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn validate_columns(columns: &[&EditableStructureColumn], warnings: &mut Vec<String>) {
|
||||
let mut names = std::collections::HashSet::new();
|
||||
for column in columns {
|
||||
if clean(&column.name).is_empty() {
|
||||
warnings.push("Column name cannot be empty.".to_string());
|
||||
}
|
||||
if clean(&column.data_type).is_empty() {
|
||||
warnings.push(format!(
|
||||
"Column \"{}\" type cannot be empty.",
|
||||
if column.name.is_empty() { "(new)" } else { &column.name }
|
||||
));
|
||||
}
|
||||
let key = clean(&column.name).to_lowercase();
|
||||
if !key.is_empty() && !names.insert(key) {
|
||||
warnings.push(format!("Column \"{}\" is duplicated.", column.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// PostgreSQL GIN indexes on varchar/text/character columns without an explicit
|
||||
/// operator class are a common trap: the default `array_ops` class is rarely what
|
||||
/// the user wants, and `gin_trgm_ops` (from `pg_trgm` extension) or similar
|
||||
/// classes are almost always needed. This is a warning, not an error, because
|
||||
/// the default class may be deliberate in some cases.
|
||||
fn validate_gin_opclass_warnings(options: &TableStructureSqlOptions, warnings: &mut Vec<String>) {
|
||||
let capabilities = capabilities_for(options.database_type, options.driver_profile.as_deref());
|
||||
if capabilities.dialect != StructureDialect::Postgres {
|
||||
return;
|
||||
}
|
||||
|
||||
// Build a lookup: column name (lowercased) → data type for active columns.
|
||||
let col_types: std::collections::HashMap<String, &str> = options
|
||||
.columns
|
||||
.iter()
|
||||
.filter(|col| !col.marked_for_drop)
|
||||
.map(|col| (clean(&col.name).to_lowercase(), col.data_type.as_str()))
|
||||
.collect();
|
||||
|
||||
let text_like = |dt: &str| -> bool {
|
||||
let lower = dt.to_lowercase();
|
||||
lower == "text"
|
||||
|| lower.starts_with("character varying")
|
||||
|| lower.starts_with("varchar")
|
||||
|| lower.starts_with("character")
|
||||
|| lower == "char"
|
||||
};
|
||||
|
||||
for index in options.indexes.iter().filter(|idx| {
|
||||
!idx.marked_for_drop
|
||||
&& clean(&idx.index_type).eq_ignore_ascii_case("GIN")
|
||||
&& (idx.original.is_none() || has_existing_index_change(idx))
|
||||
}) {
|
||||
for (i, col) in index.columns.iter().enumerate() {
|
||||
let col_name = clean(col);
|
||||
if col_name.is_empty() {
|
||||
continue;
|
||||
}
|
||||
// Expression columns are not matched against table column types.
|
||||
let is_expr =
|
||||
index.original.as_ref().and_then(|orig| orig.key_is_expression.get(i).copied()).unwrap_or(false);
|
||||
if is_expr {
|
||||
continue;
|
||||
}
|
||||
// Check if this column has a text-like type.
|
||||
let Some(dt) = col_types.get(&col_name.to_lowercase()) else {
|
||||
continue;
|
||||
};
|
||||
if !text_like(dt) {
|
||||
continue;
|
||||
}
|
||||
// Check if opclass is explicitly provided.
|
||||
let has_opclass =
|
||||
index.column_opclasses.get(i).and_then(|o| o.as_deref()).map(|v| !clean(v).is_empty()).unwrap_or(false);
|
||||
if !has_opclass {
|
||||
warnings.push(format!(
|
||||
"GIN index \"{}\" on column \"{}\" (type {}) has no operator class specified. Consider adding an operator class, e.g. `gin_trgm_ops` (requires pg_trgm extension) for text search.",
|
||||
index.name, col_name, dt
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user