From 5083e349de63b865b88ed6a23dad532a336866e3 Mon Sep 17 00:00:00 2001 From: j-rafique Date: Sat, 13 Jul 2024 14:40:52 +0500 Subject: [PATCH] [PSL-1225] update primary-key for files table --- common/storage/ticketstore/files.go | 42 --------- common/storage/ticketstore/store.go | 5 ++ .../unique_constraint_migration.go | 87 +++++++++++++++++++ .../services/cascaderegister/service.go | 19 ---- 4 files changed, 92 insertions(+), 61 deletions(-) create mode 100644 common/storage/ticketstore/unique_constraint_migration.go diff --git a/common/storage/ticketstore/files.go b/common/storage/ticketstore/files.go index 0e503296f..9c5bbb338 100644 --- a/common/storage/ticketstore/files.go +++ b/common/storage/ticketstore/files.go @@ -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) } @@ -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, diff --git a/common/storage/ticketstore/store.go b/common/storage/ticketstore/store.go index c47fb81fa..94b0550dd 100644 --- a/common/storage/ticketstore/store.go +++ b/common/storage/ticketstore/store.go @@ -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;", diff --git a/common/storage/ticketstore/unique_constraint_migration.go b/common/storage/ticketstore/unique_constraint_migration.go new file mode 100644 index 000000000..b7963153d --- /dev/null +++ b/common/storage/ticketstore/unique_constraint_migration.go @@ -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 +} diff --git a/walletnode/services/cascaderegister/service.go b/walletnode/services/cascaderegister/service.go index b3032e312..69f936daf 100644 --- a/walletnode/services/cascaderegister/service.go +++ b/walletnode/services/cascaderegister/service.go @@ -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 {