Skip to content

Commit e0a3f59

Browse files
committed
feat(database): improve migration path handling and directory creation
- Added support for a configured migration path in .forge.yaml, allowing users to specify a custom location for migrations. - Enhanced logic to check for the existence of the migration directory and create it if it doesn't exist. - Improved error handling for directory creation to provide clearer feedback to users.
1 parent 5ef93a7 commit e0a3f59

1 file changed

Lines changed: 48 additions & 15 deletions

File tree

cmd/forge/plugins/database.go

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -651,28 +651,39 @@ func (p *DatabasePlugin) markApplied(ctx cli.CommandContext) error {
651651
// Helper functions
652652

653653
func (p *DatabasePlugin) loadMigrations() (*migrate.Migrations, error) {
654-
// Try multiple possible migration paths
655-
possiblePaths := []string{
656-
filepath.Join(p.config.RootDir, "migrations"), // Standard location
657-
filepath.Join(p.config.RootDir, "database", "migrations"), // Alternative location
658-
}
659-
660654
var migrationPath string
661655

662-
for _, path := range possiblePaths {
663-
if info, err := os.Stat(path); err == nil && info.IsDir() {
664-
migrationPath = path
656+
// First priority: Check if migrations_path is configured in .forge.yaml
657+
if p.config != nil && p.config.Database.MigrationsPath != "" {
658+
migrationPath = p.config.Database.MigrationsPath
665659

666-
break
660+
// Make relative paths absolute based on project root
661+
if !filepath.IsAbs(migrationPath) {
662+
migrationPath = filepath.Join(p.config.RootDir, migrationPath)
663+
}
664+
} else {
665+
// Fallback: Try multiple possible migration paths
666+
possiblePaths := []string{
667+
filepath.Join(p.config.RootDir, "migrations"), // Standard location
668+
filepath.Join(p.config.RootDir, "database", "migrations"), // Alternative location
669+
}
670+
671+
for _, path := range possiblePaths {
672+
if info, err := os.Stat(path); err == nil && info.IsDir() {
673+
migrationPath = path
674+
break
675+
}
667676
}
668677
}
669678

670-
// If no migrations directory exists, create one
679+
// If no migrations directory found, use default
671680
if migrationPath == "" {
672681
migrationPath = filepath.Join(p.config.RootDir, "migrations")
673-
if err := os.MkdirAll(migrationPath, 0755); err != nil {
674-
return nil, fmt.Errorf("failed to create migrations directory: %w", err)
675-
}
682+
}
683+
684+
// Create directory if it doesn't exist
685+
if err := os.MkdirAll(migrationPath, 0755); err != nil {
686+
return nil, fmt.Errorf("failed to create migrations directory: %w", err)
676687
}
677688

678689
// Create migrations collection and discover SQL files
@@ -693,7 +704,29 @@ func (p *DatabasePlugin) loadMigrations() (*migrate.Migrations, error) {
693704
}
694705

695706
func (p *DatabasePlugin) getMigrationPath() (string, error) {
696-
// Try multiple possible migration paths
707+
// First priority: Check if migrations_path is configured in .forge.yaml
708+
if p.config != nil && p.config.Database.MigrationsPath != "" {
709+
migrationPath := p.config.Database.MigrationsPath
710+
711+
// Make relative paths absolute based on project root
712+
if !filepath.IsAbs(migrationPath) {
713+
migrationPath = filepath.Join(p.config.RootDir, migrationPath)
714+
}
715+
716+
// Check if the configured path exists
717+
if info, err := os.Stat(migrationPath); err == nil && info.IsDir() {
718+
return migrationPath, nil
719+
}
720+
721+
// If configured but doesn't exist, create it
722+
if err := os.MkdirAll(migrationPath, 0755); err != nil {
723+
return "", fmt.Errorf("failed to create configured migrations directory %s: %w", migrationPath, err)
724+
}
725+
726+
return migrationPath, nil
727+
}
728+
729+
// Fallback: Try multiple possible migration paths
697730
possiblePaths := []string{
698731
filepath.Join(p.config.RootDir, "migrations"), // Standard location
699732
filepath.Join(p.config.RootDir, "database", "migrations"), // Alternative location

0 commit comments

Comments
 (0)