Skip to content

Commit

Permalink
Add migrate command for database migration tasks (#5501)
Browse files Browse the repository at this point in the history
* Add migrate command for database migration tasks

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Enhance database migration command documentation

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

---------

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
  • Loading branch information
Warashi authored Jan 21, 2025
1 parent e095ff4 commit e0dfc18
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/pipectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pipe-cd/pipecd/pkg/app/pipectl/cmd/encrypt"
"github.com/pipe-cd/pipecd/pkg/app/pipectl/cmd/event"
"github.com/pipe-cd/pipecd/pkg/app/pipectl/cmd/initialize"
"github.com/pipe-cd/pipecd/pkg/app/pipectl/cmd/migrate"
"github.com/pipe-cd/pipecd/pkg/app/pipectl/cmd/piped"
"github.com/pipe-cd/pipecd/pkg/app/pipectl/cmd/planpreview"
"github.com/pipe-cd/pipecd/pkg/cli"
Expand All @@ -41,6 +42,7 @@ func main() {
piped.NewCommand(),
encrypt.NewCommand(),
initialize.NewCommand(),
migrate.NewCommand(),
)

if err := app.Run(); err != nil {
Expand Down
80 changes: 80 additions & 0 deletions pkg/app/pipectl/cmd/migrate/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package migrate

import (
"context"

"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/pipe-cd/pipecd/pkg/app/server/service/apiservice"
"github.com/pipe-cd/pipecd/pkg/cli"
)

type database struct {
root *command

applications []string
}

func newDatabaseCommand(root *command) *cobra.Command {
c := &database{
root: root,
}
cmd := &cobra.Command{
Use: "database",
Short: "Do migration tasks for database.",
Long: "Make existing applications compatible with plugin-architectured piped. Once you execute this command for an application, it can be deployed using plugin-architectured piped.",
RunE: cli.WithContext(c.run),
}

cmd.Flags().StringSliceVar(&c.applications, "applications", c.applications, "The list of application to migrate database.")
cmd.MarkFlagRequired("applications")
return cmd
}

func (c *database) run(ctx context.Context, input cli.Input) error {
client, err := c.root.clientOptions.NewClient(ctx)
if err != nil {
input.Logger.Error("failed to create client", zap.Error(err))
return err
}

for _, appID := range c.applications {
input.Logger.Info("migrating database", zap.String("application", appID))
if err := c.migrateApplication(ctx, client, appID); err != nil {
input.Logger.Error("failed to migrate database", zap.String("application", appID), zap.Error(err))
return err
}
input.Logger.Info("successfully migrated database", zap.String("application", appID))
}

return nil
}

func (c *database) migrateApplication(ctx context.Context, client apiservice.Client, appID string) error {
req := &apiservice.MigrateDatabaseRequest{
Target: &apiservice.MigrateDatabaseRequest_Application_{
Application: &apiservice.MigrateDatabaseRequest_Application{
ApplicationId: appID,
},
},
}
if _, err := client.MigrateDatabase(ctx, req); err != nil {
return err
}
return nil
}
41 changes: 41 additions & 0 deletions pkg/app/pipectl/cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package migrate

import (
"github.com/spf13/cobra"

"github.com/pipe-cd/pipecd/pkg/app/pipectl/client"
)

type command struct {
clientOptions *client.Options
}

func NewCommand() *cobra.Command {
c := &command{
clientOptions: &client.Options{},
}
cmd := &cobra.Command{
Use: "migrate",
Short: "Do migration tasks.",
}

cmd.AddCommand(newDatabaseCommand(c))

c.clientOptions.RegisterPersistentFlags(cmd)

return cmd
}

0 comments on commit e0dfc18

Please sign in to comment.