Skip to content

Commit

Permalink
Merge pull request #83 from local-deploy/DL-T-81
Browse files Browse the repository at this point in the history
Added option to dump only specific database tables
  • Loading branch information
varrcan committed May 23, 2023
2 parents 69956e3 + b77a118 commit 534dea9
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 16 deletions.
8 changes: 5 additions & 3 deletions command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
database bool
files bool
override []string
tables []string
pullWaitGroup sync.WaitGroup
sshClient *client.Client
)
Expand All @@ -41,12 +42,13 @@ Laravel: only the database is downloaded`,
RunE: func(cmd *cobra.Command, args []string) error {
return deployRun()
},
Example: "dl deploy\ndl deploy -d\ndl deploy -f -o bitrix,upload",
Example: "dl deploy\ndl deploy -d\ndl deploy -d -t b_user,b_file\ndl deploy -f\ndl deploy -f -o bitrix,upload",
ValidArgs: []string{"--database", "--files", "--override"},
}
cmd.Flags().BoolVarP(&database, "database", "d", false, "Dump only database from server")
cmd.Flags().BoolVarP(&files, "files", "f", false, "Download only files from server")
cmd.Flags().StringSliceVarP(&override, "override", "o", nil, "Override downloaded files (comma separated values)")
cmd.Flags().StringSliceVarP(&tables, "tables", "t", nil, "Dump only specified tables (comma separated values)")
return cmd
}

Expand Down Expand Up @@ -85,7 +87,7 @@ func deployService(ctx context.Context) error {
if len(project.Env.GetString("TELEPORT")) > 0 {
sshClient = &client.Client{Config: &client.Config{FwType: "bitrix"}}
fmt.Println("Deploy using Teleport")
return teleport.DeployTeleport(ctx, database, files, override)
return teleport.DeployTeleport(ctx, database, files, override, tables)
}

sshClient, err = getClient()
Expand Down Expand Up @@ -155,7 +157,7 @@ func startFiles(ctx context.Context, c *client.Client) {

func startDump(ctx context.Context, c *client.Client) {
defer pullWaitGroup.Done()
project.DumpDb(ctx, c)
project.DumpDb(ctx, c, tables)
}

func detectFw() (string, error) {
Expand Down
3 changes: 3 additions & 0 deletions docs/dl_deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ dl deploy [flags]
```
dl deploy
dl deploy -d
dl deploy -d -t b_user,b_file
dl deploy -f
dl deploy -f -o bitrix,upload
```

Expand All @@ -32,6 +34,7 @@ dl deploy -f -o bitrix,upload
-f, --files Download only files from server
-h, --help help for deploy
-o, --override strings Override downloaded files (comma separated values)
-t, --tables strings Dump only specified tables (comma separated values)
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion docs/dl_self-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Update dl

### Synopsis

Downloading the latest version of the app.
Downloading the latest version of the app (if installed via bash script).

```
dl self-update [flags]
Expand Down
70 changes: 64 additions & 6 deletions project/deploy_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
var remotePhpPath string

// DumpDb Database import from server
func DumpDb(ctx context.Context, client *client.Client) {
func DumpDb(ctx context.Context, client *client.Client, tables []string) {
var db *DbSettings
var err error

Expand Down Expand Up @@ -66,16 +66,22 @@ func DumpDb(ctx context.Context, client *client.Client) {
db.Port = "3306"
}

err = c.mysqlDump(ctx, db)
if len(tables) > 0 {
dumpTables := strings.Join(tables, " ")
err = c.mysqlDumpTables(ctx, db, dumpTables)
} else {
err = c.mysqlDump(ctx, db)
}

if err != nil {
w.Event(progress.ErrorMessageEvent("Failed to create database dump", fmt.Sprint(err)))
w.Event(progress.Event{ID: "Files", Status: progress.Error})
w.Event(progress.Event{ID: "Database", Status: progress.Error})
return
}

err = c.downloadDump(ctx)
if err != nil {
w.Event(progress.Event{ID: "Files", Status: progress.Error})
w.Event(progress.Event{ID: "Database", Status: progress.Error})
return
}

Expand Down Expand Up @@ -239,6 +245,58 @@ func (c SshClient) mysqlDump(ctx context.Context, db *DbSettings) error {
return nil
}

// mysqlDumpTables Create only tables dump
func (c SshClient) mysqlDumpTables(ctx context.Context, db *DbSettings, dumpTables string) error {
w := progress.ContextWriter(ctx)
w.Event(progress.Event{ID: "Create database dump", ParentID: "Database", Status: progress.Working})

dump := c.checkMySqlDumpAvailable()
if dump != nil {
return errors.New("mysqldump not installed, database dump not possible")
}

dumpDataParams := db.DumpDataTablesParams()
dumpCmd := strings.Join([]string{"cd", c.Config.Catalog, "&&",
"mysqldump",
dumpDataParams,
db.DataBase,
dumpTables,
"|",
"gzip > " + c.Config.Catalog + "/production.sql.gz",
}, " ")
logrus.Infof("Run command: %s", dumpCmd)
_, err := c.Run(dumpCmd)

if err != nil {
return err
}

w.Event(progress.Event{ID: "Create database dump", ParentID: "Database", Status: progress.Done})

return nil
}

// DumpDataTablesParams options for only tables dump
func (d DbSettings) DumpDataTablesParams() string {
params := []string{
"--host=" + d.Host,
"--port=" + d.Port,
"--user=" + d.Login,
"--password=" + strconv.Quote(d.Password),
"--single-transaction=1",
"--force",
"--lock-tables=false",
"--no-tablespaces",
}

mysqlVersion := Env.GetString("MYSQL_VERSION")
if mysqlVersion == "8.0" {
params = append(params, "--column-statistics=0")
}

return strings.Join(params, " ")
}

func (c SshClient) checkMySqlDumpAvailable() error {
logrus.Info("Check if mysqldump available")
dumpCmd := strings.Join([]string{"cd", c.Config.Catalog, "&&", "which mysqldump"}, " ")
Expand Down Expand Up @@ -377,8 +435,8 @@ func (c SshClient) ImportDb(ctx context.Context) {
strSQL := `"UPDATE b_option SET VALUE = 'Y' WHERE MODULE_ID = 'main' AND NAME = 'update_devsrv';
UPDATE b_lang SET SERVER_NAME='` + site + `.localhost' WHERE LID='s1';
UPDATE b_lang SET b_lang.DOC_ROOT='' WHERE 1=(SELECT DOC_ROOT FROM (SELECT COUNT(LID) FROM b_lang) as cnt);
INSERT INTO b_lang_domain VALUES ('s1', '` + local + `');
INSERT INTO b_lang_domain VALUES ('s1', '` + nip + `');"`
INSERT IGNORE INTO b_lang_domain VALUES ('s1', '` + local + `');
INSERT IGNORE INTO b_lang_domain VALUES ('s1', '` + nip + `');"`

commandUpdate := "echo " + strSQL + " | " + docker + " exec -i " + siteDB + " /usr/bin/mysql --user=" + mysqlUser + " --password=" + mysqlPassword + " --host=db " + mysqlDB + ""
logrus.Infof("Run command: %s", commandUpdate)
Expand Down
8 changes: 4 additions & 4 deletions utils/teleport/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var pullWaitGroup sync.WaitGroup
var tsh string

// DeployTeleport Deploy using teleport
func DeployTeleport(ctx context.Context, database bool, files bool, override []string) error {
func DeployTeleport(ctx context.Context, database bool, files bool, override []string, tables []string) error {
var err error
w := progress.ContextWriter(ctx)

Expand Down Expand Up @@ -47,7 +47,7 @@ func DeployTeleport(ctx context.Context, database bool, files bool, override []s
os.Exit(1)
}
pullWaitGroup.Add(1)
go startDump(ctx, client)
go startDump(ctx, client, tables)
}

pullWaitGroup.Wait()
Expand All @@ -60,7 +60,7 @@ func startFiles(ctx context.Context, t *teleport, override []string) {
copyFiles(ctx, t, override)
}

func startDump(ctx context.Context, t *teleport) {
func startDump(ctx context.Context, t *teleport, tables []string) {
defer pullWaitGroup.Done()
dumpDb(ctx, t)
dumpDb(ctx, t, tables)
}
41 changes: 39 additions & 2 deletions utils/teleport/deploy_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var remotePhpPath string

func dumpDb(ctx context.Context, t *teleport) {
func dumpDb(ctx context.Context, t *teleport, tables []string) {
var db *project.DbSettings
var err error

Expand Down Expand Up @@ -56,7 +56,13 @@ func dumpDb(ctx context.Context, t *teleport) {
db.Port = "3306"
}

err = t.mysqlDump(ctx, db)
if len(tables) > 0 {
dumpTables := strings.Join(tables, " ")
err = t.mysqlDumpTables(ctx, db, dumpTables)
} else {
err = t.mysqlDump(ctx, db)
}

if err != nil {
w.Event(progress.ErrorMessageEvent("Failed to create database dump", fmt.Sprint(err)))
w.Event(progress.Event{ID: "Files", Status: progress.Error})
Expand Down Expand Up @@ -173,6 +179,37 @@ func (t *teleport) mysqlDump(ctx context.Context, db *project.DbSettings) error
return nil
}

// mysqlDumpTables Create only tables dump
func (t *teleport) mysqlDumpTables(ctx context.Context, db *project.DbSettings, dumpTables string) error {
w := progress.ContextWriter(ctx)
w.Event(progress.Event{ID: "Create database dump", ParentID: "Database", Status: progress.Working})

dump := t.checkMySqlDumpAvailable()
if dump != nil {
return errors.New("mysqldump not installed, database dump not possible")
}

dumpDataParams := db.DumpDataTablesParams()
dumpCmd := strings.Join([]string{"cd", t.Catalog, "&&",
"mysqldump",
dumpDataParams,
db.DataBase,
dumpTables,
"|",
"gzip > " + t.Catalog + "/production.sql.gz",
}, " ")
logrus.Infof("Run command: %s", dumpCmd)
_, err := t.run(dumpCmd)

if err != nil {
return err
}

w.Event(progress.Event{ID: "Create database dump", ParentID: "Database", Status: progress.Done})

return nil
}

func (t *teleport) checkMySqlDumpAvailable() error {
logrus.Info("Check if mysqldump available")
dumpCmd := strings.Join([]string{"cd", t.Catalog, "&&", "which mysqldump"}, " ")
Expand Down

0 comments on commit 534dea9

Please sign in to comment.