mirror of
https://github.com/rajnandan1/kener.git
synced 2026-06-23 04:10:22 +00:00
28 lines
919 B
TypeScript
28 lines
919 B
TypeScript
import type { Knex } from "knex";
|
|
|
|
export async function up(knex: Knex): Promise<void> {
|
|
const hasCol = await knex.schema.hasColumn("users", "is_owner");
|
|
if (!hasCol) {
|
|
await knex.schema.alterTable("users", (table) => {
|
|
table.string("is_owner").defaultTo("NO").notNullable();
|
|
});
|
|
|
|
// Set the first user (by id) as owner, if any users exist.
|
|
// This only runs when the column has just been added to avoid
|
|
// overwriting an existing owner on migration re-run.
|
|
const firstUser = await knex("users").orderBy("id", "asc").first();
|
|
if (firstUser) {
|
|
await knex("users").where("id", firstUser.id).update({ is_owner: "YES" });
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function down(knex: Knex): Promise<void> {
|
|
const hasCol = await knex.schema.hasColumn("users", "is_owner");
|
|
if (hasCol) {
|
|
await knex.schema.alterTable("users", (table) => {
|
|
table.dropColumn("is_owner");
|
|
});
|
|
}
|
|
}
|