1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00

Tools: Test migration upgrade and downgrade on Joplin Server

This commit is contained in:
Laurent Cozic 2021-12-29 11:54:27 +01:00
parent a595b8f250
commit 863d894af1
4 changed files with 153 additions and 77 deletions

View File

@ -12,5 +12,8 @@ module.exports = {
slowTestThreshold: 60,
setupFilesAfterEnv: [`${__dirname}/jest.setup.js`],
setupFilesAfterEnv: [
'jest-expect-message',
`${__dirname}/jest.setup.js`,
],
};

View File

@ -59,9 +59,10 @@
},
"devDependencies": {
"@joplin/tools": "~2.7",
"@rmp135/sql-ts": "^1.7.0",
"@rmp135/sql-ts": "^1.12.1",
"@types/fs-extra": "^8.0.0",
"@types/jest": "^26.0.15",
"@types/jest-expect-message": "^1.0.3",
"@types/jsdom": "^16.2.6",
"@types/koa": "2.0.49",
"@types/markdown-it": "^12.0.0",
@ -71,6 +72,7 @@
"@types/zxcvbn": "^4.4.1",
"gulp": "^4.0.2",
"jest": "^26.6.3",
"jest-expect-message": "^1.0.2",
"jsdom": "^16.4.0",
"node-mocks-http": "^1.10.0",
"source-map-support": "^0.5.13",

View File

@ -1,74 +1,72 @@
it('should pass', async function() {
expect(true).toBe(true);
import { afterAllTests, beforeAllDb, beforeEachDb, db } from './utils/testing/testUtils';
import sqlts from '@rmp135/sql-ts';
import { DbConnection, migrateDown, migrateUp, nextMigration } from './db';
async function dbSchemaSnapshot(db: DbConnection): Promise<any> {
return sqlts.toTypeScript({}, db as any);
}
describe('db', function() {
beforeAll(async () => {
await beforeAllDb('db', { autoMigrate: false });
});
afterAll(async () => {
await afterAllTests();
});
beforeEach(async () => {
await beforeEachDb();
});
it('should allow upgrading and downgrading schema', async function() {
// Migrations before that didn't have a down() step.
const ignoreAllBefore = '20210819165350_user_flags';
// Some migrations produce no changes visible to sql-ts, in particular
// when the migration only adds a constraint or an index. In this case
// we skip the migration. Ideally we should test these too but for now
// that will do.
const doNoCheckUpgrade = [
'20211030103016_item_owner_name_unique',
'20211111134329_storage_index',
];
let startProcessing = false;
while (true) {
await migrateUp(db());
if (!startProcessing) {
const next = await nextMigration(db());
if (next === ignoreAllBefore) {
startProcessing = true;
} else {
continue;
}
}
const next = await nextMigration(db());
if (!next) break;
const initialSchema = await dbSchemaSnapshot(db());
await migrateUp(db());
const afterUpgradeSchema = await dbSchemaSnapshot(db());
if (!doNoCheckUpgrade.includes(next)) {
expect(initialSchema, `Schema upgrade did not produce a new schema. In migration: ${next}`).not.toEqual(afterUpgradeSchema);
}
await migrateDown(db());
const afterRollbackSchema = await dbSchemaSnapshot(db());
expect(initialSchema, `Schema rollback did not produce previous schema. In migration: ${next}`).toEqual(afterRollbackSchema);
}
});
});
// import { afterAllTests, beforeAllDb, beforeEachDb, db } from "./utils/testing/testUtils";
// import sqlts from '@rmp135/sql-ts';
// import config from "./config";
// import { connectDb, DbConnection, disconnectDb, migrateDown, migrateList, migrateUp, nextMigration } from "./services/database/types";
// async function dbSchemaSnapshot(db:DbConnection):Promise<any> {
// return sqlts.toObject({
// client: 'sqlite',
// knex: db,
// // 'connection': {
// // 'filename': config().database.name,
// // },
// useNullAsDefault: true,
// } as any)
// // return JSON.stringify(definitions);
// }
// describe('db', function() {
// beforeAll(async () => {
// await beforeAllDb('db', { autoMigrate: false });
// });
// afterAll(async () => {
// await afterAllTests();
// });
// beforeEach(async () => {
// await beforeEachDb();
// });
// it('should allow downgrading schema', async function() {
// const ignoreAllBefore = '20210819165350_user_flags';
// let startProcessing = false;
// //console.info(await dbSchemaSnapshot());
// while (true) {
// await migrateUp(db());
// if (!startProcessing) {
// const next = await nextMigration(db());
// if (next === ignoreAllBefore) {
// startProcessing = true;
// } else {
// continue;
// }
// }
// if (!(await nextMigration(db()))) break;
// // await disconnectDb(db());
// // const beforeSchema = await dbSchemaSnapshot(db());
// // console.info(beforeSchema);
// // await connectDb(db());
// // await migrateUp(db());
// // await migrateDown(db());
// // const afterSchema = await dbSchemaSnapshot(db());
// // // console.info(beforeSchema);
// // // console.info(afterSchema);
// // expect(beforeSchema).toEqual(afterSchema);
// }
// });
// });

View File

@ -3356,9 +3356,10 @@ __metadata:
"@joplin/renderer": ~2.7
"@joplin/tools": ~2.7
"@koa/cors": ^3.1.0
"@rmp135/sql-ts": ^1.7.0
"@rmp135/sql-ts": ^1.12.1
"@types/fs-extra": ^8.0.0
"@types/jest": ^26.0.15
"@types/jest-expect-message": ^1.0.3
"@types/jsdom": ^16.2.6
"@types/koa": 2.0.49
"@types/markdown-it": ^12.0.0
@ -3377,6 +3378,7 @@ __metadata:
gulp: ^4.0.2
html-entities: ^1.3.1
jest: ^26.6.3
jest-expect-message: ^1.0.2
jquery: ^3.6.0
jsdom: ^16.4.0
knex: 0.95.4
@ -4788,7 +4790,7 @@ __metadata:
languageName: node
linkType: hard
"@rmp135/sql-ts@npm:^1.6.0, @rmp135/sql-ts@npm:^1.7.0":
"@rmp135/sql-ts@npm:^1.12.1, @rmp135/sql-ts@npm:^1.6.0":
version: 1.12.1
resolution: "@rmp135/sql-ts@npm:1.12.1"
dependencies:
@ -5368,6 +5370,25 @@ __metadata:
languageName: node
linkType: hard
"@types/jest-expect-message@npm:^1.0.3":
version: 1.0.3
resolution: "@types/jest-expect-message@npm:1.0.3"
dependencies:
"@types/jest": "*"
checksum: bec6da7ca4abc6dc1e21b6e418f3c910a8cc8ede56ffd21853eaccfc808cf20bd34e894b6e3688a28fbd02a3b513acab432eaefa4d73139ea77ffb2c81767e0a
languageName: node
linkType: hard
"@types/jest@npm:*":
version: 27.0.3
resolution: "@types/jest@npm:27.0.3"
dependencies:
jest-diff: ^27.0.0
pretty-format: ^27.0.0
checksum: 3683a9945821966f6dccddf337219a5d682633687c9d30df859223db553589f63e9b2c34e69f0cc845c86ffcf115742f25c12ea03c8d33d2244890fdc0af61e2
languageName: node
linkType: hard
"@types/jest@npm:^25.1.3":
version: 25.2.3
resolution: "@types/jest@npm:25.2.3"
@ -6619,6 +6640,13 @@ __metadata:
languageName: node
linkType: hard
"ansi-styles@npm:^5.0.0":
version: 5.2.0
resolution: "ansi-styles@npm:5.2.0"
checksum: d7f4e97ce0623aea6bc0d90dcd28881ee04cba06c570b97fd3391bd7a268eedfd9d5e2dd4fdcbdd82b8105df5faf6f24aaedc08eaf3da898e702db5948f63469
languageName: node
linkType: hard
"ansi-wrap@npm:0.1.0, ansi-wrap@npm:^0.1.0":
version: 0.1.0
resolution: "ansi-wrap@npm:0.1.0"
@ -11875,6 +11903,13 @@ __metadata:
languageName: node
linkType: hard
"diff-sequences@npm:^27.4.0":
version: 27.4.0
resolution: "diff-sequences@npm:27.4.0"
checksum: 66d04033e8632eeacdd029b4ecaf87d233d475e4b0cd1cee035eda99e70e1a7f803507d72f2677990ef526f28a2f6e5709af8d94dcdc0682b8884a3a646190a1
languageName: node
linkType: hard
"diff@npm:^1.3.2":
version: 1.4.0
resolution: "diff@npm:1.4.0"
@ -18084,6 +18119,18 @@ __metadata:
languageName: node
linkType: hard
"jest-diff@npm:^27.0.0":
version: 27.4.2
resolution: "jest-diff@npm:27.4.2"
dependencies:
chalk: ^4.0.0
diff-sequences: ^27.4.0
jest-get-type: ^27.4.0
pretty-format: ^27.4.2
checksum: e5bcdb4f27747795b74a56d56a9545d7fc8f1671a1251d580aea1a7a52df5db044f62ec24f2abc68305f0226d918a443f3b88d9a82f8d0dc4aaa079b621ab091
languageName: node
linkType: hard
"jest-docblock@npm:^26.0.0":
version: 26.0.0
resolution: "jest-docblock@npm:26.0.0"
@ -18135,6 +18182,13 @@ __metadata:
languageName: node
linkType: hard
"jest-expect-message@npm:^1.0.2":
version: 1.0.2
resolution: "jest-expect-message@npm:1.0.2"
checksum: 940ac8cf38415f5d8512dfa52496764aa0e1dc21672d0f0d85c0362474384a4f5277cec5dec78f120bbd4ccf5f8a3c7b47fe48401ac82a8b4e0239fb9477aa06
languageName: node
linkType: hard
"jest-get-type@npm:^25.2.6":
version: 25.2.6
resolution: "jest-get-type@npm:25.2.6"
@ -18149,6 +18203,13 @@ __metadata:
languageName: node
linkType: hard
"jest-get-type@npm:^27.4.0":
version: 27.4.0
resolution: "jest-get-type@npm:27.4.0"
checksum: bb9b70e420009fdaed3026d5bccd01569f92c7500f9f544d862796d4f4efa93ced5484864b2f272c7748bfb5bfd3268d48868b169c51ab45fe5b45b9519b6e46
languageName: node
linkType: hard
"jest-haste-map@npm:^26.5.2, jest-haste-map@npm:^26.6.2":
version: 26.6.2
resolution: "jest-haste-map@npm:26.6.2"
@ -24054,6 +24115,18 @@ __metadata:
languageName: node
linkType: hard
"pretty-format@npm:^27.0.0, pretty-format@npm:^27.4.2":
version: 27.4.2
resolution: "pretty-format@npm:27.4.2"
dependencies:
"@jest/types": ^27.4.2
ansi-regex: ^5.0.1
ansi-styles: ^5.0.0
react-is: ^17.0.1
checksum: 0daaf00c4dcb35493e57d30147e8045d0c45cb47fc4c94e3ab1892401abe939627c39975c77cc81eb2581aaa5b12bf23ef669fa550bec68b396fb79dd8c10afa
languageName: node
linkType: hard
"pretty-hrtime@npm:^1.0.0":
version: 1.0.3
resolution: "pretty-hrtime@npm:1.0.3"