Added gigs_total_disk_space to host_disks. (#15753)

Added gigs_total_disk_space to host_disks.

This is just the migration that is needed for the rest of the changes in
#15058
This commit is contained in:
Victor Lyuboslavsky 2023-12-21 12:13:04 -06:00 committed by GitHub
parent 367459fe02
commit 72621e31ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 2 deletions

View File

@ -0,0 +1,26 @@
package tables
import (
"database/sql"
"fmt"
)
func init() {
MigrationClient.AddMigration(Up_20231219143041, Down_20231219143041)
}
func Up_20231219143041(tx *sql.Tx) error {
stmt := `
ALTER TABLE host_disks
ADD COLUMN gigs_total_disk_space decimal(10,2) NOT NULL DEFAULT '0.00';
`
if _, err := tx.Exec(stmt); err != nil {
return fmt.Errorf("add gigs_total_disk_space to host_disks: %w", err)
}
return nil
}
func Down_20231219143041(tx *sql.Tx) error {
return nil
}

View File

@ -0,0 +1,35 @@
package tables
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestUp_20231219143041(t *testing.T) {
db := applyUpToPrev(t)
insertStmt := `INSERT INTO host_disks (host_id) VALUES (1)`
_, err := db.Exec(insertStmt)
require.NoError(t, err)
// Apply current migration.
applyNext(t, db)
type diskSpace struct {
HostID uint `db:"host_id"`
GigsTotalDiskSpace float64 `db:"gigs_total_disk_space"`
}
var ds diskSpace
err = db.Get(&ds, `SELECT host_id, gigs_total_disk_space from host_disks where host_id = 1`)
require.NoError(t, err)
assert.Equal(t, float64(0), ds.GigsTotalDiskSpace)
_, err = db.Exec(`INSERT INTO host_disks (host_id, gigs_total_disk_space) VALUES (2, 1.5)`)
require.NoError(t, err)
err = db.Get(&ds, `SELECT host_id, gigs_total_disk_space from host_disks where host_id = 2`)
require.NoError(t, err)
assert.Equal(t, 1.5, ds.GigsTotalDiskSpace)
}

File diff suppressed because one or more lines are too long