Skip to content

Commit

Permalink
feat: Added robust database migration layer
Browse files Browse the repository at this point in the history
  • Loading branch information
bahattincinic committed Jun 10, 2024
1 parent 5e708fc commit f2f9bea
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 40 deletions.
23 changes: 13 additions & 10 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1500,13 +1500,10 @@ const docTemplate = `{
"database.Schema": {
"type": "object",
"properties": {
"db_name": {
"field_db_name": {
"type": "string"
},
"model": {
"type": "string"
},
"name": {
"table_name": {
"type": "string"
},
"type": {
Expand Down Expand Up @@ -1560,7 +1557,10 @@ const docTemplate = `{
"type": "integer"
},
"end_lat_lng": {
"type": "string"
"type": "array",
"items": {
"type": "integer"
}
},
"external_id": {
"type": "string"
Expand Down Expand Up @@ -1629,7 +1629,10 @@ const docTemplate = `{
"type": "string"
},
"start_lat_lng": {
"type": "string"
"type": "array",
"items": {
"type": "integer"
}
},
"time_zone": {
"type": "string"
Expand Down Expand Up @@ -1816,9 +1819,6 @@ const docTemplate = `{
"distance": {
"type": "number"
},
"frame_type": {
"type": "string"
},
"id": {
"type": "string"
},
Expand All @@ -1830,6 +1830,9 @@ const docTemplate = `{
},
"primary": {
"type": "boolean"
},
"type": {
"type": "string"
}
}
},
Expand Down
23 changes: 13 additions & 10 deletions api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1489,13 +1489,10 @@
"database.Schema": {
"type": "object",
"properties": {
"db_name": {
"field_db_name": {
"type": "string"
},
"model": {
"type": "string"
},
"name": {
"table_name": {
"type": "string"
},
"type": {
Expand Down Expand Up @@ -1549,7 +1546,10 @@
"type": "integer"
},
"end_lat_lng": {
"type": "string"
"type": "array",
"items": {
"type": "integer"
}
},
"external_id": {
"type": "string"
Expand Down Expand Up @@ -1618,7 +1618,10 @@
"type": "string"
},
"start_lat_lng": {
"type": "string"
"type": "array",
"items": {
"type": "integer"
}
},
"time_zone": {
"type": "string"
Expand Down Expand Up @@ -1805,9 +1808,6 @@
"distance": {
"type": "number"
},
"frame_type": {
"type": "string"
},
"id": {
"type": "string"
},
Expand All @@ -1819,6 +1819,9 @@
},
"primary": {
"type": "boolean"
},
"type": {
"type": "string"
}
}
},
Expand Down
18 changes: 10 additions & 8 deletions api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,9 @@ definitions:
type: object
database.Schema:
properties:
db_name:
field_db_name:
type: string
model:
type: string
name:
table_name:
type: string
type:
type: string
Expand Down Expand Up @@ -148,7 +146,9 @@ definitions:
elapsed_time:
type: integer
end_lat_lng:
type: string
items:
type: integer
type: array
external_id:
type: string
flagged:
Expand Down Expand Up @@ -194,7 +194,9 @@ definitions:
start_date_local:
type: string
start_lat_lng:
type: string
items:
type: integer
type: array
time_zone:
type: string
total_elevation_gain:
Expand Down Expand Up @@ -323,8 +325,6 @@ definitions:
type: string
distance:
type: number
frame_type:
type: string
id:
type: string
model_name:
Expand All @@ -333,6 +333,8 @@ definitions:
type: string
primary:
type: boolean
type:
type: string
type: object
models.LoginType:
enum:
Expand Down
16 changes: 6 additions & 10 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/bahattincinic/fitwave/config"
"github.com/bahattincinic/fitwave/models"
"github.com/bahattincinic/fitwave/database/migrations"
pkgerrors "github.com/pkg/errors"
"go.uber.org/zap"
"gorm.io/driver/mysql"
Expand All @@ -19,6 +19,7 @@ type Database struct {
db *gorm.DB
cfg *config.Config
log *zap.Logger
mig *migrations.Migration
}

func NewDatabase(ctx context.Context, log *zap.Logger, cfg *config.Config) (*Database, error) {
Expand All @@ -39,11 +40,14 @@ func NewDatabase(ctx context.Context, log *zap.Logger, cfg *config.Config) (*Dat
return nil, pkgerrors.Wrap(err, "Open")
}

mig := migrations.NewMigration(db)

d := &Database{
db: db,
ctx: ctx,
log: log,
cfg: cfg,
mig: mig,
}

if cfg.Database.AutoMigrate {
Expand All @@ -56,15 +60,7 @@ func NewDatabase(ctx context.Context, log *zap.Logger, cfg *config.Config) (*Dat
}

func (d *Database) Migrate() error {
m := []interface{}{
&models.Activity{},
&models.Athlete{},
&models.Gear{},
&models.Config{},
&models.Dashboard{},
&models.Component{},
}
return d.db.AutoMigrate(m...)
return d.mig.Migrate()
}

func (d *Database) BeginTransaction() *gorm.DB {
Expand Down
99 changes: 99 additions & 0 deletions database/migrations/202404171309_strava.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package migrations

import (
"time"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/datatypes"
"gorm.io/gorm"
)

// createStravaTables returns the migrations for creating Strava-related tables.
func (m *Migration) createStravaTables() []*gormigrate.Migration {
return []*gormigrate.Migration{{
ID: "202404171309",
Migrate: func(tx *gorm.DB) error {
type athlete struct {
Id int64 `gorm:"primaryKey;autoIncrement:false"`
FirstName string
LastName string
ProfileMedium string
Profile string
City string
State string
Country string
Gender string
Friend string
Follower string
Premium bool
CreatedAt time.Time
UpdatedAt time.Time
ApproveFollowers bool
BadgeTypeId int
Stats datatypes.JSON
}
type gear struct {
Id string `gorm:"primaryKey;autoIncrement:false"`
Name string
Primary bool
Distance float64
BrandName string
ModelName string
Type string
Description string
AthleteID int64
Athlete *athlete `gorm:"foreignkey:AthleteID"`
}
type activity struct {
Id int64 `gorm:"primaryKey;autoIncrement:false"`
ExternalId string
UploadId int64
Name string
Distance float64
MovingTime int
ElapsedTime int
TotalElevationGain float64
Type string
StartDate time.Time
StartDateLocal time.Time
TimeZone string
StartLocation datatypes.JSON
EndLocation datatypes.JSON
City string
State string
Country string
AchievementCount int
KudosCount int
CommentCount int
AthleteCount int
PhotoCount int
Map datatypes.JSON
Trainer bool
Commute bool
Manual bool
Private bool
Flagged bool
AverageSpeed float64
MaximumSpeed float64
AverageCadence float64
AverageTemperature float64
AveragePower float64
WeightedAveragePower int
Kilojoules float64
DeviceWatts bool
AverageHeartRate float64
MaximumHeartRate float64
Truncated int
HasKudos bool
AthleteID uint
Athlete athlete `gorm:"foreignkey:AthleteID"`
GearID *string
Gear *athlete `gorm:"foreignkey:GearID"`
}
return tx.AutoMigrate(&activity{}, &athlete{}, &gear{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("activities", "gears", "athletes")
},
}}
}
31 changes: 31 additions & 0 deletions database/migrations/202404171310_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package migrations

import (
"time"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

// createConfigTable returns the migration for creating the Config table.
func (m *Migration) createConfigTable() []*gormigrate.Migration {
return []*gormigrate.Migration{{
ID: "202404171310",
Migrate: func(tx *gorm.DB) error {
type config struct {
ID uint `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
ClientId int
ClientSecret string
LoginType string
LoginUsername string
LoginPassword string
}
return tx.AutoMigrate(&config{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("configs")
},
}}
}
39 changes: 39 additions & 0 deletions database/migrations/202404171311_dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package migrations

import (
"time"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/datatypes"
"gorm.io/gorm"
)

// createDashboardTables returns the migrations for creating Dashboard-related tables.
func (m *Migration) createDashboardTables() []*gormigrate.Migration {
return []*gormigrate.Migration{{
ID: "202404171311",
Migrate: func(tx *gorm.DB) error {
type dashboard struct {
ID uint `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
Name string
}
type component struct {
ID uint `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
DashboardID uint
Dashboard dashboard `gorm:"foreignkey:DashboardID"`
Name string
Query string
Type string
Configs datatypes.JSON
}
return tx.AutoMigrate(&dashboard{}, &component{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("dashboards", "components")
},
}}
}
Loading

0 comments on commit f2f9bea

Please sign in to comment.