Skip to content

Commit 8b1bf09

Browse files
committed
Merge remote-tracking branch 'origin/master' into lance6716/improve-re
# Conflicts: # driver/driver_test.go
2 parents 87b41a9 + 898d4e4 commit 8b1bf09

File tree

11 files changed

+317
-163
lines changed

11 files changed

+317
-163
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
go: [ "1.26", "1.25", "1.24" ]
14-
os: [ ubuntu-latest, ubuntu-24.04, ubuntu-22.04 ]
14+
os: [ ubuntu-latest, ubuntu-24.04, ubuntu-22.04, ubuntu-24.04-arm ]
1515
name: Tests Go ${{ matrix.go }} on ${{ matrix.os }} # This name is used in main branch protection rules
1616
runs-on: ${{ matrix.os }}
1717

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<img src="go-mysql-logo.png" width="150px">
2+
13
# go-mysql
24

35
A pure Go library to handle MySQL network protocol and replication as used by MySQL and MariaDB.
@@ -17,7 +19,7 @@ This library has been tested or deployed on the following operating systems and
1719
|------------------|--------------|-------------------|----|--------------------------------------------------------------------------------------------------------------------------------|
1820
| Linux | amd64 ||| Check GitHub Actions of this project. |
1921
| Linux | s390x ||| A daily CI runs on an s390x VM, supported by the [IBM Z and LinuxONE Community](https://www.ibm.com/community/z/open-source/). |
20-
| Linux | arm64 || | Deployed in a production environment of a user. |
22+
| Linux | arm64 || | Deployed in a production environment of a user. |
2123
| Linux | arm ||| A test in CI to make sure builds for 32-bits platforms work. |
2224
| FreeBSD | amd64 ||| Sporadically tested by developers. |
2325

@@ -350,6 +352,37 @@ func main() {
350352
}
351353
```
352354

355+
### Structured Connector
356+
357+
If you prefer a structured configuration over a DSN string, you can use
358+
`database/sql.OpenDB` with `driver.Connector`:
359+
360+
```go
361+
package main
362+
363+
import (
364+
"database/sql"
365+
"net/url"
366+
367+
"github.com/go-mysql-org/go-mysql/driver"
368+
)
369+
370+
func main() {
371+
connector := driver.Connector{
372+
Addr: "127.0.0.1:3306",
373+
User: "root",
374+
DB: "test",
375+
Params: url.Values{
376+
// same option keys as the standard DSN form
377+
"timeout": []string{"10s"},
378+
},
379+
}
380+
381+
db := sql.OpenDB(connector)
382+
db.Close()
383+
}
384+
```
385+
353386
### Driver Options
354387

355388
Configuration options can be provided by the standard DSN (Data Source Name).

canal/handler.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package canal
33
import (
44
"github.com/go-mysql-org/go-mysql/mysql"
55
"github.com/go-mysql-org/go-mysql/replication"
6+
"github.com/go-mysql-org/go-mysql/schema"
67
)
78

89
type EventHandler interface {
@@ -21,6 +22,9 @@ type EventHandler interface {
2122
// You'll get the original executed query, with comments if present.
2223
// It will be called before OnRow.
2324
OnRowsQueryEvent(e *replication.RowsQueryEvent) error
25+
// OnTableNotFound is called when a Rows Event references a table object
26+
// that no longer exists.
27+
OnTableNotFound(*replication.EventHeader, *replication.RowsEvent) error
2428
String() string
2529
}
2630

@@ -51,6 +55,11 @@ func (h *DummyEventHandler) OnRowsQueryEvent(*replication.RowsQueryEvent) error
5155
return nil
5256
}
5357

58+
// OnTableNotFound is called for row events for reference tables that are not found
59+
func (h *DummyEventHandler) OnTableNotFound(header *replication.EventHeader, e *replication.RowsEvent) error {
60+
return schema.ErrTableNotExist
61+
}
62+
5463
func (h *DummyEventHandler) String() string { return "DummyEventHandler" }
5564

5665
// `SetEventHandler` registers the sync handler, you must register your

canal/sync.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,15 @@ func (c *Canal) handleRowsEvent(e *replication.BinlogEvent) error {
270270

271271
t, err := c.GetTable(schemaName, tableName)
272272
if err != nil {
273-
e := errors.Cause(err)
273+
cause := errors.Cause(err)
274274
// ignore errors below
275-
if e == ErrExcludedTable || e == schema.ErrTableNotExist || e == schema.ErrMissingTableMeta {
276-
err = nil
275+
if cause == ErrExcludedTable || cause == schema.ErrMissingTableMeta {
276+
return nil
277+
}
278+
// Allow handler to decide what to do when table is missing.
279+
if cause == schema.ErrTableNotExist {
280+
return c.eventHandler.OnTableNotFound(e.Header, ev)
277281
}
278-
279282
return err
280283
}
281284
var action string

driver/connector_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package driver
2+
3+
import (
4+
"database/sql"
5+
"net/url"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestConnector_OpenDB(t *testing.T) {
12+
srv := createMockServer(t)
13+
defer srv.Stop()
14+
15+
connector := Connector{
16+
Addr: "127.0.0.1:3307",
17+
User: *testUser,
18+
Password: *testPassword,
19+
DB: "test",
20+
Params: url.Values{
21+
// Just make sure Params are accepted when using Connector directly.
22+
"timeout": []string{"1s"},
23+
},
24+
}
25+
26+
db := sql.OpenDB(connector)
27+
defer db.Close()
28+
29+
var a uint64
30+
var b string
31+
err := db.QueryRow("select * from table;").Scan(&a, &b)
32+
require.NoError(t, err)
33+
require.EqualValues(t, 1, a)
34+
require.Equal(t, "hello world", b)
35+
}

0 commit comments

Comments
 (0)