mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-05 12:50:29 +02:00
Server: Fixed owner_id migration for SQLite
This commit is contained in:
parent
b0e3e1b50e
commit
f1c4d35ef3
@ -1,17 +1,42 @@
|
|||||||
import { Knex } from 'knex';
|
import { Knex } from 'knex';
|
||||||
import { DbConnection } from '../db';
|
import { DbConnection, isPostgres } from '../db';
|
||||||
|
|
||||||
export async function up(db: DbConnection): Promise<any> {
|
export async function up(db: DbConnection): Promise<any> {
|
||||||
await db.schema.alterTable('items', (table: Knex.CreateTableBuilder) => {
|
await db.schema.alterTable('items', (table: Knex.CreateTableBuilder) => {
|
||||||
table.string('owner_id', 32).defaultTo('').notNullable();
|
table.string('owner_id', 32).defaultTo('').notNullable();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (isPostgres(db)) {
|
||||||
await db.raw(`
|
await db.raw(`
|
||||||
UPDATE items
|
UPDATE items
|
||||||
SET owner_id = user_items.user_id
|
SET owner_id = user_items.user_id
|
||||||
FROM user_items
|
FROM user_items
|
||||||
WHERE user_items.item_id = items.id
|
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.
|
||||||
|
|
||||||
|
interface Row {
|
||||||
|
id: string;
|
||||||
|
user_id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await db.schema.alterTable('items', (table: Knex.CreateTableBuilder) => {
|
await db.schema.alterTable('items', (table: Knex.CreateTableBuilder) => {
|
||||||
table.string('owner_id', 32).notNullable().alter();
|
table.string('owner_id', 32).notNullable().alter();
|
||||||
|
Loading…
Reference in New Issue
Block a user