Simplify database migrations

This commit is contained in:
Fedor Indutny 2025-08-06 10:32:08 -07:00 committed by GitHub
commit e6809c95db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
106 changed files with 4661 additions and 6814 deletions

View file

@ -8,61 +8,45 @@ import { isRecord } from '../../util/isRecord';
import { isIterable } from '../../util/iterables';
export default function updateToSchemaVersion55(
currentVersion: number,
db: WritableDB,
logger: LoggerType
): void {
if (currentVersion >= 55) {
return;
}
const deleteJobsInQueue = db.prepare(
'DELETE FROM jobs WHERE queueType = $queueType'
);
db.transaction(() => {
const deleteJobsInQueue = db.prepare(
'DELETE FROM jobs WHERE queueType = $queueType'
);
// First, make sure that report spam job data has e164 and serverGuids
const reportSpamJobs = getJobsInQueue(db, 'report spam');
deleteJobsInQueue.run({ queueType: 'report spam' });
// First, make sure that report spam job data has e164 and serverGuids
const reportSpamJobs = getJobsInQueue(db, 'report spam');
deleteJobsInQueue.run({ queueType: 'report spam' });
reportSpamJobs.forEach(job => {
const { data, id } = job;
reportSpamJobs.forEach(job => {
const { data, id } = job;
if (!isRecord(data)) {
logger.warn(`report spam queue job ${id} was missing valid data`);
return;
}
if (!isRecord(data)) {
logger.warn(
`updateToSchemaVersion55: report spam queue job ${id} was missing valid data`
);
return;
}
const { e164, serverGuids } = data;
if (typeof e164 !== 'string') {
logger.warn(`report spam queue job ${id} had a non-string e164`);
return;
}
const { e164, serverGuids } = data;
if (typeof e164 !== 'string') {
logger.warn(
`updateToSchemaVersion55: report spam queue job ${id} had a non-string e164`
);
return;
}
if (!isIterable(serverGuids)) {
logger.warn(`report spam queue job ${id} had a non-iterable serverGuids`);
return;
}
if (!isIterable(serverGuids)) {
logger.warn(
`updateToSchemaVersion55: report spam queue job ${id} had a non-iterable serverGuids`
);
return;
}
const newJob = {
...job,
queueType: 'report spam',
data: {
uuid: e164, // this looks odd, but they are both strings and interchangeable in the server API
serverGuids,
},
};
const newJob = {
...job,
queueType: 'report spam',
data: {
uuid: e164, // this looks odd, but they are both strings and interchangeable in the server API
serverGuids,
},
};
insertJob(db, newJob);
});
db.pragma('user_version = 55');
})();
logger.info('updateToSchemaVersion55: success!');
insertJob(db, newJob);
});
}