mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-14 16:27:35 +08:00
* 🐛 fix(data-viewer): 修复ClickHouse尾部分页异常并增强DuckDB复杂类型兼容 - DataViewer 新增 ClickHouse 反向分页策略,修复最后页与倒数页查询失败 - DuckDB 查询失败时按列类型生成安全 SELECT,复杂类型转 VARCHAR 重试 - 分页状态统一使用 currentPage 回填,避免页码与总数推导不一致 - 增强查询异常日志与重试路径,降低大表场景卡顿与误报 * ✨ feat(frontend-driver): 驱动管理支持快速搜索并优化信息展示 - 新增搜索框,支持按 DuckDB/ClickHouse 等关键字快速定位驱动 - 显示“匹配 x / y”统计与无结果提示 - 优化头部区域排版,提升透明/暗色场景下的视觉对齐 * 🔧 fix(connection-modal): 修复多数据源URI导入解析并校正Oracle服务名校验 - 新增单主机URI解析映射,兼容 postgres/postgresql、sqlserver、redis、tdengine、dameng(dm)、kingbase、highgo、vastbase、clickhouse、oracle - 抽取 parseSingleHostUri 复用逻辑,统一 host/port/user/password/database 回填行为 - Oracle 连接新增服务名必填校验,移除“服务名为空回退用户名”的隐式逻辑 - 连接弹窗补充 Oracle 服务名输入项与 URI 示例 * 🐛 fix(query-export): 修复查询结果导出卡住并统一按数据源能力控制导出路径 - 查询结果页导出增加稳定兜底,异常时确保 loading 关闭避免持续转圈 - DataGrid 导出逻辑按数据源能力分流,优先走后端 ExportQuery 并保留结果集导出降级 - QueryEditor 传递结果导出 SQL,保证查询结果导出范围与当前结果一致 - 后端补充 ExportData/ExportQuery 关键日志,提升导出链路可观测性 * 🐛 fix(precision): 修复查询链路与分页统计的大整数精度丢失 - 代理响应数据解码改为 UseNumber,避免默认 float64 吞精度 - 统一归一化 json.Number 与超界整数,超出 JS 安全范围转字符串 - 修复 DataViewer 总数解析,超大值不再误转 Number 参与分页 - refs #142 * 🐛 fix(driver-manager): 修复驱动管理网络告警重复并强化代理引导 - 新增下载链路域名探测,区分“GitHub可达但驱动下载链路不可达” - 网络不可达场景仅保留红色强提醒,移除重复二级告警 - 强提醒增加“打开全局代理设置”入口,优先引导使用 GoNavi 全局代理 - 统一网络检测与目录说明提示图标尺寸,修复加载期视觉不一致 - refs #141 * ♻️ refactor(frontend-interaction): 统一标签拖拽与暗色主题交互实现 - 重构Tab拖拽排序实现,统一为可配置拖拽引擎 - 规范拖拽与点击事件边界,提升交互一致性 - 统一多组件暗色透明样式策略,减少硬编码色值 - 提升Redis/表格/连接面板在透明模式下的观感一致性 - refs #144 * ♻️ refactor(update-state): 重构在线更新状态流并按版本统一进度展示 - 重构更新检查与下载状态同步流程,减少前后端状态分叉 - 进度展示严格绑定 latestVersion,避免跨版本状态串用 - 优化 about 打开场景的静默检查状态回填逻辑 - 统一下载弹窗关闭/后台隐藏行为 - 保持现有安装流程并补齐目录打开能力 * 🎨 style(sidebar-log): 将SQL执行日志入口调整为悬浮胶囊样式 - 移除侧栏底部整条日志入口容器 - 新增悬浮按钮阴影/边框/透明背景并适配明暗主题 - 为树区域预留底部空间避免入口遮挡内容 * ✨ feat(redis-cluster): 支持集群模式逻辑多库隔离与 0-15 库切换 - 前端恢复 Redis 集群场景下 db0-db15 的数据库选择与展示 - 后端新增集群逻辑库命名空间前缀映射,统一 key/pattern 读写隔离 - 覆盖扫描、读取、写入、删除、重命名等核心操作的键映射规则 - 集群命令通道支持 SELECT 逻辑切库与 FLUSHDB 逻辑库清空 - refs #145 * ✨ feat(DataGrid): 大数据表虚拟滚动性能优化及UI一致性修复 - 启用动态虚拟滚动(数据量≥500行自动切换),解决万行数据表卡顿问题 - 虚拟模式下EditableCell改用div渲染,CSS选择器从元素级改为类级适配虚拟DOM - 修复虚拟模式双水平滚动条:样式化rc-virtual-list内置滚动条为胶囊外观,禁用自定义外部滚动条 - 为rc-virtual-list水平滚动条添加鼠标滚轮支持(MutationObserver + marginLeft驱动) - 修复白色主题透明模式下列名悬浮Tooltip对比度不足的问题 - 新增白色主题全局滚动条样式适配透明模式(App.css) - App.tsx主题token与组件样式优化 - refs #147 * 🔧 chore(app): 清理 App.tsx 类型告警并收敛前端壳层实现 - 清除未使用代码和冗余状态 - 替换弃用 API 以消除 IDE 提示 - 显式处理浮动 Promise 避免告警 - 保持现有更新检查和代理设置行为不变 * 🔧 fix(ci): 修复 Windows AMD64 下 DuckDB 驱动构建链路 - 将 DuckDB 工具链准备切换为优先使用 MSYS2 - 增加 gcc 和 g++ 存在性校验与版本验证 - 在 MSYS2 异常时回退 Chocolatey 安装 MinGW - 保持 Windows ARM64 跳过 DuckDB 构建与平台支持一致 * 🔧 fix(ci): 修复 Windows AMD64 下 DuckDB 驱动构建链路 - 将 DuckDB 工具链准备切换为优先使用 MSYS2 - 增加 gcc 和 g++ 存在性校验与版本验证 - 在 MSYS2 异常时回退 Chocolatey 安装 MinGW - 保持 Windows ARM64 跳过 DuckDB 构建与平台支持一致 * 🔧 fix(ci): 修复 Windows AMD64 下 DuckDB 驱动构建工具链 - 将 DuckDB 编译链从 MINGW64 切换为 MSYS2 UCRT64 - 修正 Windows AMD64 的 gcc 和 g++ 探测路径 - 增加 DuckDB 编译器版本校验步骤 * 📝 docs(contributing): 补充中英文贡献指南并统一 README 入口 - 新增英文版 CONTRIBUTING.md 作为正式贡献文档 - 新增中文版 CONTRIBUTING.zh-CN.md 作为中文贡献说明 - 调整 README 和 README.zh-CN 的贡献入口指向对应语言文档 * - feat(connection,metadata,kingbase): 增强多数据源连接能力并修复金仓/达梦/Oracle/ClickHouse兼容性问题 (#188) (#190) * feat(http-tunnel): 支持独立 HTTP 隧道连接并覆盖多数据源 refs #168 * fix(kingbase-data-grid): 修复金仓打开表卡顿并降低对象渲染开销 refs #178 * fix(kingbase-transaction): 修复金仓事务提交重复引号导致语法错误 refs #176 * fix(driver-agent): 修复老版本 Win10 升级后金仓驱动代理启动失败 refs #177 * chore(ci): 新增手动触发的 macOS 测试构建工作流 * chore(ci): 允许测试工作流在当前分支自动触发 * fix(query-editor): 修复 SQL 编辑中光标随机跳到末尾 refs #185 * feat(data-sync): 增加差异 SQL 预览能力便于审核 refs #174 * fix(clickhouse-connect): 自动识别并回退 HTTP/Native 协议连接 refs #181 * fix(oracle-metadata): 修复视图与函数加载按 schema 过滤异常 refs #155 * fix(dameng-databases): 修复显示全部库时数据库列表不完整 refs #154 * fix(connection,db-list): 统一处理空列表返回并修复达梦连接测试报错 refs #157 Co-authored-by: 辣条 <69459608+tianqijiuyun-latiao@users.noreply.github.com> * ✨ feat(release-notes): 支持自动生成 Release 更新说明并区分配置文件命名 * 🔁 chore(sync): 回灌 main 到 dev (#192) * - feat(connection,metadata,kingbase): 增强多数据源连接能力并修复金仓/达梦/Oracle/ClickHouse兼容性问题 (#188) * feat(http-tunnel): 支持独立 HTTP 隧道连接并覆盖多数据源 refs #168 * fix(kingbase-data-grid): 修复金仓打开表卡顿并降低对象渲染开销 refs #178 * fix(kingbase-transaction): 修复金仓事务提交重复引号导致语法错误 refs #176 * fix(driver-agent): 修复老版本 Win10 升级后金仓驱动代理启动失败 refs #177 * chore(ci): 新增手动触发的 macOS 测试构建工作流 * chore(ci): 允许测试工作流在当前分支自动触发 * fix(query-editor): 修复 SQL 编辑中光标随机跳到末尾 refs #185 * feat(data-sync): 增加差异 SQL 预览能力便于审核 refs #174 * fix(clickhouse-connect): 自动识别并回退 HTTP/Native 协议连接 refs #181 * fix(oracle-metadata): 修复视图与函数加载按 schema 过滤异常 refs #155 * fix(dameng-databases): 修复显示全部库时数据库列表不完整 refs #154 * fix(connection,db-list): 统一处理空列表返回并修复达梦连接测试报错 refs #157 * Release/0.5.3 (#191) --------- Co-authored-by: 辣条 <69459608+tianqijiuyun-latiao@users.noreply.github.com> Co-authored-by: Syngnat <92659908+Syngnat@users.noreply.github.com> * 🐛 fix(branch-sync): 修复 main 回灌 dev 时 mergeable 异步计算导致漏开自动合并 - 增加 mergeable 状态轮询,避免新建同步 PR 后立即返回 UNKNOWN - 在合并状态未稳定时输出中文告警与执行摘要 - 保持冲突分支、待计算分支与自动合并分支的处理路径清晰 * 🔁 chore(sync): 回灌 main 到 dev (#195) * - feat(connection,metadata,kingbase): 增强多数据源连接能力并修复金仓/达梦/Oracle/ClickHouse兼容性问题 (#188) * feat(http-tunnel): 支持独立 HTTP 隧道连接并覆盖多数据源 refs #168 * fix(kingbase-data-grid): 修复金仓打开表卡顿并降低对象渲染开销 refs #178 * fix(kingbase-transaction): 修复金仓事务提交重复引号导致语法错误 refs #176 * fix(driver-agent): 修复老版本 Win10 升级后金仓驱动代理启动失败 refs #177 * chore(ci): 新增手动触发的 macOS 测试构建工作流 * chore(ci): 允许测试工作流在当前分支自动触发 * fix(query-editor): 修复 SQL 编辑中光标随机跳到末尾 refs #185 * feat(data-sync): 增加差异 SQL 预览能力便于审核 refs #174 * fix(clickhouse-connect): 自动识别并回退 HTTP/Native 协议连接 refs #181 * fix(oracle-metadata): 修复视图与函数加载按 schema 过滤异常 refs #155 * fix(dameng-databases): 修复显示全部库时数据库列表不完整 refs #154 * fix(connection,db-list): 统一处理空列表返回并修复达梦连接测试报错 refs #157 * Release/0.5.3 (#191) * - chore(ci): 新增全平台测试包手动构建工作流 tianqijiuyun-latiao 今天 下午4:26 (#194) * feat(http-tunnel): 支持独立 HTTP 隧道连接并覆盖多数据源 refs #168 * fix(kingbase-data-grid): 修复金仓打开表卡顿并降低对象渲染开销 refs #178 * fix(kingbase-transaction): 修复金仓事务提交重复引号导致语法错误 refs #176 * fix(driver-agent): 修复老版本 Win10 升级后金仓驱动代理启动失败 refs #177 * chore(ci): 新增手动触发的 macOS 测试构建工作流 * chore(ci): 允许测试工作流在当前分支自动触发 * fix(query-editor): 修复 SQL 编辑中光标随机跳到末尾 refs #185 * feat(data-sync): 增加差异 SQL 预览能力便于审核 refs #174 * fix(clickhouse-connect): 自动识别并回退 HTTP/Native 协议连接 refs #181 * fix(oracle-metadata): 修复视图与函数加载按 schema 过滤异常 refs #155 * fix(dameng-databases): 修复显示全部库时数据库列表不完整 refs #154 * fix(connection,db-list): 统一处理空列表返回并修复达梦连接测试报错 refs #157 * fix(kingbase): 补齐主键识别并优化宽表卡顿 refs #176 refs #178 * fix(query-execution): 支持带前置注释的读查询结果识别 * chore(ci): 新增全平台测试包手动构建工作流 --------- Co-authored-by: 辣条 <69459608+tianqijiuyun-latiao@users.noreply.github.com> Co-authored-by: Syngnat <92659908+Syngnat@users.noreply.github.com> * ♻️ refactor(frontend-sync): 优化桌面交互细节并移除 main 回灌 dev 自动化 - 优化新建连接、主题设置、侧边栏工具区与 SQL 日志的界面表现 - 调整分页、筛选、透明模式与弹窗样式,统一整体交互层次 - 收口外观参数生效逻辑并补齐多组件适配 - 删除 sync-main-to-dev 工作流并同步维护者手动回灌说明 * feat: 统一筛选条件逻辑按钮宽度 (#201) * 🐛 fix(oracle-query): 修复 Oracle 表数据分页 SQL 兼容问题 refs #196 (#202) * ✨ feat(datasource): 支持 DuckDB Parquet 文件模式并优化弹窗打开链路 - 统一 DuckDB 文件库与 Parquet 文件接入能力 - 补充 URI、文件选择、只读挂载与连接缓存键处理 - 去掉数据源卡片点击前的同步驱动查询,修复打开卡顿 * ✨ feat(datasource): 支持 DuckDB Parquet 文件模式并优化弹窗打开链路 - 统一 DuckDB 文件库与 Parquet 文件接入能力 - 补充 URI、文件选择、只读挂载与连接缓存键处理 - 去掉数据源卡片点击前的同步驱动查询,修复打开卡顿 - refs #166 * 🐛 fix(dameng): 修复达梦连接成功后数据库列表为空问题 - 调整达梦数据库列表获取策略,优先回退查询当前 schema 与当前用户 - 保留可见用户与 owner 聚合逻辑,兼容低权限账号场景 - 补充前端空列表提示与后端单元测试,降低排查成本 - close #203 * ✨ feat(data-sync): 扩展跨库迁移链路并优化数据同步交互 - 统一同库同步与跨库迁移入口,补充模式区分与风险提示 - 扩展 ClickHouse 与 PG-like 双向迁移,并新增 PG-like、ClickHouse、TDengine 到 MongoDB 的迁移路由 - 完善 TDengine 目标端建表规划、回归测试与需求追踪文档 - refs #51 --------- Co-authored-by: Syngnat <yangguofeng919@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: 辣条 <69459608+tianqijiuyun-latiao@users.noreply.github.com> Co-authored-by: TSS <266256496+Zencok@users.noreply.github.com>
658 lines
29 KiB
Go
658 lines
29 KiB
Go
package sync
|
||
|
||
import (
|
||
"GoNavi-Wails/internal/connection"
|
||
"GoNavi-Wails/internal/db"
|
||
"fmt"
|
||
"strconv"
|
||
"strings"
|
||
)
|
||
|
||
type mySQLLikeToTDenginePlanner struct{}
|
||
|
||
type pgLikeToTDenginePlanner struct{}
|
||
|
||
type clickHouseToTDenginePlanner struct{}
|
||
|
||
type tdengineToTDenginePlanner struct{}
|
||
|
||
func (mySQLLikeToTDenginePlanner) Name() string { return "mysqllike-tdengine-planner" }
|
||
|
||
func (mySQLLikeToTDenginePlanner) SupportLevel(ctx MigrationBuildContext) MigrationSupportLevel {
|
||
sourceType := resolveMigrationDBType(ctx.Config.SourceConfig)
|
||
targetType := resolveMigrationDBType(ctx.Config.TargetConfig)
|
||
if isMySQLLikeSourceType(sourceType) && targetType == "tdengine" {
|
||
return MigrationSupportLevelFull
|
||
}
|
||
return MigrationSupportLevelUnsupported
|
||
}
|
||
|
||
func (mySQLLikeToTDenginePlanner) BuildPlan(ctx MigrationBuildContext) (SchemaMigrationPlan, []connection.ColumnDefinition, []connection.ColumnDefinition, error) {
|
||
return buildMySQLLikeToTDenginePlan(ctx.Config, ctx.TableName, ctx.SourceDB, ctx.TargetDB)
|
||
}
|
||
|
||
func (pgLikeToTDenginePlanner) Name() string { return "pglike-tdengine-planner" }
|
||
|
||
func (pgLikeToTDenginePlanner) SupportLevel(ctx MigrationBuildContext) MigrationSupportLevel {
|
||
sourceType := resolveMigrationDBType(ctx.Config.SourceConfig)
|
||
targetType := resolveMigrationDBType(ctx.Config.TargetConfig)
|
||
if isPGLikeSource(sourceType) && targetType == "tdengine" {
|
||
return MigrationSupportLevelFull
|
||
}
|
||
return MigrationSupportLevelUnsupported
|
||
}
|
||
|
||
func (pgLikeToTDenginePlanner) BuildPlan(ctx MigrationBuildContext) (SchemaMigrationPlan, []connection.ColumnDefinition, []connection.ColumnDefinition, error) {
|
||
return buildPGLikeToTDenginePlan(ctx.Config, ctx.TableName, ctx.SourceDB, ctx.TargetDB)
|
||
}
|
||
|
||
func buildMySQLLikeToTDenginePlan(config SyncConfig, tableName string, sourceDB db.Database, targetDB db.Database) (SchemaMigrationPlan, []connection.ColumnDefinition, []connection.ColumnDefinition, error) {
|
||
return buildSourceToTDenginePlan(config, tableName, sourceDB, targetDB, isMySQLLikeTDengineTimestampCandidate, buildMySQLLikeToTDengineCreateTableSQL)
|
||
}
|
||
|
||
func buildPGLikeToTDenginePlan(config SyncConfig, tableName string, sourceDB db.Database, targetDB db.Database) (SchemaMigrationPlan, []connection.ColumnDefinition, []connection.ColumnDefinition, error) {
|
||
return buildSourceToTDenginePlan(config, tableName, sourceDB, targetDB, isPGLikeTDengineTimestampCandidate, buildPGLikeToTDengineCreateTableSQL)
|
||
}
|
||
|
||
func buildClickHouseToTDenginePlan(config SyncConfig, tableName string, sourceDB db.Database, targetDB db.Database) (SchemaMigrationPlan, []connection.ColumnDefinition, []connection.ColumnDefinition, error) {
|
||
return buildSourceToTDenginePlan(config, tableName, sourceDB, targetDB, isClickHouseTDengineTimestampCandidate, buildClickHouseToTDengineCreateTableSQL)
|
||
}
|
||
|
||
func buildTDengineToTDenginePlan(config SyncConfig, tableName string, sourceDB db.Database, targetDB db.Database) (SchemaMigrationPlan, []connection.ColumnDefinition, []connection.ColumnDefinition, error) {
|
||
return buildSourceToTDenginePlan(config, tableName, sourceDB, targetDB, isTDengineTDengineTimestampCandidate, buildTDengineToTDengineCreateTableSQL)
|
||
}
|
||
|
||
func (clickHouseToTDenginePlanner) Name() string { return "clickhouse-tdengine-planner" }
|
||
|
||
func (clickHouseToTDenginePlanner) SupportLevel(ctx MigrationBuildContext) MigrationSupportLevel {
|
||
sourceType := resolveMigrationDBType(ctx.Config.SourceConfig)
|
||
targetType := resolveMigrationDBType(ctx.Config.TargetConfig)
|
||
if sourceType == "clickhouse" && targetType == "tdengine" {
|
||
return MigrationSupportLevelFull
|
||
}
|
||
return MigrationSupportLevelUnsupported
|
||
}
|
||
|
||
func (clickHouseToTDenginePlanner) BuildPlan(ctx MigrationBuildContext) (SchemaMigrationPlan, []connection.ColumnDefinition, []connection.ColumnDefinition, error) {
|
||
return buildClickHouseToTDenginePlan(ctx.Config, ctx.TableName, ctx.SourceDB, ctx.TargetDB)
|
||
}
|
||
|
||
func (tdengineToTDenginePlanner) Name() string { return "tdengine-tdengine-planner" }
|
||
|
||
func (tdengineToTDenginePlanner) SupportLevel(ctx MigrationBuildContext) MigrationSupportLevel {
|
||
sourceType := resolveMigrationDBType(ctx.Config.SourceConfig)
|
||
targetType := resolveMigrationDBType(ctx.Config.TargetConfig)
|
||
if sourceType == "tdengine" && targetType == "tdengine" {
|
||
return MigrationSupportLevelFull
|
||
}
|
||
return MigrationSupportLevelUnsupported
|
||
}
|
||
|
||
func (tdengineToTDenginePlanner) BuildPlan(ctx MigrationBuildContext) (SchemaMigrationPlan, []connection.ColumnDefinition, []connection.ColumnDefinition, error) {
|
||
return buildTDengineToTDenginePlan(ctx.Config, ctx.TableName, ctx.SourceDB, ctx.TargetDB)
|
||
}
|
||
|
||
type tdengineTimestampCandidate func(connection.ColumnDefinition) bool
|
||
|
||
type tdengineCreateTableBuilder func(string, []connection.ColumnDefinition, int) (string, []string, []string)
|
||
|
||
func buildSourceToTDenginePlan(config SyncConfig, tableName string, sourceDB db.Database, targetDB db.Database, isTimestamp tdengineTimestampCandidate, buildCreateSQL tdengineCreateTableBuilder) (SchemaMigrationPlan, []connection.ColumnDefinition, []connection.ColumnDefinition, error) {
|
||
plan := SchemaMigrationPlan{}
|
||
sourceType := resolveMigrationDBType(config.SourceConfig)
|
||
targetType := resolveMigrationDBType(config.TargetConfig)
|
||
plan.SourceSchema, plan.SourceTable = normalizeSchemaAndTable(sourceType, config.SourceConfig.Database, tableName)
|
||
plan.TargetSchema, plan.TargetTable = normalizeSchemaAndTable(targetType, config.TargetConfig.Database, tableName)
|
||
plan.SourceQueryTable = qualifiedNameForQuery(sourceType, plan.SourceSchema, plan.SourceTable, tableName)
|
||
plan.TargetQueryTable = qualifiedNameForQuery(targetType, plan.TargetSchema, plan.TargetTable, tableName)
|
||
plan.PlannedAction = "使用已有目标表导入"
|
||
|
||
sourceCols, sourceExists, err := inspectTableColumns(sourceDB, plan.SourceSchema, plan.SourceTable)
|
||
if err != nil {
|
||
return plan, nil, nil, fmt.Errorf("获取源表字段失败: %w", err)
|
||
}
|
||
if !sourceExists {
|
||
return plan, nil, nil, fmt.Errorf("源表不存在或无列定义: %s", tableName)
|
||
}
|
||
|
||
plan.Warnings = append(plan.Warnings, tdengineTargetBaseWarnings()...)
|
||
timestampIndex := findTDengineTimestampColumn(sourceCols, isTimestamp)
|
||
if timestampIndex < 0 {
|
||
plan.Warnings = append(plan.Warnings, tdengineTargetMissingTimeWarning())
|
||
}
|
||
|
||
targetCols, targetExists, err := inspectTableColumns(targetDB, plan.TargetSchema, plan.TargetTable)
|
||
if err != nil {
|
||
return plan, sourceCols, nil, fmt.Errorf("获取目标表字段失败: %w", err)
|
||
}
|
||
plan.TargetTableExists = targetExists
|
||
|
||
strategy := normalizeTargetTableStrategy(config.TargetTableStrategy)
|
||
if targetExists {
|
||
missing := diffMissingColumnNames(sourceCols, targetCols)
|
||
if len(missing) > 0 {
|
||
plan.Warnings = append(plan.Warnings, fmt.Sprintf("目标表缺失字段 %d 个:%s", len(missing), strings.Join(missing, ", ")))
|
||
}
|
||
if strategy != "existing_only" {
|
||
plan.Warnings = append(plan.Warnings, "TDengine 目标端当前不自动补齐已有目标表字段,请先确认目标表结构")
|
||
}
|
||
return dedupeSchemaMigrationPlan(plan), sourceCols, targetCols, nil
|
||
}
|
||
|
||
switch strategy {
|
||
case "existing_only":
|
||
plan.PlannedAction = "目标表不存在,需先手工创建"
|
||
plan.Warnings = append(plan.Warnings, "当前策略要求目标表已存在,执行时不会自动建表")
|
||
return dedupeSchemaMigrationPlan(plan), sourceCols, targetCols, nil
|
||
case "smart", "auto_create_if_missing":
|
||
if timestampIndex < 0 {
|
||
plan.PlannedAction = "源表未识别到可映射为 TDengine 首列的时间列,无法自动建表"
|
||
plan.UnsupportedObjects = append(plan.UnsupportedObjects, "TDengine regular table 首列必须为 TIMESTAMP,当前源表缺少可直接映射的时间列")
|
||
return dedupeSchemaMigrationPlan(plan), sourceCols, targetCols, nil
|
||
}
|
||
plan.AutoCreate = true
|
||
plan.PlannedAction = "目标表不存在,将自动建表后导入"
|
||
createSQL, warnings, unsupported := buildCreateSQL(plan.TargetQueryTable, sourceCols, timestampIndex)
|
||
plan.CreateTableSQL = createSQL
|
||
plan.Warnings = append(plan.Warnings, warnings...)
|
||
plan.UnsupportedObjects = append(plan.UnsupportedObjects, unsupported...)
|
||
return dedupeSchemaMigrationPlan(plan), sourceCols, targetCols, nil
|
||
default:
|
||
return dedupeSchemaMigrationPlan(plan), sourceCols, targetCols, nil
|
||
}
|
||
}
|
||
|
||
func tdengineTargetBaseWarnings() []string {
|
||
return []string{
|
||
"TDengine 目标端当前仅支持 INSERT 写入;若存在差异 update/delete,执行期会被拒绝",
|
||
"TDengine 目标端 auto-create 当前仅创建基础表;索引、外键、触发器、supertable/TAGS/TTL 不会自动迁移",
|
||
}
|
||
}
|
||
|
||
func tdengineTargetMissingTimeWarning() string {
|
||
return "源表缺少可映射的时间列,自动建表将不可用;如需继续,请先人工准备 TDengine 目标表与时间列"
|
||
}
|
||
|
||
func findTDengineTimestampColumn(sourceCols []connection.ColumnDefinition, candidate tdengineTimestampCandidate) int {
|
||
preferred := []string{"ts", "timestamp", "event_time", "eventtime", "created_at", "create_time", "occurred_at"}
|
||
for _, name := range preferred {
|
||
for idx, col := range sourceCols {
|
||
if !candidate(col) {
|
||
continue
|
||
}
|
||
if strings.EqualFold(strings.TrimSpace(col.Name), name) {
|
||
return idx
|
||
}
|
||
}
|
||
}
|
||
for idx, col := range sourceCols {
|
||
if candidate(col) {
|
||
return idx
|
||
}
|
||
}
|
||
return -1
|
||
}
|
||
|
||
func reorderTDengineColumns(sourceCols []connection.ColumnDefinition, timestampIndex int) []connection.ColumnDefinition {
|
||
if timestampIndex <= 0 || timestampIndex >= len(sourceCols) {
|
||
cloned := make([]connection.ColumnDefinition, len(sourceCols))
|
||
copy(cloned, sourceCols)
|
||
return cloned
|
||
}
|
||
ordered := make([]connection.ColumnDefinition, 0, len(sourceCols))
|
||
ordered = append(ordered, sourceCols[timestampIndex])
|
||
for idx, col := range sourceCols {
|
||
if idx == timestampIndex {
|
||
continue
|
||
}
|
||
ordered = append(ordered, col)
|
||
}
|
||
return ordered
|
||
}
|
||
|
||
func buildMySQLLikeToTDengineCreateTableSQL(targetQueryTable string, sourceCols []connection.ColumnDefinition, timestampIndex int) (string, []string, []string) {
|
||
ordered := reorderTDengineColumns(sourceCols, timestampIndex)
|
||
columnDefs := make([]string, 0, len(ordered))
|
||
warnings := make([]string, 0)
|
||
unsupported := []string{"源表索引/外键/触发器/唯一约束/自增语义当前不会自动迁移到 TDengine"}
|
||
if timestampIndex != 0 && timestampIndex >= 0 && timestampIndex < len(sourceCols) {
|
||
warnings = append(warnings, fmt.Sprintf("TDengine 基础表要求时间列优先,已将字段 %s 调整为首列", sourceCols[timestampIndex].Name))
|
||
}
|
||
for idx, col := range ordered {
|
||
def, colWarnings := mapMySQLLikeColumnToTDengine(col, idx == 0)
|
||
warnings = append(warnings, colWarnings...)
|
||
columnDefs = append(columnDefs, fmt.Sprintf("%s %s", quoteIdentByType("tdengine", col.Name), def))
|
||
}
|
||
createSQL := fmt.Sprintf("CREATE TABLE %s (\n %s\n)", quoteQualifiedIdentByType("tdengine", targetQueryTable), strings.Join(columnDefs, ",\n "))
|
||
return createSQL, dedupeStrings(warnings), dedupeStrings(unsupported)
|
||
}
|
||
|
||
func buildPGLikeToTDengineCreateTableSQL(targetQueryTable string, sourceCols []connection.ColumnDefinition, timestampIndex int) (string, []string, []string) {
|
||
ordered := reorderTDengineColumns(sourceCols, timestampIndex)
|
||
columnDefs := make([]string, 0, len(ordered))
|
||
warnings := make([]string, 0)
|
||
unsupported := []string{"源表索引/外键/触发器/唯一约束/identity/sequence 语义当前不会自动迁移到 TDengine"}
|
||
if timestampIndex != 0 && timestampIndex >= 0 && timestampIndex < len(sourceCols) {
|
||
warnings = append(warnings, fmt.Sprintf("TDengine 基础表要求时间列优先,已将字段 %s 调整为首列", sourceCols[timestampIndex].Name))
|
||
}
|
||
for idx, col := range ordered {
|
||
def, colWarnings := mapPGLikeColumnToTDengine(col, idx == 0)
|
||
warnings = append(warnings, colWarnings...)
|
||
columnDefs = append(columnDefs, fmt.Sprintf("%s %s", quoteIdentByType("tdengine", col.Name), def))
|
||
}
|
||
createSQL := fmt.Sprintf("CREATE TABLE %s (\n %s\n)", quoteQualifiedIdentByType("tdengine", targetQueryTable), strings.Join(columnDefs, ",\n "))
|
||
return createSQL, dedupeStrings(warnings), dedupeStrings(unsupported)
|
||
}
|
||
|
||
func buildClickHouseToTDengineCreateTableSQL(targetQueryTable string, sourceCols []connection.ColumnDefinition, timestampIndex int) (string, []string, []string) {
|
||
ordered := reorderTDengineColumns(sourceCols, timestampIndex)
|
||
columnDefs := make([]string, 0, len(ordered))
|
||
warnings := make([]string, 0)
|
||
unsupported := []string{"源表 ORDER BY/PARTITION/TTL/Projection/物化视图 语义当前不会自动迁移到 TDengine"}
|
||
if timestampIndex != 0 && timestampIndex >= 0 && timestampIndex < len(sourceCols) {
|
||
warnings = append(warnings, fmt.Sprintf("TDengine 基础表要求时间列优先,已将字段 %s 调整为首列", sourceCols[timestampIndex].Name))
|
||
}
|
||
for idx, col := range ordered {
|
||
def, colWarnings := mapClickHouseColumnToTDengine(col, idx == 0)
|
||
warnings = append(warnings, colWarnings...)
|
||
columnDefs = append(columnDefs, fmt.Sprintf("%s %s", quoteIdentByType("tdengine", col.Name), def))
|
||
}
|
||
createSQL := fmt.Sprintf("CREATE TABLE %s (\n %s\n)", quoteQualifiedIdentByType("tdengine", targetQueryTable), strings.Join(columnDefs, ",\n "))
|
||
return createSQL, dedupeStrings(warnings), dedupeStrings(unsupported)
|
||
}
|
||
|
||
func buildTDengineToTDengineCreateTableSQL(targetQueryTable string, sourceCols []connection.ColumnDefinition, timestampIndex int) (string, []string, []string) {
|
||
ordered := reorderTDengineColumns(sourceCols, timestampIndex)
|
||
columnDefs := make([]string, 0, len(ordered))
|
||
warnings := make([]string, 0)
|
||
unsupported := []string{"源表 supertable/TAGS/TTL/保留策略/索引 语义当前不会自动迁移到 TDengine regular table"}
|
||
if timestampIndex != 0 && timestampIndex >= 0 && timestampIndex < len(sourceCols) {
|
||
warnings = append(warnings, fmt.Sprintf("TDengine 基础表要求时间列优先,已将字段 %s 调整为首列", sourceCols[timestampIndex].Name))
|
||
}
|
||
for idx, col := range ordered {
|
||
def, colWarnings := mapTDengineColumnToTDengine(col, idx == 0)
|
||
warnings = append(warnings, colWarnings...)
|
||
columnDefs = append(columnDefs, fmt.Sprintf("%s %s", quoteIdentByType("tdengine", col.Name), def))
|
||
}
|
||
createSQL := fmt.Sprintf("CREATE TABLE %s (\n %s\n)", quoteQualifiedIdentByType("tdengine", targetQueryTable), strings.Join(columnDefs, ",\n "))
|
||
return createSQL, dedupeStrings(warnings), dedupeStrings(unsupported)
|
||
}
|
||
|
||
func isMySQLLikeTDengineTimestampCandidate(col connection.ColumnDefinition) bool {
|
||
raw := strings.ToLower(strings.TrimSpace(col.Type))
|
||
clean := strings.ReplaceAll(raw, " unsigned", "")
|
||
clean = strings.ReplaceAll(clean, " zerofill", "")
|
||
return strings.HasPrefix(clean, "timestamp") || strings.HasPrefix(clean, "datetime")
|
||
}
|
||
|
||
func isPGLikeTDengineTimestampCandidate(col connection.ColumnDefinition) bool {
|
||
raw := strings.ToLower(strings.TrimSpace(col.Type))
|
||
return strings.HasPrefix(raw, "timestamp")
|
||
}
|
||
|
||
func isClickHouseTDengineTimestampCandidate(col connection.ColumnDefinition) bool {
|
||
lower, _ := unwrapClickHouseTDengineType(col.Type)
|
||
return strings.HasPrefix(lower, "datetime")
|
||
}
|
||
|
||
func isTDengineTDengineTimestampCandidate(col connection.ColumnDefinition) bool {
|
||
base, _ := parseTDengineType(col.Type)
|
||
return base == "TIMESTAMP"
|
||
}
|
||
|
||
func mapMySQLLikeColumnToTDengine(col connection.ColumnDefinition, forceTimestamp bool) (string, []string) {
|
||
warnings := make([]string, 0)
|
||
if forceTimestamp {
|
||
if !isMySQLLikeTDengineTimestampCandidate(col) {
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 已提升为 TDengine 首列 TIMESTAMP", col.Name, col.Type))
|
||
}
|
||
return "TIMESTAMP", warnings
|
||
}
|
||
|
||
raw := strings.ToLower(strings.TrimSpace(col.Type))
|
||
if raw == "" {
|
||
return "VARCHAR(1024)", []string{fmt.Sprintf("字段 %s 类型为空,已降级为 VARCHAR(1024)", col.Name)}
|
||
}
|
||
unsigned := strings.Contains(raw, "unsigned")
|
||
clean := strings.ReplaceAll(raw, " unsigned", "")
|
||
clean = strings.ReplaceAll(clean, " zerofill", "")
|
||
isAutoIncrement := strings.Contains(strings.ToLower(strings.TrimSpace(col.Extra)), "auto_increment")
|
||
if isAutoIncrement {
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 自增语义不会迁移到 TDengine", col.Name))
|
||
}
|
||
if col.Key == "PRI" || col.Key == "PK" {
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 主键语义不会按关系型约束迁移到 TDengine", col.Name))
|
||
}
|
||
|
||
switch {
|
||
case strings.HasPrefix(clean, "tinyint(1)") && !unsigned && !isAutoIncrement:
|
||
return "BOOL", warnings
|
||
case strings.HasPrefix(clean, "tinyint"):
|
||
if unsigned {
|
||
return "UTINYINT", warnings
|
||
}
|
||
return "TINYINT", warnings
|
||
case strings.HasPrefix(clean, "smallint"):
|
||
if unsigned {
|
||
return "USMALLINT", warnings
|
||
}
|
||
return "SMALLINT", warnings
|
||
case strings.HasPrefix(clean, "mediumint"), strings.HasPrefix(clean, "int"), strings.HasPrefix(clean, "integer"):
|
||
if unsigned {
|
||
return "UINT", warnings
|
||
}
|
||
return "INT", warnings
|
||
case strings.HasPrefix(clean, "bigint"):
|
||
if unsigned {
|
||
return "UBIGINT", warnings
|
||
}
|
||
return "BIGINT", warnings
|
||
case strings.HasPrefix(clean, "decimal"), strings.HasPrefix(clean, "numeric"):
|
||
return normalizeTDengineDecimalType(clean), warnings
|
||
case strings.HasPrefix(clean, "float"):
|
||
return "FLOAT", warnings
|
||
case strings.HasPrefix(clean, "double"):
|
||
return "DOUBLE", warnings
|
||
case strings.HasPrefix(clean, "date"):
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 date 已降级映射为 TIMESTAMP", col.Name))
|
||
return "TIMESTAMP", warnings
|
||
case strings.HasPrefix(clean, "timestamp"), strings.HasPrefix(clean, "datetime"):
|
||
return "TIMESTAMP", warnings
|
||
case strings.HasPrefix(clean, "time"):
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 暂无稳定 TDengine 时间-only 映射,已降级为 VARCHAR(64)", col.Name, col.Type))
|
||
return "VARCHAR(64)", warnings
|
||
case strings.HasPrefix(clean, "char("), strings.HasPrefix(clean, "varchar("):
|
||
return fmt.Sprintf("VARCHAR(%d)", normalizeTDengineVarcharLength(extractFirstTypeLength(clean), 255)), warnings
|
||
case strings.HasPrefix(clean, "tinytext"), strings.HasPrefix(clean, "text"), strings.HasPrefix(clean, "mediumtext"), strings.HasPrefix(clean, "longtext"):
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 已降级为 VARCHAR(4096)", col.Name, col.Type))
|
||
return "VARCHAR(4096)", warnings
|
||
case strings.HasPrefix(clean, "json"):
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 因 TDengine JSON 仅适用于 TAG,已降级为 VARCHAR(4096)", col.Name, col.Type))
|
||
return "VARCHAR(4096)", warnings
|
||
case strings.HasPrefix(clean, "enum"), strings.HasPrefix(clean, "set"):
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 已降级为 VARCHAR(255)", col.Name, col.Type))
|
||
return "VARCHAR(255)", warnings
|
||
case strings.HasPrefix(clean, "binary"), strings.HasPrefix(clean, "varbinary"), strings.HasPrefix(clean, "tinyblob"), strings.HasPrefix(clean, "blob"), strings.HasPrefix(clean, "mediumblob"), strings.HasPrefix(clean, "longblob"):
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 已按字符串语义降级为 VARCHAR(4096)", col.Name, col.Type))
|
||
return "VARCHAR(4096)", warnings
|
||
default:
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 暂无专门 TDengine 映射,已降级为 VARCHAR(1024)", col.Name, col.Type))
|
||
return "VARCHAR(1024)", warnings
|
||
}
|
||
}
|
||
|
||
func mapPGLikeColumnToTDengine(col connection.ColumnDefinition, forceTimestamp bool) (string, []string) {
|
||
warnings := make([]string, 0)
|
||
if forceTimestamp {
|
||
if raw := strings.ToLower(strings.TrimSpace(col.Type)); !strings.HasPrefix(raw, "timestamp") {
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 已提升为 TDengine 首列 TIMESTAMP", col.Name, col.Type))
|
||
}
|
||
return "TIMESTAMP", warnings
|
||
}
|
||
|
||
raw := strings.ToLower(strings.TrimSpace(col.Type))
|
||
if raw == "" {
|
||
return "VARCHAR(1024)", []string{fmt.Sprintf("字段 %s 类型为空,已降级为 VARCHAR(1024)", col.Name)}
|
||
}
|
||
if col.Key == "PRI" || col.Key == "PK" {
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 主键语义不会按关系型约束迁移到 TDengine", col.Name))
|
||
}
|
||
if strings.Contains(strings.ToLower(strings.TrimSpace(col.Extra)), "identity") || strings.Contains(strings.ToLower(strings.TrimSpace(col.Extra)), "auto_increment") {
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 自增/identity 语义不会迁移到 TDengine", col.Name))
|
||
}
|
||
|
||
switch {
|
||
case raw == "boolean" || strings.HasPrefix(raw, "bool"):
|
||
return "BOOL", warnings
|
||
case raw == "smallint":
|
||
return "SMALLINT", warnings
|
||
case raw == "integer" || raw == "int4":
|
||
return "INT", warnings
|
||
case raw == "bigint" || raw == "int8":
|
||
return "BIGINT", warnings
|
||
case strings.HasPrefix(raw, "numeric"), strings.HasPrefix(raw, "decimal"):
|
||
return normalizeTDengineDecimalType(raw), warnings
|
||
case raw == "real" || raw == "float4":
|
||
return "FLOAT", warnings
|
||
case raw == "double precision" || raw == "float8":
|
||
return "DOUBLE", warnings
|
||
case raw == "date":
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 date 已降级映射为 TIMESTAMP", col.Name))
|
||
return "TIMESTAMP", warnings
|
||
case strings.HasPrefix(raw, "timestamp"):
|
||
return "TIMESTAMP", warnings
|
||
case strings.HasPrefix(raw, "time"):
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 暂无稳定 TDengine 时间-only 映射,已降级为 VARCHAR(64)", col.Name, col.Type))
|
||
return "VARCHAR(64)", warnings
|
||
case strings.HasPrefix(raw, "character varying("), strings.HasPrefix(raw, "varchar("), strings.HasPrefix(raw, "character("), strings.HasPrefix(raw, "char("):
|
||
return fmt.Sprintf("VARCHAR(%d)", normalizeTDengineVarcharLength(extractFirstTypeLength(raw), 255)), warnings
|
||
case raw == "text":
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 text 已降级为 VARCHAR(4096)", col.Name))
|
||
return "VARCHAR(4096)", warnings
|
||
case raw == "uuid":
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 uuid 已降级为 VARCHAR(36)", col.Name))
|
||
return "VARCHAR(36)", warnings
|
||
case raw == "json" || raw == "jsonb":
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 因 TDengine JSON 仅适用于 TAG,已降级为 VARCHAR(4096)", col.Name, col.Type))
|
||
return "VARCHAR(4096)", warnings
|
||
case raw == "bytea":
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 bytea 已按字符串语义降级为 VARCHAR(4096)", col.Name))
|
||
return "VARCHAR(4096)", warnings
|
||
case strings.HasSuffix(raw, "[]") || strings.HasPrefix(raw, "array"):
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 已降级为 VARCHAR(4096)", col.Name, col.Type))
|
||
return "VARCHAR(4096)", warnings
|
||
case raw == "user-defined":
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 为用户自定义类型,已降级为 VARCHAR(1024)", col.Name))
|
||
return "VARCHAR(1024)", warnings
|
||
default:
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 暂无专门 TDengine 映射,已降级为 VARCHAR(1024)", col.Name, col.Type))
|
||
return "VARCHAR(1024)", warnings
|
||
}
|
||
}
|
||
|
||
func mapClickHouseColumnToTDengine(col connection.ColumnDefinition, forceTimestamp bool) (string, []string) {
|
||
warnings := make([]string, 0)
|
||
if forceTimestamp {
|
||
if !isClickHouseTDengineTimestampCandidate(col) {
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 已提升为 TDengine 首列 TIMESTAMP", col.Name, col.Type))
|
||
}
|
||
return "TIMESTAMP", warnings
|
||
}
|
||
|
||
lower, _ := unwrapClickHouseTDengineType(col.Type)
|
||
if lower == "" {
|
||
return "VARCHAR(1024)", []string{fmt.Sprintf("字段 %s 类型为空,已降级为 VARCHAR(1024)", col.Name)}
|
||
}
|
||
|
||
switch {
|
||
case lower == "bool" || lower == "boolean":
|
||
return "BOOL", warnings
|
||
case lower == "int8":
|
||
return "TINYINT", warnings
|
||
case lower == "uint8":
|
||
return "UTINYINT", warnings
|
||
case lower == "int16":
|
||
return "SMALLINT", warnings
|
||
case lower == "uint16":
|
||
return "USMALLINT", warnings
|
||
case lower == "int32":
|
||
return "INT", warnings
|
||
case lower == "uint32":
|
||
return "UINT", warnings
|
||
case lower == "int64":
|
||
return "BIGINT", warnings
|
||
case lower == "uint64":
|
||
return "UBIGINT", warnings
|
||
case lower == "float32":
|
||
return "FLOAT", warnings
|
||
case lower == "float64":
|
||
return "DOUBLE", warnings
|
||
case lower == "date":
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 date 已降级映射为 TIMESTAMP", col.Name))
|
||
return "TIMESTAMP", warnings
|
||
case strings.HasPrefix(lower, "datetime"):
|
||
return "TIMESTAMP", warnings
|
||
case lower == "string":
|
||
return "VARCHAR(1024)", warnings
|
||
case lower == "uuid":
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 uuid 已降级为 VARCHAR(36)", col.Name))
|
||
return "VARCHAR(36)", warnings
|
||
case lower == "json", strings.HasPrefix(lower, "map("), strings.HasPrefix(lower, "array("), strings.HasPrefix(lower, "tuple("), strings.HasPrefix(lower, "nested("):
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 已降级为 VARCHAR(4096)", col.Name, col.Type))
|
||
return "VARCHAR(4096)", warnings
|
||
case strings.HasPrefix(lower, "enum8("), strings.HasPrefix(lower, "enum16("):
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 枚举类型 %s 已降级为 VARCHAR(255)", col.Name, col.Type))
|
||
return "VARCHAR(255)", warnings
|
||
case clickHouseDecimalPattern.MatchString(lower):
|
||
parts := clickHouseDecimalPattern.FindStringSubmatch(lower)
|
||
return fmt.Sprintf("DECIMAL(%s,%s)", parts[2], parts[3]), warnings
|
||
case clickHouseStringArgsPattern.MatchString(lower):
|
||
parts := clickHouseStringArgsPattern.FindStringSubmatch(lower)
|
||
length, err := strconv.Atoi(parts[1])
|
||
if err != nil {
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s FixedString 长度解析失败,已降级为 VARCHAR(255)", col.Name))
|
||
return "VARCHAR(255)", warnings
|
||
}
|
||
return fmt.Sprintf("VARCHAR(%d)", normalizeTDengineVarcharLength(length, 255)), warnings
|
||
default:
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 暂无专门 TDengine 映射,已降级为 VARCHAR(1024)", col.Name, col.Type))
|
||
return "VARCHAR(1024)", warnings
|
||
}
|
||
}
|
||
|
||
func mapTDengineColumnToTDengine(col connection.ColumnDefinition, forceTimestamp bool) (string, []string) {
|
||
warnings := make([]string, 0)
|
||
if forceTimestamp {
|
||
if !isTDengineTDengineTimestampCandidate(col) {
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 已提升为 TDengine 首列 TIMESTAMP", col.Name, col.Type))
|
||
}
|
||
return "TIMESTAMP", warnings
|
||
}
|
||
|
||
base, length := parseTDengineType(col.Type)
|
||
if base == "" {
|
||
return "VARCHAR(1024)", []string{fmt.Sprintf("字段 %s 类型为空,已降级为 VARCHAR(1024)", col.Name)}
|
||
}
|
||
if isTDengineTagColumn(col) {
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 为 TDengine TAG 列,迁移到 regular table 后将降级为普通字段", col.Name))
|
||
}
|
||
|
||
switch base {
|
||
case "BOOL", "BOOLEAN":
|
||
return "BOOL", warnings
|
||
case "TINYINT":
|
||
return "TINYINT", warnings
|
||
case "UTINYINT":
|
||
return "UTINYINT", warnings
|
||
case "SMALLINT":
|
||
return "SMALLINT", warnings
|
||
case "USMALLINT":
|
||
return "USMALLINT", warnings
|
||
case "INT", "INTEGER":
|
||
return "INT", warnings
|
||
case "UINT":
|
||
return "UINT", warnings
|
||
case "BIGINT":
|
||
return "BIGINT", warnings
|
||
case "UBIGINT":
|
||
return "UBIGINT", warnings
|
||
case "FLOAT":
|
||
return "FLOAT", warnings
|
||
case "DOUBLE":
|
||
return "DOUBLE", warnings
|
||
case "DECIMAL", "NUMERIC":
|
||
return normalizeTDengineDecimalType(col.Type), warnings
|
||
case "TIMESTAMP":
|
||
return "TIMESTAMP", warnings
|
||
case "DATE":
|
||
return "DATE", warnings
|
||
case "JSON":
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 JSON 在 TDengine regular table 中不保留 TAG 语义,已降级为 VARCHAR(4096)", col.Name))
|
||
return "VARCHAR(4096)", warnings
|
||
case "BINARY", "NCHAR", "VARCHAR", "VARBINARY":
|
||
if length > 0 {
|
||
return fmt.Sprintf("%s(%d)", base, normalizeTDengineVarcharLength(length, length)), warnings
|
||
}
|
||
fallback := 255
|
||
if base == "VARCHAR" {
|
||
fallback = 1024
|
||
}
|
||
return fmt.Sprintf("%s(%d)", base, fallback), warnings
|
||
default:
|
||
warnings = append(warnings, fmt.Sprintf("字段 %s 类型 %s 暂无专门 TDengine 同库映射,已降级为 VARCHAR(1024)", col.Name, col.Type))
|
||
return "VARCHAR(1024)", warnings
|
||
}
|
||
}
|
||
|
||
func unwrapClickHouseTDengineType(raw string) (string, bool) {
|
||
text := strings.TrimSpace(raw)
|
||
lower := strings.ToLower(text)
|
||
nullable := false
|
||
for {
|
||
switched := false
|
||
if strings.HasPrefix(lower, "nullable(") && strings.HasSuffix(lower, ")") {
|
||
text = strings.TrimSpace(text[len("Nullable(") : len(text)-1])
|
||
lower = strings.ToLower(text)
|
||
nullable = true
|
||
switched = true
|
||
}
|
||
if strings.HasPrefix(lower, "lowcardinality(") && strings.HasSuffix(lower, ")") {
|
||
text = strings.TrimSpace(text[len("LowCardinality(") : len(text)-1])
|
||
lower = strings.ToLower(text)
|
||
switched = true
|
||
}
|
||
if !switched {
|
||
break
|
||
}
|
||
}
|
||
return lower, nullable
|
||
}
|
||
|
||
func normalizeTDengineDecimalType(raw string) string {
|
||
text := strings.TrimSpace(raw)
|
||
if text == "" {
|
||
return "DECIMAL(38,10)"
|
||
}
|
||
lower := strings.ToLower(text)
|
||
if strings.HasPrefix(lower, "numeric") {
|
||
return "DECIMAL" + text[len("numeric"):]
|
||
}
|
||
if strings.HasPrefix(lower, "decimal") {
|
||
return "DECIMAL" + text[len("decimal"):]
|
||
}
|
||
return "DECIMAL(38,10)"
|
||
}
|
||
|
||
func normalizeTDengineVarcharLength(length int, fallback int) int {
|
||
if fallback <= 0 {
|
||
fallback = 255
|
||
}
|
||
if length <= 0 {
|
||
return fallback
|
||
}
|
||
if length > 16384 {
|
||
return 16384
|
||
}
|
||
return length
|
||
}
|
||
|
||
func extractFirstTypeLength(raw string) int {
|
||
start := strings.Index(raw, "(")
|
||
if start < 0 {
|
||
return 0
|
||
}
|
||
end := strings.Index(raw[start+1:], ")")
|
||
if end < 0 {
|
||
return 0
|
||
}
|
||
inside := strings.TrimSpace(raw[start+1 : start+1+end])
|
||
if inside == "" {
|
||
return 0
|
||
}
|
||
parts := strings.SplitN(inside, ",", 2)
|
||
length, err := strconv.Atoi(strings.TrimSpace(parts[0]))
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
return length
|
||
}
|