Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sqlite): add table and column information to sync schema #582

Merged
merged 86 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
4cd1eb8
feat(test): add test for sql execute and check database schema
d-bytebase Jan 29, 2022
0811bf8
feat: address comment
d-bytebase Jan 29, 2022
f3a90d7
Merge branch 'main' into B95BF4A1
d-bytebase Jan 29, 2022
d3c1c5c
feat(test): add initial stub for fake gitlab
d-bytebase Jan 29, 2022
85daf01
Merge branch 'main' into B95BF4A1
d-bytebase Jan 29, 2022
0f7ae67
feat(test): add test for creating repository and fake createProjectHook
d-bytebase Jan 29, 2022
5600f9c
Merge branch 'main' into B95BF4A1
d-bytebase Jan 29, 2022
a685656
refactor: get rid of QueryParams and move some queries to database dr…
d-bytebase Jan 29, 2022
de36657
refactor: move insert history logic into its functor
d-bytebase Jan 30, 2022
75a950d
refactor: merge main
d-bytebase Jan 30, 2022
4b7844c
Merge branch 'main' into B95BF4A1
d-bytebase Jan 30, 2022
47d47e4
Merge branch 'main' into B95BF4A1
d-bytebase Jan 30, 2022
60e8dd5
feat(test): add test for simulating basic gitlab commit
d-bytebase Jan 30, 2022
d2194d6
feat: address comment
d-bytebase Jan 30, 2022
62e93b4
Merge branch 'main' into B95BF4A1
d-bytebase Jan 31, 2022
6745cc6
Merge branch 'main' into B95BF4A1
d-bytebase Feb 1, 2022
972bd3b
feat(test): implement test for vcs schema update, and fixed a few VCS…
d-bytebase Feb 1, 2022
81fa956
feat: address comment
d-bytebase Feb 2, 2022
83a99a1
feat: address comment
d-bytebase Feb 2, 2022
a85d84b
feat: address comment
d-bytebase Feb 2, 2022
d308b63
feat: address comment
d-bytebase Feb 2, 2022
11a923d
feat: address comment
d-bytebase Feb 2, 2022
4819d7d
Merge branch 'main' into B95BF4A1
d-bytebase Feb 2, 2022
fca46af
Merge branch 'main' into B95BF4A1
d-bytebase Feb 3, 2022
bb5890c
refactor: some refactoring of moving common code to helper functions
d-bytebase Feb 3, 2022
d7d22e6
Merge branch 'main' into B95BF4A1
d-bytebase Feb 3, 2022
3f9565b
feat(test): add test for tenant schema update
d-bytebase Feb 3, 2022
2615ddd
Update tests/tenant_test.go
d-bytebase Feb 3, 2022
160481a
Update tests/tenant_test.go
d-bytebase Feb 3, 2022
569ee47
feat: address comment
d-bytebase Feb 3, 2022
300783d
Merge branch 'main' into B95BF4A1
d-bytebase Feb 3, 2022
1b31668
Merge branch 'B95BF4A1' of https://github.com/bytebase/bytebase into …
d-bytebase Feb 3, 2022
6777f6a
Merge branch 'main' into B95BF4A1
d-bytebase Feb 3, 2022
1540e99
feat(test): test tenant schema update with VCS
d-bytebase Feb 3, 2022
e6a1a5d
feat: address comment
d-bytebase Feb 3, 2022
152e683
feat: address comment
d-bytebase Feb 4, 2022
2020dd1
feat: address comment
d-bytebase Feb 4, 2022
a954ef7
feat: address comment
d-bytebase Feb 4, 2022
ddb70a9
feat: address comment
d-bytebase Feb 4, 2022
1af5f17
feat: address comment
d-bytebase Feb 4, 2022
acf143c
Merge branch 'main' into B95BF4A1
d-bytebase Feb 4, 2022
7776458
feat(test): add tenant test for cases with database name template
d-bytebase Feb 4, 2022
f2d555f
Merge branch 'main' into B95BF4A1
d-bytebase Feb 4, 2022
868f63b
feat(test): use temp for data directory otherwise files will be in te…
d-bytebase Feb 4, 2022
1a1798c
fix(543): migrate clickhouse driver to v2.0.7
d-bytebase Feb 5, 2022
ccfb0ad
Merge branch 'main' into B95BF4A1
d-bytebase Feb 5, 2022
d8d21be
Merge branch 'main' into B95BF4A1
d-bytebase Feb 5, 2022
db13326
Merge branch 'main' into B95BF4A1
d-bytebase Feb 6, 2022
147c742
refactor: enable parallel test so that testing can scale
d-bytebase Feb 6, 2022
c2bc7ff
feat: address comment
d-bytebase Feb 6, 2022
b8eecc1
Merge branch 'main' into B95BF4A1
d-bytebase Feb 6, 2022
780cc90
Merge branch 'main' into B95BF4A1
d-bytebase Feb 7, 2022
e39df1f
Merge branch 'main' into B95BF4A1
d-bytebase Feb 7, 2022
e47dd85
fix: wait for all runners to finish before we shutdown the server; ot…
d-bytebase Feb 7, 2022
24838ba
feat: address comment
d-bytebase Feb 8, 2022
eb365b5
Merge branch 'main' into B95BF4A1
d-bytebase Feb 8, 2022
42bf54c
fix(tenancy): disallow ENV_NAME token in file path template for proje…
d-bytebase Feb 8, 2022
dc97835
fix: address comment
d-bytebase Feb 8, 2022
9db0e01
Merge branch 'main' into B95BF4A1
d-bytebase Feb 8, 2022
16c4e10
Merge branch 'main' into B95BF4A1
d-bytebase Feb 8, 2022
df3994e
Merge branch 'main' into B95BF4A1
d-bytebase Feb 8, 2022
36f304b
refactor: fix lint warnings
d-bytebase Feb 8, 2022
a735af9
refactor: re-enable errcheck in golinter
d-bytebase Feb 8, 2022
3dcf3f5
refactor: re-enable errcheck in golinter
d-bytebase Feb 8, 2022
5e13066
refactor: re-enable errcheck in golinter
d-bytebase Feb 8, 2022
3bb1eb4
Merge branch 'main' into B95BF4A1
d-bytebase Feb 8, 2022
bf7f20f
refactor: enable errcheck
d-bytebase Feb 8, 2022
8a7a2a7
refactor: enable errcheck
d-bytebase Feb 8, 2022
e782136
refactor: enable errcheck
d-bytebase Feb 8, 2022
5093a6e
refactor: enable errcheck lint
d-bytebase Feb 8, 2022
e5f0b15
Update server/instance.go
d-bytebase Feb 9, 2022
30d6f6f
Update server/instance.go
d-bytebase Feb 9, 2022
686d083
Merge branch 'main' into B95BF4A1
d-bytebase Feb 9, 2022
dd47cbd
refactor: fix some lint warnings
d-bytebase Feb 9, 2022
fabb827
Merge branch 'B95BF4A1' of https://github.com/bytebase/bytebase into …
d-bytebase Feb 9, 2022
06b8fd9
Merge branch 'main' into B95BF4A1
d-bytebase Feb 9, 2022
d85549a
Merge branch 'main' into B95BF4A1
d-bytebase Feb 9, 2022
703688f
feat(test): add test for migration history
d-bytebase Feb 9, 2022
29747a9
Update tests/schema_update_test.go
d-bytebase Feb 9, 2022
35b8ea3
feat: address comment
d-bytebase Feb 9, 2022
73f2d64
feat: small change
d-bytebase Feb 9, 2022
2401a21
feat: address comment
d-bytebase Feb 9, 2022
fd919f0
Merge branch 'main' into B95BF4A1
d-bytebase Feb 9, 2022
91f96e6
Merge branch 'main' into B95BF4A1
d-bytebase Feb 10, 2022
6a8db66
Merge branch 'main' into B95BF4A1
d-bytebase Feb 11, 2022
a2798c1
feat(sqlite): add table and column information to sync schema
d-bytebase Feb 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 16 additions & 12 deletions plugin/db/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,38 @@ type Column struct {
// Nullable isn't supported for ClickHouse.
Nullable bool
Type 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 int64
Type 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 string
RowCount 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
}
Expand Down
78 changes: 78 additions & 0 deletions plugin/db/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,90 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.User, []*db.Schema,

var schema db.Schema
schema.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.
d-bytebase marked this conversation as resolved.
Show resolved Hide resolved
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, &notnull, &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 {
Expand Down