diff --git a/storagenode/storagenodedb/database.go b/storagenode/storagenodedb/database.go index 3d66715e3..aa2ee59f4 100644 --- a/storagenode/storagenodedb/database.go +++ b/storagenode/storagenodedb/database.go @@ -1410,6 +1410,42 @@ func (db *DB) Migration(ctx context.Context) *migrate.Migration { return errs.Wrap(err) } + return nil + }), + }, + { + DB: db.satellitesDB, + Description: "Make satellite_id foreign key in satellite_exit_progress table", + Version: 41, + Action: migrate.Func(func(ctx context.Context, _ *zap.Logger, rdb tagsql.DB, rtx tagsql.Tx) (err error) { + _, err = rtx.Exec(ctx, ` + CREATE TABLE satellite_exit_progress_new ( + satellite_id BLOB NOT NULL, + initiated_at TIMESTAMP, + finished_at TIMESTAMP, + starting_disk_usage INTEGER NOT NULL, + bytes_deleted INTEGER NOT NULL, + completion_receipt BLOB, + FOREIGN KEY (satellite_id) REFERENCES satellites (node_id) + ); + + INSERT INTO satellite_exit_progress_new SELECT + satellite_id, + initiated_at, + finished_at, + starting_disk_usage, + bytes_deleted, + completion_receipt + FROM satellite_exit_progress; + + DROP TABLE satellite_exit_progress; + + ALTER TABLE satellite_exit_progress_new RENAME TO satellite_exit_progress; + `) + if err != nil { + return errs.Wrap(err) + } + return nil }), }, diff --git a/storagenode/storagenodedb/schema.go b/storagenode/storagenodedb/schema.go index febe699f8..bd93b4e3c 100644 --- a/storagenode/storagenodedb/schema.go +++ b/storagenode/storagenodedb/schema.go @@ -577,8 +577,7 @@ func Schema() map[string]*dbschema.Schema { "satellites": &dbschema.Schema{ Tables: []*dbschema.Table{ &dbschema.Table{ - Name: "satellite_exit_progress", - PrimaryKey: []string{"satellite_id"}, + Name: "satellite_exit_progress", Columns: []*dbschema.Column{ &dbschema.Column{ Name: "bytes_deleted", @@ -604,6 +603,7 @@ func Schema() map[string]*dbschema.Schema { Name: "satellite_id", Type: "BLOB", IsNullable: false, + Reference: &dbschema.Reference{Table: "satellites", Column: "node_id", OnDelete: "", OnUpdate: ""}, }, &dbschema.Column{ Name: "starting_disk_usage", diff --git a/storagenode/storagenodedb/testdata/multidbsnapshot.go b/storagenode/storagenodedb/testdata/multidbsnapshot.go index 6c94897bd..5db2c36ea 100644 --- a/storagenode/storagenodedb/testdata/multidbsnapshot.go +++ b/storagenode/storagenodedb/testdata/multidbsnapshot.go @@ -55,6 +55,7 @@ var States = MultiDBStates{ &v38, &v39, &v40, + &v41, }, } diff --git a/storagenode/storagenodedb/testdata/v41.go b/storagenode/storagenodedb/testdata/v41.go new file mode 100644 index 000000000..84faa8976 --- /dev/null +++ b/storagenode/storagenodedb/testdata/v41.go @@ -0,0 +1,47 @@ +// Copyright (C) 2020 Storj Labs, Inc. +// See LICENSE for copying information. + +package testdata + +import ( + "storj.io/storj/storagenode/storagenodedb" +) + +var v41 = MultiDBState{ + Version: 41, + DBStates: DBStates{ + storagenodedb.UsedSerialsDBName: v40.DBStates[storagenodedb.UsedSerialsDBName], + storagenodedb.StorageUsageDBName: v40.DBStates[storagenodedb.StorageUsageDBName], + storagenodedb.ReputationDBName: v40.DBStates[storagenodedb.ReputationDBName], + storagenodedb.PieceSpaceUsedDBName: v40.DBStates[storagenodedb.PieceSpaceUsedDBName], + storagenodedb.PieceInfoDBName: v40.DBStates[storagenodedb.PieceInfoDBName], + storagenodedb.PieceExpirationDBName: v40.DBStates[storagenodedb.PieceExpirationDBName], + storagenodedb.OrdersDBName: v40.DBStates[storagenodedb.OrdersDBName], + storagenodedb.BandwidthDBName: v40.DBStates[storagenodedb.BandwidthDBName], + storagenodedb.SatellitesDBName: &DBState{ + SQL: ` + CREATE TABLE satellites ( + node_id BLOB NOT NULL, + added_at TIMESTAMP NOT NULL, + status INTEGER NOT NULL, + PRIMARY KEY (node_id) + ); + CREATE TABLE satellite_exit_progress ( + satellite_id BLOB NOT NULL, + initiated_at TIMESTAMP, + finished_at TIMESTAMP, + starting_disk_usage INTEGER NOT NULL, + bytes_deleted INTEGER NOT NULL, + completion_receipt BLOB, + FOREIGN KEY (satellite_id) REFERENCES satellites (node_id) + ); + INSERT INTO satellites VALUES(X'0ed28abb2813e184a1e98b0f6605c4911ea468c7e8433eb583e0fca7ceac3000','2019-09-10 20:00:00+00:00', 0); + INSERT INTO satellite_exit_progress VALUES(X'0ed28abb2813e184a1e98b0f6605c4911ea468c7e8433eb583e0fca7ceac3000','2019-09-10 20:00:00+00:00', null, 100, 0, null); + `, + }, + storagenodedb.DeprecatedInfoDBName: v40.DBStates[storagenodedb.DeprecatedInfoDBName], + storagenodedb.NotificationsDBName: v40.DBStates[storagenodedb.NotificationsDBName], + storagenodedb.HeldAmountDBName: v40.DBStates[storagenodedb.HeldAmountDBName], + storagenodedb.PricingDBName: v40.DBStates[storagenodedb.PricingDBName], + }, +}