-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds a new command called `bootstrap`. It runs all the migrations in the specified folder. Migration files are JSON files, ordered alphabetically. Help: ``` > go run . bootstrap -h Bootstrap a new database from a directory of migration files. All files in the directory will be executed in alphabetical order. All migrations are completed. Usage: pgroll bootstrap <folder> [flags] Flags: -h, --help help for bootstrap ``` Closes #269
- Loading branch information
Showing
4 changed files
with
94 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"slices" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var bootstrapCmd = &cobra.Command{ | ||
Use: "bootstrap <folder>", | ||
Short: "Bootstrap a new database from a directory of migration files", | ||
Long: `Bootstrap a new database from a directory of migration files. All files in the directory will be executed | ||
in lexicographical order. All migrations are completed.`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
migrationsDir := args[0] | ||
|
||
m, err := NewRoll(cmd.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
defer m.Close() | ||
|
||
// open folder and read all json files | ||
files, err := os.ReadDir(migrationsDir) | ||
if err != nil { | ||
return fmt.Errorf("reading migration directory: %w", err) | ||
} | ||
migrationFiles := []string{} | ||
for _, file := range files { | ||
if file.IsDir() || filepath.Ext(file.Name()) != ".json" { | ||
continue | ||
} | ||
migrationFiles = append(migrationFiles, filepath.Join(migrationsDir, file.Name())) | ||
} | ||
slices.Sort(migrationFiles) | ||
|
||
for _, fileName := range migrationFiles { | ||
if err := runMigrationFromFile(cmd.Context(), m, fileName, true); err != nil { | ||
return fmt.Errorf("running migration file '%s': %w", fileName, err) | ||
} | ||
} | ||
|
||
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
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