forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge torrust#269: feat: [torrust#261] store original info-hashes
110e159 test: [torrust#261]: do not allow uploading two torrents with the same canonical infohash (Jose Celano) 3b7a762 feat: [torrust#261] store original infohashes (Jose Celano) Pull request description: When you upload a torrent, the infohash might change if the `info` dictionary contains non-standard fields because we remove them. That leads to a different infohash. We store the original infohash in a new table so that we can know if the torrent was previously uploaded. If we do not store the original infohash we could reject uploads producing the same canonical infohash. Still, there is no way for the user to ask if a torrent exists with a given original infohash. They only would be able to interact with the API with the canonical infohash. Sometimes it's useful to use the original infohash, for instance, if you are importing torrents from an external source and you want to check if the original torrent (with the original infohash) was already uploaded. ACKs for top commit: josecelano: ACK 110e159 Tree-SHA512: 867ef055d51b43f602006df1d7ea9054918505b1603c572eb717bb6a98740ecea3c83f3dcd7c505e70b98a6fb70b5f22b83c8a7e0a4424e130ab1f17aa450dcc
- Loading branch information
Showing
23 changed files
with
608 additions
and
66 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
46 changes: 46 additions & 0 deletions
46
migrations/mysql/20230905091837_torrust_multiple_original_infohashes.sql
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,46 @@ | ||
-- Step 1: Create a new table with all infohashes | ||
CREATE TABLE torrust_torrent_info_hashes ( | ||
info_hash CHAR(40) NOT NULL, | ||
canonical_info_hash CHAR(40) NOT NULL, | ||
original_is_known BOOLEAN NOT NULL, | ||
PRIMARY KEY(info_hash), | ||
FOREIGN KEY(canonical_info_hash) REFERENCES torrust_torrents(info_hash) ON DELETE CASCADE | ||
); | ||
|
||
-- Step 2: Create one record for each torrent with only the canonical infohash. | ||
-- The original infohash is NULL so we do not know if it was the same. | ||
-- This happens if the uploaded torrent was uploaded before introducing | ||
-- the feature to store the original infohash | ||
INSERT INTO torrust_torrent_info_hashes (info_hash, canonical_info_hash, original_is_known) | ||
SELECT info_hash, info_hash, FALSE | ||
FROM torrust_torrents | ||
WHERE original_info_hash IS NULL; | ||
|
||
-- Step 3: Create one record for each torrent with the same original and | ||
-- canonical infohashes. | ||
INSERT INTO torrust_torrent_info_hashes (info_hash, canonical_info_hash, original_is_known) | ||
SELECT info_hash, info_hash, TRUE | ||
FROM torrust_torrents | ||
WHERE original_info_hash IS NOT NULL | ||
AND info_hash = original_info_hash; | ||
|
||
-- Step 4: Create two records for each torrent with a different original and | ||
-- canonical infohashes. One record with the same original and canonical | ||
-- infohashes and one record with the original infohash and the canonical | ||
-- one. | ||
-- Insert the canonical infohash | ||
INSERT INTO torrust_torrent_info_hashes (info_hash, canonical_info_hash, original_is_known) | ||
SELECT info_hash, info_hash, TRUE | ||
FROM torrust_torrents | ||
WHERE original_info_hash IS NOT NULL | ||
AND info_hash != original_info_hash; | ||
-- Insert the original infohash pointing to the canonical | ||
INSERT INTO torrust_torrent_info_hashes (info_hash, canonical_info_hash, original_is_known) | ||
SELECT original_info_hash, info_hash, TRUE | ||
FROM torrust_torrents | ||
WHERE original_info_hash IS NOT NULL | ||
AND info_hash != original_info_hash; | ||
|
||
-- Step 5: Delete the `torrust_torrents::original_info_hash` column | ||
ALTER TABLE torrust_torrents DROP COLUMN original_info_hash; | ||
|
48 changes: 48 additions & 0 deletions
48
migrations/sqlite3/20230905091837_torrust_multiple_original_infohashes.sql
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,48 @@ | ||
-- Step 1: Create a new table with all infohashes | ||
CREATE TABLE IF NOT EXISTS torrust_torrent_info_hashes ( | ||
info_hash TEXT NOT NULL, | ||
canonical_info_hash TEXT NOT NULL, | ||
original_is_known BOOLEAN NOT NULL, | ||
PRIMARY KEY(info_hash), | ||
FOREIGN KEY(canonical_info_hash) REFERENCES torrust_torrents (info_hash) ON DELETE CASCADE | ||
); | ||
|
||
-- Step 2: Create one record for each torrent with only the canonical infohash. | ||
-- The original infohash is NULL so we do not know if it was the same. | ||
-- This happens if the uploaded torrent was uploaded before introducing | ||
-- the feature to store the original infohash | ||
INSERT INTO torrust_torrent_info_hashes (info_hash, canonical_info_hash, original_is_known) | ||
SELECT info_hash, info_hash, FALSE | ||
FROM torrust_torrents | ||
WHERE original_info_hash is NULL; | ||
|
||
-- Step 3: Create one record for each torrent with the same original and | ||
-- canonical infohashes. | ||
INSERT INTO torrust_torrent_info_hashes (info_hash, canonical_info_hash, original_is_known) | ||
SELECT info_hash, info_hash, TRUE | ||
FROM torrust_torrents | ||
WHERE original_info_hash is NOT NULL | ||
AND info_hash = original_info_hash; | ||
|
||
-- Step 4: Create two records for each torrent with a different original and | ||
-- canonical infohashes. One record with the same original and canonical | ||
-- infohashes and one record with the original infohash and the canonical | ||
-- one. | ||
-- Insert the canonical infohash | ||
INSERT INTO torrust_torrent_info_hashes (info_hash, canonical_info_hash, original_is_known) | ||
SELECT info_hash, info_hash, TRUE | ||
FROM torrust_torrents | ||
WHERE original_info_hash is NOT NULL | ||
AND info_hash != original_info_hash; | ||
-- Insert the original infohash pointing to the canonical | ||
INSERT INTO torrust_torrent_info_hashes (info_hash, canonical_info_hash, original_is_known) | ||
SELECT original_info_hash, info_hash, TRUE | ||
FROM torrust_torrents | ||
WHERE original_info_hash is NOT NULL | ||
AND info_hash != original_info_hash; | ||
|
||
-- Step 5: Delete the `torrust_torrents::original_info_hash` column | ||
-- SQLite 2021-03-12 (3.35.0) supports DROP COLUMN | ||
-- https://www.sqlite.org/lang_altertable.html#alter_table_drop_column | ||
ALTER TABLE torrust_torrents DROP COLUMN original_info_hash; | ||
|
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
Oops, something went wrong.