-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package gobulk | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
type Client struct { | ||
DbmsName string | ||
InputUrl string | ||
OutputUrl string | ||
} | ||
|
||
type Column struct { | ||
Field string | ||
Type string | ||
Null string | ||
Key string | ||
Default *string | ||
Extra string | ||
} | ||
|
||
var tableName string | ||
var columnName string | ||
|
||
func NewClient(DbmsName string, InputUrl string, OutputUrl string) *Client { | ||
return &Client{DbmsName: DbmsName, InputUrl: InputUrl, OutputUrl: OutputUrl} | ||
} | ||
|
||
func (c *Client) Sync() error { | ||
// input | ||
inputDb, err := sql.Open(c.DbmsName, c.InputUrl) | ||
if err != nil { | ||
return err | ||
} | ||
err = inputDb.Ping() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// output | ||
outputDb, err := sql.Open(c.DbmsName, c.OutputUrl) | ||
if err != nil { | ||
return err | ||
} | ||
err = outputDb.Ping() | ||
if err != nil { | ||
return err | ||
} | ||
defer inputDb.Close() | ||
defer outputDb.Close() | ||
|
||
tables, err := inputDb.Query("show tables") | ||
if err != nil { | ||
return err | ||
} | ||
for tables.Next() { | ||
err := tables.Scan(&tableName) | ||
if err != nil { | ||
return err | ||
} | ||
log.Println("=== table:" + tableName + " ===") | ||
columns, err := inputDb.Query("show columns from " + tableName) | ||
for columns.Next() { | ||
var column Column | ||
err := columns.Scan(&column.Field, &column.Type, &column.Null, &column.Key, &column.Default, &column.Extra) | ||
if err != nil { | ||
return err | ||
} | ||
log.Println("column: " + column.Field) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/prometheus/common/log" | ||
"github.com/yuta17/gobulk" | ||
) | ||
|
||
func main() { | ||
inputUrl := "root@tcp(127.0.0.1:3306)/campfire_development" | ||
outputUrl := "root@tcp(127.0.0.1:3306)/campfire_development_copy" | ||
client := gobulk.NewClient("mysql", inputUrl, outputUrl) | ||
err := client.Sync() | ||
if err != nil { | ||
log.Errorln(err) | ||
} | ||
} |