-
Notifications
You must be signed in to change notification settings - Fork 124
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
7 changed files
with
175 additions
and
10 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
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
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,19 @@ | ||
package migrate_cmd | ||
|
||
import ( | ||
up_cmd "github.com/nucleuscloud/neosync/backend/internal/cmds/mgmt/migrate/up" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "serve", | ||
Short: "Parent command for serving", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cmd.Help() | ||
}, | ||
} | ||
|
||
cmd.AddCommand(up_cmd.NewCmd()) | ||
return cmd | ||
} |
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,116 @@ | ||
package up_cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
"github.com/nucleuscloud/neosync/backend/internal/nucleusdb" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func NewCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "up", | ||
Short: "Run database migrations", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.SilenceUsage = true | ||
connStr, err := getDbUrl() | ||
if err != nil { | ||
return err | ||
} | ||
return Up( | ||
cmd.Context(), | ||
connStr, | ||
"", | ||
slog.New(slog.NewJSONHandler(os.Stdout, nil)), | ||
) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func Up( | ||
ctx context.Context, | ||
connStr string, | ||
schemaDir string, | ||
logger *slog.Logger, | ||
) error { | ||
|
||
var absSchemaDir string | ||
if filepath.IsAbs(schemaDir) { | ||
absSchemaDir = schemaDir | ||
} else { | ||
a, err := filepath.Abs(schemaDir) | ||
if err != nil { | ||
return err | ||
} | ||
absSchemaDir = a | ||
} | ||
|
||
m, err := migrate.New( | ||
fmt.Sprintf("file://%s", strings.TrimPrefix(absSchemaDir, "file://")), | ||
connStr, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = m.Up() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getDbUrl() (string, error) { | ||
dburl := viper.GetString("DB_URL") | ||
if dburl != "" { | ||
return dburl, nil | ||
} | ||
|
||
dbHost := viper.GetString("DB_HOST") | ||
if dbHost == "" { | ||
return "", fmt.Errorf("must provide DB_HOST in environment") | ||
} | ||
|
||
dbPort := viper.GetInt("DB_PORT") | ||
if dbPort == 0 { | ||
return "", fmt.Errorf("must provide DB_PORT in environment") | ||
} | ||
|
||
dbName := viper.GetString("DB_NAME") | ||
if dbName == "" { | ||
return "", fmt.Errorf("must provide DB_NAME in environment") | ||
} | ||
|
||
dbUser := viper.GetString("DB_USER") | ||
if dbUser == "" { | ||
return "", fmt.Errorf("must provide DB_USER in environment") | ||
} | ||
|
||
dbPass := viper.GetString("DB_PASS") | ||
if dbPass == "" { | ||
return "", fmt.Errorf("must provide DB_PASS in environment") | ||
} | ||
|
||
sslMode := "require" | ||
if viper.IsSet("DB_SSL_DISABLE") && viper.GetBool("DB_SSL_DISABLE") { | ||
sslMode = "disable" | ||
} | ||
|
||
return nucleusdb.GetDbUrl(&nucleusdb.ConnectConfig{ | ||
Host: dbHost, | ||
Port: dbPort, | ||
Database: dbName, | ||
User: dbUser, | ||
Pass: dbPass, | ||
SslMode: &sslMode, | ||
}), 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
2 changes: 1 addition & 1 deletion
2
backend/sql/postgresql/schema/20231030180638_account-temporal-config.down.sql
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
ALTER TABLE | ||
neosync_api.accounts | ||
DROP COLUMN IF NOT EXISTS temporal_config; | ||
DROP COLUMN IF EXISTS temporal_config; |
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