Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PSL-1225] update primary-key for files table #902

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
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
42 changes: 0 additions & 42 deletions common/storage/ticketstore/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type FilesQueries interface {
GetFileByID(fileID string) (*types.File, error)
GetFilesByBaseFileID(baseFileID string) ([]*types.File, error)
GetFileByTaskID(taskID string) (*types.File, error)
GetFilesByBaseFileIDAndConcludedCheck(baseFileID string, isConcluded bool) ([]*types.File, error)
GetUnCompletedFiles() ([]*types.File, error)
}

Expand Down Expand Up @@ -163,47 +162,6 @@ func (s *TicketStore) GetFilesByBaseFileID(baseFileID string) ([]*types.File, er
return files, nil
}

// GetFilesByBaseFileIDAndConcludedCheck retrieves un-concluded files by base_file_id and is_concluded check from the files table.
func (s *TicketStore) GetFilesByBaseFileIDAndConcludedCheck(baseFileID string, isConcluded bool) ([]*types.File, error) {
const selectQuery = `
SELECT file_id, upload_timestamp, path, file_index, base_file_id, task_id,
reg_txid, activation_txid, req_burn_txn_amount, burn_txn_id,
req_amount, is_concluded, cascade_metadata_ticket_id, uuid_key,
hash_of_original_big_file, name_of_original_big_file_with_ext,
size_of_original_big_file, data_type_of_original_big_file,
start_block, done_block, pastel_id, passphrase
FROM files
WHERE base_file_id = ? AND is_concluded = ?;`

rows, err := s.db.Query(selectQuery, baseFileID, isConcluded)
if err != nil {
return nil, err
}
defer rows.Close()

var files []*types.File
for rows.Next() {
var file types.File
err := rows.Scan(
&file.FileID, &file.UploadTimestamp, &file.Path, &file.FileIndex, &file.BaseFileID, &file.TaskID,
&file.RegTxid, &file.ActivationTxid, &file.ReqBurnTxnAmount, &file.BurnTxnID,
&file.ReqAmount, &file.IsConcluded, &file.CascadeMetadataTicketID, &file.UUIDKey,
&file.HashOfOriginalBigFile, &file.NameOfOriginalBigFileWithExt,
&file.SizeOfOriginalBigFile, &file.DataTypeOfOriginalBigFile,
&file.StartBlock, &file.DoneBlock, &file.PastelID, &file.Passphrase)
if err != nil {
return nil, err
}
files = append(files, &file)
}

if err = rows.Err(); err != nil {
return nil, err
}

return files, nil
}

func (s *TicketStore) GetUnCompletedFiles() ([]*types.File, error) {
const selectQuery = `
SELECT file_id, upload_timestamp, path, file_index, base_file_id, task_id,
Expand Down
5 changes: 5 additions & 0 deletions common/storage/ticketstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ func OpenTicketingDb() (TicketStorageInterface, error) {
_, _ = db.Exec(alterFilesTablePassphrase)
_, _ = db.Exec(addUniqueConstraint)

err = removePrimaryKeyMigration(*db)
if err != nil {
log.WithContext(context.Background()).WithError(err).Error("error removing primary-key from file-id")
}

pragmas := []string{
"PRAGMA synchronous=NORMAL;",
"PRAGMA cache_size=-262144;",
Expand Down
87 changes: 87 additions & 0 deletions common/storage/ticketstore/unique_constraint_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ticketstore

import (
"fmt"
"log"

"github.com/jmoiron/sqlx"
)

func removePrimaryKeyMigration(db sqlx.DB) error {
var columns []struct {
Cid int `db:"cid"`
Name string `db:"name"`
Type string `db:"type"`
Notnull int `db:"notnull"`
Dflt_value *string `db:"dflt_value"`
Pk int `db:"pk"`
}

err := db.Select(&columns, "PRAGMA table_info(files);")
if err != nil {
return err
}

primaryKeyColumns := []string{}
for _, column := range columns {
if column.Pk == 1 || column.Pk == 2 {
primaryKeyColumns = append(primaryKeyColumns, column.Name)
}
}

// Check if the primary key is only on file_id
if len(primaryKeyColumns) == 1 && primaryKeyColumns[0] == "file_id" {
tx := db.MustBegin()

// Create the new table with the desired schema
tx.MustExec(`
CREATE TABLE IF NOT EXISTS files_new (
file_id TEXT NOT NULL,
upload_timestamp DATETIME,
path TEXT,
file_index TEXT,
base_file_id TEXT,
task_id TEXT,
reg_txid TEXT,
activation_txid TEXT,
req_burn_txn_amount FLOAT,
burn_txn_id TEXT,
req_amount FLOAT,
is_concluded BOOLEAN,
cascade_metadata_ticket_id TEXT,
uuid_key TEXT,
hash_of_original_big_file TEXT,
name_of_original_big_file_with_ext TEXT,
size_of_original_big_file FLOAT,
data_type_of_original_big_file TEXT,
start_block INTEGER,
done_block INTEGER,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
pastel_id TEXT,
passphrase TEXT,
PRIMARY KEY (file_id, base_file_id)
);
`)

// Copy data from the old table to the new table
tx.MustExec(`INSERT INTO files_new SELECT * FROM files;`)

// Drop the old table
tx.MustExec(`DROP TABLE files;`)

// Rename the new table to the old table name
tx.MustExec(`ALTER TABLE files_new RENAME TO files;`)

err = tx.Commit()
if err != nil {
log.Fatalln(err)
}

log.Println("primary-key update migration executed successfully")
} else {
fmt.Println("primary key constraint already existed on file_id & base_file_id. Not executing the migration")
}

return nil
}
19 changes: 0 additions & 19 deletions walletnode/services/cascaderegister/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,25 +568,6 @@ func (service *CascadeRegistrationService) SortFilesWithHigherAmounts(files type
return files
}

func (service *CascadeRegistrationService) GetConcludedVolumesByBaseFileID(BaseFileID string) (types.Files, error) {
concludedVolumes, err := service.ticketDB.GetFilesByBaseFileIDAndConcludedCheck(BaseFileID, true)
if err != nil {
return nil, err
}

return concludedVolumes, nil
}

func (service *CascadeRegistrationService) GetUnConcludedVolumesByBaseFileID(BaseFileID string) (types.Files, error) {

UnConcludedVolumes, err := service.ticketDB.GetFilesByBaseFileIDAndConcludedCheck(BaseFileID, false)
if err != nil {
return nil, err
}

return UnConcludedVolumes, nil
}

func (service *CascadeRegistrationService) GetBurnTxIdByAmount(ctx context.Context, amount int64) (string, error) {
txID, err := service.pastelHandler.PastelClient.SendToAddress(ctx, service.pastelHandler.GetBurnAddress(), amount)
if err != nil {
Expand Down
Loading