Skip to content

Commit 1d798a0

Browse files
committed
fix: migrations
1 parent 127b326 commit 1d798a0

File tree

1 file changed

+2
-74
lines changed

1 file changed

+2
-74
lines changed

pkg/application/migrations.go

+2-74
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"embed"
66
"fmt"
7-
"log"
87
"os"
98
"strings"
109
"time"
@@ -81,47 +80,6 @@ func (m *migrationManager) CollectSchema(ctx context.Context) error {
8180
return schemaCollector.StoreMigrations(upChanges, downChanges)
8281
}
8382

84-
// CollectMigrations loads the migration files from the migrations directory
85-
func (m *migrationManager) CollectMigrations() ([]*migrate.Migration, error) {
86-
entries, err := os.ReadDir(m.migrationsDir)
87-
if err != nil {
88-
if os.IsNotExist(err) {
89-
return []*migrate.Migration{}, nil
90-
}
91-
return nil, fmt.Errorf("failed to read migrations directory: %w", err)
92-
}
93-
94-
var migrations []*migrate.Migration
95-
for _, entry := range entries {
96-
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
97-
continue
98-
}
99-
100-
content, err := os.ReadFile(fmt.Sprintf("%s/%s", m.migrationsDir, entry.Name()))
101-
if err != nil {
102-
return nil, fmt.Errorf("failed to read migration file %s: %w", entry.Name(), err)
103-
}
104-
105-
migration := &migrate.Migration{
106-
Id: entry.Name(),
107-
}
108-
109-
// Split content by ;; to separate migration queries
110-
queries := strings.Split(string(content), ";;")
111-
for _, query := range queries {
112-
query = strings.TrimSpace(query)
113-
if query == "" {
114-
continue
115-
}
116-
migration.Up = append(migration.Up, query)
117-
}
118-
119-
migrations = append(migrations, migration)
120-
}
121-
122-
return migrations, nil
123-
}
124-
12583
func newTxError(migration *migrate.PlannedMigration, err error) *migrate.TxError {
12684
return &migrate.TxError{
12785
Migration: migration.Migration,
@@ -197,29 +155,8 @@ func (m *migrationManager) applyMigrations(
197155
return applied, nil
198156
}
199157

200-
// Internal in-memory migration source that respects the order of migrations.
201-
type memoryMigrationSourceInternal struct {
202-
Migrations []*migrate.Migration
203-
}
204-
205-
// FindMigrations returns the list of unsorted migrations. Giving up deterministic order in favor of
206-
// the order in which the migrations were added.
207-
func (m *memoryMigrationSourceInternal) FindMigrations() ([]*migrate.Migration, error) {
208-
migrations := make([]*migrate.Migration, len(m.Migrations))
209-
copy(migrations, m.Migrations)
210-
return migrations, nil
211-
}
212-
213158
func (m *migrationManager) Run() error {
214159
db := stdlib.OpenDB(*m.pool.Config().ConnConfig)
215-
migrations, err := m.CollectMigrations()
216-
if err != nil {
217-
return err
218-
}
219-
if len(migrations) == 0 {
220-
log.Printf("No migrations found")
221-
return nil
222-
}
223160
migrationSource := &migrate.FileMigrationSource{
224161
Dir: m.migrationsDir,
225162
}
@@ -234,22 +171,13 @@ func (m *migrationManager) Run() error {
234171
return err
235172
}
236173
m.logger.Infof("Applied %d migrations", applied)
237-
log.Printf("Applied %d migrations", applied)
238174
return nil
239175
}
240176

241177
func (m *migrationManager) Rollback() error {
242178
db := stdlib.OpenDB(*m.pool.Config().ConnConfig)
243-
migrations, err := m.CollectMigrations()
244-
if err != nil {
245-
return err
246-
}
247-
if len(migrations) == 0 {
248-
log.Printf("No migrations found")
249-
return nil
250-
}
251-
migrationSource := &migrate.MemoryMigrationSource{
252-
Migrations: migrations,
179+
migrationSource := &migrate.FileMigrationSource{
180+
Dir: m.migrationsDir,
253181
}
254182
ms := migrate.MigrationSet{}
255183
plannedMigrations, dbMap, err := ms.PlanMigration(db, "postgres", migrationSource, migrate.Down, 0)

0 commit comments

Comments
 (0)