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
41 changes: 40 additions & 1 deletion apps/backend/__tests__/unit/repositories/nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
nodesRepository,
Node,
} from '../../../src/infrastructure/repositories/objects/nodes.js'
import { metadataRepository } from '../../../src/infrastructure/repositories/objects/metadata.js'
import { dbMigration } from '../../utils/dbMigrate.js'
import { MetadataType } from '@autonomys/auto-dag-data'
import { MetadataType, OffchainMetadata } from '@autonomys/auto-dag-data'

describe('Nodes Repository', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -469,6 +470,44 @@ describe('Nodes Repository', () => {

expect(result?.block_published_on).toBe(12345)
expect(result?.tx_published_on).toBe('tx-hash')
expect(result?.encoded_node).toBe('test-encoded-node-published')
})

it('should clear encoded_node on publish when metadata is already archived', async () => {
const rootCid = 'test-root-cid-publish-archived'
const headCid = 'test-head-cid-publish-archived'
const metadata: OffchainMetadata = {
totalSize: 100n,
type: 'file',
dataCid: 'test-data-cid-publish-archived',
totalChunks: 1,
chunks: [],
name: 'test-file-publish-archived',
}

await metadataRepository.setMetadata(rootCid, headCid, metadata)
await metadataRepository.markAsArchived(headCid)

const node: Node = {
cid: 'test-cid-publish-after-archive',
root_cid: rootCid,
head_cid: headCid,
type: 'file',
encoded_node: 'data-that-should-be-cleared',
piece_index: null,
piece_offset: null,
block_published_on: null,
tx_published_on: null,
}

await nodesRepository.saveNode(node)
await nodesRepository.updateNodePublishedOn(node.cid, 99999, 'tx-recovery')

const result = await nodesRepository.getNode(node.cid)

expect(result?.block_published_on).toBe(99999)
expect(result?.tx_published_on).toBe('tx-recovery')
expect(result?.encoded_node).toBeNull()
})

it('should get uploaded nodes by root CID', async () => {
Expand Down
12 changes: 11 additions & 1 deletion apps/backend/src/infrastructure/repositories/objects/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,17 @@ const updateNodePublishedOn = async (
const db = await getDatabase()

return db.query({
text: 'UPDATE nodes SET block_published_on = $1, tx_published_on = $2 WHERE cid = $3',
text: `UPDATE nodes
SET block_published_on = $1,
tx_published_on = $2,
encoded_node = CASE
WHEN EXISTS (
SELECT 1 FROM metadata
WHERE head_cid = nodes.head_cid AND is_archived = true
) THEN NULL
ELSE encoded_node
END
WHERE cid = $3`,
values: [blockPublishedOn, txPublishedOn, cid],
})
}
Expand Down
Loading