Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Backfill realm stats based on user and apikey stats #527

Merged
merged 1 commit into from
Sep 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pkg/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,57 @@ func (db *Database) getMigrations(ctx context.Context) *gormigrate.Gormigrate {
return tx.Exec("ALTER TABLE realms ALTER COLUMN require_date SET NULL").Error
},
},
{
ID: "00039-RealmStatsToDate",
Migrate: func(tx *gorm.DB) error {
logger.Debugw("db migrations: changing realm stats to date")
return tx.Exec("ALTER TABLE realm_stats ALTER COLUMN date TYPE date").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE realm_stats ALTER COLUMN date TYPE timestamp with time zone").Error
},
},
{
ID: "00040-BackfillRealmStats",
Migrate: func(tx *gorm.DB) error {
logger.Debugw("db migrations: back-filling realm stats")
sqls := []string{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is worth the over haul now but would it be safer and more performant to use batch calls?

*I just started learning about this so I don't have a good understanding of the true advantages of removing round trip to database between calls.

`
INSERT INTO realm_stats (
SELECT date, realm_id, SUM(codes_issued) AS codes_issued
FROM user_stats
WHERE user_stats.date < date('2020-09-15')
GROUP BY 1, 2
ORDER BY 1 DESC
) ON CONFLICT(date, realm_id) DO UPDATE
SET codes_issued = realm_stats.codes_issued + excluded.codes_issued
`,
`
INSERT INTO realm_stats (
SELECT date, authorized_apps.realm_id AS realm_id, SUM(codes_issued) AS codes_issued
FROM authorized_app_stats
JOIN authorized_apps
ON authorized_app_stats.authorized_app_id = authorized_apps.id
WHERE authorized_app_stats.date < date('2020-09-15')
GROUP BY 1, 2
ORDER BY 1 DESC
) ON CONFLICT(date, realm_id) DO UPDATE
SET codes_issued = realm_stats.codes_issued + excluded.codes_issued
`,
}

for _, sql := range sqls {
if err := tx.Exec(sql).Error; err != nil {
return err
}
}

return nil
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
})
}

Expand Down