MCPcopy Index your code
hub / github.com/dnote/dnote / getMigrationFiles

Function getMigrationFiles

pkg/server/database/migrate.go:74–111  ·  view source on GitHub ↗

getMigrationFiles reads, validates, and sorts migration files

(fsys fs.FS)

Source from the content-addressed store, hash-verified

72
73// getMigrationFiles reads, validates, and sorts migration files
74func getMigrationFiles(fsys fs.FS) ([]migrationFile, error) {
75 entries, err := fs.ReadDir(fsys, ".")
76 if err != nil {
77 return nil, errors.Wrap(err, "reading migration directory")
78 }
79
80 var migrations []migrationFile
81 seen := make(map[int]string)
82 for _, e := range entries {
83 name := e.Name()
84
85 if err := validateMigrationFilename(name); err != nil {
86 return nil, err
87 }
88
89 // Parse version
90 var v int
91 fmt.Sscanf(name, "%d", &v)
92
93 // Check for duplicate version numbers
94 if existing, found := seen[v]; found {
95 return nil, errors.Errorf("duplicate migration version %d: %s and %s", v, existing, name)
96 }
97 seen[v] = name
98
99 migrations = append(migrations, migrationFile{
100 filename: name,
101 version: v,
102 })
103 }
104
105 // Sort by version
106 sort.Slice(migrations, func(i, j int) bool {
107 return migrations[i].version < migrations[j].version
108 })
109
110 return migrations, nil
111}
112
113// migrate runs migrations from the provided filesystem
114func migrate(db *gorm.DB, fsys fs.FS) error {

Callers 1

migrateFunction · 0.85

Calls 2

ReadDirMethod · 0.45

Tested by

no test coverage detected