Skip to content
Snippets Groups Projects
Commit e17ebb61 authored by Mathieu Ponton's avatar Mathieu Ponton Committed by Pierre Ecarlat
Browse files

feat(migration): added a new script that add the freeWorkShop field as a...

parent 0489a9b4
No related branches found
No related tags found
2 merge requests!404V3.1.0 (sans impression),!384feat(migration): added a new script that add the freeWorkShop field as a...
......@@ -2,25 +2,27 @@ import { Db } from 'mongodb';
import { getDb } from '../migrations-utils/db';
export const up = async () => {
const db: Db = await getDb();
await db.collection('structures').updateMany({}, { $unset: { lockdownActivity: '' } });
await db.collection('parameters').drop();
console.log(`Update done: removed 'lockdownActivity' from 'structures' and deleted the 'parameters' collection`);
try {
const db: Db = await getDb();
await db.collection('structures').updateMany({}, { $unset: { lockdownActivity: '' } });
await db.collection('parameters').drop();
console.log(`Update done: removed 'lockdownActivity' from 'structures' and deleted the 'parameters' collection`);
} catch (error) {
console.error('Error dropping lockdownActivity:', error);
}
};
export const down = async () => {
const db: Db = await getDb();
const updateResult = await db.collection('structures').updateMany({}, { $set: { lockdownActivity: '' } });
await db
.collection('parameters')
.insertOne({ lockdownInfoDisplay: false, createdAt: new Date(), updatedAt: new Date() });
console.log(
`Downgrade done: added lockdownActivity to ${updateResult.modifiedCount} structures and added the 'parameters' collection back with its contents`
);
try {
const db: Db = await getDb();
const updateResult = await db.collection('structures').updateMany({}, { $set: { lockdownActivity: '' } });
await db
.collection('parameters')
.insertOne({ lockdownInfoDisplay: false, createdAt: new Date(), updatedAt: new Date() });
console.log(
`Downgrade done: added lockdownActivity to ${updateResult.modifiedCount} structures and added the 'parameters' collection back with its contents`
);
} catch (error) {
console.error('Error restoring a lockdownActivity collection:', error);
}
};
import { Db } from 'mongodb';
import { getDb } from '../migrations-utils/db';
export const up = async () => {
try {
const db: Db = await getDb();
const structures = await db.collection('structures').find().toArray();
for (const structure of structures) {
let freeWorkShopValue: string;
if (typeof structure.freeWorkShop === 'boolean') {
freeWorkShopValue = structure.freeWorkShop ? 'Oui' : 'Non';
} else if (structure.freeWorkShop === 'underCondition') {
freeWorkShopValue = 'Oui, sous condition';
} else {
freeWorkShopValue = structure.freeWorkShop;
}
await db
.collection('structures')
.updateOne(
{ _id: structure._id },
{ $set: { 'categories.freeWorkShop': freeWorkShopValue }, $unset: { freeWorkShop: '' } }
);
}
console.log(`Update done: Fixed invalid freeWorkShops values and move it to categories.`);
} catch (error) {
console.error('Error while fixing moving freeWorkShops to categories:', error);
}
};
export const down = async () => {
try {
const db: Db = await getDb();
const structures = await db.collection('structures').find().toArray();
for (const structure of structures) {
const { categories } = structure;
await db
.collection('structures')
.updateOne(
{ _id: structure._id },
{ $set: { freeWorkShop: categories.freeWorkShop }, $unset: { 'categories.freeWorkShop': '' } }
);
}
console.log(`Downgrade done: moved freeWorkShop back from categories`);
} catch (error) {
console.error('Error moving back freeWorkShop:', error);
}
};
import { getDb } from '../migrations-utils/db';
export const up = async () => {
try {
const db = await getDb();
const newCategory = {
modules: [
{
id: 'Oui',
name: 'Accompagnements gratuits',
},
{
id: 'Oui, sous condition',
name: 'Accompagnements gratuits sous conditions',
},
{
id: 'Non',
name: 'Accompagnements payants',
},
],
name: 'Gratuité',
theme: 'Gratuité',
id: 'freeWorkShop',
};
await db.collection('categories').insertOne(newCategory);
console.log(`Update done: New category 'freeWorkShop' added successfully.`);
} catch (error) {
console.error(`Error while creating the new category 'freeWorkShop':`, error);
}
};
export const down = async () => {
try {
const db = await getDb();
await db.collection('categories').deleteOne({ id: 'freeWorkShop' });
console.log(`Downgrade done: removed the 'freeWorkShop' category.`);
} catch (error) {
console.error(`Error while removing the 'freeWorkShop' category:`, error);
}
};
import { Db } from 'mongodb';
import { getDb } from '../migrations-utils/db';
export const up = async () => {
try {
let document;
const db: Db = await getDb();
const cursor = db.collection('categories').find({ name: 'Démarches en ligne' });
while ((document = await cursor.next())) {
await db.collection('categories').updateOne({ _id: document._id }, { $set: { theme: 'Compétences numériques' } });
}
console.log(`Update done: 'Démarches en ligne' theme has been changed to 'Compétences numériques'`);
} catch (error) {
console.error('Error while changing theme:', error);
}
};
export const down = async () => {
const db: Db = await getDb();
try {
let document;
const cursor = db.collection('categories').find({ name: 'Démarches en ligne' });
while ((document = await cursor.next())) {
await db.collection('categories').updateOne({ _id: document._id }, { $set: { theme: 'Démarches en ligne' } });
}
console.log(`Downgrade done: 'Compétences numériques' theme has been changed back to 'Démarches en ligne'`);
} catch (error) {
console.error('Error while restoring theme:', error);
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment