{"modulePath":"github.com/carlhuth/pkg","version":"v0.0.0-20181112064025-0a8238ebf07a","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package binlogsync\n\nPackage binlogsync adds event listener to a MySQL compatible binlog, based on pkg myreplicator.\n\n## Constants\n\n```go\nconst (\n\tMySQLFlavor   = \"mysql\"\n\tMariaDBFlavor = \"mariadb\"\n)\n```\nUse flavor for different MySQL versions,\n\n```go\nconst (\n\tConfigPathBackendPosition     = `sql/binlogsync/master_position`\n\tConfigPathIncludeTableRegex   = `sql/binlogsync/include_table_regex`\n\tConfigPathExcludeTableRegex   = `sql/binlogsync/exclude_table_regex`\n\tConfigPathBinlogStartFile     = `sql/binlogsync/binlog_start_file`\n\tConfigPathBinlogStartPosition = `sql/binlogsync/binlog_start_position`\n\tConfigPathBinlogSlaveID       = `sql/binlogsync/binlog_slave_id`\n\tConfigPathServerFlavor        = `sql/binlogsync/server_flavor`\n)\n```\nConfiguration paths for config.Service\n\n```go\nconst (\n\tUpdateAction = \"update\"\n\tInsertAction = \"insert\"\n\tDeleteAction = \"delete\"\n)\n```\nAction constants to figure out the type of an event. Those constants will be passed to the interface RowsEventHandler.\n\n## Types\n\n```go\ntype Canal struct {\n\t// contains filtered or unexported fields\n}\n```\nCanal can sync your MySQL data. MySQL must use the binlog format ROW.\n\n```go\nfunc NewCanal(dsn string, db DBConFactory, opt *Options) (*Canal, error)\n```\nNewCanal creates a new canal object to start reading the MySQL binary log. The DSN is need to setup two different connections. One connection for reading the binary stream and the 2nd connection to execute queries. The 2nd argument \\`db\\` gets used to executed the queries, like setting variables or getting table information. Default database flavor is \\`mariadb\\`. export CS\\_DSN='root:PASSWORD@tcp(localhost:3306)/DATABASE\\_NAME\n\n```go\nfunc (c *Canal) CatchMasterPos(timeout time.Duration) error\n```\nCatchMasterPos reads the current master position and waits until we reached it.\n\n```go\nfunc (c *Canal) CheckBinlogRowImage(ctx context.Context, image string) error\n```\nCheckBinlogRowImage checks MySQL binlog row image, must be in FULL, MINIMAL, NOBLOB\n\n```go\nfunc (c *Canal) ClearTableCache(db string, table string)\n```\nClearTableCache clear table cache\n\n```go\nfunc (c *Canal) Close() error\n```\nClose closes all underlying connections\n\n```go\nfunc (c *Canal) FindTable(ctx context.Context, tableName string) (*ddl.Table, error)\n```\nFindTable tries to find a table by its ID. If the table cannot be found by the first search, it will add the table to the internal map and performs a column load from the information\\_schema and then returns the fully defined table. Only tables which are found in the database name of the DSN get loaded.\n\n```go\nfunc (c *Canal) FlushBinlog() error\n```\nFlushBinlog executes FLUSH BINARY LOGS.\n\n```go\nfunc (c *Canal) GetMasterPos() (ms ddl.MasterStatus, err error)\n```\n\n```go\nfunc (c *Canal) RegisterRowsEventHandler(tableName string, h ...RowsEventHandler)\n```\nRegisterRowsEventHandler adds a new event handler to the internal list. If a table name gets provided the event handler is bound to that exact table name, if the table has not been excluded via the global regexes. An empty tableName calls the event handler for all tables.\n\n```go\nfunc (c *Canal) Start(ctx context.Context) error\n```\nStart starts the sync process in the background as a goroutine. You can stop the goroutine via the context.\n\n```go\nfunc (c *Canal) SyncedPosition() ddl.MasterStatus\n```\nSyncedPosition returns the current synced position as retrieved from the SQl server.\n\n```go\nfunc (c *Canal) WaitUntilPos(pos ddl.MasterStatus, timeout time.Duration) error\n```\nWaitUntilPos flushes the binary logs until we've reached the desired position.\n\n```go\ntype DBConFactory func(dsn string) (*dml.ConnPool, error)\n```\nDBConFactory creates a new database connection.\n\n```go\nfunc WithDB(db *sql.DB) DBConFactory\n```\nWithDB allows to set your own DB connection.\n\n```go\nfunc WithMySQL() DBConFactory\n```\nWithMySQL adds the database/sql.DB driver including a ping to the database from the provided DSN.\n\n```go\ntype Options struct {\n\t// ConfigScoped defines the configuration to load the following fields from.\n\t// If not set the data won't be loaded.\n\tConfigScoped config.Scoped\n\t// ConfigSet used to persists the master position of the binlog stream.\n\tConfigSet config.Setter\n\tLog       log.Logger\n\tTLSConfig *tls.Config // Needs some rework\n\t// IncludeTableRegex defines the regex which matches the allowed table\n\t// names. Default state of WithIncludeTables is empty, this will include all\n\t// tables.\n\tIncludeTableRegex []string\n\t// ExcludeTableRegex defines the regex which matches the excluded table\n\t// names. Default state of WithExcludeTables is empty, ignores exluding and\n\t// includes all tables.\n\tExcludeTableRegex []string\n\n\tBinlogStartFile     string\n\tBinlogStartPosition uint64\n\tBinlogSlaveId       uint64\n\t// Flavor defines if `mariadb` or `mysql` should be used. Defaults to\n\t// `mariadb`.\n\tFlavor                   string\n\tMasterStatusQueryTimeout time.Duration\n\t// OnClose runs before the database connection gets closed and after the\n\t// syncer has been closed. The syncer does not \"see\" the changes comming\n\t// from the queries executed in the call back.\n\tOnClose func(*dml.ConnPool) error\n}\n```\nOptions provides multiple options for NewCanal. Part of those options can get loaded via config.Scoped.\n\n```go\ntype RowsEventHandler interface {\n\t// Do function handles a RowsEvent bound to a specific database. If it\n\t// returns an error behaviour of \"Interrupted\", the canal type will stop the\n\t// syncer. Binlog has three update event version, v0, v1 and v2. For v1 and\n\t// v2, the rows number must be even. Two rows for one event, format is\n\t// [before update row, after update row] for update v0, only one row for a\n\t// event, and we don't support this version yet. The Do function will run in\n\t// its own Goroutine. The provided argument `t` of type ddl.Table must only\n\t// be used for reading, changing `t` causes race conditions.\n\tDo(ctx context.Context, action string, t *ddl.Table, rows [][]interface{}) error\n\t// Complete runs before a binlog rotation event happens. Same error rules\n\t// apply here like for function Do(). The Complete function will run in its\n\t// own Goroutine.\n\tComplete(context.Context) error\n\t// String returns the name of the handler\n\tString() string\n}\n```\nRowsEventHandler calls your code when an event gets dispatched.\n\n","path":"github.com/carlhuth/pkg/sql/binlogsync","name":"binlogsync","synopsis":"Package binlogsync adds event listener to a MySQL compatible binlog, based on pkg myreplicator.","isRedistributable":true}
