Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Mar 22, 2024
1 parent 2d3c5db commit 89bf370
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
10 changes: 6 additions & 4 deletions sources/mysql/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ const defaultErrorRetries = 10

type mysqlAdapter struct {
db *sql.DB
dbName string
table mysql.Table
columns []schema.Column
fieldConverters []transformer.FieldConverter
scannerCfg scan.ScannerConfig
}

func NewMySQLAdapter(db *sql.DB, tableCfg config.MySQLTable) (mysqlAdapter, error) {
func NewMySQLAdapter(db *sql.DB, dbName string, tableCfg config.MySQLTable) (mysqlAdapter, error) {
slog.Info("Loading metadata for table")
table, err := mysql.LoadTable(db, tableCfg.Name)
if err != nil {
Expand All @@ -38,10 +39,10 @@ func NewMySQLAdapter(db *sql.DB, tableCfg config.MySQLTable) (mysqlAdapter, erro
return mysqlAdapter{}, err
}

return newMySQLAdapter(db, *table, columns, tableCfg.ToScannerConfig(defaultErrorRetries))
return newMySQLAdapter(db, dbName, *table, columns, tableCfg.ToScannerConfig(defaultErrorRetries))
}

func newMySQLAdapter(db *sql.DB, table mysql.Table, columns []schema.Column, scannerCfg scan.ScannerConfig) (mysqlAdapter, error) {
func newMySQLAdapter(db *sql.DB, dbName string, table mysql.Table, columns []schema.Column, scannerCfg scan.ScannerConfig) (mysqlAdapter, error) {
fieldConverters := make([]transformer.FieldConverter, len(columns))
for i, col := range columns {
converter, err := valueConverterForType(col.Type, col.Opts)
Expand All @@ -53,6 +54,7 @@ func newMySQLAdapter(db *sql.DB, table mysql.Table, columns []schema.Column, sca

return mysqlAdapter{
db: db,
dbName: dbName,
table: table,
columns: columns,
fieldConverters: fieldConverters,
Expand All @@ -65,7 +67,7 @@ func (m mysqlAdapter) TableName() string {
}

func (m mysqlAdapter) TopicSuffix() string {
return strings.ReplaceAll(m.table.Name, `"`, ``)
return fmt.Sprintf("%s.%s", m.dbName, strings.ReplaceAll(m.table.Name, `"`, ``))
}

func (m mysqlAdapter) FieldConverters() []transformer.FieldConverter {
Expand Down
8 changes: 4 additions & 4 deletions sources/mysql/adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestMySQLAdapter_TableName(t *testing.T) {
table := mysql.Table{
Name: "table1",
}
adapter, err := newMySQLAdapter(nil, table, []schema.Column{}, scan.ScannerConfig{})
adapter, err := newMySQLAdapter(nil, "foo", table, []schema.Column{}, scan.ScannerConfig{})
assert.NoError(t, err)
assert.Equal(t, "table1", adapter.TableName())
}
Expand All @@ -32,18 +32,18 @@ func TestMySQLAdapter_TopicSuffix(t *testing.T) {
table: mysql.Table{
Name: "table1",
},
expected: "table1",
expected: "db.table1",
},
{
table: mysql.Table{
Name: `"PublicStatus"`,
},
expected: "PublicStatus",
expected: "db.PublicStatus",
},
}

for _, tc := range tcs {
adapter, err := newMySQLAdapter(nil, tc.table, []schema.Column{}, scan.ScannerConfig{})
adapter, err := newMySQLAdapter(nil, "db", tc.table, []schema.Column{}, scan.ScannerConfig{})
assert.NoError(t, err)
assert.Equal(t, tc.expected, adapter.TopicSuffix())
}
Expand Down
12 changes: 7 additions & 5 deletions sources/mysql/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
)

type Source struct {
cfg config.MySQL
db *sql.DB
cfg config.MySQL
db *sql.DB
dbName string
}

func Load(cfg config.MySQL) (*Source, error) {
Expand All @@ -28,8 +29,9 @@ func Load(cfg config.MySQL) (*Source, error) {
return nil, fmt.Errorf("failed to connect to MySQL: %w", err)
}
return &Source{
cfg: cfg,
db: db,
cfg: cfg,
db: db,
dbName: cfg.Database,
}, nil
}

Expand All @@ -50,7 +52,7 @@ func (s Source) snapshotTable(ctx context.Context, writer kafkalib.BatchWriter,
logger := slog.With(slog.String("table", tableCfg.Name))
snapshotStartTime := time.Now()

adapter, err := adapter.NewMySQLAdapter(s.db, tableCfg)
adapter, err := adapter.NewMySQLAdapter(s.db, s.dbName, tableCfg)
if err != nil {
return fmt.Errorf("failed to create MySQL adapter: %w", err)
}
Expand Down

0 comments on commit 89bf370

Please sign in to comment.