• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mishankov / platforma / 18815787624

26 Oct 2025 09:06AM UTC coverage: 9.617% (+0.04%) from 9.581%
18815787624

Pull #9

github

mishankov
ci: add prealloc linter to golangci configuration
Pull Request #9: Linter configuration

11 of 42 new or added lines in 7 files covered. (26.19%)

69 existing lines in 7 files now uncovered.

98 of 1019 relevant lines covered (9.62%)

0.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/database/database.go
1
package database
2

3
import (
4
        "context"
5
        "slices"
6
        "time"
7

8
        "github.com/mishankov/platforma/log"
9

10
        "github.com/jmoiron/sqlx"
11
        _ "github.com/lib/pq"
12
)
13

14
type Database struct {
15
        *sqlx.DB
16
        repositories map[string]any
17
        migrators    map[string]shemer
18
}
19

UNCOV
20
func New(connection string) (*Database, error) {
×
21
        db, err := sqlx.Connect("postgres", connection)
×
22
        if err != nil {
×
23
                return nil, err
×
24
        }
×
25
        return &Database{DB: db, repositories: make(map[string]any), migrators: make(map[string]shemer)}, nil
×
26
}
27

UNCOV
28
func (db *Database) RegisterRepository(name string, repository any) {
×
29
        db.repositories[name] = repository
×
30

×
31
        if migr, ok := repository.(shemer); ok {
×
32
                db.migrators[name] = migr
×
33
        }
×
34
}
35

UNCOV
36
func (db *Database) Migrate(ctx context.Context) error {
×
37
        if _, err := db.ExecContext(ctx, "CREATE TABLE IF NOT EXISTS platforma_migrations (repository TEXT, id TEXT, timestamp TIMESTAMP)"); err != nil {
×
38
                return err
×
39
        }
×
40

41
        // Select data from platforma_migrations table
UNCOV
42
        var migrationsState []migrations
×
43
        err := db.SelectContext(ctx, &migrationsState, "SELECT * FROM platforma_migrations")
×
44
        if err != nil {
×
45
                return err
×
46
        }
×
47

UNCOV
48
        appliedMigrations := []Migration{}
×
49
        migrationErr := error(nil)
×
50

×
51
        for repoName, migr := range db.migrators {
×
52
                repoMigrations, repoSchema := migr.Schema()
×
53

×
54
                repoHasMigrations := slices.ContainsFunc(migrationsState, func(m migrations) bool {
×
55
                        return m.Repository == repoName
×
56
                })
×
57

58
                // If repo does not has migrations apply schema and exit
UNCOV
59
                if !repoHasMigrations {
×
60
                        for _, query := range repoSchema.Queries {
×
61
                                if _, err := db.ExecContext(ctx, query); err != nil {
×
62
                                        migrationErr = err
×
63
                                        break
×
64
                                }
UNCOV
65
                                log.InfoContext(ctx, "schema applied", "repository", repoName)
×
66
                        }
67

68
                        // Log that schema applied
UNCOV
69
                        if _, err := db.ExecContext(ctx, "INSERT INTO platforma_migrations (repository, timestamp) VALUES ($1, $2)", repoName, time.Now()); err != nil {
×
70
                                return err
×
71
                        }
×
72

73
                        // If schema is applied, log that all migrations are also applied
UNCOV
74
                        for _, migration := range repoMigrations {
×
75
                                if _, err := db.ExecContext(ctx, "INSERT INTO platforma_migrations (repository, id, timestamp) VALUES ($1, $2, $3)", repoName, migration.ID, time.Now()); err != nil {
×
76
                                        return err
×
77
                                }
×
78
                        }
79

UNCOV
80
                        continue
×
81
                }
82

UNCOV
83
                for _, migration := range repoMigrations {
×
84
                        migration.repository = repoName
×
85

×
86
                        // Check if migration has been applied
×
87
                        migrationHasApplied := slices.ContainsFunc(migrationsState, func(m migrations) bool {
×
88
                                return m.Repository == repoName && m.MigrationId.String == migration.ID
×
89
                        })
×
90

UNCOV
91
                        if migrationHasApplied {
×
92
                                continue
×
93
                        }
94

UNCOV
95
                        if _, err := db.ExecContext(ctx, migration.Up); err != nil {
×
96
                                migrationErr = err
×
97
                                log.ErrorContext(ctx, "failed to apply migration for repository", "migration", migration.ID, "repository", repoName)
×
98
                                break
×
99
                        }
100

UNCOV
101
                        appliedMigrations = append(appliedMigrations, migration)
×
102
                        log.InfoContext(ctx, "applied migration for repository", "migration", migration.ID, "repository", repoName)
×
103

×
104
                        // Log that migration applied
×
105
                        if _, err := db.ExecContext(ctx, "INSERT INTO platforma_migrations (repository, id, timestamp) VALUES ($1, $2, $3)", repoName, migration.ID, time.Now()); err != nil {
×
106
                                return err
×
107
                        }
×
108
                }
109

UNCOV
110
                if migrationErr != nil {
×
111
                        break
×
112
                }
113
        }
114

UNCOV
115
        if migrationErr != nil {
×
116
                for _, migration := range slices.Backward(appliedMigrations) {
×
117
                        if _, err := db.ExecContext(ctx, migration.Down); err != nil {
×
118
                                log.ErrorContext(ctx, "failed to rollback migration %s for repository %s", migration.ID, migration.repository)
×
119
                                return err
×
120
                        }
×
121

UNCOV
122
                        if _, err := db.ExecContext(ctx, "DELETE FROM platforma_migrations WHERE repository = $1 AND id = $2", migration.repository, migration.ID); err != nil {
×
123
                                return err
×
124
                        }
×
125
                }
126
        }
127

UNCOV
128
        return migrationErr
×
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc