Skip to content

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

snowflake driver: add table to sync schema.

spinningbot35161209a1e6
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 6ee13edeb7..520f9e04e8 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -70,21 +70,21 @@ type DBTable struct {
CreatedTs int64
UpdatedTs int64
Type string
- // Engine isn't supported for Postgres.
+ // Engine isn't supported for Postgres, Snowflake.
Engine string
- // Collation isn't supported for Postgres, ClickHouse.
+ // Collation isn't supported for Postgres, ClickHouse, Snowflake.
Collation string
RowCount int64
DataSize int64
- // IndexSize isn't ClickHouse.
+ // IndexSize isn't ClickHouse, Snowflake.
IndexSize int64
- // DataFree isn't supported for Postgres, ClickHouse.
+ // DataFree isn't supported for Postgres, ClickHouse, Snowflake.
DataFree int64
- // CreateOptions isn't supported for Postgres, ClickHouse.
+ // CreateOptions isn't supported for Postgres, ClickHouse, Snowflake.
CreateOptions string
Comment string
ColumnList []DBColumn
- // IndexList isn't supported for ClickHouse.
+ // IndexList isn't supported for ClickHouse, Snowflake.
IndexList []DBIndex
}
diff --git a/plugin/db/snowflake/snowflake.go b/plugin/db/snowflake/snowflake.go
index 60bd746113..91a2590843 100644
--- a/plugin/db/snowflake/snowflake.go
+++ b/plugin/db/snowflake/snowflake.go
@@ -137,6 +137,11 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSch
var schema db.DBSchema
schema.Name = database
+ tableList, viewList, err := driver.syncTableSchema(ctx, database)
+ if err != nil {
+ return nil, nil, err
+ }
+ schema.TableList, schema.ViewList = tableList, viewList
schemaList = append(schemaList, &schema)
}
@@ -144,6 +149,60 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSch
return userList, schemaList, nil
}
+func (driver *Driver) syncTableSchema(ctx context.Context, database string) ([]db.DBTable, []db.DBView, error) {
+ // Query table info
+ var excludedSchemaList []string
+
+ // Skip all system schemas.
+ for k := range systemSchemas {
+ excludedSchemaList = append(excludedSchemaList, fmt.Sprintf("'%s'", k))
+ }
+ tableWhere := fmt.Sprintf("TABLE_SCHEMA NOT IN (%s)", strings.Join(excludedSchemaList, ", "))
+ query := fmt.Sprintf(`
+ SELECT
+ TABLE_SCHEMA,
+ TABLE_NAME,
+ DATE_PART(EPOCH_SECOND, CREATED),
+ DATE_PART(EPOCH_SECOND, LAST_ALTERED),
+ TABLE_TYPE,
+ ROW_COUNT,
+ BYTES,
+ IFNULL(COMMENT, '')
+ FROM %s.INFORMATION_SCHEMA.TABLES
+ WHERE TABLE_TYPE = 'BASE TABLE' AND %s`, database, tableWhere)
+ tableRows, err := driver.db.QueryContext(ctx, query)
+ if err != nil {
+ return nil, nil, util.FormatErrorWithQuery(err, query)
+ }
+ defer tableRows.Close()
+
+ var tables []db.DBTable
+ for tableRows.Next() {
+ var schemaName, tableName string
+ var table db.DBTable
+ if err := tableRows.Scan(
+ &schemaName,
+ &tableName,
+ &table.CreatedTs,
+ &table.UpdatedTs,
+ &table.Type,
+ &table.RowCount,
+ &table.DataSize,
+ &table.Comment,
+ ); err != nil {
+ return nil, nil, err
+ }
+
+ table.Name = fmt.Sprintf("%s.%s", schemaName, tableName)
+ tables = append(tables, table)
+ }
+ if err := tableRows.Err(); err != nil {
+ return nil, nil, err
+ }
+
+ return tables, nil, nil
+}
+
func (driver *Driver) getDatabases(ctx context.Context) ([]string, error) {
if err := driver.UseRole(ctx, accountAdminRole); err != nil {
return nil, err