fix(Curated Collections): ensure resources are not duplicated on fetch-latest

This commit is contained in:
Jake Turner 2026-02-04 21:32:10 -08:00 committed by Jake Turner
parent d63c5bc668
commit 6b17e6ff68
3 changed files with 41 additions and 6 deletions

View File

@ -6,6 +6,14 @@ import type { BelongsTo } from '@adonisjs/lucid/types/relations'
export default class CuratedCollectionResource extends BaseModel {
static namingStrategy = new SnakeCaseNamingStrategy()
static indexes = [
{
name: 'curated_collection_resources_unique',
columns: ['curated_collection_slug', 'url'],
unique: true,
},
]
@column({ isPrimary: true })
declare id: number

View File

@ -286,18 +286,26 @@ export class MapService implements IMapService {
})
for (const collection of validated.collections) {
const collectionResult = await CuratedCollection.updateOrCreate(
{ slug: collection.slug },
const { resources, ...restCollection } = collection; // we'll handle resources separately
// Upsert the collection itself
await CuratedCollection.updateOrCreate(
{ slug: restCollection.slug },
{
...collection,
...restCollection,
type: 'map',
}
)
logger.info(`[MapService] Upserted curated collection: ${collection.slug}`)
logger.info(`[MapService] Upserted curated collection: ${restCollection.slug}`)
// Upsert collection's resources
const resourcesResult = await CuratedCollectionResource.updateOrCreateMany('url', resources.map((res) => ({
...res,
curated_collection_slug: restCollection.slug, // add the foreign key
})))
await collectionResult.related('resources').createMany(collection.resources)
logger.info(
`[MapService] Upserted ${collection.resources.length} resources for collection: ${collection.slug}`
`[MapService] Upserted ${resourcesResult.length} resources for collection: ${restCollection.slug}`
)
}

View File

@ -0,0 +1,19 @@
import { BaseSchema } from '@adonisjs/lucid/schema'
export default class extends BaseSchema {
protected tableName = 'curated_collection_resources'
async up() {
this.schema.alterTable(this.tableName, (table) => {
table.unique(['curated_collection_slug', 'url'], {
indexName: 'curated_collection_resources_unique',
})
})
}
async down() {
this.schema.alterTable(this.tableName, (table) => {
table.dropUnique(['curated_collection_slug', 'url'], 'curated_collection_resources_unique')
})
}
}