Skip to content

Commit 541f15c

Browse files
authored
Merge pull request #271 from erizocosmico/feature/rename-columns
*: rename columns for easier natural joins, add repository_id
2 parents 2b51b2f + d2c1e60 commit 541f15c

16 files changed

+302
-282
lines changed

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ A MySQL client is needed to connect to the server. For example:
4040

4141
```bash
4242
$ mysql -q -u root -h 127.0.0.1
43-
MySQL [(none)]> SELECT hash, author_email, author_name FROM commits LIMIT 2;
44-
SELECT hash, author_email, author_name FROM commits LIMIT 2;
43+
MySQL [(none)]> SELECT commit_hash, commit_author_email, commit_author_name FROM commits LIMIT 2;
44+
SELECT commit_hash, commit_author_email, commit_author_name FROM commits LIMIT 2;
4545
+------------------------------------------+---------------------+-----------------------+
46-
| hash | author_email | author_name |
46+
| commit_hash | commit_author_email | commit_author_name |
4747
+------------------------------------------+---------------------+-----------------------+
4848
| 003dc36e0067b25333cb5d3a5ccc31fd028a1c83 | [email protected] | Santiago M. Mola |
4949
| 01ace9e4d144aaeb50eb630fed993375609bcf55 | [email protected] | Antonio Navarro Perez |
@@ -70,13 +70,13 @@ gitbase exposes the following tables:
7070

7171
| Name | Columns |
7272
|:-------------|:------------------------------------------------------------------------------------------------------------------|
73-
| repositories | id |
74-
| remotes | repository_id, name, push_url, fetch_url, push_refspec, fetch_refspec |
75-
| commits | hash, author_name, author_email, author_when, committer_name, committer_email, committer_when, message, tree_hash |
76-
| blobs | hash, size, content |
77-
| refs | repository_id, name, hash |
78-
| tree_entries | tree_hash, entry_hash, mode, name |
79-
| references | repository_id, name, hash |
73+
| repositories | repository_id |
74+
| remotes | repository_id, remote_name, remote_push_url, remote_fetch_url, remote_push_refspec, remote_fetch_refspec |
75+
| commits | repository_id, commit_hash, commit_author_name, commit_author_email, commit_author_when, committer_name, committer_email, committer_when, commit_message, tree_hash |
76+
| blobs | repository_id, blob_hash, blob_size, blob_content |
77+
| refs | repository_id, ref_name, commit_hash |
78+
| tree_entries | repository_id, tree_hash, blob_hash, tree_entry_mode, tree_entry_name |
79+
| references | repository_id, ref_name, commit_hash |
8080

8181
## Functions
8282

@@ -101,30 +101,30 @@ To make some common tasks easier for the user, there are some functions to inter
101101

102102
### Get all the HEAD references from all the repositories
103103
```sql
104-
SELECT * FROM refs WHERE name = 'HEAD'
104+
SELECT * FROM refs WHERE ref_name = 'HEAD'
105105
```
106106

107107
### Commits that appears in more than one reference
108108

109109
```sql
110110
SELECT * FROM (
111-
SELECT COUNT(c.hash) AS num, c.hash
111+
SELECT COUNT(c.commit_hash) AS num, c.commit_hash
112112
FROM refs r
113113
INNER JOIN commits c
114-
ON history_idx(r.hash, c.hash) >= 0
115-
GROUP BY c.hash
114+
ON history_idx(r.commit_hash, c.commit_hash) >= 0
115+
GROUP BY c.commit_hash
116116
) t WHERE num > 1
117117
```
118118

119119
### Get the number of blobs per HEAD commit
120120
```sql
121-
SELECT COUNT(c.hash), c.hash
121+
SELECT COUNT(c.commit_hash), c.commit_hash
122122
FROM refs r
123123
INNER JOIN commits c
124-
ON r.name = 'HEAD' AND history_idx(r.hash, c.hash) >= 0
124+
ON r.ref_name = 'HEAD' AND history_idx(r.commit_hash, c.commit_hash) >= 0
125125
INNER JOIN blobs b
126-
ON commit_has_blob(c.hash, b.hash)
127-
GROUP BY c.hash
126+
ON commit_has_blob(c.commit_hash, b.commit_hash)
127+
GROUP BY c.commit_hash
128128
```
129129

130130
### Get commits per commiter, per month in 2015
@@ -134,11 +134,11 @@ SELECT COUNT(*) as num_commits, month, repo_id, committer_email
134134
FROM (
135135
SELECT
136136
MONTH(committer_when) as month,
137-
r.id as repo_id,
137+
r.repository_id as repo_id,
138138
committer_email
139139
FROM repositories r
140-
INNER JOIN refs ON refs.repository_id = r.id AND refs.name = 'HEAD'
141-
INNER JOIN commits c ON YEAR(committer_when) = 2015 AND history_idx(refs.hash, c.hash) >= 0
140+
INNER JOIN refs ON refs.repository_id = r.repository_id AND refs.ref_name = 'HEAD'
141+
INNER JOIN commits c ON YEAR(committer_when) = 2015 AND history_idx(refs.commit_hash, c.commit_hash) >= 0
142142
) as t
143143
GROUP BY committer_email, month, repo_id
144144
```

blobs.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ type blobsTable struct{}
3030

3131
// BlobsSchema is the schema for the blobs table.
3232
var BlobsSchema = sql.Schema{
33-
{Name: "hash", Type: sql.Text, Nullable: false, Source: BlobsTableName},
34-
{Name: "size", Type: sql.Int64, Nullable: false, Source: BlobsTableName},
35-
{Name: "content", Type: sql.Blob, Nullable: false, Source: BlobsTableName},
33+
{Name: "repository_id", Type: sql.Text, Nullable: false, Source: BlobsTableName},
34+
{Name: "blob_hash", Type: sql.Text, Nullable: false, Source: BlobsTableName},
35+
{Name: "blob_size", Type: sql.Int64, Nullable: false, Source: BlobsTableName},
36+
{Name: "blob_content", Type: sql.Blob, Nullable: false, Source: BlobsTableName},
3637
}
3738

3839
var _ sql.PushdownProjectionAndFiltersTable = (*blobsTable)(nil)
@@ -97,13 +98,13 @@ func (r *blobsTable) WithProjectAndFilters(
9798
span, ctx := ctx.Span("gitbase.BlobsTable")
9899
iter, err := rowIterWithSelectors(
99100
ctx, BlobsSchema, BlobsTableName, filters,
100-
[]string{"hash"},
101+
[]string{"blob_hash"},
101102
func(selectors selectors) (RowRepoIter, error) {
102-
if len(selectors["hash"]) == 0 {
103+
if len(selectors["blob_hash"]) == 0 {
103104
return &blobIter{readContent: shouldReadContent(columns)}, nil
104105
}
105106

106-
hashes, err := selectors.textValues("hash")
107+
hashes, err := selectors.textValues("blob_hash")
107108
if err != nil {
108109
return nil, err
109110
}
@@ -124,6 +125,7 @@ func (r *blobsTable) WithProjectAndFilters(
124125
}
125126

126127
type blobIter struct {
128+
repoID string
127129
iter *object.BlobIter
128130
readContent bool
129131
}
@@ -134,7 +136,7 @@ func (i *blobIter) NewIterator(repo *Repository) (RowRepoIter, error) {
134136
return nil, err
135137
}
136138

137-
return &blobIter{iter: iter, readContent: i.readContent}, nil
139+
return &blobIter{repoID: repo.ID, iter: iter, readContent: i.readContent}, nil
138140
}
139141

140142
func (i *blobIter) Next() (sql.Row, error) {
@@ -143,7 +145,7 @@ func (i *blobIter) Next() (sql.Row, error) {
143145
return nil, err
144146
}
145147

146-
return blobToRow(o, i.readContent)
148+
return blobToRow(i.repoID, o, i.readContent)
147149
}
148150

149151
func (i *blobIter) Close() error {
@@ -182,15 +184,15 @@ func (i *blobsByHashIter) Next() (sql.Row, error) {
182184
return nil, err
183185
}
184186

185-
return blobToRow(blob, i.readContent)
187+
return blobToRow(i.repo.ID, blob, i.readContent)
186188
}
187189
}
188190

189191
func (i *blobsByHashIter) Close() error {
190192
return nil
191193
}
192194

193-
func blobToRow(c *object.Blob, readContent bool) (sql.Row, error) {
195+
func blobToRow(repoID string, c *object.Blob, readContent bool) (sql.Row, error) {
194196
var content []byte
195197
var isAllowed = blobsAllowBinary
196198
if !isAllowed && readContent {
@@ -214,6 +216,7 @@ func blobToRow(c *object.Blob, readContent bool) (sql.Row, error) {
214216
}
215217

216218
return sql.NewRow(
219+
repoID,
217220
c.Hash.String(),
218221
c.Size,
219222
content,
@@ -260,7 +263,7 @@ func shouldReadContent(columns []sql.Expression) bool {
260263
var found bool
261264
expression.Inspect(e, func(e sql.Expression) bool {
262265
gf, ok := e.(*expression.GetField)
263-
found = ok && gf.Table() == BlobsTableName && gf.Name() == "content"
266+
found = ok && gf.Table() == BlobsTableName && gf.Name() == "blob_content"
264267
return !found
265268
})
266269

blobs_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ func TestBlobsLimit(t *testing.T) {
8383
require.Len(rows, len(expected))
8484
for i, row := range rows {
8585
e := expected[i]
86-
require.Equal(e.hash, row[0].(string))
87-
require.Equal(e.bytes, row[1].(int64))
88-
require.Equal(e.empty, len(row[2].([]byte)) == 0)
86+
require.Equal(e.hash, row[1].(string))
87+
require.Equal(e.bytes, row[2].(int64))
88+
require.Equal(e.empty, len(row[3].([]byte)) == 0)
8989
}
9090
}
9191

@@ -105,7 +105,7 @@ func TestBlobsPushdown(t *testing.T) {
105105

106106
iter, err = table.WithProjectAndFilters(session, nil, []sql.Expression{
107107
expression.NewEquals(
108-
expression.NewGetFieldWithTable(0, sql.Text, BlobsTableName, "hash", false),
108+
expression.NewGetFieldWithTable(1, sql.Text, BlobsTableName, "blob_hash", false),
109109
expression.NewLiteral("32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", sql.Text),
110110
),
111111
})
@@ -117,15 +117,15 @@ func TestBlobsPushdown(t *testing.T) {
117117

118118
iter, err = table.WithProjectAndFilters(session, nil, []sql.Expression{
119119
expression.NewLessThan(
120-
expression.NewGetFieldWithTable(1, sql.Int64, BlobsTableName, "size", false),
120+
expression.NewGetFieldWithTable(2, sql.Int64, BlobsTableName, "blob_size", false),
121121
expression.NewLiteral(int64(10), sql.Int64),
122122
),
123123
})
124124
require.NoError(err)
125125

126126
iter, err = table.WithProjectAndFilters(session, nil, []sql.Expression{
127127
expression.NewEquals(
128-
expression.NewGetFieldWithTable(0, sql.Text, BlobsTableName, "hash", false),
128+
expression.NewGetFieldWithTable(1, sql.Text, BlobsTableName, "blob_hash", false),
129129
expression.NewLiteral("not exists", sql.Text),
130130
),
131131
})

commits.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ import (
99
"gopkg.in/src-d/go-git.v4/plumbing/object"
1010
)
1111

12-
type commitsTable struct {
13-
}
12+
type commitsTable struct{}
1413

1514
// CommitsSchema is the schema for the commits table.
1615
var CommitsSchema = sql.Schema{
17-
{Name: "hash", Type: sql.Text, Nullable: false, Source: CommitsTableName},
18-
{Name: "author_name", Type: sql.Text, Nullable: false, Source: CommitsTableName},
19-
{Name: "author_email", Type: sql.Text, Nullable: false, Source: CommitsTableName},
20-
{Name: "author_when", Type: sql.Timestamp, Nullable: false, Source: CommitsTableName},
16+
{Name: "repository_id", Type: sql.Text, Nullable: false, Source: CommitsTableName},
17+
{Name: "commit_hash", Type: sql.Text, Nullable: false, Source: CommitsTableName},
18+
{Name: "commit_author_name", Type: sql.Text, Nullable: false, Source: CommitsTableName},
19+
{Name: "commit_author_email", Type: sql.Text, Nullable: false, Source: CommitsTableName},
20+
{Name: "commit_author_when", Type: sql.Timestamp, Nullable: false, Source: CommitsTableName},
2121
{Name: "committer_name", Type: sql.Text, Nullable: false, Source: CommitsTableName},
2222
{Name: "committer_email", Type: sql.Text, Nullable: false, Source: CommitsTableName},
2323
{Name: "committer_when", Type: sql.Timestamp, Nullable: false, Source: CommitsTableName},
24-
{Name: "message", Type: sql.Text, Nullable: false, Source: CommitsTableName},
24+
{Name: "commit_message", Type: sql.Text, Nullable: false, Source: CommitsTableName},
2525
{Name: "tree_hash", Type: sql.Text, Nullable: false, Source: CommitsTableName},
26-
{Name: "parents", Type: sql.Array(sql.Text), Nullable: false, Source: CommitsTableName},
26+
{Name: "commit_parents", Type: sql.Array(sql.Text), Nullable: false, Source: CommitsTableName},
2727
}
2828

2929
var _ sql.PushdownProjectionAndFiltersTable = (*commitsTable)(nil)
@@ -88,13 +88,13 @@ func (r *commitsTable) WithProjectAndFilters(
8888
span, ctx := ctx.Span("gitbase.CommitsTable")
8989
iter, err := rowIterWithSelectors(
9090
ctx, CommitsSchema, CommitsTableName, filters,
91-
[]string{"hash"},
91+
[]string{"commit_hash"},
9292
func(selectors selectors) (RowRepoIter, error) {
93-
if len(selectors["hash"]) == 0 {
93+
if len(selectors["commit_hash"]) == 0 {
9494
return new(commitIter), nil
9595
}
9696

97-
hashes, err := selectors.textValues("hash")
97+
hashes, err := selectors.textValues("commit_hash")
9898
if err != nil {
9999
return nil, err
100100
}
@@ -112,7 +112,8 @@ func (r *commitsTable) WithProjectAndFilters(
112112
}
113113

114114
type commitIter struct {
115-
iter object.CommitIter
115+
repoID string
116+
iter object.CommitIter
116117
}
117118

118119
func (i *commitIter) NewIterator(repo *Repository) (RowRepoIter, error) {
@@ -121,7 +122,7 @@ func (i *commitIter) NewIterator(repo *Repository) (RowRepoIter, error) {
121122
return nil, err
122123
}
123124

124-
return &commitIter{iter: iter}, nil
125+
return &commitIter{repoID: repo.ID, iter: iter}, nil
125126
}
126127

127128
func (i *commitIter) Next() (sql.Row, error) {
@@ -130,7 +131,7 @@ func (i *commitIter) Next() (sql.Row, error) {
130131
return nil, err
131132
}
132133

133-
return commitToRow(o), nil
134+
return commitToRow(i.repoID, o), nil
134135
}
135136

136137
func (i *commitIter) Close() error {
@@ -168,16 +169,17 @@ func (i *commitsByHashIter) Next() (sql.Row, error) {
168169
return nil, err
169170
}
170171

171-
return commitToRow(commit), nil
172+
return commitToRow(i.repo.ID, commit), nil
172173
}
173174
}
174175

175176
func (i *commitsByHashIter) Close() error {
176177
return nil
177178
}
178179

179-
func commitToRow(c *object.Commit) sql.Row {
180+
func commitToRow(repoID string, c *object.Commit) sql.Row {
180181
return sql.NewRow(
182+
repoID,
181183
c.Hash.String(),
182184
c.Author.Name,
183185
c.Author.Email,

commits_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestCommitsPushdown(t *testing.T) {
6161

6262
iter, err = table.WithProjectAndFilters(session, nil, []sql.Expression{
6363
expression.NewEquals(
64-
expression.NewGetFieldWithTable(0, sql.Text, CommitsTableName, "hash", false),
64+
expression.NewGetFieldWithTable(1, sql.Text, CommitsTableName, "blob_hash", false),
6565
expression.NewLiteral("918c48b83bd081e863dbe1b80f8998f058cd8294", sql.Text),
6666
),
6767
})
@@ -73,7 +73,7 @@ func TestCommitsPushdown(t *testing.T) {
7373

7474
iter, err = table.WithProjectAndFilters(session, nil, []sql.Expression{
7575
expression.NewEquals(
76-
expression.NewGetFieldWithTable(0, sql.Text, CommitsTableName, "hash", false),
76+
expression.NewGetFieldWithTable(1, sql.Text, CommitsTableName, "blob_hash", false),
7777
expression.NewLiteral("not exists", sql.Text),
7878
),
7979
})
@@ -85,7 +85,7 @@ func TestCommitsPushdown(t *testing.T) {
8585

8686
iter, err = table.WithProjectAndFilters(session, nil, []sql.Expression{
8787
expression.NewEquals(
88-
expression.NewGetFieldWithTable(2, sql.Text, CommitsTableName, "author_email", false),
88+
expression.NewGetFieldWithTable(3, sql.Text, CommitsTableName, "commit_author_email", false),
8989
expression.NewLiteral("[email protected]", sql.Text),
9090
),
9191
})
@@ -97,11 +97,11 @@ func TestCommitsPushdown(t *testing.T) {
9797

9898
iter, err = table.WithProjectAndFilters(session, nil, []sql.Expression{
9999
expression.NewEquals(
100-
expression.NewGetFieldWithTable(2, sql.Text, CommitsTableName, "author_email", false),
100+
expression.NewGetFieldWithTable(3, sql.Text, CommitsTableName, "commit_author_email", false),
101101
expression.NewLiteral("[email protected]", sql.Text),
102102
),
103103
expression.NewEquals(
104-
expression.NewGetFieldWithTable(7, sql.Text, CommitsTableName, "message", false),
104+
expression.NewGetFieldWithTable(8, sql.Text, CommitsTableName, "commit_message", false),
105105
expression.NewLiteral("vendor stuff\n", sql.Text),
106106
),
107107
})
@@ -196,8 +196,8 @@ func TestCommitsParents(t *testing.T) {
196196

197197
for i, test := range tests {
198198
t.Run(test.name, func(t *testing.T) {
199-
hash := rows[i][0]
200-
parents := rows[i][9]
199+
hash := rows[i][1]
200+
parents := rows[i][10]
201201
require.Equal(t, test.hash, hash)
202202
require.ElementsMatch(t, test.parents, parents)
203203
})

0 commit comments

Comments
 (0)