Skip to content

Commit 877acd0

Browse files
committed
store migration in strings, remove sorting logic
1 parent 3a6aee0 commit 877acd0

File tree

1 file changed

+9
-55
lines changed

1 file changed

+9
-55
lines changed

dbos/system_database.go

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ package dbos
22

33
import (
44
"context"
5-
"embed"
5+
_ "embed"
66
"errors"
77
"fmt"
8-
"io/fs"
98
"log/slog"
10-
"path/filepath"
11-
"sort"
12-
"strconv"
139
"strings"
1410
"sync"
1511
"time"
@@ -119,8 +115,13 @@ func createDatabaseIfNotExists(ctx context.Context, databaseURL string, logger *
119115
return nil
120116
}
121117

122-
//go:embed migrations/*.sql
123-
var migrationFiles embed.FS
118+
//go:embed migrations/1_initial_dbos_schema.sql
119+
var migration1 string
120+
121+
// migrations contains all migration files with their version numbers
122+
var migrations = []migrationFile{
123+
{version: 1, sql: migration1},
124+
}
124125

125126
const (
126127
_DBOS_MIGRATION_TABLE = "dbos_migrations"
@@ -178,11 +179,7 @@ func runMigrations(databaseURL string) error {
178179
return fmt.Errorf("failed to get current migration version: %v", err)
179180
}
180181

181-
// Read and parse migration files
182-
migrations, err := parseMigrationFiles()
183-
if err != nil {
184-
return fmt.Errorf("failed to parse migration files: %v", err)
185-
}
182+
// Use the embedded migrations slice
186183

187184
// Apply migrations starting from the next version
188185
for _, migration := range migrations {
@@ -226,49 +223,6 @@ type migrationFile struct {
226223
sql string
227224
}
228225

229-
func parseMigrationFiles() ([]migrationFile, error) {
230-
var migrations []migrationFile
231-
232-
entries, err := fs.ReadDir(migrationFiles, "migrations")
233-
if err != nil {
234-
return nil, fmt.Errorf("failed to read migration directory: %v", err)
235-
}
236-
237-
for _, entry := range entries {
238-
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
239-
continue
240-
}
241-
242-
// Extract version from filename (e.g., "1_initial_dbos_schema.sql" -> 1)
243-
parts := strings.SplitN(entry.Name(), "_", 2)
244-
if len(parts) < 2 {
245-
continue
246-
}
247-
248-
version, err := strconv.ParseInt(parts[0], 10, 64)
249-
if err != nil {
250-
continue // Skip files with invalid version format
251-
}
252-
253-
// Read migration SQL content
254-
content, err := fs.ReadFile(migrationFiles, filepath.Join("migrations", entry.Name()))
255-
if err != nil {
256-
return nil, fmt.Errorf("failed to read migration file %s: %v", entry.Name(), err)
257-
}
258-
259-
migrations = append(migrations, migrationFile{
260-
version: version,
261-
sql: string(content),
262-
})
263-
}
264-
265-
// Sort migrations by version
266-
sort.Slice(migrations, func(i, j int) bool {
267-
return migrations[i].version < migrations[j].version
268-
})
269-
270-
return migrations, nil
271-
}
272226

273227
// New creates a new SystemDatabase instance and runs migrations
274228
func newSystemDatabase(ctx context.Context, databaseURL string, logger *slog.Logger) (systemDatabase, error) {

0 commit comments

Comments
 (0)