mirror of
https://github.com/laurent22/joplin.git
synced 2025-03-26 21:12:59 +02:00
Server: Fixed items.owner_id migration
This commit is contained in:
parent
dca13b3a68
commit
a753429277
@ -15,6 +15,7 @@ enum ArgvCommand {
|
||||
|
||||
interface Argv {
|
||||
command: ArgvCommand;
|
||||
disableTransactions?: boolean;
|
||||
}
|
||||
|
||||
export default class MigrateCommand extends BaseCommand {
|
||||
@ -53,13 +54,13 @@ export default class MigrateCommand extends BaseCommand {
|
||||
public async run(argv: Argv, runContext: RunContext): Promise<void> {
|
||||
const commands: Record<ArgvCommand, Function> = {
|
||||
up: async () => {
|
||||
await migrateUp(runContext.db);
|
||||
await migrateUp(runContext.db, argv.disableTransactions);
|
||||
},
|
||||
down: async () => {
|
||||
await migrateDown(runContext.db);
|
||||
await migrateDown(runContext.db, argv.disableTransactions);
|
||||
},
|
||||
latest: async () => {
|
||||
await migrateLatest(runContext.db);
|
||||
await migrateLatest(runContext.db, argv.disableTransactions);
|
||||
},
|
||||
list: async () => {
|
||||
const s = (await migrateList(runContext.db)) as string;
|
||||
|
@ -1,41 +1,52 @@
|
||||
import { Knex } from 'knex';
|
||||
import { DbConnection, isPostgres } from '../db';
|
||||
import { DbConnection } from '../db';
|
||||
|
||||
export async function up(db: DbConnection): Promise<any> {
|
||||
await db.schema.alterTable('items', (table: Knex.CreateTableBuilder) => {
|
||||
table.string('owner_id', 32).defaultTo('').notNullable();
|
||||
});
|
||||
if (!(await db.schema.hasColumn('items', 'owner_id'))) {
|
||||
await db.schema.alterTable('items', (table: Knex.CreateTableBuilder) => {
|
||||
table.string('owner_id', 32).defaultTo('').notNullable();
|
||||
});
|
||||
}
|
||||
|
||||
if (isPostgres(db)) {
|
||||
await db.raw(`
|
||||
UPDATE items
|
||||
SET owner_id = user_items.user_id
|
||||
FROM user_items
|
||||
WHERE user_items.item_id = items.id
|
||||
`);
|
||||
} else {
|
||||
// Very inefficient way to set the owner_id but SQLite is probably not
|
||||
// used with very large dataset.
|
||||
// This query never finishes - so can't use it
|
||||
|
||||
interface Row {
|
||||
id: string;
|
||||
user_id: string;
|
||||
// await db.raw(`
|
||||
// UPDATE items^
|
||||
// SET owner_id = user_items.user_id
|
||||
// FROM user_items
|
||||
// WHERE user_items.item_id = items.id
|
||||
// `);
|
||||
|
||||
interface Row {
|
||||
id: string;
|
||||
user_id: string;
|
||||
}
|
||||
|
||||
const pageSize = 10000;
|
||||
|
||||
const itemCount = (await db('items')
|
||||
.count('id', { as: 'total' })
|
||||
.where('owner_id', '=', '')
|
||||
.first())['total'];
|
||||
|
||||
let itemDone = 0;
|
||||
|
||||
while (true) {
|
||||
const items: Row[] = await db('items')
|
||||
.join('user_items', 'items.id', 'user_items.item_id')
|
||||
.select(['items.id', 'user_items.user_id'])
|
||||
.where('owner_id', '=', '')
|
||||
.limit(pageSize);
|
||||
|
||||
if (!items.length) break;
|
||||
|
||||
console.info(`Processing items ${itemDone} / ${itemCount}`);
|
||||
|
||||
for (const item of items) {
|
||||
await db('items').update({ owner_id: item.user_id }).where('id', '=', item.id);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
const items: Row[] = await
|
||||
db('items')
|
||||
.join('user_items', 'items.id', 'user_items.item_id')
|
||||
.select(['items.id', 'user_items.user_id'])
|
||||
.where('owner_id', '=', '')
|
||||
.limit(10000);
|
||||
|
||||
if (!items.length) break;
|
||||
|
||||
for (const item of items) {
|
||||
await db('items').update({ owner_id: item.user_id }).where('id', '=', item.id);
|
||||
}
|
||||
}
|
||||
itemDone += items.length;
|
||||
}
|
||||
|
||||
await db.schema.alterTable('items', (table: Knex.CreateTableBuilder) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user