Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(sqlite): add table and column information to sync schema (#582)
Danny Xu6e1c418cc97b
diff --git a/plugin/db/driver.go b/plugin/db/driver.goindex fae54183e1..da90700a19 100644--- a/plugin/db/driver.go+++ b/plugin/db/driver.go@@ -69,34 +69,38 @@ type Column struct {// Nullable isn't supported for ClickHouse.Nullable boolType string- // CharacterSet isn't supported for Postgres, ClickHouse..+ // CharacterSet isn't supported for Postgres, ClickHouse, SQLite.CharacterSet string- // Collation isn't supported for ClickHouse.+ // Collation isn't supported for ClickHouse, SQLite.Collation string- Comment string+ // Comment isn't supported for SQLite.+ Comment string}// Table is the database table.type Table struct {Name string- // CreatedTs isn't supported for ClickHouse.+ // CreatedTs isn't supported for ClickHouse, SQLite.CreatedTs int64+ // UpdatedTs isn't supported for SQLite.UpdatedTs int64Type string- // Engine isn't supported for Postgres, Snowflake.+ // Engine isn't supported for Postgres, Snowflake, SQLite.Engine string- // Collation isn't supported for Postgres, ClickHouse, Snowflake.+ // Collation isn't supported for Postgres, ClickHouse, Snowflake, SQLite.Collation stringRowCount int64- DataSize int64- // IndexSize isn't supported for ClickHouse, Snowflake.+ // DataSize isn't supported for SQLite.+ DataSize int64+ // IndexSize isn't supported for ClickHouse, Snowflake, SQLite.IndexSize int64- // DataFree isn't supported for Postgres, ClickHouse, Snowflake.+ // DataFree isn't supported for Postgres, ClickHouse, Snowflake, SQLite.DataFree int64- // CreateOptions isn't supported for Postgres, ClickHouse, Snowflake.+ // CreateOptions isn't supported for Postgres, ClickHouse, Snowflake, SQLite.CreateOptions string- Comment string- ColumnList []Column+ // Comment isn't supported for SQLite.+ Comment string+ ColumnList []Column// IndexList isn't supported for ClickHouse, Snowflake.IndexList []Index}diff --git a/plugin/db/sqlite/sqlite.go b/plugin/db/sqlite/sqlite.goindex 56ef9c2bbb..e4ad2473a0 100644--- a/plugin/db/sqlite/sqlite.go+++ b/plugin/db/sqlite/sqlite.go@@ -123,12 +123,90 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.User, []*db.Schema,var schema db.Schemaschema.Name = dbName++ sqldb, err := driver.GetDbConnection(ctx, dbName)+ if err != nil {+ return nil, nil, fmt.Errorf("failed to get database connection for %q: %s", dbName, err)+ }+ txn, err := sqldb.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})+ if err != nil {+ return nil, nil, err+ }+ defer txn.Rollback()+// TODO(d-bytebase): retrieve database schema such as tables and indices.+ tbls, err := getTables(txn)+ if err != nil {+ return nil, nil, err+ }+ schema.TableList = tbls++ if err := txn.Commit(); err != nil {+ return nil, nil, err+ }+schemaList = append(schemaList, &schema)}return nil, schemaList, nil}+// getTables gets all tables of a database.+func getTables(txn *sql.Tx) ([]db.Table, error) {+ var tables []db.Table+ query := "SELECT name FROM sqlite_schema WHERE type ='table' AND name NOT LIKE 'sqlite_%';"+ rows, err := txn.Query(query)+ if err != nil {+ return nil, err+ }+ defer rows.Close()++ var tableNames []string+ for rows.Next() {+ var name string+ if err := rows.Scan(&name); err != nil {+ return nil, err+ }+ tableNames = append(tableNames, name)+ }+ for _, name := range tableNames {+ var tbl db.Table+ tbl.Name = name+ tbl.Type = "BASE TABLE"++ // Get columns: cid, name, type, notnull, dflt_value, pk.+ query := fmt.Sprintf("pragma table_info(%s);", name)+ rows, err := txn.Query(query)+ if err != nil {+ return nil, err+ }+ defer rows.Close()++ for rows.Next() {+ var col db.Column++ var cid int+ var notnull, pk bool+ var name, ctype string+ var dfltValue sql.NullString+ if err := rows.Scan(&cid, &name, &ctype, ¬null, &dfltValue, &pk); err != nil {+ return nil, err+ }+ col.Position = cid+ col.Name = name+ col.Nullable = !notnull+ col.Type = ctype+ if dfltValue.Valid {+ col.Default = &dfltValue.String+ }++ tbl.ColumnList = append(tbl.ColumnList, col)+ }++ tables = append(tables, tbl)+ }+ return tables, nil+}+func (driver *Driver) getDatabases() ([]string, error) {files, err := ioutil.ReadDir(driver.dir)if err != nil {