Skip to content

Commit

Permalink
chore: migration des valeurs 'legacy' dans les métadonnées (#1143)
Browse files Browse the repository at this point in the history
* chore: migration des valeurs 'legacy' dans les métadonnées
* fix: mise à jour des migrations afin d'être compatible avec les données de production
  • Loading branch information
ggrossetie authored Dec 13, 2024
1 parent f1bae73 commit c0b599a
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ import React, { useCallback, useMemo } from 'react'
import { merge } from 'allof-merge'
import PropTypes from 'prop-types'
import Form from '../../Form'
import { convertLegacyValues } from '../../metadata/MetadataValues.js'
import uiSchema from '../../../schemas/article-ui-schema.json'
import schema from '../../../schemas/article-metadata.schema.json'

export default function ArticleEditorMetadataForm({
metadata,
onChange = () => {},
}) {
const formData = convertLegacyValues(metadata)
const schemaMerged = useMemo(() => merge(schema), [schema])
const handleChange = useCallback(
(newFormData) => onChange(newFormData),
[onChange]
)
return (
<Form
formData={formData}
formData={metadata}
schema={schemaMerged}
uiSchema={uiSchema}
onChange={handleChange}
Expand Down
4 changes: 1 addition & 3 deletions front/src/components/metadata/MetadataForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react'
import PropTypes from 'prop-types'

import Form from '../Form.jsx'
import { convertLegacyValues } from './MetadataValues.js'

/**
* @param data Values in JSON format
Expand All @@ -13,10 +12,9 @@ import { convertLegacyValues } from './MetadataValues.js'
* @constructor
*/
export default function MetadataForm({ data, schema, uiSchema, onChange }) {
const formData = convertLegacyValues(data)
return (
<Form
formData={formData}
formData={data}
schema={schema}
uiSchema={uiSchema}
onChange={onChange}
Expand Down
32 changes: 0 additions & 32 deletions front/src/components/metadata/MetadataValues.js

This file was deleted.

7 changes: 5 additions & 2 deletions graphql/helpers/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,10 @@ function fromLegacyFormat (metadata) {
translations,
...extra
} = metadata
const translatedAbstracts = abstract?.filter(a => a?.lang !== lang)
const abstractNormalized = typeof abstract === 'string'
? [{ lang: 'fr', text_f: abstract }]
: abstract
const translatedAbstracts = abstractNormalized?.filter(a => a?.lang !== lang)
const translatedTitles = translatedTitle?.filter(a => a?.lang !== lang)
const translatedKeywords = keywords?.filter(a => a?.lang !== lang)
const languages = Array.from(new Set([...translatedAbstracts?.map(a => a?.lang) ?? [], ...translatedTitles?.map(t => t?.lang) ?? [], ...translatedKeywords?.map(k => k?.lang) ?? []]))
Expand Down Expand Up @@ -486,7 +489,7 @@ function fromLegacyFormat (metadata) {
title: title_f,
subtitle: subtitle_f,
acknowledgements,
abstract: abstract?.find(a => a?.lang === lang)?.text_f,
abstract: abstractNormalized?.find(a => a?.lang === lang)?.text_f,
keywords: keywords?.find(k => k?.lang === lang)?.list_f,
controlledKeywords: controlledKeywords,
publicationDate: date,
Expand Down
50 changes: 27 additions & 23 deletions graphql/migrations/20211118080000-working-version.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
exports.up = async function (db) {
const articles = await db._find('articles', {})

for await (const article of articles) {
const latestVersionId = article.versions.slice(-1)[0]

if (typeof article.workingVersion === 'undefined') {
if (latestVersionId) {
const [latestVersion] = await db._find('versions', { _id: latestVersionId })
await db._run('update', 'articles', {
query: { _id: article._id },
update: {
$set: {
workingVersion: {
bib: latestVersion.bib,
md: latestVersion.md,
yaml: latestVersion.yaml,
},
const mongo = await db._run('getDbInstance', true)
const articles = mongo.collection('articles')
const versions = mongo.collection('versions')
const articlesCursor = articles.find({})
try {
while (await articlesCursor.hasNext()) {
const article = await articlesCursor.next()
if (typeof article.workingVersion === 'undefined') {
const latestVersionId = article.versions.slice(-1)[0]
if (latestVersionId) {
const [latestVersion] = await versions.findOne({ _id: latestVersionId })
await articles.updateOne({ _id: article._id }, {
$set: {
workingVersion: {
bib: latestVersion.bib,
md: latestVersion.md,
yaml: latestVersion.yaml,
}
}
},
},
options: { upsert: false }
})

// if the latest version is "autosave", remove!
await db.collections('versions').deleteOne({ _id: latestVersionId, autosave: true })
{ upsert: false }
)
// if the latest version is "autosave", remove!
await versions.deleteOne({ _id: latestVersionId, autosave: true })
}
}
}
} finally {
await articlesCursor.close()
await mongo.close()
}
}

Expand Down
33 changes: 19 additions & 14 deletions graphql/migrations/20220112080000-article-contributors.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
exports.up = async function (db) {
const articles = await db._find('articles', {})

for await (const article of articles) {
const [ ownerId, ...contributors ] = article.owners ?? [article.owner]

await db._run('update', 'articles', {
query: { _id: article._id },
update: {
$set: {
owner: ownerId,
contributors: contributors.map(userId => ({ user: userId, roles: ['read', 'write']}))
}
}
})
const mongo = await db._run('getDbInstance', true)
const articles = mongo.collection('articles')
const articlesCursor = articles.find({})
try {
while (await articlesCursor.hasNext()) {
const article = await articlesCursor.next()
const [ownerId, ...contributors] = article.owners ?? [article.owner]
await articles.updateOne(
{ _id: article._id },
{
$set: {
owner: ownerId,
contributors: contributors.map(userId => ({ user: userId, roles: ['read', 'write'] }))
}
})
}
} finally {
await articlesCursor.close()
await mongo.close()
}
}

Expand Down
60 changes: 55 additions & 5 deletions graphql/migrations/20230113080000-sparse-username-index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
exports.up = async function (db) {
const mongo = await db._run("getDbInstance", true)
await mongo
.collection('users')
.createIndex({ username: 1 }, { sparse: true, unique: true })

return mongo.close()
try {
const users = mongo.collection("users")
const duplicateUsernameList = await users.aggregate([
{
$match: {
"username": {
"$exists": true
}
}
},
{
$group: {
_id: "$username",
count: {
$sum: 1
},
duplicates: {
$addToSet: "$_id"
}
}
},
{
$match: {
count: {
$gt: 1
}
}
},
{
$project: {
_id: 0,
name: "$_id",
email: "$email",
duplicates: 1
}
}
]).toArray()
// update user with non-unique username
for (const item of duplicateUsernameList) {
for (const duplicate of item.duplicates) {
console.log(`Username ${item.name} is non unique, update username to email on user with id: ${duplicate}`)
await users.updateOne(
{ _id: duplicate },
[{
$set: {
username: "$email"
}
}]
)
}
}
await users.createIndex({ username: 1 }, { sparse: true, unique: true })
} finally {
await mongo.close()
}
}

exports.down = async function (db) {
Expand Down
34 changes: 19 additions & 15 deletions graphql/migrations/20230504080000-sorting-indexes.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
exports.up = async function (db) {
const mongo = await db._run("getDbInstance", true)
await mongo
.collection('articles')
.createIndex({ updatedAt: -1 }, { unique: false })
await mongo
.collection('tags')
.createIndex({ name: -1 }, { unique: false })
await mongo
.collection('tags')
.createIndex({ createdAt: -1 }, { unique: false })
await mongo
.collection('articles')
.createIndex({ createdAt: -1 }, { unique: false })
await mongo
.collection('versions')
.createIndex({ createdAt: -1 }, { unique: false })
try {
await mongo
.collection('articles')
.createIndex({ updatedAt: -1 }, { unique: false })
await mongo
.collection('tags')
.createIndex({ name: -1 }, { unique: false })
await mongo
.collection('tags')
.createIndex({ createdAt: -1 }, { unique: false })
await mongo
.collection('articles')
.createIndex({ createdAt: -1 }, { unique: false })
await mongo
.collection('versions')
.createIndex({ createdAt: -1 }, { unique: false })
} finally {
await mongo.close()
}
}

exports.down = async function (db) {
Expand Down
3 changes: 0 additions & 3 deletions graphql/migrations/20230505080000-tag-color-hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,15 @@ const colours = {

exports.up = async function (db) {
const tags = await db._find('tags', {})

for await (const tag of tags) {
let color

if (tag.color && tag.color.charAt(0) !== '#') {
color = colours[tag.color] || '#eeeeee'
} else if (tag.color && tag.color.length < 7) {
color = '#cccccc'
} else if (!tag.color) {
color = '#eeeeee'
}

if (color) {
await db._run('update', 'tags', {
query: { _id: tag._id },
Expand Down
33 changes: 19 additions & 14 deletions graphql/migrations/20230626080000-version-owner.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
exports.up = async function (db) {
const versions = await db._find('versions', {"owner": null })

for await (const version of versions) {
const articleId = version.article
const articles = await db._find('articles', { _id: articleId })

for await (const article of articles) {
if (article.owner !== null) {
await db._run('update', 'versions', {
query: { _id: version._id },
update: {
const mongo = await db._run('getDbInstance', true)
const articles = mongo.collection('articles')
const versions = mongo.collection('versions')
const versionsCursor = versions.find({ "owner": null })
try {
while (await versionsCursor.hasNext()) {
const version = await versionsCursor.next()
const articleId = version.article
const article = await articles.findOne({ _id: articleId })
if (article && article.owner !== null) {
versions.updateOne(
{ _id: version._id },
{
$set: {
owner: article.owner,
},
}
},
options: { upsert: false }
})
{ upsert: false }
)
}
}
} finally {
await versionsCursor.close()
await mongo.close()
}
}

Expand Down
Loading

0 comments on commit c0b599a

Please sign in to comment.