Skip to content

Commit 3d9d07e

Browse files
committed
Fix bad assumptions about Cypher MERGE with media saving
How it actually works is if the complete pattern doesn't exist, then the complete pattern is created. Which means if the media didn't exist, it would create a FileVersion node. We never want to create a FileVersion node here, so pull that out to a match and only merge if we have a FileVersion given.
1 parent 7faf0e6 commit 3d9d07e

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/components/file/media/detect-existing-media.migration.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { FileVersion } from '../dto';
66
import { FileRepository } from '../file.repository';
77
import { MediaService } from './media.service';
88

9-
@Migration('2023-09-06T12:00:00')
9+
@Migration('2023-09-06T13:00:00')
1010
export class DetectExistingMediaMigration extends BaseMigration {
1111
constructor(
1212
private readonly mediaService: MediaService,
@@ -18,6 +18,7 @@ export class DetectExistingMediaMigration extends BaseMigration {
1818
async up() {
1919
await this.fixVideoLabels();
2020
await this.fixVideoBuggedDuration();
21+
await this.fixFileVersionFromBuggedMediaMerge();
2122
await this.dropAllDuplicatedMedia();
2223

2324
const detect = async (f: FileVersion) => {
@@ -99,6 +100,18 @@ export class DetectExistingMediaMigration extends BaseMigration {
99100
`.executeAndLogStats();
100101
}
101102

103+
private async fixFileVersionFromBuggedMediaMerge() {
104+
await this.db.query().raw`
105+
match (badFv:FileVersion)
106+
where not badFv:BaseNode
107+
with badFv
108+
match (realFv:FileVersion:BaseNode { id: badFv.id }),
109+
(badFv)-->(media:Media)
110+
merge (realFv)-[:media]->(media)
111+
detach delete badFv
112+
`.executeAndLogStats();
113+
}
114+
102115
private async dropAllDuplicatedMedia() {
103116
await this.db.query().raw`
104117
match (fv:FileVersion)-->(media:Media)

src/components/file/media/media.repository.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,27 @@ export class MediaRepository extends CommonRepository {
4949
const res = input.__typename
5050
? EnhancedResource.of(resolveMedia(input as AnyMedia))
5151
: undefined;
52+
const tempId = await generateId();
5253
const query = this.db
5354
.query()
54-
.merge([
55-
node('fv', 'FileVersion', input.file ? { id: input.file } : {}),
56-
relation('out', '', 'media'),
57-
node('node', 'Media', input.id ? { id: input.id } : {}),
55+
.match([
56+
input.file ? [node('fv', 'FileVersion', { id: input.file })] : [],
57+
input.id ? [node('node', 'Media', { id: input.id })] : [],
5858
])
59-
.onCreate.set({
60-
values: { 'node.id': await generateId() },
61-
variables: { 'node.createdAt': 'datetime()' },
62-
})
59+
.apply((q) =>
60+
input.file
61+
? q
62+
.merge([
63+
node('fv'),
64+
relation('out', '', 'media'),
65+
node('node', input.id ? undefined : 'Media'),
66+
])
67+
.onCreate.set({
68+
values: { 'node.id': tempId },
69+
variables: { 'node.createdAt': 'datetime()' },
70+
})
71+
: q,
72+
)
6373
.setValues({ node: toDbShape(input) }, true)
6474
.with('node, fv')
6575
// Update the labels if typename is given, and maybe changed.

0 commit comments

Comments
 (0)