todos-contra-el-fuego-web/imports/startup/server/migrations.js
vjrj 25dc1b99c0 WIP(meteor3): boot progresses past linker + several async fixes
Past the client-bundle underscore linker (pin underscore@1.6.4) and now working
through the server async/await migration on Meteor 3.1 + Mongo 7:

- Removed fibers (imports/startup/server/fibers.js + import) — gone in Meteor 3.
- accounts/oauth.js: upsert -> upsertAsync (top-level await).
- migrations.js: startup async + await Migrations.migrateTo (async in 2.x).
  mongo7 marked at version 18 so historical up() bodies (still sync Mongo) don't
  run — prod data restores already-migrated. Documented.
- subsUnion.js: fully async (fetchAsync/findOneAsync/countAsync/upsertAsync,
  observeAsync with async callbacks).
- facts.js: dropped dead sync findOne.
- email.js: ostrio:mailer 2.5 is a default export (was named import); dropped
  removed static MailTime.Template (built-in default '{{{html}}}').

NEXT wall + remaining chain (multi-session):
1. aldeed:collection2@4.2.0 needs simpl-schema 3.x (app has 1.x) -> 'attachSchema
   is not a function'. Requires upgrading npm simpl-schema to 3.x and migrating
   all collection schemas (SimpleSchema.RegEx.Id etc.) across ~8 files.
2. Rest.js + helpers (countRealFires/firesUnion/whichAreFalsePositives/
   fireFromHash/subscriptionsInsert/upsertFalsePositive) -> *Async.
3. Patch vendored restivus to await async endpoint handlers.
4. methods/publications/comments to async; cron SyncedCron(quave)/Facts imports.
5. alanning:roles@1.2.10 + gadicohen:sitemaps@0.0.17 warn incompatible -> bump.
6. React 16 -> 18 render root.

Run with: NODE_OPTIONS='--dns-result-order=ipv4first --no-network-family-autoselection'
MONGO_URL='mongodb://localhost:27019/fuegos?replicaSet=rs0'
2026-07-13 23:46:45 +02:00

266 lines
8.3 KiB
JavaScript

/* global Migrations */
/* eslint-disable import/no-absolute-path */
import { Meteor } from 'meteor/meteor';
import Comments from '/imports/api/Comments/Comments';
import { Accounts } from 'meteor/accounts-base';
import randomHex from 'crypto-random-hex';
import UserSubsToFiresCollection from '/imports/api/Subscriptions/Subscriptions';
import FireAlertsCollection from '/imports/api/FireAlerts/FireAlerts';
import SiteSettings from '/imports/api/SiteSettings/SiteSettings';
import FalsePositives from '/imports/api/FalsePositives/FalsePositives';
import Industries from '/imports/api/Industries/Industries';
import IndustryRegistries from '/imports/api/Industries/IndustryRegistries';
import Notifications from '/imports/api/Notifications/Notifications';
import ActiveFiresUnion from '/imports/api/ActiveFiresUnion/ActiveFiresUnion';
import { Mongo } from 'meteor/mongo';
Meteor.startup(async () => {
// https://github.com/percolatestudio/meteor-migrations
Migrations.config({
// Log job run details to console
log: Meteor.isProduction
});
Migrations.add({
version: 1,
up: function migrateIds() {
// https://docs.mongodb.com/manual/reference/operator/query/type/
Meteor.users.find({ _id: { $type: 7 } }).forEach((user) => {
const migratedUser = user;
const id = user._id.valueOf();
console.log(`Migrating id of user: ${JSON.stringify(user)}`);
Meteor.users.remove({ _id: user._id });
migratedUser._id = id;
Meteor.users.insert(migratedUser);
});
}
});
Migrations.add({
version: 2,
up: function migrateSubsForeignKey() {
UserSubsToFiresCollection.find({ owner: null }).forEach((sub) => {
console.log(`Migrating subs of chatId: ${sub.chatId}`);
const subsUser = Meteor.users.findOne({ telegramChatId: sub.chatId });
if (subsUser) {
console.log(`Migrating linking to user: ${JSON.stringify(subsUser)}`);
UserSubsToFiresCollection.update({ _id: sub._id }, { $set: { owner: subsUser._id } });
} else {
// create user with chatId and def language
const username = `tel${sub.chatId.toString().replace(/^-/, '')}`;
console.log(`Linking to new user: ${username}`);
const newUserId = Accounts.createUser({
username,
password: randomHex(50),
profile: { name: {} }
});
Meteor.users.update({ _id: newUserId }, {
$set: {
emails: [],
roles: ['user'],
telegramChatId: sub.chatId,
lang: 'es'
}
});
UserSubsToFiresCollection.update({ _id: sub._id }, { $set: { owner: newUserId } });
}
});
}
});
Migrations.add({
version: 3,
up: function emptySubsTypes() {
UserSubsToFiresCollection.find({ type: null }).forEach((sub) => {
UserSubsToFiresCollection.update({ _id: sub._id }, { $set: { type: 'telegram' } });
});
}
});
Migrations.add({
version: 4,
up: function deleteOldAlertFiresAndIndexes() {
FireAlertsCollection.remove({ createdAd: null });
const raw = FireAlertsCollection.rawCollection();
raw.createIndex({ ourid: '2dsphere' });
raw.createIndex({ when: 1 });
raw.createIndex({ updatedAt: 1 });
raw.createIndex({ createdAt: 1 });
raw.createIndex({ ourid: 1, type: 1 });
}
});
Migrations.add({
version: 5,
up: function siteSettingsIndex() {
// other way:
SiteSettings._ensureIndex({ name: 1 }, { unique: 1 });
}
});
Migrations.add({
version: 6,
up: function falsePositiveIndexes() {
FalsePositives._ensureIndex({ chatId: 1 });
FalsePositives._ensureIndex({ owner: 1 });
FalsePositives._ensureIndex({ fireId: 1 });
FalsePositives._ensureIndex({ type: 1 });
FalsePositives._ensureIndex({ geo: '2dsphere' });
}
});
Migrations.add({
version: 7,
up: function defLangIfNull() {
Meteor.users.find({ lang: null }).forEach((user) => {
Meteor.users.update({ _id: user._id }, {
$set: {
lang: 'es'
}
});
});
}
});
Migrations.add({
version: 8,
up: function siteSettingsAddIndex() {
SiteSettings._ensureIndex({ isPublic: 1 });
SiteSettings.find({ isPublic: null }).forEach((setting) => {
SiteSettings.update({ _id: setting._id }, { $set: { isPublic: true } });
});
}
});
Migrations.add({
version: 9,
up: function siteSettingsAddIndex() {
Industries._ensureIndex({ registry: 1 });
Industries._ensureIndex({ geo: '2dsphere' });
// https://www.eea.europa.eu/data-and-maps/data/member-states-reporting-art-7-under-the-european-pollutant-release-and-transfer-register-e-prtr-regulation-16
IndustryRegistries.insert({
_id: '1', name: 'E-PRTR', agency: 'EEA', region: 'EU'
});
// https://www.epa.gov/enviro/epa-frs-facilities-state-single-file-csv-download
IndustryRegistries.insert({
_id: '2', name: 'FRS', agency: 'EPA', region: 'US'
});
}
});
Migrations.add({
version: 10,
up: function siteSettingsAddIndex() {
IndustryRegistries.insert({
_id: '3', name: 'NPRI', agency: 'GCODP', region: 'Canada'
});
IndustryRegistries.insert({
_id: '4', name: 'NPI', agency: 'DEE', region: 'Australia'
});
}
});
Migrations.add({
version: 11,
up: function noAnonComments() {
Comments.remove({ isAnonymous: true });
}
});
Migrations.add({
version: 12,
up: function setTelegramUsersBotId() {
Meteor.users.update({ telegramChatId: { $ne: null }, telegramBot: null }, {
$set: {
telegramBot: 'es'
}
}, { multi: true });
}
});
Migrations.add({
version: 13,
up: function removeTelegramBotFromUsersId() {
Meteor.users.update({}, {
$unset: {
telegramBot: ''
}
}, { multi: true });
}
});
Migrations.add({
version: 14,
up: function setTelegramUsersBotId() {
UserSubsToFiresCollection.update({ chatId: { $ne: null }, telegramBot: null }, {
$set: {
telegramBot: 'es'
}
}, { multi: true });
}
});
Migrations.add({
version: 15,
up: function moveToFalsePositivesUppercase() {
/* const falsepositiveslower = new Mongo.Collection('falsepositives', { idGeneration: 'MONGO' });
* falsepositiveslower.find({}).forEach((falseDoc) => {
* FalsePositives.insert(falseDoc);
* });*/
// TODO remove falsepositives lowercase collection
}
});
Migrations.add({
version: 16,
up: function moveToFalsePositivesUppercaseWithUser() {
const falsepositiveslower = new Mongo.Collection('falsepositives', { idGeneration: 'MONGO' });
const now = new Date();
falsepositiveslower.find({}).forEach((falseDoc) => {
const user = Meteor.users.findOne({ telegramChatId: falseDoc.chatId });
if (user) {
falseDoc.owner= user._id;
}
falseDoc.type = 'industry';
falseDoc.createdAt = now;
falseDoc.updatedAt = now;
FalsePositives.insert(falseDoc);
});
}
});
Migrations.add({
version: 17,
up: function renameWebNotifiedField() {
Notifications.update({ webNotified: { $exists: true } }, {
$rename: { webNotifiedAt: 'notifiedAt' }
}, { upsert: false, multi: true });
Notifications.update({ webNotified: { $exists: true } }, {
$rename: { webNotified: 'notified' }
}, { upsert: false, multi: true });
}
});
Migrations.add({
version: 18,
up: () => {
const raw = ActiveFiresUnion.rawCollection();
raw.createIndex({ centerid: '2dsphere' });
raw.createIndex({ shape: '2dsphere' });
raw.createIndex({ when: 1 });
raw.createIndex({ createdAt: 1 });
raw.createIndex({ updatedAt: 1 });
}
});
// Set createdAt in users & subs
// Meteor 3: migrateTo is async now.
// NOTE: the individual up() bodies below still use SYNC Mongo — they only run
// on a DB whose migration version is behind. Production data is restored from
// rsmain already at the latest version, so they never execute. If migrations
// must ever run on a truly-fresh DB, their up() bodies need async conversion.
await Migrations.migrateTo('latest');
// Migrations.migrateTo('14,rerun');
});