-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added robust database migration layer
- Loading branch information
1 parent
5e708fc
commit d0c892c
Showing
7 changed files
with
214 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}, | ||
}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}, | ||
}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}, | ||
}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package migrations | ||
|
||
import ( | ||
"github.com/go-gormigrate/gormigrate/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Migration struct { | ||
migrations []*gormigrate.Migration | ||
migrate *gormigrate.Gormigrate | ||
} | ||
|
||
func NewMigration(db *gorm.DB) *Migration { | ||
m := &Migration{} | ||
m.migrate = gormigrate.New(db, gormigrate.DefaultOptions, m.Get()) | ||
return m | ||
} | ||
|
||
// Get returns the list of all migrations to be applied. | ||
func (m *Migration) Get() []*gormigrate.Migration { | ||
var migrations []*gormigrate.Migration | ||
|
||
migrations = append(migrations, m.createStravaTables()...) | ||
migrations = append(migrations, m.createConfigTable()...) | ||
migrations = append(migrations, m.createDashboardTables()...) | ||
|
||
return migrations | ||
} | ||
|
||
// Migrate applies the migrations to the database. | ||
func (m *Migration) Migrate() error { | ||
return m.migrate.Migrate() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters