You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

58 lines
1.8 KiB

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);
}