parent
94dd7a0dd6
commit
1148bc6bff
6 changed files with 104 additions and 4 deletions
@ -0,0 +1,58 @@ |
||||
import * as Knex from "knex"; |
||||
|
||||
/* |
||||
This migration converts the storeLinks column from JSON to plain text. |
||||
The reason is that there are too many differences between the JSON support |
||||
of different back-ends, making plain text easier to deal with. |
||||
*/ |
||||
|
||||
async function castStoreLinks(table: string, knex: any) { |
||||
await knex.schema.alterTable(table, (t: any) => { |
||||
t.string('storeLinksTemp'); |
||||
}); |
||||
await knex(table).update({ |
||||
storeLinksTemp: knex.raw('CAST("storeLinks" AS VARCHAR(255))') |
||||
}) |
||||
await knex.schema.alterTable(table, (t: any) => { |
||||
t.dropColumn('storeLinks'); |
||||
}); |
||||
await knex.schema.alterTable(table, (t: any) => { |
||||
t.renameColumn('storeLinksTemp', 'storeLinks'); |
||||
}); |
||||
} |
||||
|
||||
async function revertStoreLinks(table: string, knex: any) { |
||||
await knex.schema.alterTable(table, (t: any) => { |
||||
t.json('storeLinksTemp'); |
||||
}); |
||||
if (knex.client.config.client === 'sqlite3') { |
||||
await knex(table).update({ |
||||
storeLinksTemp: knex.raw('"storeLinks"') |
||||
}) |
||||
} else { |
||||
await knex(table).update({ |
||||
storeLinksTemp: knex.raw('CAST("storeLinks" AS json)') |
||||
}) |
||||
} |
||||
await knex.schema.alterTable(table, (t: any) => { |
||||
t.dropColumn('storeLinks'); |
||||
}); |
||||
await knex.schema.alterTable(table, (t: any) => { |
||||
t.renameColumn('storeLinksTemp', 'storeLinks'); |
||||
}); |
||||
} |
||||
|
||||
export async function up(knex: Knex): Promise<void> { |
||||
console.log("Knex client:", knex.client.config); |
||||
await castStoreLinks('songs', knex); |
||||
await castStoreLinks('albums', knex); |
||||
await castStoreLinks('artists', knex); |
||||
} |
||||
|
||||
|
||||
export async function down(knex: Knex): Promise<void> { |
||||
await revertStoreLinks('songs', knex); |
||||
await revertStoreLinks('albums', knex); |
||||
await revertStoreLinks('artists', knex); |
||||
} |
||||
|
Loading…
Reference in new issue