@jskit-ai/auth-provider-local-db-core 0.1.193 → 0.1.194
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -0
- package/migrations/auth_local_db_initial-collation.cjs +18 -0
- package/migrations/auth_local_db_oauth-collation.cjs +22 -0
- package/migrations/auth_local_db_protection-collation.cjs +18 -0
- package/migrations/auth_local_db_used_passes-collation.cjs +18 -0
- package/migrations/auth_local_db_users_second_factors-collation.cjs +23 -0
- package/package.json +3 -3
- package/test/migrationCollation.mysql.test.js +223 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- Each table migration has a companion that Knex runs just before it, `auth_local_db_initial-collation`, `auth_local_db_oauth-collation`, `auth_local_db_protection-collation`, `auth_local_db_used_passes-collation` and `auth_local_db_users_second_factors-collation` (run `npm run db:migrate`): on MySQL and MariaDB each creates its migration's tables in the collation of `auth_local_users` (or `users`), not the database's default. On a database whose default differs from its tables', `auth_local_db_users_second_factors` failed (errno 150) and left an empty, unrecorded `auth_local_second_factors`, after recording `auth_local_used_passes` in the database's default; the next migrate creates the first again with its key to `auth_local_users`, converts the second keeping its rows, and adds the key a second try recorded without. A database whose tables match its default changes nothing. No released migration changed.
|
|
5
6
|
- A user's waiting email change, in the new nullable column `auth_local_users.pending_email` (migration `auth_local_db_users_pending_email`; run `npm run db:migrate`), behind the backend contract's `pendingEmail`, `users.updatePendingEmail` and `users.updateEmail`. Changing the email clears the waiting change and sets `email_verified_at`, which the link sent to the address proves; an email another account has is refused with `auth.local.accounts.exists`.
|
|
6
7
|
- Sessions keep where and when they were last seen, in the new nullable columns `auth_local_sessions.ip`, `user_agent` and `last_seen_at` (migration `auth_local_db_sessions_metadata`; run `npm run db:migrate`), behind the backend contract's session fields and `sessions.touch`. `auth.local.sessions.listForUser(userId, options)` answers an account's sessions that are neither revoked nor expired, the newest first, every one of them in one read, each with `ip`, `userAgent` and `lastSeenAt`, and refuses a missing id with a `TypeError`: for an application's list of a person's sessions, its own or an operator's.
|
|
7
8
|
- `auth.local.accounts` can disable an account and enable it again: `disable(userId, { message }, options)` refuses every sign-in and session of the account from its next request, and a sign-in that proves who it is (the right password, a linked provider or a one-time pass) is refused with 403 `auth.local.login.disabled` and `message`, at most 500 characters, as its text, or "This account is disabled." without one; `enable(userId, options)` lets it sign in again and clears the message. Both answer the account, now with `disabledMessage` ("" when it has none), or null when there is no such account, and refuse a missing id, or a message that is not text of at most 500 characters, with a `TypeError`. Neither revokes sessions (`auth.local.sessions.revokeForUser` does). Until now nothing set `disabled`. Upgrading: run `npm run db:migrate` for the new migration `auth_local_db_users_disabled_message`, which adds the nullable `auth_local_users.disabled_message` column.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Knex sorts this file just before auth_local_db_initial.cjs ("-" sorts before "."), so it runs first on every
|
|
2
|
+
// database that has not run it. It runs that released migration with each table it creates in the collation of
|
|
3
|
+
// auth_local_users, or of the next table listed that exists, instead of the database's default, and repairs a table
|
|
4
|
+
// that default left: see migrateInExistingCollation. The released migration then finds its tables in place.
|
|
5
|
+
const released = require("./auth_local_db_initial.cjs");
|
|
6
|
+
|
|
7
|
+
exports.up = async function up(knex) {
|
|
8
|
+
const { migrateInExistingCollation } = await import("@jskit-ai/database-runtime/server/tableCollationMigrations");
|
|
9
|
+
await migrateInExistingCollation(knex, {
|
|
10
|
+
migration: "auth_local_db_initial.cjs",
|
|
11
|
+
up: released.up,
|
|
12
|
+
tables: ["auth_local_users", "auth_local_sessions", "auth_local_recovery"],
|
|
13
|
+
referenceTables: ["auth_local_users", "users"]
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// The released migration's down drops its tables.
|
|
18
|
+
exports.down = async function down() {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Knex sorts this file just before auth_local_db_oauth.cjs ("-" sorts before "."), so it runs first on every
|
|
2
|
+
// database that has not run it. It runs that released migration with each table it creates in the collation of
|
|
3
|
+
// auth_local_users, or of the next table listed that exists, instead of the database's default, and repairs a table
|
|
4
|
+
// that default left: see migrateInExistingCollation. The released migration then finds its tables in place.
|
|
5
|
+
const released = require("./auth_local_db_oauth.cjs");
|
|
6
|
+
|
|
7
|
+
exports.up = async function up(knex) {
|
|
8
|
+
const { migrateInExistingCollation } = await import("@jskit-ai/database-runtime/server/tableCollationMigrations");
|
|
9
|
+
await migrateInExistingCollation(knex, {
|
|
10
|
+
migration: "auth_local_db_oauth.cjs",
|
|
11
|
+
up: released.up,
|
|
12
|
+
tables: ["auth_local_identities", "auth_local_oauth_attempts"],
|
|
13
|
+
referenceTables: ["auth_local_users", "users"],
|
|
14
|
+
foreignKeys: [
|
|
15
|
+
{ tableName: "auth_local_identities", columnName: "user_id", parentTable: "auth_local_users",
|
|
16
|
+
name: "fk_auth_local_identities_user", onUpdate: "RESTRICT", onDelete: "CASCADE" }
|
|
17
|
+
]
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// The released migration's down drops its tables.
|
|
22
|
+
exports.down = async function down() {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Knex sorts this file just before auth_local_db_protection.cjs ("-" sorts before "."), so it runs first on every
|
|
2
|
+
// database that has not run it. It runs that released migration with each table it creates in the collation of
|
|
3
|
+
// auth_local_users, or of the next table listed that exists, instead of the database's default, and repairs a table
|
|
4
|
+
// that default left: see migrateInExistingCollation. The released migration then finds its tables in place.
|
|
5
|
+
const released = require("./auth_local_db_protection.cjs");
|
|
6
|
+
|
|
7
|
+
exports.up = async function up(knex) {
|
|
8
|
+
const { migrateInExistingCollation } = await import("@jskit-ai/database-runtime/server/tableCollationMigrations");
|
|
9
|
+
await migrateInExistingCollation(knex, {
|
|
10
|
+
migration: "auth_local_db_protection.cjs",
|
|
11
|
+
up: released.up,
|
|
12
|
+
tables: ["auth_local_sign_in_failures", "auth_local_registrations"],
|
|
13
|
+
referenceTables: ["auth_local_users", "users"]
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// The released migration's down drops its tables.
|
|
18
|
+
exports.down = async function down() {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Knex sorts this file just before auth_local_db_used_passes.cjs ("-" sorts before "."), so it runs first on every
|
|
2
|
+
// database that has not run it. It runs that released migration with each table it creates in the collation of
|
|
3
|
+
// auth_local_users, or of the next table listed that exists, instead of the database's default, and repairs a table
|
|
4
|
+
// that default left: see migrateInExistingCollation. The released migration then finds its tables in place.
|
|
5
|
+
const released = require("./auth_local_db_used_passes.cjs");
|
|
6
|
+
|
|
7
|
+
exports.up = async function up(knex) {
|
|
8
|
+
const { migrateInExistingCollation } = await import("@jskit-ai/database-runtime/server/tableCollationMigrations");
|
|
9
|
+
await migrateInExistingCollation(knex, {
|
|
10
|
+
migration: "auth_local_db_used_passes.cjs",
|
|
11
|
+
up: released.up,
|
|
12
|
+
tables: ["auth_local_used_passes"],
|
|
13
|
+
referenceTables: ["auth_local_users", "users"]
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// The released migration's down drops its tables.
|
|
18
|
+
exports.down = async function down() {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Knex sorts this file just before auth_local_db_users_second_factors.cjs ("-" sorts before "."), so it runs first
|
|
2
|
+
// on every database that has not run it. It runs that released migration with each table it creates in the
|
|
3
|
+
// collation of auth_local_users, or of the next table listed that exists, instead of the database's default, and
|
|
4
|
+
// repairs a table that default left: see migrateInExistingCollation. The released migration then finds its tables
|
|
5
|
+
// in place.
|
|
6
|
+
const released = require("./auth_local_db_users_second_factors.cjs");
|
|
7
|
+
|
|
8
|
+
exports.up = async function up(knex) {
|
|
9
|
+
const { migrateInExistingCollation } = await import("@jskit-ai/database-runtime/server/tableCollationMigrations");
|
|
10
|
+
await migrateInExistingCollation(knex, {
|
|
11
|
+
migration: "auth_local_db_users_second_factors.cjs",
|
|
12
|
+
up: released.up,
|
|
13
|
+
tables: ["auth_local_second_factors"],
|
|
14
|
+
referenceTables: ["auth_local_users", "users"],
|
|
15
|
+
foreignKeys: [
|
|
16
|
+
{ tableName: "auth_local_second_factors", columnName: "user_id", parentTable: "auth_local_users",
|
|
17
|
+
name: "fk_auth_local_second_factors_user", onUpdate: "RESTRICT", onDelete: "CASCADE" }
|
|
18
|
+
]
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// The released migration's down drops its tables.
|
|
23
|
+
exports.down = async function down() {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/auth-provider-local-db-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.194",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
18
|
"@jskit-ai/auth-core": "0.2.32",
|
|
19
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
19
|
+
"@jskit-ai/database-runtime": "0.1.278",
|
|
20
20
|
"@jskit-ai/kernel": "0.2.28",
|
|
21
|
-
"@jskit-ai/resource-runtime": "0.2.
|
|
21
|
+
"@jskit-ai/resource-runtime": "0.2.42"
|
|
22
22
|
},
|
|
23
23
|
"description": "Database-backed local auth storage backend for JSKIT local auth.",
|
|
24
24
|
"jskit": {
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { readdirSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import test from "node:test";
|
|
6
|
+
import knexLib from "knex";
|
|
7
|
+
import { requireTestDatabase } from "@jskit-ai/database-runtime/server/testDatabases";
|
|
8
|
+
|
|
9
|
+
// Local auth's migrations on a MariaDB/MySQL database whose default collation is not its tables', as when a dump is
|
|
10
|
+
// restored onto a server with another default. Knex runs them as an application does: every file of the migrations
|
|
11
|
+
// directory, sorted, that it has not recorded. A released migration created its table in the database's default, so
|
|
12
|
+
// MariaDB refused auth_local_second_factors' key to auth_local_users (errno 150) and left the database half-migrated.
|
|
13
|
+
|
|
14
|
+
const MIGRATIONS = path.resolve(import.meta.dirname, "../migrations");
|
|
15
|
+
const require = createRequire(import.meta.url);
|
|
16
|
+
// The files an application ran before this fix: every migration but the "-collation" companions.
|
|
17
|
+
const RELEASED = readdirSync(MIGRATIONS).filter((file) => file.endsWith(".cjs") && !file.endsWith("-collation.cjs")).sort();
|
|
18
|
+
// What an application on a catalog before 0.2.36 had run; moving to 0.2.92 then ran five more migrations and failed on
|
|
19
|
+
// the sixth, auth_local_db_users_second_factors.cjs.
|
|
20
|
+
const EARLY = Object.freeze(["auth_local_db_initial.cjs", "auth_local_db_oauth.cjs", "auth_local_db_protection.cjs"]);
|
|
21
|
+
const TABLES_COLLATION = "utf8mb4_general_ci";
|
|
22
|
+
const OTHER_DEFAULT = "utf8mb4_unicode_ci";
|
|
23
|
+
const SECOND_FACTOR_KEY = "fk_auth_local_second_factors_user";
|
|
24
|
+
// The runtime's own MySQL connection settings (database-runtime's resolveKnexConnectionFromEnvironment).
|
|
25
|
+
const RUNTIME_MYSQL = Object.freeze({ supportBigNumbers: true, bigNumberStrings: true, dateStrings: ["DATE"], timezone: "Z" });
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A database of the test's own, named after TEST_DB_NAME with the suffix and dropped after the test, created with the
|
|
29
|
+
* collation as its default, and a Knex on it.
|
|
30
|
+
* @param {import("node:test").TestContext} context
|
|
31
|
+
* @param {string} suffix
|
|
32
|
+
* @param {string} collation
|
|
33
|
+
*/
|
|
34
|
+
async function openDatabase(context, suffix, collation) {
|
|
35
|
+
const name = `${requireTestDatabase(process.env)}_${suffix}`;
|
|
36
|
+
if (name.length > 64) throw new Error(`Test database name ${name} is longer than MySQL's 64 characters.`);
|
|
37
|
+
const server = { host: process.env.DB_HOST, port: Number(process.env.DB_PORT) || 3306, user: process.env.DB_USER, password: process.env.DB_PASSWORD };
|
|
38
|
+
const admin = knexLib({ client: "mysql2", connection: server, pool: { min: 0, max: 1 } });
|
|
39
|
+
const connect = () => knexLib({ client: "mysql2", connection: { ...server, database: name, ...RUNTIME_MYSQL }, pool: { min: 0, max: 2 } });
|
|
40
|
+
await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
|
|
41
|
+
await admin.raw(`CREATE DATABASE ?? CHARACTER SET utf8mb4 COLLATE ${collation}`, [name]);
|
|
42
|
+
let knex = connect();
|
|
43
|
+
context.after(async () => {
|
|
44
|
+
try {
|
|
45
|
+
await knex.destroy();
|
|
46
|
+
await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
|
|
47
|
+
} finally {
|
|
48
|
+
await admin.destroy();
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return Object.freeze({
|
|
52
|
+
knex,
|
|
53
|
+
// The database's default changes under its tables, as restoring a dump onto another server's default does. A
|
|
54
|
+
// connection keeps the default it found when it chose the database, so the Knex it answers connects anew.
|
|
55
|
+
async restoredWithDefault(other) {
|
|
56
|
+
await admin.raw(`ALTER DATABASE ?? CHARACTER SET utf8mb4 COLLATE ${other}`, [name]);
|
|
57
|
+
await knex.destroy();
|
|
58
|
+
knex = connect();
|
|
59
|
+
return knex;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Runs these released files, as an application on the release that shipped them did. */
|
|
65
|
+
function migrateReleased(knex, files) {
|
|
66
|
+
return knex.migrate.latest({
|
|
67
|
+
migrationSource: {
|
|
68
|
+
getMigrations: async () => files,
|
|
69
|
+
getMigrationName: (file) => file,
|
|
70
|
+
getMigration: async (file) => require(path.join(MIGRATIONS, file))
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Runs the migrations directory as it is now, with the settings createKnexMigrationConfig gives a package's. */
|
|
76
|
+
function migrate(knex) {
|
|
77
|
+
return knex.migrate.latest({ directory: [MIGRATIONS], extension: "cjs", sortDirsSeparately: true });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Each local auth table's collation. */
|
|
81
|
+
async function collations(knex) {
|
|
82
|
+
const [rows] = await knex.raw(
|
|
83
|
+
"SELECT TABLE_NAME AS name, TABLE_COLLATION AS collation FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME LIKE 'auth\\_local\\_%' ORDER BY TABLE_NAME"
|
|
84
|
+
);
|
|
85
|
+
return Object.fromEntries(rows.map(({ name, collation }) => [name, collation]));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The migrations Knex has recorded, sorted. */
|
|
89
|
+
async function recorded(knex) {
|
|
90
|
+
return (await knex("knex_migrations").pluck("name")).sort();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Whether auth_local_second_factors has its key to auth_local_users. */
|
|
94
|
+
async function hasSecondFactorKey(knex) {
|
|
95
|
+
const [rows] = await knex.raw(
|
|
96
|
+
"SELECT 1 FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME = ? AND TABLE_NAME = 'auth_local_second_factors' AND REFERENCED_TABLE_NAME = 'auth_local_users'",
|
|
97
|
+
[SECOND_FACTOR_KEY]
|
|
98
|
+
);
|
|
99
|
+
return rows.length === 1;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The whole schema of the database's tables but Knex's own: tables, columns, indexes and keys. */
|
|
103
|
+
async function schemaOf(knex) {
|
|
104
|
+
const outsideKnex = "TABLE_SCHEMA = DATABASE() AND TABLE_NAME NOT LIKE 'knex\\_migrations%'";
|
|
105
|
+
const [tables] = await knex.raw(`SELECT TABLE_NAME, ENGINE, TABLE_COLLATION FROM information_schema.TABLES WHERE ${outsideKnex} ORDER BY 1`);
|
|
106
|
+
const [columns] = await knex.raw(
|
|
107
|
+
`SELECT TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, CHARACTER_SET_NAME, COLLATION_NAME, EXTRA
|
|
108
|
+
FROM information_schema.COLUMNS WHERE ${outsideKnex} ORDER BY 1, 3`
|
|
109
|
+
);
|
|
110
|
+
const [indexes] = await knex.raw(
|
|
111
|
+
`SELECT TABLE_NAME, INDEX_NAME, NON_UNIQUE, SEQ_IN_INDEX, COLUMN_NAME FROM information_schema.STATISTICS WHERE ${outsideKnex} ORDER BY 1, 2, 4`
|
|
112
|
+
);
|
|
113
|
+
const [keys] = await knex.raw(
|
|
114
|
+
`SELECT TABLE_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, UPDATE_RULE, DELETE_RULE FROM information_schema.REFERENTIAL_CONSTRAINTS
|
|
115
|
+
WHERE CONSTRAINT_SCHEMA = DATABASE() ORDER BY 1, 2`
|
|
116
|
+
);
|
|
117
|
+
return { tables, columns, indexes, keys };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const account = { id: "usr_ada", email: "ada@example.com", display_name: "Ada", password_algorithm: "scrypt", password_version: "1",
|
|
121
|
+
password_salt: "salt", password_hash: "hash", disabled: false, created_at: "2026-11-01 08:00:00", updated_at: "2026-11-01 08:00:00" };
|
|
122
|
+
const usedPass = { id: "c0ffee", expires_at: "2026-12-01 08:00:00", created_at: "2026-11-01 08:00:00" };
|
|
123
|
+
|
|
124
|
+
test("each companion sorts just before the released migration it runs, as Knex sorts a directory's files", () => {
|
|
125
|
+
const files = readdirSync(MIGRATIONS).filter((file) => file.endsWith(".cjs")).sort();
|
|
126
|
+
for (const file of files.filter((name) => name.endsWith("-collation.cjs"))) {
|
|
127
|
+
assert.equal(files[files.indexOf(file) + 1], file.replace("-collation.cjs", ".cjs"), file);
|
|
128
|
+
}
|
|
129
|
+
assert.deepEqual(files.filter((file) => file.endsWith("-collation.cjs")).map((file) => file.replace("-collation.cjs", "")), [
|
|
130
|
+
"auth_local_db_initial",
|
|
131
|
+
"auth_local_db_oauth",
|
|
132
|
+
"auth_local_db_protection",
|
|
133
|
+
"auth_local_db_used_passes",
|
|
134
|
+
"auth_local_db_users_second_factors"
|
|
135
|
+
], "every released migration that creates a table has its companion");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("a database whose tables differ from its default creates each new table in its tables' collation", async (context) => {
|
|
139
|
+
const database = await openDatabase(context, "coll_behind", TABLES_COLLATION);
|
|
140
|
+
await migrateReleased(database.knex, EARLY);
|
|
141
|
+
const knex = await database.restoredWithDefault(OTHER_DEFAULT);
|
|
142
|
+
|
|
143
|
+
await migrate(knex);
|
|
144
|
+
|
|
145
|
+
const tables = await collations(knex);
|
|
146
|
+
assert.deepEqual(Object.keys(tables), [
|
|
147
|
+
"auth_local_identities", "auth_local_oauth_attempts", "auth_local_recovery", "auth_local_registrations",
|
|
148
|
+
"auth_local_second_factors", "auth_local_sessions", "auth_local_sign_in_failures", "auth_local_used_passes", "auth_local_users"
|
|
149
|
+
]);
|
|
150
|
+
for (const [table, collation] of Object.entries(tables)) assert.equal(collation, TABLES_COLLATION, table);
|
|
151
|
+
assert.equal(await hasSecondFactorKey(knex), true);
|
|
152
|
+
assert.deepEqual(await recorded(knex), readdirSync(MIGRATIONS).filter((file) => file.endsWith(".cjs")).sort());
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("a database the released migrations left half-migrated recovers on the next migrate", async (context) => {
|
|
156
|
+
const database = await openDatabase(context, "coll_half", TABLES_COLLATION);
|
|
157
|
+
await migrateReleased(database.knex, EARLY);
|
|
158
|
+
const knex = await database.restoredWithDefault(OTHER_DEFAULT);
|
|
159
|
+
await assert.rejects(migrateReleased(knex, RELEASED), /errno: 150|incorrectly formed/u);
|
|
160
|
+
|
|
161
|
+
// The state the refused key left, as dogandgroom's database measured it.
|
|
162
|
+
assert.deepEqual(await recorded(knex), [
|
|
163
|
+
...EARLY,
|
|
164
|
+
"auth_local_db_sessions_metadata.cjs",
|
|
165
|
+
"auth_local_db_used_passes.cjs",
|
|
166
|
+
"auth_local_db_users_disabled_message.cjs",
|
|
167
|
+
"auth_local_db_users_email_verified.cjs",
|
|
168
|
+
"auth_local_db_users_pending_email.cjs"
|
|
169
|
+
].sort());
|
|
170
|
+
assert.equal((await collations(knex)).auth_local_used_passes, OTHER_DEFAULT);
|
|
171
|
+
assert.equal((await collations(knex)).auth_local_second_factors, OTHER_DEFAULT);
|
|
172
|
+
assert.equal(await hasSecondFactorKey(knex), false);
|
|
173
|
+
assert.equal(Number((await knex("auth_local_second_factors").count({ rows: "*" }))[0].rows), 0);
|
|
174
|
+
await knex("auth_local_users").insert(account);
|
|
175
|
+
await knex("auth_local_used_passes").insert(usedPass);
|
|
176
|
+
|
|
177
|
+
await migrate(knex);
|
|
178
|
+
|
|
179
|
+
const tables = await collations(knex);
|
|
180
|
+
for (const [table, collation] of Object.entries(tables)) assert.equal(collation, TABLES_COLLATION, table);
|
|
181
|
+
assert.equal(await hasSecondFactorKey(knex), true, "the empty, unrecorded table is replaced with its key");
|
|
182
|
+
assert.deepEqual(await knex("auth_local_used_passes").select("id"), [{ id: usedPass.id }], "the converted table keeps its row");
|
|
183
|
+
assert.equal(await knex.schema.hasColumn("auth_local_sessions", "second_factor_at"), true);
|
|
184
|
+
assert.deepEqual(await recorded(knex), readdirSync(MIGRATIONS).filter((file) => file.endsWith(".cjs")).sort());
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("a half-migrated database the released migrations then completed gets its tables converted and its key", async (context) => {
|
|
188
|
+
const database = await openDatabase(context, "coll_retried", TABLES_COLLATION);
|
|
189
|
+
await migrateReleased(database.knex, EARLY);
|
|
190
|
+
const knex = await database.restoredWithDefault(OTHER_DEFAULT);
|
|
191
|
+
await assert.rejects(migrateReleased(knex, RELEASED), /errno: 150|incorrectly formed/u);
|
|
192
|
+
// A second try of the released migrations finds the table and records it, without its key.
|
|
193
|
+
await migrateReleased(knex, RELEASED);
|
|
194
|
+
assert.deepEqual(await recorded(knex), RELEASED);
|
|
195
|
+
assert.equal((await collations(knex)).auth_local_second_factors, OTHER_DEFAULT);
|
|
196
|
+
assert.equal(await hasSecondFactorKey(knex), false);
|
|
197
|
+
await knex("auth_local_users").insert(account);
|
|
198
|
+
await knex("auth_local_used_passes").insert(usedPass);
|
|
199
|
+
await knex("auth_local_second_factors").insert({ user_id: account.id, secret_sealed: "sealed", last_used_step: 0,
|
|
200
|
+
recovery_code_hashes: "", failures: 0, created_at: "2026-11-01 08:00:00", updated_at: "2026-11-01 08:00:00" });
|
|
201
|
+
|
|
202
|
+
await migrate(knex);
|
|
203
|
+
|
|
204
|
+
for (const [table, collation] of Object.entries(await collations(knex))) assert.equal(collation, TABLES_COLLATION, table);
|
|
205
|
+
assert.equal(await hasSecondFactorKey(knex), true);
|
|
206
|
+
assert.deepEqual(await knex("auth_local_second_factors").pluck("user_id"), [account.id]);
|
|
207
|
+
assert.deepEqual(await knex("auth_local_used_passes").pluck("id"), [usedPass.id]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("a database whose tables match its default sees no change, and a new one gets the released schema", async (context) => {
|
|
211
|
+
const { knex: migrated } = await openDatabase(context, "coll_match", TABLES_COLLATION);
|
|
212
|
+
await migrateReleased(migrated, RELEASED);
|
|
213
|
+
const before = await schemaOf(migrated);
|
|
214
|
+
|
|
215
|
+
await migrate(migrated);
|
|
216
|
+
|
|
217
|
+
assert.deepEqual(await schemaOf(migrated), before);
|
|
218
|
+
assert.deepEqual(await recorded(migrated), readdirSync(MIGRATIONS).filter((file) => file.endsWith(".cjs")).sort());
|
|
219
|
+
|
|
220
|
+
const { knex: fresh } = await openDatabase(context, "coll_fresh", TABLES_COLLATION);
|
|
221
|
+
await migrate(fresh);
|
|
222
|
+
assert.deepEqual(await schemaOf(fresh), before, "the companions change nothing on a new database");
|
|
223
|
+
});
|