2 Commits

Author SHA1 Message Date
26eabab106 fix: Invalid syntax on CREATE Trigger fixed.
All checks were successful
CI / build (push) Successful in 4m17s
Additional regex deployed to complete the transformation.
2024-09-26 16:08:22 +12:00
d44cad5ebd fix: Multiline regex for definers. 🐛
Prevent missing SQL statements.
Added "_" to expression
2024-09-05 14:15:42 +12:00

View File

@@ -96,6 +96,7 @@ func WithPassword(password string) Option {
}
}
func (c *Client) Dump() error {
// Construct schema output
fmt.Printf("%s Dumping schema %s from %s\n\r",
style.Success(icon.Info),
@@ -103,6 +104,14 @@ func (c *Client) Dump() error {
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
)
f, _ := os.OpenFile(filepath.Join(c.storagePath, c.databaseName+"-keys.sql"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
_, err := f.WriteString("SET FOREIGN_KEY_CHECKS=0;\n")
if err != nil {
return err
}
f.Close()
response, err := exec.Command(c.executable,
"--host="+c.hostname,
"--port="+strconv.Itoa(c.port),
@@ -111,6 +120,7 @@ func (c *Client) Dump() error {
"--no-create-db",
"--no-data",
"--skip-triggers",
"--compact",
"--result-file", filepath.Join(c.storagePath, c.databaseName+"-schema.sql"),
c.databaseName).CombinedOutput()
if err != nil {
@@ -145,6 +155,7 @@ func (c *Client) Dump() error {
"--no-create-db",
"--no-create-info",
"--skip-triggers",
"--compact",
"--result-file", filepath.Join(c.storagePath, c.databaseName+"-data.sql"),
c.databaseName).Run()
removeDefiners(filepath.Join(c.storagePath, c.databaseName+"-data.sql"))
@@ -172,6 +183,7 @@ func (c *Client) Dump() error {
"--no-data",
"--triggers",
"--routines",
"--compact",
"--result-file", filepath.Join(c.storagePath, c.databaseName+"-routines.sql"),
c.databaseName).Run()
removeDefiners(filepath.Join(c.storagePath, c.databaseName+"-routines.sql"))
@@ -189,11 +201,13 @@ func removeDefiners(filename string) {
// Regex 1: .*(DEFINER=[a-zA-Z0-9\x60%@]+).* (Used by procedures/funcs)
// Regex 2: .*(\/\*\!50003.*!50003+).* (Used by triggers)
// Regex 3: (\/\*\!50013.*DEFINER \*\/) (Used in schema)
// Regex 4: ^(.TRIGGER).(\x60.*) (Fix the create triggers)
expressions := make([]string, 3)
expressions[0] = `s/(^.*)(.DEFINER=[a-zA-Z0-9\x60%@]+)(.*)/$1$3/g`
expressions := make([]string, 4)
expressions[0] = `s/(^.*)(.DEFINER=[a-zA-Z0-9_\x60%@]+)(.*)/$1$3/g`
expressions[1] = `s/(.*)(\/\*\!50003.*!50003+)(.*)/$1$3/g`
expressions[2] = `s/(\/\*\!50013.*DEFINER \*\/)//g`
expressions[3] = `s/(^.TRIGGER).(\x60.*)/CREATE$1 $2/g`
if !strings.Contains(filename, "data") {
for _, re := range expressions {
@@ -221,10 +235,11 @@ func removeDefiners(filename string) {
}
func (c *Client) Combine() (string, error) {
var files [3]string
files[0] = "schema"
files[1] = "data"
files[2] = "routines"
var files [4]string
files[0] = "keys"
files[1] = "schema"
files[2] = "data"
files[3] = "routines"
filepath.Join(c.storagePath, c.databaseName+"-backup.sql")
err := concatenate.FilesToFile(filepath.Join(c.storagePath, c.databaseName+"-backup.sql"),
@@ -233,6 +248,7 @@ func (c *Client) Combine() (string, error) {
filepath.Join(c.storagePath, c.databaseName+"-"+files[0]+".sql"),
filepath.Join(c.storagePath, c.databaseName+"-"+files[1]+".sql"),
filepath.Join(c.storagePath, c.databaseName+"-"+files[2]+".sql"),
filepath.Join(c.storagePath, c.databaseName+"-"+files[3]+".sql"),
)
if err != nil {
return "", err