refactor:获取表索引,默认过滤主键索引

This commit is contained in:
zongyangleo
2024-03-27 08:22:26 +08:00
parent fc166650b3
commit 54ad19f97e
6 changed files with 14 additions and 29 deletions

View File

@@ -294,18 +294,10 @@ func (d *dbAppImpl) DumpDb(ctx context.Context, reqParam *DumpDbReq) error {
indexs, err := dbMeta.GetTableIndex(tableName)
biz.ErrIsNil(err)
// 过滤主键索引
idxs := make([]dbi.Index, 0)
for _, idx := range indexs {
if !idx.IsPrimaryKey {
idxs = append(idxs, idx)
}
}
if len(idxs) > 0 {
if len(indexs) > 0 {
// 最后添加索引
writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表索引: %s \n-- ----------------------------\n", tableName))
sqlArr := dbMeta.GenerateIndexDDL(idxs, tabInfo)
sqlArr := dbMeta.GenerateIndexDDL(indexs, tabInfo)
for _, sqlStr := range sqlArr {
writer.WriteString(sqlStr + ";\n")
}

View File

@@ -324,18 +324,10 @@ func (app *dbTransferAppImpl) transferIndex(_ context.Context, tableInfo dbi.Tab
return nil
}
// 过滤主键索引
idxs := make([]dbi.Index, 0)
for _, idx := range indexs {
if !idx.IsPrimaryKey {
idxs = append(idxs, idx)
}
}
if len(idxs) == 0 {
if len(indexs) == 0 {
return nil
}
// 通过表名、索引信息生成建索引语句,并执行到目标表
return targetDialect.CreateIndex(tableInfo, idxs)
return targetDialect.CreateIndex(tableInfo, indexs)
}

View File

@@ -44,6 +44,7 @@ WHERE
SELECT
database ()
)
AND index_name != 'PRIMARY'
AND table_name = ?
ORDER BY
index_name asc,

View File

@@ -39,14 +39,12 @@ SELECT ai.INDEX_NAME AS INDEX_NAME,
AND aic.INDEX_NAME = ai.INDEX_NAME
AND aic.TABLE_NAME = ai.TABLE_NAME
AND ROWNUM = 1) AS INDEX_COMMENT,
CASE
WHEN ai.INDEX_NAME like 'PK_%%' THEN 1
WHEN ai.INDEX_NAME like 'SYS_%%' THEN 1
ELSE 0
END AS IS_PRIMARY
0 AS IS_PRIMARY
FROM ALL_INDEXES ai
WHERE ai.OWNER = (SELECT sys_context('USERENV', 'CURRENT_SCHEMA') FROM DUAL)
AND ai.table_name = '%s'
AND ai.INDEX_NAME not like 'PK_%%'
AND ai.INDEX_NAME not like 'SYS_%%'
---------------------------------------
--ORACLE_COLUMN_MA 表列信息
SELECT a.TABLE_NAME as TABLE_NAME,

View File

@@ -35,19 +35,20 @@ where
order by c.relname
---------------------------------------
--PGSQL_INDEX_INFO 表索引信息
SELECT indexname AS "indexName",
SELECT a.indexname AS "indexName",
'BTREE' AS "IndexType",
case when indexdef like 'CREATE UNIQUE INDEX%%' then 1 else 0 end as "isUnique",
case when a.indexdef like 'CREATE UNIQUE INDEX%%' then 1 else 0 end as "isUnique",
obj_description(b.oid, 'pg_class') AS "indexComment",
indexdef AS "indexDef",
c.attname AS "columnName",
c.attnum AS "seqInIndex",
case when indexname like '%_pkey' then 1 else 0 end as "isPrimaryKey"
case when a.indexname like '%_pkey' then 1 else 0 end AS "isPrimaryKey"
FROM pg_indexes a
join pg_class b on a.indexname = b.relname
join pg_attribute c on b.oid = c.attrelid
WHERE a.schemaname = (select current_schema())
AND a.tablename = '%s';
AND a.tablename = '%s'
AND a.indexname not like '%_pkey'
---------------------------------------
--PGSQL_COLUMN_MA 表列信息
SELECT a.table_name AS "tableName",

View File

@@ -144,6 +144,7 @@ func (md *MssqlMetaData) GetPrimaryKey(tablename string) (string, error) {
return columns[0].ColumnName, nil
}
// 需要收集唯一键涉及的字段,所以需要查询出带主键的索引
func (md *MssqlMetaData) getTableIndexWithPK(tableName string) ([]dbi.Index, error) {
_, res, err := md.dc.Query(dbi.GetLocalSql(MSSQL_META_FILE, MSSQL_INDEX_INFO_KEY), md.dc.Info.CurrentSchema(), tableName)
if err != nil {