Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ if err != nil {
list, err := q.List(context.Background())
```

### 5. Set migration files directory before creating migration file

If you want to set migration files directory before creating migration file, you can use `SetMigrationFilesDir` method. This is useful when you want to dynamically set the migration files directory, e.g. passing it as a command-line argument.

```go
q.SetMigrationFilesDir("migrations")
q.Create("add_users_table")

// or using chain
q.SetMigrationFilesDir("migrations").Create("add_users_table")
```

## 📁 Migration Interface

Each migration must implement the following interface:
Expand Down Expand Up @@ -306,4 +318,4 @@ func main() {

MIT License

---
---
14 changes: 10 additions & 4 deletions gomigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ func New(config *Config) (*GoMigration, error) {
return nil, fmt.Errorf("invalid migration table name: %w", err)
}

if !migrationDirExists(config.MigrationFilesDir) {
return nil, fmt.Errorf("migration directory %q does not exist", config.MigrationFilesDir)
}

config.Driver.SetMigrationTableName(config.MigrationTableName)

return &GoMigration{
Expand Down Expand Up @@ -77,9 +73,19 @@ func (q *GoMigration) Register(migrations ...Migration) error {
return nil
}

// Set migration files directory.
func (q *GoMigration) SetMigrationFilesDir(dir string) *GoMigration {
q.migrationFilesDir = dir
return q
}

// Create generates a new migration file using the given name.
// The generated file includes a timestamp prefix and basic template content.
func (q *GoMigration) Create(fileName string) error {
if !migrationDirExists(q.migrationFilesDir) {
return fmt.Errorf("migration directory %q does not exist", q.migrationFilesDir)
}

if fileName == "" {
return ErrMigrationNameNotProvided
}
Expand Down
6 changes: 6 additions & 0 deletions gomigration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ func TestGoMigration_List(t *testing.T) {
driver.AssertExpectations(t)
}

func TestSetMigrationFilesDir(t *testing.T) {
q := &GoMigration{}
q.SetMigrationFilesDir("migrations")
assert.Equal(t, "migrations", q.migrationFilesDir)
}

// dummyMigration is a simple implementation of the Migration interface for testing.
type dummyMigration struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func sanitizeMigrationName(name string) (string, error) {
// sanitizeTableName validates the table name. Returns an error if it contains
// invalid characters.
func sanitizeTableName(name string) (string, error) {
valid := regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
valid := regexp.MustCompile(`^[a-zA-Z0-9_.]+$`)
if !valid.MatchString(name) {
return "", fmt.Errorf("invalid table name: %s", name)
}
Expand Down