Skip to content

Commit e0fc962

Browse files
authored
Merge pull request #902 from pastelnetwork/PSL-1225_fixUniqueConstraint
[PSL-1225] update primary-key for files table
2 parents 8e6b576 + 5083e34 commit e0fc962

File tree

4 files changed

+92
-61
lines changed

4 files changed

+92
-61
lines changed

common/storage/ticketstore/files.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ type FilesQueries interface {
1111
GetFileByID(fileID string) (*types.File, error)
1212
GetFilesByBaseFileID(baseFileID string) ([]*types.File, error)
1313
GetFileByTaskID(taskID string) (*types.File, error)
14-
GetFilesByBaseFileIDAndConcludedCheck(baseFileID string, isConcluded bool) ([]*types.File, error)
1514
GetUnCompletedFiles() ([]*types.File, error)
1615
}
1716

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

166-
// GetFilesByBaseFileIDAndConcludedCheck retrieves un-concluded files by base_file_id and is_concluded check from the files table.
167-
func (s *TicketStore) GetFilesByBaseFileIDAndConcludedCheck(baseFileID string, isConcluded bool) ([]*types.File, error) {
168-
const selectQuery = `
169-
SELECT file_id, upload_timestamp, path, file_index, base_file_id, task_id,
170-
reg_txid, activation_txid, req_burn_txn_amount, burn_txn_id,
171-
req_amount, is_concluded, cascade_metadata_ticket_id, uuid_key,
172-
hash_of_original_big_file, name_of_original_big_file_with_ext,
173-
size_of_original_big_file, data_type_of_original_big_file,
174-
start_block, done_block, pastel_id, passphrase
175-
FROM files
176-
WHERE base_file_id = ? AND is_concluded = ?;`
177-
178-
rows, err := s.db.Query(selectQuery, baseFileID, isConcluded)
179-
if err != nil {
180-
return nil, err
181-
}
182-
defer rows.Close()
183-
184-
var files []*types.File
185-
for rows.Next() {
186-
var file types.File
187-
err := rows.Scan(
188-
&file.FileID, &file.UploadTimestamp, &file.Path, &file.FileIndex, &file.BaseFileID, &file.TaskID,
189-
&file.RegTxid, &file.ActivationTxid, &file.ReqBurnTxnAmount, &file.BurnTxnID,
190-
&file.ReqAmount, &file.IsConcluded, &file.CascadeMetadataTicketID, &file.UUIDKey,
191-
&file.HashOfOriginalBigFile, &file.NameOfOriginalBigFileWithExt,
192-
&file.SizeOfOriginalBigFile, &file.DataTypeOfOriginalBigFile,
193-
&file.StartBlock, &file.DoneBlock, &file.PastelID, &file.Passphrase)
194-
if err != nil {
195-
return nil, err
196-
}
197-
files = append(files, &file)
198-
}
199-
200-
if err = rows.Err(); err != nil {
201-
return nil, err
202-
}
203-
204-
return files, nil
205-
}
206-
207165
func (s *TicketStore) GetUnCompletedFiles() ([]*types.File, error) {
208166
const selectQuery = `
209167
SELECT file_id, upload_timestamp, path, file_index, base_file_id, task_id,

common/storage/ticketstore/store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ func OpenTicketingDb() (TicketStorageInterface, error) {
133133
_, _ = db.Exec(alterFilesTablePassphrase)
134134
_, _ = db.Exec(addUniqueConstraint)
135135

136+
err = removePrimaryKeyMigration(*db)
137+
if err != nil {
138+
log.WithContext(context.Background()).WithError(err).Error("error removing primary-key from file-id")
139+
}
140+
136141
pragmas := []string{
137142
"PRAGMA synchronous=NORMAL;",
138143
"PRAGMA cache_size=-262144;",
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package ticketstore
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/jmoiron/sqlx"
8+
)
9+
10+
func removePrimaryKeyMigration(db sqlx.DB) error {
11+
var columns []struct {
12+
Cid int `db:"cid"`
13+
Name string `db:"name"`
14+
Type string `db:"type"`
15+
Notnull int `db:"notnull"`
16+
Dflt_value *string `db:"dflt_value"`
17+
Pk int `db:"pk"`
18+
}
19+
20+
err := db.Select(&columns, "PRAGMA table_info(files);")
21+
if err != nil {
22+
return err
23+
}
24+
25+
primaryKeyColumns := []string{}
26+
for _, column := range columns {
27+
if column.Pk == 1 || column.Pk == 2 {
28+
primaryKeyColumns = append(primaryKeyColumns, column.Name)
29+
}
30+
}
31+
32+
// Check if the primary key is only on file_id
33+
if len(primaryKeyColumns) == 1 && primaryKeyColumns[0] == "file_id" {
34+
tx := db.MustBegin()
35+
36+
// Create the new table with the desired schema
37+
tx.MustExec(`
38+
CREATE TABLE IF NOT EXISTS files_new (
39+
file_id TEXT NOT NULL,
40+
upload_timestamp DATETIME,
41+
path TEXT,
42+
file_index TEXT,
43+
base_file_id TEXT,
44+
task_id TEXT,
45+
reg_txid TEXT,
46+
activation_txid TEXT,
47+
req_burn_txn_amount FLOAT,
48+
burn_txn_id TEXT,
49+
req_amount FLOAT,
50+
is_concluded BOOLEAN,
51+
cascade_metadata_ticket_id TEXT,
52+
uuid_key TEXT,
53+
hash_of_original_big_file TEXT,
54+
name_of_original_big_file_with_ext TEXT,
55+
size_of_original_big_file FLOAT,
56+
data_type_of_original_big_file TEXT,
57+
start_block INTEGER,
58+
done_block INTEGER,
59+
created_at DATETIME NOT NULL,
60+
updated_at DATETIME NOT NULL,
61+
pastel_id TEXT,
62+
passphrase TEXT,
63+
PRIMARY KEY (file_id, base_file_id)
64+
);
65+
`)
66+
67+
// Copy data from the old table to the new table
68+
tx.MustExec(`INSERT INTO files_new SELECT * FROM files;`)
69+
70+
// Drop the old table
71+
tx.MustExec(`DROP TABLE files;`)
72+
73+
// Rename the new table to the old table name
74+
tx.MustExec(`ALTER TABLE files_new RENAME TO files;`)
75+
76+
err = tx.Commit()
77+
if err != nil {
78+
log.Fatalln(err)
79+
}
80+
81+
log.Println("primary-key update migration executed successfully")
82+
} else {
83+
fmt.Println("primary key constraint already existed on file_id & base_file_id. Not executing the migration")
84+
}
85+
86+
return nil
87+
}

walletnode/services/cascaderegister/service.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -568,25 +568,6 @@ func (service *CascadeRegistrationService) SortFilesWithHigherAmounts(files type
568568
return files
569569
}
570570

571-
func (service *CascadeRegistrationService) GetConcludedVolumesByBaseFileID(BaseFileID string) (types.Files, error) {
572-
concludedVolumes, err := service.ticketDB.GetFilesByBaseFileIDAndConcludedCheck(BaseFileID, true)
573-
if err != nil {
574-
return nil, err
575-
}
576-
577-
return concludedVolumes, nil
578-
}
579-
580-
func (service *CascadeRegistrationService) GetUnConcludedVolumesByBaseFileID(BaseFileID string) (types.Files, error) {
581-
582-
UnConcludedVolumes, err := service.ticketDB.GetFilesByBaseFileIDAndConcludedCheck(BaseFileID, false)
583-
if err != nil {
584-
return nil, err
585-
}
586-
587-
return UnConcludedVolumes, nil
588-
}
589-
590571
func (service *CascadeRegistrationService) GetBurnTxIdByAmount(ctx context.Context, amount int64) (string, error) {
591572
txID, err := service.pastelHandler.PastelClient.SendToAddress(ctx, service.pastelHandler.GetBurnAddress(), amount)
592573
if err != nil {

0 commit comments

Comments
 (0)