Skip to content

Commit 7649420

Browse files
committed
wip: more things
1 parent aff3608 commit 7649420

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

api/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ RUN go mod download
5151

5252
COPY ./api/entrypoint-dev.sh /entrypoint.sh
5353

54+
RUN mkdir -p /migrations
55+
COPY ./api/store/pg/migrations/ /migrations/
56+
5457
WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub/api
5558

5659
RUN mkdir -p /templates
@@ -68,6 +71,9 @@ COPY --from=builder /go/src/github.com/shellhub-io/shellhub/api/api /api
6871

6972
RUN mkdir -p /templates
7073

74+
RUN mkdir -p /migrations
75+
COPY ./api/store/pg/migrations/ /migrations/
76+
7177
COPY ./install.sh /templates/install.sh
7278

7379
ENTRYPOINT /api server
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS devices;

api/store/pg/options/run-migration.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,38 @@ func RunMigrations() Option {
3030
return err
3131
}
3232

33-
log.WithField("path", migrationsPath).Info("Applying migrations")
34-
3533
m, err := migrate.NewWithDatabaseInstance("file://"+migrationsPath, envs.DefaultBackend.Get("POSTGRES_DB"), driver)
3634
if err != nil {
35+
log.WithError(err).Error("failed to create migrate instance")
36+
3737
return errors.Join(ErrMigrationFail, err)
3838
}
3939

4040
if version, dirty, _ := m.Version(); dirty {
4141
log.WithField("version", version).
4242
WithField("dirty", dirty).
43-
Info("migrations are dirty. manual fix required")
43+
WithError(err).
44+
Error("migrations are dirty. manual fix required")
4445

4546
return errors.Join(ErrMigrationFail, err)
4647
}
4748

49+
log.WithField("path", migrationsPath).Info("applying migrations")
50+
4851
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
52+
log.WithError(err).Error("failed to apply migrations")
53+
4954
return errors.Join(ErrMigrationFail, err)
5055
}
5156

57+
log.Info("migrations applied")
58+
5259
return nil
5360
}
5461
}
5562

5663
func fetchMigrationsPath() (string, error) {
57-
cwd, err := os.Getwd()
58-
if err != nil {
59-
return "", err
60-
}
61-
62-
migrationsPath := filepath.Join(cwd, "store", "pg", "migrations")
64+
migrationsPath := filepath.Join("/", "migrations")
6365

6466
if _, err := os.Stat(migrationsPath); os.IsNotExist(err) {
6567
log.WithField("path", migrationsPath).Info("Migrations directory not found, creating it")
@@ -70,5 +72,33 @@ func fetchMigrationsPath() (string, error) {
7072
return "", err
7173
}
7274

75+
files, err := os.ReadDir(migrationsPath)
76+
if err != nil {
77+
log.WithError(err).WithField("path", migrationsPath).Error("failed to read migrations directory")
78+
return "", err
79+
}
80+
81+
if len(files) == 0 {
82+
log.Error("no migration files found in directory")
83+
} else {
84+
log.WithField("path", migrationsPath).WithField("total_files", len(files)).Info("migration files directory content:")
85+
86+
for i, file := range files {
87+
fileInfo, err := file.Info()
88+
if err != nil {
89+
log.WithField("filename", file.Name()).WithError(err).Warn("failed to get file info")
90+
continue
91+
}
92+
93+
log.WithFields(log.Fields{
94+
"index": i,
95+
"name": file.Name(),
96+
"size": fileInfo.Size(),
97+
"is_dir": file.IsDir(),
98+
"mode": fileInfo.Mode().String(),
99+
}).Info("migration file")
100+
}
101+
}
102+
73103
return migrationsPath, nil
74104
}

0 commit comments

Comments
 (0)