Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: new mssql sync (#15622)

rebelicefc31b130c9c9
diff --git a/backend/api/v1/database_converter.go b/backend/api/v1/database_converter.go
index d5efeee76a..069b4ac187 100644
--- a/backend/api/v1/database_converter.go
+++ b/backend/api/v1/database_converter.go
@@ -441,18 +441,22 @@ func convertStoreTablePartitionMetadata(partition *storepb.TablePartitionMetadat
func convertStoreColumnMetadata(column *storepb.ColumnMetadata) *v1pb.ColumnMetadata {
metadata := &v1pb.ColumnMetadata{
- Name: column.Name,
- Position: column.Position,
- HasDefault: column.DefaultValue != nil,
- DefaultOnNull: column.DefaultOnNull,
- OnUpdate: column.OnUpdate,
- Nullable: column.Nullable,
- Type: column.Type,
- CharacterSet: column.CharacterSet,
- Collation: column.Collation,
- Comment: column.Comment,
- UserComment: column.UserComment,
- Generation: convertStoreGenerationMetadata(column.Generation),
+ Name: column.Name,
+ Position: column.Position,
+ HasDefault: column.DefaultValue != nil,
+ DefaultOnNull: column.DefaultOnNull,
+ OnUpdate: column.OnUpdate,
+ Nullable: column.Nullable,
+ Type: column.Type,
+ CharacterSet: column.CharacterSet,
+ Collation: column.Collation,
+ Comment: column.Comment,
+ UserComment: column.UserComment,
+ Generation: convertStoreGenerationMetadata(column.Generation),
+ IdentityGeneration: v1pb.ColumnMetadata_IdentityGeneration(column.IdentityGeneration),
+ IsIdentity: column.IsIdentity,
+ IdentitySeed: column.IdentitySeed,
+ IdentityIncrement: column.IdentityIncrement,
}
if metadata.HasDefault {
switch value := column.DefaultValue.(type) {
@@ -884,17 +888,21 @@ func convertV1TablePartitionMetadata(tablePartition *v1pb.TablePartitionMetadata
func convertV1ColumnMetadata(column *v1pb.ColumnMetadata) *storepb.ColumnMetadata {
metadata := &storepb.ColumnMetadata{
- Name: column.Name,
- Position: column.Position,
- Nullable: column.Nullable,
- DefaultOnNull: column.DefaultOnNull,
- Type: column.Type,
- CharacterSet: column.CharacterSet,
- Collation: column.Collation,
- Comment: column.Comment,
- UserComment: column.UserComment,
- OnUpdate: column.OnUpdate,
- Generation: convertV1GenerationMetadata(column.Generation),
+ Name: column.Name,
+ Position: column.Position,
+ Nullable: column.Nullable,
+ DefaultOnNull: column.DefaultOnNull,
+ Type: column.Type,
+ CharacterSet: column.CharacterSet,
+ Collation: column.Collation,
+ Comment: column.Comment,
+ UserComment: column.UserComment,
+ OnUpdate: column.OnUpdate,
+ Generation: convertV1GenerationMetadata(column.Generation),
+ IdentityGeneration: storepb.ColumnMetadata_IdentityGeneration(column.IdentityGeneration),
+ IsIdentity: column.IsIdentity,
+ IdentitySeed: column.IdentitySeed,
+ IdentityIncrement: column.IdentityIncrement,
}
if column.HasDefault {
diff --git a/backend/plugin/db/mssql/dump.go b/backend/plugin/db/mssql/dump.go
index 64b893382c..9e62acd25c 100644
--- a/backend/plugin/db/mssql/dump.go
+++ b/backend/plugin/db/mssql/dump.go
@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"log/slog"
- "strings"
"github.com/pkg/errors"
@@ -446,21 +445,6 @@ func assembleForeignKey(out io.Writer, fkMeta []*foreignKeyMeta) error {
return nil
}
-func referentialAction(action int) string {
- switch action {
- case 0:
- return "NO ACTION"
- case 1:
- return "CASCADE"
- case 2:
- return "SET NULL"
- case 3:
- return "SET DEFAULT"
- default:
- return "NO ACTION"
- }
-}
-
func mergeFKMetaMap(fkMetaMap []*foreignKeyMeta) [][]*foreignKeyMeta {
var result [][]*foreignKeyMeta
lastIdx := 0
@@ -1122,18 +1106,6 @@ type tableMeta struct {
currentValue sql.NullString
}
-func quote(s string) string {
- return fmt.Sprintf("N'%s'", s)
-}
-
-func quoteList(schemas []string) string {
- var quoted []string
- for _, schema := range schemas {
- quoted = append(quoted, quote(schema))
- }
- return strings.Join(quoted, ",")
-}
-
func dumpTableTxn(ctx context.Context, txn *sql.Tx, schemas []string) (map[string][]*tableMeta, error) {
tableMap := make(map[string][]*tableMeta)
slog.Debug("running dump table query", slog.String("schemas", fmt.Sprintf("%v", schemas)))
diff --git a/backend/plugin/db/mssql/sync.go b/backend/plugin/db/mssql/sync.go
index 1a43dd29f2..b226009927 100644
--- a/backend/plugin/db/mssql/sync.go
+++ b/backend/plugin/db/mssql/sync.go
@@ -68,11 +68,11 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchema
if err != nil {
return nil, errors.Wrapf(err, "failed to get schemas from database %q", driver.databaseName)
}
- columnMap, err := getTableColumns(txn)
+ columnMap, err := getTableColumns(txn, schemaNames)
if err != nil {
return nil, errors.Wrapf(err, "failed to get table columns from database %q", driver.databaseName)
}
- tableMap, err := getTables(txn, columnMap)
+ tableMap, err := getTables(txn, schemaNames, columnMap)
if err != nil {
return nil, errors.Wrapf(err, "failed to get tables from database %q", driver.databaseName)
}
@@ -139,18 +139,22 @@ func getSchemas(txn *sql.Tx) ([]string, error) {
}
// getTables gets all tables of a database.
-func getTables(txn *sql.Tx, columnMap map[db.TableKey][]*storepb.ColumnMetadata) (map[string][]*storepb.TableMetadata, error) {
- indexMap, err := getIndexes(txn)
+func getTables(txn *sql.Tx, schemas []string, columnMap map[db.TableKey][]*storepb.ColumnMetadata) (map[string][]*storepb.TableMetadata, error) {
+ indexMap, err := getKeyAndIndexes(txn, schemas)
if err != nil {
return nil, errors.Wrapf(err, "failed to get indices")
}
- fkMap, err := getForeignKeys(txn)
+ fkMap, err := getForeignKeys(txn, schemas)
if err != nil {
return nil, errors.Wrapf(err, "failed to get foreign keys")
}
- // TODO(d): foreign keys.
+ checkMap, err := getChecks(txn, schemas)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get checks")
+ }
+
tableMap := make(map[string][]*storepb.TableMetadata)
query := `
SELECT
@@ -191,6 +195,7 @@ func getTables(txn *sql.Tx, columnMap map[db.TableKey][]*storepb.ColumnMetadata)
table.Columns = columnMap[key]
table.Indexes = indexMap[key]
table.ForeignKeys = fkMap[key]
+ table.CheckConstraints = checkMap[key]
if comment.Valid {
table.Comment = comment.String
}
@@ -204,79 +209,124 @@ func getTables(txn *sql.Tx, columnMap map[db.TableKey][]*storepb.ColumnMetadata)
return tableMap, nil
}
-// https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-foreign-key-columns-transact-sql?view=sql-server-ver16#example-query
-var listForeignKeyQuery = `
-SELECT fk.name AS ForeignKeyName,
- s_parent.name AS ParentSchemaName,
- t_parent.name AS ParentTableName,
- c_parent.name AS ParentColumnName,
- s_child.name AS ReferencedSchemaName,
- t_child.name AS ReferencedTableName,
- c_child.name AS ReferencedColumnName,
- CASE fk.delete_referential_action
- WHEN 0 THEN ''
- WHEN 1 THEN 'CASCADE'
- WHEN 2 THEN 'SET NULL'
- WHEN 3 THEN 'SET DEFAULT'
- END AS OnDeleteAction,
- CASE fk.update_referential_action
- WHEN 0 THEN ''
- WHEN 1 THEN 'CASCADE'
- WHEN 2 THEN 'SET NULL'
- WHEN 3 THEN 'SET DEFAULT'
- END AS OnUpdateAction
-FROM sys.foreign_keys fk
-INNER JOIN sys.foreign_key_columns fkc
- ON fkc.constraint_object_id = fk.object_id
-INNER JOIN sys.tables t_parent
- ON t_parent.object_id = fk.parent_object_id
-INNER JOIN sys.schemas s_parent
- ON s_parent.schema_id = t_parent.schema_id
-INNER JOIN sys.columns c_parent
- ON fkc.parent_column_id = c_parent.column_id
- AND c_parent.object_id = t_parent.object_id
-INNER JOIN sys.tables t_child
- ON t_child.object_id = fk.referenced_object_id
-INNER JOIN sys.schemas s_child
- ON s_child.schema_id = t_child.schema_id
-INNER JOIN sys.columns c_child
- ON c_child.object_id = t_child.object_id
- AND fkc.referenced_column_id = c_child.column_id
-ORDER BY fk.name, t_parent.name, t_child.name, c_parent.name, c_child.name;
-`
-
-func getForeignKeys(txn *sql.Tx) (map[db.TableKey][]*storepb.ForeignKeyMetadata, error) {
- fkMap := make(map[db.TableKey]map[string]*storepb.ForeignKeyMetadata)
+func getChecks(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.CheckConstraintMetadata, error) {
+ checkMap := make(map[db.TableKey][]*storepb.CheckConstraintMetadata)
+ dumpCheckConstraintSQL := fmt.Sprintf(`
+ SELECT
+ t.schema_name,
+ t.name AS table_name,
+ c.name,
+ c.comment,
+ c.definition
+ FROM
+ (SELECT s.name as schema_name, o.name, o.object_id, o.type FROM sys.all_objects o LEFT JOIN sys.schemas s ON s.schema_id = o.schema_id WHERE s.name in (%s) ) t
+ INNER JOIN (SELECT ch.name, ch.object_id, ch.parent_object_id, ch.is_disabled, CAST(p.[value] AS nvarchar(4000)) AS comment, ch.is_not_for_replication, ch.definition FROM sys.check_constraints ch LEFT JOIN sys.extended_properties p ON p.major_id = ch.object_id AND p.minor_id = 0 AND p.name = 'MS_Description') c ON c.parent_object_id = t.object_id
+ LEFT JOIN sys.objects co ON co.object_id = c.object_id
+ ORDER BY t.schema_name ASC, t.object_id ASC, c.object_id ASC
+ `, quoteList(schemas))
- rows, err := txn.Query(listForeignKeyQuery)
+ rows, err := txn.Query(dumpCheckConstraintSQL)
+ if err != nil {
+ return nil, util.FormatErrorWithQuery(err, dumpCheckConstraintSQL)
+ }
+ defer rows.Close()
+ for rows.Next() {
+ var schemaName, tableName, checkName, comment, definition sql.NullString
+ if err := rows.Scan(&schemaName, &tableName, &checkName, &comment, &definition); err != nil {
+ return nil, err
+ }
+ if !schemaName.Valid || !tableName.Valid || !checkName.Valid || !definition.Valid {
+ continue
+ }
+ key := db.TableKey{Schema: schemaName.String, Table: tableName.String}
+ // todo: set comments.
+ _ = comment
+ checkMap[key] = append(checkMap[key], &storepb.CheckConstraintMetadata{
+ Name: checkName.String,
+ Expression: definition.String,
+ })
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+ return checkMap, nil
+}
+
+func referentialAction(action int) string {
+ switch action {
+ case 0:
+ return "NO ACTION"
+ case 1:
+ return "CASCADE"
+ case 2:
+ return "SET NULL"
+ case 3:
+ return "SET DEFAULT"
+ default:
+ return "NO ACTION"
+ }
+}
+
+func getForeignKeys(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.ForeignKeyMetadata, error) {
+ fkMap := make(map[db.TableKey]map[string]*storepb.ForeignKeyMetadata)
+ dumpForeignKeySQL := fmt.Sprintf(`
+ SELECT
+ t.schema_name,
+ t.name AS table_name,
+ f.name,
+ f.referenced_schema,
+ f.referenced_table,
+ f.comment,
+ f.delete_referential_action,
+ f.update_referential_action,
+ f.parent_column,
+ f.referenced_column
+ FROM (SELECT s.name AS schema_name, o.name, o.object_id, o.type FROM sys.all_objects o LEFT JOIN sys.schemas s ON s.schema_id = o.schema_id WHERE s.name in (%s) ) t
+ INNER JOIN (SELECT fk.object_id, fk.parent_object_id, fk.name, OBJECT_SCHEMA_NAME(fk.referenced_object_id) AS referenced_schema, OBJECT_NAME(fk.referenced_object_id) AS referenced_table, fk.is_disabled, fk.is_not_for_replication, fk.delete_referential_action, fk.update_referential_action, fc.parent_column, CAST(p.[value] AS nvarchar(4000)) AS comment, fc.referenced_column FROM sys.foreign_keys fk LEFT JOIN (SELECT fkc.constraint_object_id, pc.name AS parent_column, rc.name AS referenced_column FROM sys.foreign_key_columns fkc LEFT JOIN sys.all_columns pc ON pc.object_id = fkc.parent_object_id AND pc.column_id = fkc.parent_column_id LEFT JOIN sys.all_columns rc ON rc.object_id = fkc.referenced_object_id AND rc.column_id = fkc.referenced_column_id) fc ON fc.constraint_object_id = fk.object_id LEFT JOIN sys.extended_properties p ON p.major_id = fk.object_id AND p.minor_id = 0 AND p.name = 'MS_Description' ) f ON f.parent_object_id = t.object_id
+ LEFT JOIN sys.objects co ON co.object_id = f.object_id
+ ORDER BY t.schema_name ASC, t.object_id ASC, f.object_id ASC
+ `, quoteList(schemas))
+
+ rows, err := txn.Query(dumpForeignKeySQL)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
- var fkName, parentSchemaName, parentTableName, parentColumnName, referencedSchemaName, referencedTableName, referencedColumnName, onDelete, onUpdate string
- if err := rows.Scan(&fkName, &parentSchemaName, &parentTableName, &parentColumnName, &referencedSchemaName, &referencedTableName, &referencedColumnName, &onDelete, &onUpdate); err != nil {
+ var schemaName, tableName, fkName, referencedSchemaName, referencedTableName, comment, parentColumnName, referencedColumnName sql.NullString
+ var onDelete, onUpdate sql.NullInt32
+ if err := rows.Scan(&schemaName, &tableName, &fkName, &referencedSchemaName, &referencedTableName, &comment, &onDelete, &onUpdate, &parentColumnName, &referencedColumnName); err != nil {
return nil, err
}
- outerKey := db.TableKey{Schema: parentSchemaName, Table: parentTableName}
+ if !schemaName.Valid || !tableName.Valid || !fkName.Valid || !referencedSchemaName.Valid || !referencedTableName.Valid || !parentColumnName.Valid || !referencedColumnName.Valid {
+ continue
+ }
+ outerKey := db.TableKey{Schema: schemaName.String, Table: tableName.String}
if _, ok := fkMap[outerKey]; !ok {
fkMap[outerKey] = make(map[string]*storepb.ForeignKeyMetadata)
}
- if _, ok := fkMap[outerKey][fkName]; !ok {
- fkMap[outerKey][fkName] = &storepb.ForeignKeyMetadata{
- Name: fkName,
- Columns: []string{parentColumnName},
- ReferencedSchema: referencedSchemaName,
- ReferencedTable: referencedTableName,
- ReferencedColumns: []string{referencedColumnName},
- OnDelete: onDelete,
- OnUpdate: onUpdate,
+ if _, ok := fkMap[outerKey][fkName.String]; !ok {
+ fk := &storepb.ForeignKeyMetadata{
+ Name: fkName.String,
+ ReferencedSchema: referencedSchemaName.String,
+ ReferencedTable: referencedTableName.String,
}
- } else {
- fkMap[outerKey][fkName].Columns = append(fkMap[outerKey][fkName].Columns, parentColumnName)
- fkMap[outerKey][fkName].ReferencedColumns = append(fkMap[outerKey][fkName].ReferencedColumns, referencedColumnName)
+ // Set comments.
+ _ = comment
+
+ if onDelete.Valid {
+ fk.OnDelete = referentialAction(int(onDelete.Int32))
+ }
+ if onUpdate.Valid {
+ fk.OnUpdate = referentialAction(int(onUpdate.Int32))
+ }
+
+ fkMap[outerKey][fkName.String] = fk
}
+
+ fkMap[outerKey][fkName.String].Columns = append(fkMap[outerKey][fkName.String].Columns, parentColumnName.String)
+ fkMap[outerKey][fkName.String].ReferencedColumns = append(fkMap[outerKey][fkName.String].ReferencedColumns, referencedColumnName.String)
}
if err := rows.Err(); err != nil {
@@ -299,69 +349,112 @@ func getForeignKeys(txn *sql.Tx) (map[db.TableKey][]*storepb.ForeignKeyMetadata,
return result, nil
}
+func quote(s string) string {
+ return fmt.Sprintf("N'%s'", s)
+}
+
+func quoteList(schemas []string) string {
+ var quoted []string
+ for _, schema := range schemas {
+ quoted = append(quoted, quote(schema))
+ }
+ return strings.Join(quoted, ",")
+}
+
// getTableColumns gets the columns of a table.
-func getTableColumns(txn *sql.Tx) (map[db.TableKey][]*storepb.ColumnMetadata, error) {
+func getTableColumns(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.ColumnMetadata, error) {
columnsMap := make(map[db.TableKey][]*storepb.ColumnMetadata)
- query := `
- SELECT
- IC.table_schema,
- IC.table_name,
- IC.column_name,
- IC.data_type,
- IC.character_maximum_length,
- IC.ordinal_position,
- IC.column_default,
- IC.is_nullable,
- IC.collation_name,
- IJ.PropertyValue AS ColumnComment
- FROM INFORMATION_SCHEMA.COLUMNS IC
- LEFT JOIN (
- SELECT
- EP.value AS PropertyValue,
- S.name AS SchemaName,
- O.name AS TableName,
- C.name AS ColumnName
- FROM
- (SELECT major_id, minor_id, name, value FROM sys.extended_properties WHERE name = 'MS_Description') AS EP
- INNER JOIN sys.all_objects AS O ON EP.major_id = O.object_id
- INNER JOIN sys.schemas AS S ON O.schema_id = S.schema_id
- INNER JOIN sys.columns AS C ON EP.major_id = C.object_id AND EP.minor_id = C.column_id
- WHERE S.name IS NOT NULL AND O.name IS NOT NULL AND C.name IS NOT NULL
- ) IJ ON IJ.SchemaName = IC.table_schema AND IJ.TableName = IC.table_name AND IJ.ColumnName = IC.column_name
- ORDER BY IC.table_schema, IC.table_name, IC.ordinal_position;`
- rows, err := txn.Query(query)
+ getColumnSQL := fmt.Sprintf(`
+ SELECT
+ s.name AS schema_name,
+ OBJECT_NAME(c.object_id) AS table_name,
+ c.name AS column_name,
+ t.name AS type_name,
+ c.is_computed,
+ cc.definition,
+ cc.is_persisted,
+ c.max_length,
+ c.precision AS precision,
+ c.scale,
+ c.collation_name,
+ c.is_nullable,
+ c.is_identity,
+ d.definition AS default_value,
+ CAST(p.[value] AS nvarchar(4000)) AS comment,
+ id.seed_value AS seed_value,
+ id.increment_value AS increment_value
+ FROM sys.columns c
+ LEFT JOIN sys.computed_columns cc ON cc.object_id = c.object_id AND cc.column_id = c.column_id
+ LEFT JOIN sys.types t ON c.user_type_id = t.user_type_id
+ LEFT JOIN (SELECT so.object_id, sc.name as default_schema, so.name AS default_name, dc.definition FROM sys.objects so LEFT JOIN sys.schemas sc ON sc.schema_id = so.schema_id LEFT JOIN sys.default_constraints dc ON dc.object_id = so.object_id WHERE so.type = 'D') d ON d.object_id = c.default_object_id
+ LEFT JOIN sys.objects o ON o.object_id = c.object_id
+ LEFT JOIN sys.schemas s ON s.schema_id = o.schema_id
+ LEFT JOIN sys.identity_columns id ON c.object_id = id.object_id AND c.column_id = id.column_id
+ LEFT JOIN sys.extended_properties p ON p.major_id = c.object_id AND p.minor_id = c.column_id AND p.class = 1 AND p.name = 'MS_Description'
+ WHERE s.name in (%s)
+ ORDER BY s.name ASC, c.object_id ASC, c.column_id ASC
+ `, quoteList(schemas))
+
+ rows, err := txn.Query(getColumnSQL)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
- column := &storepb.ColumnMetadata{}
- var schemaName, tableName, columnType, nullable string
- var defaultStr, collation, comment sql.NullString
- var characterLength sql.NullInt32
- if err := rows.Scan(&schemaName, &tableName, &column.Name, &columnType, &characterLength, &column.Position, &defaultStr, &nullable, &collation, &comment); err != nil {
+ var schemaName, tableName, columnName, typeName, definition, collationName, defaultValue, comment sql.NullString
+ var isComputed, isPersisted, isNullable, isIdentity sql.NullBool
+ var maxLength, precision, scale, seedValue, incrementValue sql.NullInt64
+ if err := rows.Scan(
+ &schemaName,
+ &tableName,
+ &columnName,
+ &typeName,
+ &isComputed,
+ &definition,
+ &isPersisted,
+ &maxLength,
+ &precision,
+ &scale,
+ &collationName,
+ &isNullable,
+ &isIdentity,
+ &defaultValue,
+ &comment,
+ &seedValue,
+ &incrementValue,
+ ); err != nil {
return nil, err
}
- if characterLength.Valid {
- column.Type = fmt.Sprintf("%s(%d)", columnType, characterLength.Int32)
- } else {
- column.Type = columnType
+ if !schemaName.Valid || !tableName.Valid || !columnName.Valid || !typeName.Valid {
+ continue
}
- if defaultStr.Valid {
- // TODO: use correct default type
- column.DefaultValue = &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: defaultStr.String}
+ column := &storepb.ColumnMetadata{
+ Name: columnName.String,
}
- isNullBool, err := util.ConvertYesNo(nullable)
+ column.Type, err = getColumnType(definition, typeName, isComputed, isPersisted, precision, scale, maxLength)
if err != nil {
- return nil, err
+ return nil, errors.Errorf("failed to get column type: %v", err)
+ }
+ if isIdentity.Valid && isIdentity.Bool && seedValue.Valid && incrementValue.Valid {
+ column.IsIdentity = true
+ column.IdentitySeed = seedValue.Int64
+ column.IdentityIncrement = incrementValue.Int64
+ }
+ if collationName.Valid {
+ column.Collation = collationName.String
+ }
+ if defaultValue.Valid {
+ column.DefaultValue = &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: defaultValue.String}
+ }
+ column.Nullable = true
+ if isNullable.Valid && !isNullable.Bool {
+ column.Nullable = false
}
- column.Nullable = isNullBool
- column.Collation = collation.String
if comment.Valid {
column.Comment = comment.String
}
- key := db.TableKey{Schema: schemaName, Table: tableName}
+ key := db.TableKey{Schema: schemaName.String, Table: tableName.String}
columnsMap[key] = append(columnsMap[key], column)
}
if err := rows.Err(); err != nil {
@@ -371,59 +464,225 @@ func getTableColumns(txn *sql.Tx) (map[db.TableKey][]*storepb.ColumnMetadata, er
return columnsMap, nil
}
-// getIndexes gets all indices of a database.
-func getIndexes(txn *sql.Tx) (map[db.TableKey][]*storepb.IndexMetadata, error) {
+func getColumnType(definition, typeName sql.NullString, isComputed, isPersisted sql.NullBool, precision, scale, maxLength sql.NullInt64) (string, error) {
+ var buf strings.Builder
+ if definition.Valid && isComputed.Valid && isComputed.Bool {
+ if _, err := fmt.Fprintf(&buf, " AS %s", definition.String); err != nil {
+ return "", err
+ }
+ if isPersisted.Valid && isPersisted.Bool {
+ if _, err := fmt.Fprintf(&buf, " PERSISTED"); err != nil {
+ return "", err
+ }
+ }
+ return "", nil
+ }
+
+ if !typeName.Valid {
+ return "", errors.New("column type name is not valid")
+ }
+
+ if _, err := fmt.Fprintf(&buf, " %s", typeName.String); err != nil {
+ return "", err
+ }
+
+ switch typeName.String {
+ case "decimal", "numeric":
+ if precision.Valid && scale.Valid {
+ if _, err := fmt.Fprintf(&buf, "(%d, %d)", precision.Int64, scale.Int64); err != nil {
+ return "", err
+ }
+ } else if precision.Valid {
+ if _, err := fmt.Fprintf(&buf, "(%d)", precision.Int64); err != nil {
+ return "", err
+ }
+ }
+ case "float", "real":
+ if precision.Valid {
+ if _, err := fmt.Fprintf(&buf, "(%d)", precision.Int64); err != nil {
+ return "", err
+ }
+ }
+ case "dateoffset", "datetime2", "time":
+ if scale.Valid {
+ if _, err := fmt.Fprintf(&buf, "(%d)", scale.Int64); err != nil {
+ return "", err
+ }
+ }
+ case "char", "nchar", "varchar", "nvarchar", "binary", "varbinary":
+ if maxLength.Valid {
+ if maxLength.Int64 == -1 {
+ if _, err := fmt.Fprintf(&buf, "(max)"); err != nil {
+ return "", err
+ }
+ } else {
+ if _, err := fmt.Fprintf(&buf, "(%d)", maxLength.Int64); err != nil {
+ return "", err
+ }
+ }
+ }
+ }
+ return buf.String(), nil
+}
+
+func getIndexes(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.IndexMetadata, error) {
// MSSQL doesn't support function-based indexes.
indexMap := make(map[db.TableKey]map[string]*storepb.IndexMetadata)
- query := `
- SELECT
- s.name,
- t.name,
- ind.name,
- col.name,
- ind.type_desc,
- ind.is_primary_key,
- ind.is_unique
- FROM
- sys.indexes ind
- INNER JOIN
- sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
- INNER JOIN
- sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
- INNER JOIN
- sys.tables t ON ind.object_id = t.object_id
- INNER JOIN
- sys.schemas s ON s.schema_id = t.schema_id
- WHERE
- t.is_ms_shipped = 0 AND ind.name IS NOT NULL
- ORDER BY
- s.name, t.name, ind.name, ic.key_ordinal;`
+ query := fmt.Sprintf(`
+ SELECT
+ s.name AS schema_name,
+ o.name AS table_name,
+ i.name,
+ i.type_desc,
+ col.name AS column_name,
+ ic.is_descending_key,
+ CAST(ep.value AS NVARCHAR(MAX)) comment
+ FROM
+ sys.indexes i
+ LEFT JOIN sys.all_objects o ON o.object_id = i.object_id
+ LEFT JOIN sys.schemas s ON s.schema_id = o.schema_id
+ LEFT JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
+ LEFT JOIN sys.all_columns col ON ic.column_id = col.column_id AND ic.object_id = col.object_id
+ LEFT JOIN sys.key_constraints cons ON (cons.parent_object_id = ic.object_id AND cons.unique_index_id = i.index_id)
+ LEFT JOIN sys.extended_properties ep ON (((i.is_primary_key <> 1 AND i.is_unique_constraint <> 1 AND ep.class = 7 AND i.object_id = ep.major_id AND ep.minor_id = i.index_id) OR ((i.is_primary_key = 1 OR i.is_unique_constraint = 1) AND ep.class = 1 AND cons.object_id = ep.major_id AND ep.minor_id = 0)) AND ep.name = 'MS_Description'),
+ sys.stats stat
+ LEFT JOIN sys.all_objects so ON (stat.object_id = so.object_id)
+ WHERE (i.object_id = so.object_id OR i.object_id = so.parent_object_id) AND i.name = stat.name AND i.index_id > 0 AND (i.is_primary_key = 0 AND i.is_unique_constraint = 0) AND s.name in (%s) AND o.type IN ('U', 'S', 'V')
+ ORDER BY s.name, table_name, i.index_id, ic.key_ordinal, ic.index_column_id
+ `, quoteList(schemas))
rows, err := txn.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
- var schemaName, tableName, indexName, indexType, colName string
- var primary, unique bool
- if err := rows.Scan(&schemaName, &tableName, &indexName, &colName, &indexType, &primary, &unique); err != nil {
+ var schemaName, tableName, indexName, typeDesc, colName, comment sql.NullString
+ var isDescending sql.NullBool
+ if err := rows.Scan(&schemaName, &tableName, &indexName, &colName, &typeDesc, &isDescending, &comment); err != nil {
+ return nil, err
+ }
+
+ if !schemaName.Valid || !tableName.Valid || !indexName.Valid || !colName.Valid {
+ continue
+ }
+
+ key := db.TableKey{Schema: schemaName.String, Table: tableName.String}
+ if _, ok := indexMap[key]; !ok {
+ indexMap[key] = make(map[string]*storepb.IndexMetadata)
+ }
+ if _, ok := indexMap[key][indexName.String]; !ok {
+ index := &storepb.IndexMetadata{
+ Name: indexName.String,
+ Unique: false,
+ Primary: false,
+ IsConstraint: false,
+ }
+ if typeDesc.Valid {
+ index.Type = typeDesc.String
+ }
+ if comment.Valid {
+ index.Comment = comment.String
+ }
+ indexMap[key][indexName.String] = index
+ }
+
+ indexMap[key][indexName.String].Expressions = append(indexMap[key][indexName.String].Expressions, colName.String)
+ if isDescending.Valid && isDescending.Bool {
+ indexMap[key][indexName.String].Descending = append(indexMap[key][indexName.String].Descending, true)
+ } else {
+ indexMap[key][indexName.String].Descending = append(indexMap[key][indexName.String].Descending, false)
+ }
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+
+ tableIndexes := make(map[db.TableKey][]*storepb.IndexMetadata)
+ for k, m := range indexMap {
+ for _, v := range m {
+ tableIndexes[k] = append(tableIndexes[k], v)
+ }
+ sort.Slice(tableIndexes[k], func(i, j int) bool {
+ return tableIndexes[k][i].Name < tableIndexes[k][j].Name
+ })
+ }
+ return tableIndexes, nil
+}
+
+func getKeys(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.IndexMetadata, error) {
+ indexMap := make(map[db.TableKey]map[string]*storepb.IndexMetadata)
+ dumpKeySQL := fmt.Sprintf(`
+ SELECT
+ s.name AS schema_name,
+ o.name AS table_name,
+ i.name,
+ c.name AS column_name,
+ ic.is_descending_key,
+ i.is_primary_key,
+ i.is_unique_constraint,
+ i.type_desc,
+ CAST(p.[value] AS nvarchar(4000)) AS comment
+ FROM
+ sys.indexes i
+ LEFT JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
+ LEFT JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
+ LEFT JOIN sys.objects co ON co.parent_object_id = i.object_id AND co.name = i.name LEFT JOIN sys.objects o ON o.object_id = i.object_id
+ LEFT JOIN sys.schemas s ON s.schema_id = o.schema_id
+ LEFT JOIN sys.extended_properties p ON p.major_id = co.object_id AND p.class = 1 AND p.name = 'MS_Description'
+ WHERE i.index_id > 0 AND (i.is_primary_key = 1 OR i.is_unique_constraint = 1) AND o.type IN ('U', 'V') AND s.name in (%s)
+ ORDER BY s.name ASC, i.name ASC, ic.key_ordinal ASC
+ `, quoteList(schemas))
+
+ rows, err := txn.Query(dumpKeySQL)
+ if err != nil {
+ return nil, util.FormatErrorWithQuery(err, dumpKeySQL)
+ }
+ defer rows.Close()
+
+ for rows.Next() {
+ var schemaName, tableName, indexName, colName, typeDesc, comment sql.NullString
+ var isDescending, isPrimaryKey, isUniqueConstraint sql.NullBool
+ if err := rows.Scan(&schemaName, &tableName, &indexName, &colName, &isDescending, &isPrimaryKey, &isUniqueConstraint, &typeDesc, &comment); err != nil {
return nil, err
}
- key := db.TableKey{Schema: schemaName, Table: tableName}
+ if !schemaName.Valid || !tableName.Valid || !indexName.Valid || !colName.Valid {
+ continue
+ }
+ key := db.TableKey{Schema: schemaName.String, Table: tableName.String}
if _, ok := indexMap[key]; !ok {
indexMap[key] = make(map[string]*storepb.IndexMetadata)
}
- if _, ok := indexMap[key][indexName]; !ok {
- indexMap[key][indexName] = &storepb.IndexMetadata{
- Name: indexName,
- Type: indexType,
- Unique: unique,
- Primary: primary,
+ if _, ok := indexMap[key][indexName.String]; !ok {
+ index := &storepb.IndexMetadata{
+ Name: indexName.String,
+ Unique: false,
+ Primary: false,
+ IsConstraint: true,
+ }
+ if isPrimaryKey.Valid && isPrimaryKey.Bool {
+ index.Primary = true
+ index.Unique = true
+ }
+ if isUniqueConstraint.Valid && isUniqueConstraint.Bool {
+ index.Unique = true
+ }
+ if typeDesc.Valid {
+ index.Type = typeDesc.String
}
+ if comment.Valid {
+ index.Comment = comment.String
+ }
+ indexMap[key][indexName.String] = index
+ }
+
+ indexMap[key][indexName.String].Expressions = append(indexMap[key][indexName.String].Expressions, colName.String)
+ if isDescending.Valid && isDescending.Bool {
+ indexMap[key][indexName.String].Descending = append(indexMap[key][indexName.String].Descending, true)
+ } else {
+ indexMap[key][indexName.String].Descending = append(indexMap[key][indexName.String].Descending, false)
}
- indexMap[key][indexName].Expressions = append(indexMap[key][indexName].Expressions, colName)
}
if err := rows.Err(); err != nil {
return nil, err
@@ -441,6 +700,22 @@ func getIndexes(txn *sql.Tx) (map[db.TableKey][]*storepb.IndexMetadata, error) {
return tableIndexes, nil
}
+// getIndexes gets all indices of a database.
+func getKeyAndIndexes(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.IndexMetadata, error) {
+ keys, err := getKeys(txn, schemas)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get keys")
+ }
+ indexes, err := getIndexes(txn, schemas)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get indexes")
+ }
+ for k, v := range indexes {
+ keys[k] = append(keys[k], v...)
+ }
+ return keys, nil
+}
+
// getViews gets all views of a database.
func getViews(txn *sql.Tx, columnMap map[db.TableKey][]*storepb.ColumnMetadata) (map[string][]*storepb.ViewMetadata, error) {
viewMap := make(map[string][]*storepb.ViewMetadata)
diff --git a/frontend/src/types/proto/store/database.ts b/frontend/src/types/proto/store/database.ts
index 2d9d67862c..bf4b633f9a 100644
--- a/frontend/src/types/proto/store/database.ts
+++ b/frontend/src/types/proto/store/database.ts
@@ -624,11 +624,14 @@ export interface ColumnMetadata {
/** The user_comment is the user comment of a table parsed from the comment. */
userComment: string;
/** The generation is for generated columns. */
- generation:
- | GenerationMetadata
- | undefined;
+ generation: GenerationMetadata | undefined;
+ isIdentity: boolean;
/** The identity_generation is for identity columns, PG only. */
identityGeneration: ColumnMetadata_IdentityGeneration;
+ /** The identity_seed is for identity columns, MSSQL only. */
+ identitySeed: Long;
+ /** The identity_increment is for identity columns, MSSQL only. */
+ identityIncrement: Long;
}
export enum ColumnMetadata_IdentityGeneration {
@@ -3815,7 +3818,10 @@ function createBaseColumnMetadata(): ColumnMetadata {
comment: "",
userComment: "",
generation: undefined,
+ isIdentity: false,
identityGeneration: ColumnMetadata_IdentityGeneration.IDENTITY_GENERATION_UNSPECIFIED,
+ identitySeed: Long.ZERO,
+ identityIncrement: Long.ZERO,
};
}
@@ -3863,9 +3869,18 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
if (message.generation !== undefined) {
GenerationMetadata.encode(message.generation, writer.uint32(114).fork()).join();
}
+ if (message.isIdentity !== false) {
+ writer.uint32(136).bool(message.isIdentity);
+ }
if (message.identityGeneration !== ColumnMetadata_IdentityGeneration.IDENTITY_GENERATION_UNSPECIFIED) {
writer.uint32(120).int32(columnMetadata_IdentityGenerationToNumber(message.identityGeneration));
}
+ if (!message.identitySeed.equals(Long.ZERO)) {
+ writer.uint32(144).int64(message.identitySeed.toString());
+ }
+ if (!message.identityIncrement.equals(Long.ZERO)) {
+ writer.uint32(152).int64(message.identityIncrement.toString());
+ }
return writer;
},
@@ -3988,6 +4003,14 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
message.generation = GenerationMetadata.decode(reader, reader.uint32());
continue;
}
+ case 17: {
+ if (tag !== 136) {
+ break;
+ }
+
+ message.isIdentity = reader.bool();
+ continue;
+ }
case 15: {
if (tag !== 120) {
break;
@@ -3996,6 +4019,22 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
message.identityGeneration = columnMetadata_IdentityGenerationFromJSON(reader.int32());
continue;
}
+ case 18: {
+ if (tag !== 144) {
+ break;
+ }
+
+ message.identitySeed = Long.fromString(reader.int64().toString());
+ continue;
+ }
+ case 19: {
+ if (tag !== 152) {
+ break;
+ }
+
+ message.identityIncrement = Long.fromString(reader.int64().toString());
+ continue;
+ }
}
if ((tag & 7) === 4 || tag === 0) {
break;
@@ -4021,9 +4060,12 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
comment: isSet(object.comment) ? globalThis.String(object.comment) : "",
userComment: isSet(object.userComment) ? globalThis.String(object.userComment) : "",
generation: isSet(object.generation) ? GenerationMetadata.fromJSON(object.generation) : undefined,
+ isIdentity: isSet(object.isIdentity) ? globalThis.Boolean(object.isIdentity) : false,
identityGeneration: isSet(object.identityGeneration)
? columnMetadata_IdentityGenerationFromJSON(object.identityGeneration)
: ColumnMetadata_IdentityGeneration.IDENTITY_GENERATION_UNSPECIFIED,
+ identitySeed: isSet(object.identitySeed) ? Long.fromValue(object.identitySeed) : Long.ZERO,
+ identityIncrement: isSet(object.identityIncrement) ? Long.fromValue(object.identityIncrement) : Long.ZERO,
};
},
@@ -4071,9 +4113,18 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
if (message.generation !== undefined) {
obj.generation = GenerationMetadata.toJSON(message.generation);
}
+ if (message.isIdentity !== false) {
+ obj.isIdentity = message.isIdentity;
+ }
if (message.identityGeneration !== ColumnMetadata_IdentityGeneration.IDENTITY_GENERATION_UNSPECIFIED) {
obj.identityGeneration = columnMetadata_IdentityGenerationToJSON(message.identityGeneration);
}
+ if (!message.identitySeed.equals(Long.ZERO)) {
+ obj.identitySeed = (message.identitySeed || Long.ZERO).toString();
+ }
+ if (!message.identityIncrement.equals(Long.ZERO)) {
+ obj.identityIncrement = (message.identityIncrement || Long.ZERO).toString();
+ }
return obj;
},
@@ -4098,8 +4149,15 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
message.generation = (object.generation !== undefined && object.generation !== null)
? GenerationMetadata.fromPartial(object.generation)
: undefined;
+ message.isIdentity = object.isIdentity ?? false;
message.identityGeneration = object.identityGeneration ??
ColumnMetadata_IdentityGeneration.IDENTITY_GENERATION_UNSPECIFIED;
+ message.identitySeed = (object.identitySeed !== undefined && object.identitySeed !== null)
+ ? Long.fromValue(object.identitySeed)
+ : Long.ZERO;
+ message.identityIncrement = (object.identityIncrement !== undefined && object.identityIncrement !== null)
+ ? Long.fromValue(object.identityIncrement)
+ : Long.ZERO;
return message;
},
};
diff --git a/frontend/src/types/proto/v1/database_service.ts b/frontend/src/types/proto/v1/database_service.ts
index 4d3b3f9151..159710ab07 100644
--- a/frontend/src/types/proto/v1/database_service.ts
+++ b/frontend/src/types/proto/v1/database_service.ts
@@ -680,11 +680,14 @@ export interface ColumnMetadata {
/** The user_comment is the user comment of a column parsed from the comment. */
userComment: string;
/** The generation is the generation of a column. */
- generation:
- | GenerationMetadata
- | undefined;
+ generation: GenerationMetadata | undefined;
+ isIdentity: boolean;
/** The identity_generation is for identity columns, PG only. */
identityGeneration: ColumnMetadata_IdentityGeneration;
+ /** The identity_seed is for identity columns, MSSQL only. */
+ identitySeed: Long;
+ /** The identity_increment is for identity columns, MSSQL only. */
+ identityIncrement: Long;
}
export enum ColumnMetadata_IdentityGeneration {
@@ -4956,7 +4959,10 @@ function createBaseColumnMetadata(): ColumnMetadata {
comment: "",
userComment: "",
generation: undefined,
+ isIdentity: false,
identityGeneration: ColumnMetadata_IdentityGeneration.IDENTITY_GENERATION_UNSPECIFIED,
+ identitySeed: Long.ZERO,
+ identityIncrement: Long.ZERO,
};
}
@@ -5007,9 +5013,18 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
if (message.generation !== undefined) {
GenerationMetadata.encode(message.generation, writer.uint32(130).fork()).join();
}
+ if (message.isIdentity !== false) {
+ writer.uint32(152).bool(message.isIdentity);
+ }
if (message.identityGeneration !== ColumnMetadata_IdentityGeneration.IDENTITY_GENERATION_UNSPECIFIED) {
writer.uint32(136).int32(columnMetadata_IdentityGenerationToNumber(message.identityGeneration));
}
+ if (!message.identitySeed.equals(Long.ZERO)) {
+ writer.uint32(160).int64(message.identitySeed.toString());
+ }
+ if (!message.identityIncrement.equals(Long.ZERO)) {
+ writer.uint32(168).int64(message.identityIncrement.toString());
+ }
return writer;
},
@@ -5140,6 +5155,14 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
message.generation = GenerationMetadata.decode(reader, reader.uint32());
continue;
}
+ case 19: {
+ if (tag !== 152) {
+ break;
+ }
+
+ message.isIdentity = reader.bool();
+ continue;
+ }
case 17: {
if (tag !== 136) {
break;
@@ -5148,6 +5171,22 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
message.identityGeneration = columnMetadata_IdentityGenerationFromJSON(reader.int32());
continue;
}
+ case 20: {
+ if (tag !== 160) {
+ break;
+ }
+
+ message.identitySeed = Long.fromString(reader.int64().toString());
+ continue;
+ }
+ case 21: {
+ if (tag !== 168) {
+ break;
+ }
+
+ message.identityIncrement = Long.fromString(reader.int64().toString());
+ continue;
+ }
}
if ((tag & 7) === 4 || tag === 0) {
break;
@@ -5174,9 +5213,12 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
comment: isSet(object.comment) ? globalThis.String(object.comment) : "",
userComment: isSet(object.userComment) ? globalThis.String(object.userComment) : "",
generation: isSet(object.generation) ? GenerationMetadata.fromJSON(object.generation) : undefined,
+ isIdentity: isSet(object.isIdentity) ? globalThis.Boolean(object.isIdentity) : false,
identityGeneration: isSet(object.identityGeneration)
? columnMetadata_IdentityGenerationFromJSON(object.identityGeneration)
: ColumnMetadata_IdentityGeneration.IDENTITY_GENERATION_UNSPECIFIED,
+ identitySeed: isSet(object.identitySeed) ? Long.fromValue(object.identitySeed) : Long.ZERO,
+ identityIncrement: isSet(object.identityIncrement) ? Long.fromValue(object.identityIncrement) : Long.ZERO,
};
},
@@ -5227,9 +5269,18 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
if (message.generation !== undefined) {
obj.generation = GenerationMetadata.toJSON(message.generation);
}
+ if (message.isIdentity !== false) {
+ obj.isIdentity = message.isIdentity;
+ }
if (message.identityGeneration !== ColumnMetadata_IdentityGeneration.IDENTITY_GENERATION_UNSPECIFIED) {
obj.identityGeneration = columnMetadata_IdentityGenerationToJSON(message.identityGeneration);
}
+ if (!message.identitySeed.equals(Long.ZERO)) {
+ obj.identitySeed = (message.identitySeed || Long.ZERO).toString();
+ }
+ if (!message.identityIncrement.equals(Long.ZERO)) {
+ obj.identityIncrement = (message.identityIncrement || Long.ZERO).toString();
+ }
return obj;
},
@@ -5255,8 +5306,15 @@ export const ColumnMetadata: MessageFns<ColumnMetadata> = {
message.generation = (object.generation !== undefined && object.generation !== null)
? GenerationMetadata.fromPartial(object.generation)
: undefined;
+ message.isIdentity = object.isIdentity ?? false;
message.identityGeneration = object.identityGeneration ??
ColumnMetadata_IdentityGeneration.IDENTITY_GENERATION_UNSPECIFIED;
+ message.identitySeed = (object.identitySeed !== undefined && object.identitySeed !== null)
+ ? Long.fromValue(object.identitySeed)
+ : Long.ZERO;
+ message.identityIncrement = (object.identityIncrement !== undefined && object.identityIncrement !== null)
+ ? Long.fromValue(object.identityIncrement)
+ : Long.ZERO;
return message;
},
};
diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yaml
index e5f8a11f52..00affb9787 100644
--- a/proto/gen/grpc-doc/openapi.yaml
+++ b/proto/gen/grpc-doc/openapi.yaml
@@ -8837,6 +8837,8 @@ components:
allOf:
- $ref: '#/components/schemas/GenerationMetadata'
description: The generation is the generation of a column.
+ isIdentity:
+ type: boolean
identityGeneration:
enum:
- IDENTITY_GENERATION_UNSPECIFIED
@@ -8845,6 +8847,12 @@ components:
type: string
description: The identity_generation is for identity columns, PG only.
format: enum
+ identitySeed:
+ type: string
+ description: The identity_seed is for identity columns, MSSQL only.
+ identityIncrement:
+ type: string
+ description: The identity_increment is for identity columns, MSSQL only.
description: ColumnMetadata is the metadata for columns.
Content_Part:
type: object
diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.md
index e0884737f7..5a927128f9 100644
--- a/proto/gen/grpc-doc/store/README.md
+++ b/proto/gen/grpc-doc/store/README.md
@@ -1160,7 +1160,10 @@ ColumnMetadata is the metadata for columns.
| comment | [string](#string) | | The comment is the comment of a column. classification and user_comment is parsed from the comment. |
| user_comment | [string](#string) | | The user_comment is the user comment of a table parsed from the comment. |
| generation | [GenerationMetadata](#bytebase-store-GenerationMetadata) | | The generation is for generated columns. |
+| is_identity | [bool](#bool) | | |
| identity_generation | [ColumnMetadata.IdentityGeneration](#bytebase-store-ColumnMetadata-IdentityGeneration) | | The identity_generation is for identity columns, PG only. |
+| identity_seed | [int64](#int64) | | The identity_seed is for identity columns, MSSQL only. |
+| identity_increment | [int64](#int64) | | The identity_increment is for identity columns, MSSQL only. |
diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.html
index f3e1b0d3c2..812f07df75 100644
--- a/proto/gen/grpc-doc/store/index.html
+++ b/proto/gen/grpc-doc/store/index.html
@@ -3417,6 +3417,13 @@ classification and user_comment is parsed from the comment. </p></td>
<td><p>The generation is for generated columns. </p></td>
</tr>
+ <tr>
+ <td>is_identity</td>
+ <td><a href="#bool">bool</a></td>
+ <td></td>
+ <td><p> </p></td>
+ </tr>
+
<tr>
<td>identity_generation</td>
<td><a href="#bytebase.store.ColumnMetadata.IdentityGeneration">ColumnMetadata.IdentityGeneration</a></td>
@@ -3424,6 +3431,20 @@ classification and user_comment is parsed from the comment. </p></td>
<td><p>The identity_generation is for identity columns, PG only. </p></td>
</tr>
+ <tr>
+ <td>identity_seed</td>
+ <td><a href="#int64">int64</a></td>
+ <td></td>
+ <td><p>The identity_seed is for identity columns, MSSQL only. </p></td>
+ </tr>
+
+ <tr>
+ <td>identity_increment</td>
+ <td><a href="#int64">int64</a></td>
+ <td></td>
+ <td><p>The identity_increment is for identity columns, MSSQL only. </p></td>
+ </tr>
+
</tbody>
</table>
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index 3331bdb48d..7849d54d5e 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -2042,7 +2042,10 @@ ColumnMetadata is the metadata for columns.
| comment | [string](#string) | | The comment is the comment of a column. classification and user_comment is parsed from the comment. |
| user_comment | [string](#string) | | The user_comment is the user comment of a column parsed from the comment. |
| generation | [GenerationMetadata](#bytebase-v1-GenerationMetadata) | | The generation is the generation of a column. |
+| is_identity | [bool](#bool) | | |
| identity_generation | [ColumnMetadata.IdentityGeneration](#bytebase-v1-ColumnMetadata-IdentityGeneration) | | The identity_generation is for identity columns, PG only. |
+| identity_seed | [int64](#int64) | | The identity_seed is for identity columns, MSSQL only. |
+| identity_increment | [int64](#int64) | | The identity_increment is for identity columns, MSSQL only. |
diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.html
index e9886bc621..c06f30c041 100644
--- a/proto/gen/grpc-doc/v1/index.html
+++ b/proto/gen/grpc-doc/v1/index.html
@@ -6405,6 +6405,13 @@ classification and user_comment is parsed from the comment. </p></td>
<td><p>The generation is the generation of a column. </p></td>
</tr>
+ <tr>
+ <td>is_identity</td>
+ <td><a href="#bool">bool</a></td>
+ <td></td>
+ <td><p> </p></td>
+ </tr>
+
<tr>
<td>identity_generation</td>
<td><a href="#bytebase.v1.ColumnMetadata.IdentityGeneration">ColumnMetadata.IdentityGeneration</a></td>
@@ -6412,6 +6419,20 @@ classification and user_comment is parsed from the comment. </p></td>
<td><p>The identity_generation is for identity columns, PG only. </p></td>
</tr>
+ <tr>
+ <td>identity_seed</td>
+ <td><a href="#int64">int64</a></td>
+ <td></td>
+ <td><p>The identity_seed is for identity columns, MSSQL only. </p></td>
+ </tr>
+
+ <tr>
+ <td>identity_increment</td>
+ <td><a href="#int64">int64</a></td>
+ <td></td>
+ <td><p>The identity_increment is for identity columns, MSSQL only. </p></td>
+ </tr>
+
</tbody>
</table>
diff --git a/proto/generated-go/store/database.pb.go b/proto/generated-go/store/database.pb.go
index f3a0aff677..f614eb6d3e 100644
--- a/proto/generated-go/store/database.pb.go
+++ b/proto/generated-go/store/database.pb.go
@@ -1978,10 +1978,15 @@ type ColumnMetadata struct {
UserComment string `protobuf:"bytes,12,opt,name=user_comment,json=userComment,proto3" json:"user_comment,omitempty"`
// The generation is for generated columns.
Generation *GenerationMetadata `protobuf:"bytes,14,opt,name=generation,proto3" json:"generation,omitempty"`
+ IsIdentity bool `protobuf:"varint,17,opt,name=is_identity,json=isIdentity,proto3" json:"is_identity,omitempty"`
// The identity_generation is for identity columns, PG only.
IdentityGeneration ColumnMetadata_IdentityGeneration `protobuf:"varint,15,opt,name=identity_generation,json=identityGeneration,proto3,enum=bytebase.store.ColumnMetadata_IdentityGeneration" json:"identity_generation,omitempty"`
- unknownFields protoimpl.UnknownFields
- sizeCache protoimpl.SizeCache
+ // The identity_seed is for identity columns, MSSQL only.
+ IdentitySeed int64 `protobuf:"varint,18,opt,name=identity_seed,json=identitySeed,proto3" json:"identity_seed,omitempty"`
+ // The identity_increment is for identity columns, MSSQL only.
+ IdentityIncrement int64 `protobuf:"varint,19,opt,name=identity_increment,json=identityIncrement,proto3" json:"identity_increment,omitempty"`
+ unknownFields protoimpl.UnknownFields
+ sizeCache protoimpl.SizeCache
}
func (x *ColumnMetadata) Reset() {
@@ -2125,6 +2130,13 @@ func (x *ColumnMetadata) GetGeneration() *GenerationMetadata {
return nil
}
+func (x *ColumnMetadata) GetIsIdentity() bool {
+ if x != nil {
+ return x.IsIdentity
+ }
+ return false
+}
+
func (x *ColumnMetadata) GetIdentityGeneration() ColumnMetadata_IdentityGeneration {
if x != nil {
return x.IdentityGeneration
@@ -2132,6 +2144,20 @@ func (x *ColumnMetadata) GetIdentityGeneration() ColumnMetadata_IdentityGenerati
return ColumnMetadata_IDENTITY_GENERATION_UNSPECIFIED
}
+func (x *ColumnMetadata) GetIdentitySeed() int64 {
+ if x != nil {
+ return x.IdentitySeed
+ }
+ return 0
+}
+
+func (x *ColumnMetadata) GetIdentityIncrement() int64 {
+ if x != nil {
+ return x.IdentityIncrement
+ }
+ return 0
+}
+
type isColumnMetadata_DefaultValue interface {
isColumnMetadata_DefaultValue()
}
@@ -3937,7 +3963,7 @@ const file_store_database_proto_rawDesc = "" +
"\vLINEAR_HASH\x10\x06\x12\a\n" +
"\x03KEY\x10\a\x12\x0e\n" +
"\n" +
- "LINEAR_KEY\x10\b\"\xd5\x05\n" +
+ "LINEAR_KEY\x10\b\"\xca\x06\n" +
"\x0eColumnMetadata\x12\x12\n" +
"\x04name\x18\x01 \x01(\tR\x04name\x12\x1a\n" +
"\bposition\x18\x02 \x01(\x05R\bposition\x128\n" +
@@ -3955,8 +3981,12 @@ const file_store_database_proto_rawDesc = "" +
"\fuser_comment\x18\f \x01(\tR\vuserComment\x12B\n" +
"\n" +
"generation\x18\x0e \x01(\v2\".bytebase.store.GenerationMetadataR\n" +
- "generation\x12b\n" +
- "\x13identity_generation\x18\x0f \x01(\x0e21.bytebase.store.ColumnMetadata.IdentityGenerationR\x12identityGeneration\"U\n" +
+ "generation\x12\x1f\n" +
+ "\vis_identity\x18\x11 \x01(\bR\n" +
+ "isIdentity\x12b\n" +
+ "\x13identity_generation\x18\x0f \x01(\x0e21.bytebase.store.ColumnMetadata.IdentityGenerationR\x12identityGeneration\x12#\n" +
+ "\ridentity_seed\x18\x12 \x01(\x03R\fidentitySeed\x12-\n" +
+ "\x12identity_increment\x18\x13 \x01(\x03R\x11identityIncrement\"U\n" +
"\x12IdentityGeneration\x12#\n" +
"\x1fIDENTITY_GENERATION_UNSPECIFIED\x10\x00\x12\n" +
"\n" +
diff --git a/proto/generated-go/v1/database_service.pb.go b/proto/generated-go/v1/database_service.pb.go
index 0c6a43e3fe..30c7ad981b 100644
--- a/proto/generated-go/v1/database_service.pb.go
+++ b/proto/generated-go/v1/database_service.pb.go
@@ -2617,10 +2617,15 @@ type ColumnMetadata struct {
UserComment string `protobuf:"bytes,13,opt,name=user_comment,json=userComment,proto3" json:"user_comment,omitempty"`
// The generation is the generation of a column.
Generation *GenerationMetadata `protobuf:"bytes,16,opt,name=generation,proto3" json:"generation,omitempty"`
+ IsIdentity bool `protobuf:"varint,19,opt,name=is_identity,json=isIdentity,proto3" json:"is_identity,omitempty"`
// The identity_generation is for identity columns, PG only.
IdentityGeneration ColumnMetadata_IdentityGeneration `protobuf:"varint,17,opt,name=identity_generation,json=identityGeneration,proto3,enum=bytebase.v1.ColumnMetadata_IdentityGeneration" json:"identity_generation,omitempty"`
- unknownFields protoimpl.UnknownFields
- sizeCache protoimpl.SizeCache
+ // The identity_seed is for identity columns, MSSQL only.
+ IdentitySeed int64 `protobuf:"varint,20,opt,name=identity_seed,json=identitySeed,proto3" json:"identity_seed,omitempty"`
+ // The identity_increment is for identity columns, MSSQL only.
+ IdentityIncrement int64 `protobuf:"varint,21,opt,name=identity_increment,json=identityIncrement,proto3" json:"identity_increment,omitempty"`
+ unknownFields protoimpl.UnknownFields
+ sizeCache protoimpl.SizeCache
}
func (x *ColumnMetadata) Reset() {
@@ -2771,6 +2776,13 @@ func (x *ColumnMetadata) GetGeneration() *GenerationMetadata {
return nil
}
+func (x *ColumnMetadata) GetIsIdentity() bool {
+ if x != nil {
+ return x.IsIdentity
+ }
+ return false
+}
+
func (x *ColumnMetadata) GetIdentityGeneration() ColumnMetadata_IdentityGeneration {
if x != nil {
return x.IdentityGeneration
@@ -2778,6 +2790,20 @@ func (x *ColumnMetadata) GetIdentityGeneration() ColumnMetadata_IdentityGenerati
return ColumnMetadata_IDENTITY_GENERATION_UNSPECIFIED
}
+func (x *ColumnMetadata) GetIdentitySeed() int64 {
+ if x != nil {
+ return x.IdentitySeed
+ }
+ return 0
+}
+
+func (x *ColumnMetadata) GetIdentityIncrement() int64 {
+ if x != nil {
+ return x.IdentityIncrement
+ }
+ return 0
+}
+
type isColumnMetadata_Default interface {
isColumnMetadata_Default()
}
@@ -5964,7 +5990,7 @@ const file_v1_database_service_proto_rawDesc = "" +
"\vLINEAR_HASH\x10\x06\x12\a\n" +
"\x03KEY\x10\a\x12\x0e\n" +
"\n" +
- "LINEAR_KEY\x10\b\"\xd9\x05\n" +
+ "LINEAR_KEY\x10\b\"\xce\x06\n" +
"\x0eColumnMetadata\x12\x12\n" +
"\x04name\x18\x01 \x01(\tR\x04name\x12\x1a\n" +
"\bposition\x18\x02 \x01(\x05R\bposition\x12\x1f\n" +
@@ -5984,8 +6010,12 @@ const file_v1_database_service_proto_rawDesc = "" +
"\fuser_comment\x18\r \x01(\tR\vuserComment\x12?\n" +
"\n" +
"generation\x18\x10 \x01(\v2\x1f.bytebase.v1.GenerationMetadataR\n" +
- "generation\x12_\n" +
- "\x13identity_generation\x18\x11 \x01(\x0e2..bytebase.v1.ColumnMetadata.IdentityGenerationR\x12identityGeneration\"U\n" +
+ "generation\x12\x1f\n" +
+ "\vis_identity\x18\x13 \x01(\bR\n" +
+ "isIdentity\x12_\n" +
+ "\x13identity_generation\x18\x11 \x01(\x0e2..bytebase.v1.ColumnMetadata.IdentityGenerationR\x12identityGeneration\x12#\n" +
+ "\ridentity_seed\x18\x14 \x01(\x03R\fidentitySeed\x12-\n" +
+ "\x12identity_increment\x18\x15 \x01(\x03R\x11identityIncrement\"U\n" +
"\x12IdentityGeneration\x12#\n" +
"\x1fIDENTITY_GENERATION_UNSPECIFIED\x10\x00\x12\n" +
"\n" +
diff --git a/proto/store/database.proto b/proto/store/database.proto
index ca1d3c8007..6174650d09 100644
--- a/proto/store/database.proto
+++ b/proto/store/database.proto
@@ -463,6 +463,8 @@ message ColumnMetadata {
// The generation is for generated columns.
GenerationMetadata generation = 14;
+ bool is_identity = 17;
+
enum IdentityGeneration {
IDENTITY_GENERATION_UNSPECIFIED = 0;
ALWAYS = 1;
@@ -471,6 +473,12 @@ message ColumnMetadata {
// The identity_generation is for identity columns, PG only.
IdentityGeneration identity_generation = 15;
+
+ // The identity_seed is for identity columns, MSSQL only.
+ int64 identity_seed = 18;
+
+ // The identity_increment is for identity columns, MSSQL only.
+ int64 identity_increment = 19;
}
message GenerationMetadata {
diff --git a/proto/v1/database_service.proto b/proto/v1/database_service.proto
index 3edd0cb03b..0ee818aaec 100644
--- a/proto/v1/database_service.proto
+++ b/proto/v1/database_service.proto
@@ -745,6 +745,8 @@ message ColumnMetadata {
// The generation is the generation of a column.
GenerationMetadata generation = 16;
+ bool is_identity = 19;
+
enum IdentityGeneration {
IDENTITY_GENERATION_UNSPECIFIED = 0;
ALWAYS = 1;
@@ -753,6 +755,12 @@ message ColumnMetadata {
// The identity_generation is for identity columns, PG only.
IdentityGeneration identity_generation = 17;
+
+ // The identity_seed is for identity columns, MSSQL only.
+ int64 identity_seed = 20;
+
+ // The identity_increment is for identity columns, MSSQL only.
+ int64 identity_increment = 21;
}
message GenerationMetadata {