Skip to content

Commit 308ec74

Browse files
committed
Add Diff() function
This adds a Diff() function for generating a database diff to the Connection object.
1 parent 069b502 commit 308ec74

File tree

13 files changed

+570
-87
lines changed

13 files changed

+570
-87
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ A Go library for accessing and using SQLite databases stored remotely on DBHub.i
2020
* Download a complete database
2121
* Upload a complete database
2222
* Retrieve database commit history details (size, branch, commit list, whatever else is useful)
23-
* Diffs between database revisions
2423

2524
### Requirements
2625

@@ -81,5 +80,6 @@ Query results (JSON):
8180
* [List views](https://github.com/sqlitebrowser/go-dbhub/blob/master/examples/list_views/main.go) - List the views present in a database
8281
* [List indexes](https://github.com/sqlitebrowser/go-dbhub/blob/master/examples/list_indexes/main.go) - List the indexes present in a database
8382
* [Retrieve column details](https://github.com/sqlitebrowser/go-dbhub/blob/master/examples/column_details/main.go) - Retrieve the details of columns in a table
84-
83+
* [Generate diff between two revisions](https://github.com/sqlitebrowser/go-dbhub/blob/master/examples/diff_commits/main.go) - Figure out the differences between two databases or two versions of one database
84+
8585
Please try it out, submits PRs to extend or fix things, and report any weirdness or bugs you encounter. :smile:

dbhub.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ func (c Connection) Columns(dbowner, dbname, table string) (columns []com.APIJSO
4949
return
5050
}
5151

52+
// Diff returns the differences between two commits of two databases, or if the details on the second database are left empty,
53+
// between two commits of the same database. You can also specify the merge strategy used for the generated SQL statements.
54+
func (c Connection) Diff(dbOwnerA string, dbNameA string, commitA string, dbOwnerB string, dbNameB string, commitB string, merge MergeStrategy) (diffs com.Diffs, err error) {
55+
// Prepare the API parameters
56+
data := url.Values{}
57+
data.Set("apikey", c.APIKey)
58+
data.Set("dbowner_a", dbOwnerA)
59+
data.Set("dbname_a", dbNameA)
60+
data.Set("commit_a", commitA)
61+
data.Set("dbowner_b", dbOwnerB)
62+
data.Set("dbname_b", dbNameB)
63+
data.Set("commit_b", commitB)
64+
if merge == PreservePkMerge {
65+
data.Set("merge", "preserve_pk")
66+
} else if merge == NewPkMerge {
67+
data.Set("merge", "new_pk")
68+
} else {
69+
data.Set("merge", "none")
70+
}
71+
72+
// Fetch the diffs
73+
queryUrl := c.Server + "/v1/diff"
74+
err = sendRequest(queryUrl, data, &diffs)
75+
return
76+
}
77+
5278
// Indexes returns the list of indexes present in the database, along with the table they belong to
5379
func (c Connection) Indexes(dbowner, dbname string) (idx map[string]string, err error) {
5480
// Prepare the API parameters

examples/diff_commits/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/sqlitebrowser/go-dbhub"
8+
)
9+
10+
func main() {
11+
// Create a new DBHub.io API object
12+
db, err := dbhub.New("YOUR_API_KEY_HERE")
13+
if err != nil {
14+
log.Fatal(err)
15+
}
16+
17+
// Retrieve the differences between two commmits of the same database
18+
user := "justinclift"
19+
database := "DB4S download stats.sqlite"
20+
commit1 := "34cbeebfc347a09406707f4220cd40f60778692523d2e7d227ccd92f4125c9ea"
21+
commit2 := "bc6a07955811d86db79e9b4f7fdc3cb2360d40da793066510d792588a8bf8de2"
22+
mergeMode := dbhub.PreservePkMerge
23+
diffs, err := db.Diff(user, database, commit1, "", "", commit2, mergeMode)
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
28+
// Display the SQL statements needed to turn the first version of the database into the second.
29+
// This should produce a similar output to the sqldiff utility.
30+
fmt.Printf("SQL statements for turning the first version into the second:\n")
31+
for _, i := range diffs.Diff { // There is one item for each modified database object
32+
// Print schema changes to this object if there are any
33+
if i.Schema != nil {
34+
fmt.Printf("%s\n", i.Schema.Sql)
35+
}
36+
37+
// Loop over all data changes in this object if there are any
38+
for _, j := range i.Data {
39+
fmt.Printf("%s\n", j.Sql)
40+
}
41+
}
42+
fmt.Println()
43+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ replace (
1010
github.com/Sirupsen/logrus v1.6.0 => github.com/sirupsen/logrus v1.6.0
1111
)
1212

13-
require github.com/sqlitebrowser/dbhub.io v0.0.6
13+
require github.com/sqlitebrowser/dbhub.io v0.0.7

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ github.com/sqlitebrowser/blackfriday v9.0.0+incompatible h1:ddH/UyzasooYgGIblVU4
125125
github.com/sqlitebrowser/blackfriday v9.0.0+incompatible/go.mod h1:/zga9sqpWzcewuI83AO5JZwe9+6F9GgPDdqqdNNEL/0=
126126
github.com/sqlitebrowser/dbhub.io v0.0.6 h1:hvIXQg0F/YDPFZEBeIGTtEBS9F3N02rYiEMtn98553c=
127127
github.com/sqlitebrowser/dbhub.io v0.0.6/go.mod h1:GgZi8wkjdRGkkUVgYNuZp5i+Yps3jGt/jjRuLKRi6Po=
128+
github.com/sqlitebrowser/dbhub.io v0.0.7 h1:1DrKD5BR5FjOwMg4NDet/a8b73HVLYmjxIR7S6ieaGU=
129+
github.com/sqlitebrowser/dbhub.io v0.0.7/go.mod h1:GgZi8wkjdRGkkUVgYNuZp5i+Yps3jGt/jjRuLKRi6Po=
128130
github.com/sqlitebrowser/github_flavored_markdown v0.0.0-20190120045821-b8cf8f054e47 h1:s0+Ea95n1LrsKh6rtclU/9Qb2/5ofvnfnR7gDDiFTw8=
129131
github.com/sqlitebrowser/github_flavored_markdown v0.0.0-20190120045821-b8cf8f054e47/go.mod h1:8vPIKi5FslxCXEgfQxrFtWfdclGy6VWAc9NA1ZTYCJg=
130132
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,21 @@ type ResultRow struct {
1515
type Results struct {
1616
Rows []ResultRow
1717
}
18+
19+
// MergeStrategy specifies the type of SQL statements included in the diff results.
20+
// The SQL statements can be used for merging databases and depending on whether and
21+
// how you want to merge you should choose your merge strategy.
22+
type MergeStrategy int
23+
24+
const (
25+
// NoMerge removes any SQL statements for merging from the diff results
26+
NoMerge MergeStrategy = iota
27+
28+
// PreservePkMerge produces SQL statements which preserve the values of the primary key columns.
29+
// Executing these statements on the first database produces a database similar to the second.
30+
PreservePkMerge
31+
32+
// NewPkMerge produces SQL statements which generate new values for the primary key columns when
33+
// executed. This avoids a couple of possible conflicts and allows merging more distant databases.
34+
NewPkMerge
35+
)

0 commit comments

Comments
 (0)