Skip to content
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
1 change: 1 addition & 0 deletions apps/backend/drizzle/0001_tearful_the_fallen.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `downloads` ADD `attempts` integer DEFAULT 0 NOT NULL;
195 changes: 195 additions & 0 deletions apps/backend/drizzle/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
{
"version": "6",
"dialect": "sqlite",
"id": "0e170273-951e-4b46-b8be-8d77d6188596",
"prevId": "7511a5bb-12b1-4836-8099-498542b3d20d",
"tables": {
"downloads": {
"name": "downloads",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"torrent_filename": {
"name": "torrent_filename",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"peer_id": {
"name": "peer_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"peer_name": {
"name": "peer_name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"item_id": {
"name": "item_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"filename": {
"name": "filename",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"dest_path": {
"name": "dest_path",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"part_path": {
"name": "part_path",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"release_size": {
"name": "release_size",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"release_json": {
"name": "release_json",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"expected_bytes": {
"name": "expected_bytes",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"expected_bytes_source": {
"name": "expected_bytes_source",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"expected_bytes_mismatch": {
"name": "expected_bytes_mismatch",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": false
},
"downloaded_bytes": {
"name": "downloaded_bytes",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"attempts": {
"name": "attempts",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"status": {
"name": "status",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"started_at": {
"name": "started_at",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"updated_at": {
"name": "updated_at",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"completed_at": {
"name": "completed_at",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"error": {
"name": "error",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {
"downloads_status_idx": {
"name": "downloads_status_idx",
"columns": [
"status"
],
"isUnique": false
},
"downloads_updated_at_idx": {
"name": "downloads_updated_at_idx",
"columns": [
"updated_at"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {
"downloads_status_check": {
"name": "downloads_status_check",
"value": "\"downloads\".\"status\" in ('downloading', 'completed', 'failed', 'import_queued')"
},
"downloads_expected_bytes_source_check": {
"name": "downloads_expected_bytes_source_check",
"value": "\"downloads\".\"expected_bytes_source\" is null or \"downloads\".\"expected_bytes_source\" = 'content_length'"
}
}
}
},
"views": {},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}
7 changes: 7 additions & 0 deletions apps/backend/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1780589033291,
"tag": "0000_mysterious_vin_gonzales",
"breakpoints": true
},
{
"idx": 1,
"version": "6",
"when": 1780707145431,
"tag": "0001_tearful_the_fallen",
"breakpoints": true
}
]
}
61 changes: 61 additions & 0 deletions apps/backend/src/__tests__/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,65 @@ describe('DownloadsRepository', () => {
expect(stale.error).toContain('stale')
handle.close()
})

test('increments attempts and records a resume reset', async () => {
const handle = await openDatabase({ appConfigPath: join(tempDir, 'config.jsonc') })
const repository = new DownloadsRepository(handle.db)
const created = repository.create({
torrentFilename: 'movie.torrent',
peerId: 'peer-1',
peerName: 'Friend Jack',
itemId: 'movie:1',
filename: release.filename,
destPath: join(tempDir, release.filename),
partPath: join(tempDir, `${release.filename}.part`),
releaseSize: release.size,
release,
})

expect(repository.incrementAttempts(created.id)).toBe(1)
expect(repository.incrementAttempts(created.id)).toBe(2)
repository.updateProgress(created.id, 40)
repository.markResumeReset(created.id)

const row = repository.get(created.id)!
expect(row.attempts).toBe(2)
expect(row.downloadedBytes).toBe(0)
expect(row.status).toBe('downloading')
expect(row.error).toContain('resume validation failed')
handle.close()
})

test('lists stale downloading rows without mutating them', async () => {
const handle = await openDatabase({ appConfigPath: join(tempDir, 'config.jsonc') })
const repository = new DownloadsRepository(handle.db)
const a = repository.create({
torrentFilename: 'a.torrent',
peerId: 'peer-1',
peerName: 'Friend Jack',
itemId: 'movie:1',
filename: 'A.mkv',
destPath: join(tempDir, 'A.mkv'),
partPath: join(tempDir, 'A.mkv.part'),
releaseSize: 10,
release,
})
const b = repository.create({
torrentFilename: 'b.torrent',
peerId: 'peer-1',
peerName: 'Friend Jack',
itemId: 'movie:2',
filename: 'B.mkv',
destPath: join(tempDir, 'B.mkv'),
partPath: join(tempDir, 'B.mkv.part'),
releaseSize: 10,
release,
})
repository.markCompleted(b.id, 10)

const stale = repository.listStaleDownloads()
expect(stale.map(r => r.id)).toEqual([a.id])
expect(repository.get(a.id)?.status).toBe('downloading')
handle.close()
})
})
Loading
Loading