-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
375 additions
and
64 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
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,10 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
ALTER TABLE StorageTagged | ||
ADD TransferHost TEXT; | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
SELECT 'down SQL query'; | ||
-- +goose StatementEnd |
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,58 @@ | ||
package migrations | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"github.com/filecoin-project/boost/storagemarket/types" | ||
"github.com/pressly/goose/v3" | ||
) | ||
|
||
func init() { | ||
goose.AddMigration(UpSetStorageTaggedTransferHost, DownSetStorageTaggedTransferHost) | ||
} | ||
|
||
func UpSetStorageTaggedTransferHost(tx *sql.Tx) error { | ||
errPrefix := "setting StorageTagged.TransferHost: " | ||
qry := "SELECT Deals.ID, Deals.TransferType, Deals.TransferParams " + | ||
"FROM Deals INNER JOIN StorageTagged " + | ||
"ON Deals.ID = StorageTagged.DealUUID" | ||
rows, err := tx.Query(qry) | ||
if err != nil { | ||
return fmt.Errorf(errPrefix+"getting deals from DB: %w", err) | ||
} | ||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var id string | ||
var xferType string | ||
var params []byte | ||
err := rows.Scan(&id, &xferType, ¶ms) | ||
if err != nil { | ||
return fmt.Errorf(errPrefix+"scanning Deals row: %w", err) | ||
} | ||
|
||
dealErrPrefix := fmt.Sprintf(errPrefix+"deal %s: ", id) | ||
|
||
xfer := types.Transfer{ | ||
Type: xferType, | ||
Params: params, | ||
} | ||
host, err := xfer.Host() | ||
if err != nil { | ||
log.Warnw(dealErrPrefix+"ignoring - couldn't parse transfer params %s: '%s': %s", xferType, params, err) | ||
continue | ||
} | ||
|
||
_, err = tx.Exec("UPDATE StorageTagged SET TransferHost = ? WHERE DealUUID = ?", host, id) | ||
if err != nil { | ||
return fmt.Errorf(dealErrPrefix+"saving TransferHost to DB: %w", err) | ||
} | ||
} | ||
return rows.Err() | ||
} | ||
|
||
func DownSetStorageTaggedTransferHost(tx *sql.Tx) error { | ||
// This code is executed when the migration is rolled back. | ||
// Do nothing because sqlite doesn't support removing a column. | ||
return nil | ||
} |
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,70 @@ | ||
package migrations_tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/filecoin-project/boost/db" | ||
"github.com/filecoin-project/boost/db/migrations" | ||
"github.com/filecoin-project/boost/storagemarket/types" | ||
"github.com/pressly/goose/v3" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestStorageTaggedSetHost(t *testing.T) { | ||
req := require.New(t) | ||
ctx := context.Background() | ||
|
||
sqldb := db.CreateTestTmpDB(t) | ||
req.NoError(db.CreateAllBoostTables(ctx, sqldb, sqldb)) | ||
|
||
// Run migrations up to the one that adds the TransferHost field to StorageTagged | ||
goose.SetBaseFS(migrations.EmbedMigrations) | ||
req.NoError(goose.SetDialect("sqlite3")) | ||
req.NoError(goose.UpTo(sqldb, ".", 20220908122510)) | ||
|
||
// Generate 2 deals | ||
dealsDB := db.NewDealsDB(sqldb) | ||
deals, err := db.GenerateNDeals(2) | ||
req.NoError(err) | ||
|
||
// Set the transfer params such that each deal has a different host | ||
getHost := func(i int) string { | ||
return fmt.Sprintf("files.org:%d", 1000+i) | ||
} | ||
for i, deal := range deals { | ||
deal.Transfer = types.Transfer{ | ||
Type: "http", | ||
Params: []byte(fmt.Sprintf(`{"url":"http://%s/file.car"}`, getHost(i))), | ||
Size: uint64(1024), | ||
} | ||
err = dealsDB.Insert(ctx, &deal) | ||
req.NoError(err) | ||
} | ||
|
||
// Simulate tagging a deal | ||
taggedStorageDB := db.NewStorageDB(sqldb) | ||
err = taggedStorageDB.Tag(ctx, deals[0].DealUuid, 1024, "") | ||
req.NoError(err) | ||
|
||
// Run the migration that reads the deal transfer params and sets | ||
// StorageTagged.TransferHost | ||
req.NoError(goose.UpByOne(sqldb, ".")) | ||
|
||
// Check that after migrating up, the host is set correctly | ||
rows, err := sqldb.QueryContext(ctx, "SELECT TransferHost FROM StorageTagged") | ||
req.NoError(err) | ||
defer rows.Close() //nolint:errcheck | ||
|
||
rowIdx := 0 | ||
for ; rows.Next(); rowIdx++ { | ||
var host string | ||
err := rows.Scan(&host) | ||
req.NoError(err) | ||
req.Equal(getHost(0), host) | ||
} | ||
|
||
// Even though there are two deals in DB, there is only one deal that is | ||
// tagged, so there should only be one row | ||
req.Equal(1, rowIdx) | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.