2023-06-01 14:18:53 +09:00
|
|
|
import { Knex } from "knex";
|
2020-12-31 03:06:16 +09:00
|
|
|
|
2023-06-01 14:18:53 +09:00
|
|
|
export async function up(knex: Knex) {
|
|
|
|
await knex.schema.createTable("schema_migration", (b) => {
|
2022-07-19 23:41:50 +09:00
|
|
|
b.string("version");
|
|
|
|
b.boolean("dirty");
|
|
|
|
});
|
|
|
|
|
2023-06-01 14:18:53 +09:00
|
|
|
await knex.schema.createTable("users", (b) => {
|
2020-12-31 03:06:16 +09:00
|
|
|
b.string("username").primary().comment("user's login id");
|
2023-06-01 14:18:53 +09:00
|
|
|
b.string("password_hash", 64).notNullable();
|
|
|
|
b.string("password_salt", 64).notNullable();
|
2020-12-31 03:06:16 +09:00
|
|
|
});
|
2023-06-01 14:18:53 +09:00
|
|
|
await knex.schema.createTable("document", (b) => {
|
2020-12-31 03:06:16 +09:00
|
|
|
b.increments("id").primary();
|
|
|
|
b.string("title").notNullable();
|
2023-06-01 14:18:53 +09:00
|
|
|
b.string("content_type", 16).notNullable();
|
|
|
|
b.string("basepath", 256).notNullable().comment("directory path for resource");
|
|
|
|
b.string("filename", 256).notNullable().comment("filename");
|
2021-01-03 01:36:34 +09:00
|
|
|
b.string("content_hash").nullable();
|
2020-12-31 03:06:16 +09:00
|
|
|
b.json("additional").nullable();
|
2021-01-15 18:43:36 +09:00
|
|
|
b.integer("created_at").notNullable();
|
2022-07-19 23:41:50 +09:00
|
|
|
b.integer("modified_at").notNullable();
|
2021-01-15 18:43:36 +09:00
|
|
|
b.integer("deleted_at");
|
2023-06-01 14:18:53 +09:00
|
|
|
b.index("content_type", "content_type_index");
|
2020-12-31 03:06:16 +09:00
|
|
|
});
|
2023-06-01 14:18:53 +09:00
|
|
|
await knex.schema.createTable("tags", (b) => {
|
2020-12-31 03:06:16 +09:00
|
|
|
b.string("name").primary();
|
|
|
|
b.text("description");
|
|
|
|
});
|
2023-06-01 14:18:53 +09:00
|
|
|
await knex.schema.createTable("doc_tag_relation", (b) => {
|
2021-01-11 23:37:29 +09:00
|
|
|
b.integer("doc_id").unsigned().notNullable();
|
2020-12-31 03:06:16 +09:00
|
|
|
b.string("tag_name").notNullable();
|
2021-01-11 23:37:29 +09:00
|
|
|
b.foreign("doc_id").references("document.id");
|
2020-12-31 03:06:16 +09:00
|
|
|
b.foreign("tag_name").references("tags.name");
|
2023-06-01 14:18:53 +09:00
|
|
|
b.primary(["doc_id", "tag_name"]);
|
2020-12-31 03:06:16 +09:00
|
|
|
});
|
2023-06-01 14:18:53 +09:00
|
|
|
await knex.schema.createTable("permissions", b => {
|
|
|
|
b.string("username").notNullable();
|
2020-12-31 03:06:16 +09:00
|
|
|
b.string("name").notNullable();
|
2023-06-01 14:18:53 +09:00
|
|
|
b.primary(["username", "name"]);
|
|
|
|
b.foreign("username").references("users.username");
|
2020-12-31 03:06:16 +09:00
|
|
|
});
|
2023-06-01 14:18:53 +09:00
|
|
|
// create admin account.
|
2020-12-31 03:06:16 +09:00
|
|
|
await knex.insert({
|
2023-06-01 14:18:53 +09:00
|
|
|
username: "admin",
|
|
|
|
password_hash: "unchecked",
|
|
|
|
password_salt: "unchecked",
|
|
|
|
}).into("users");
|
|
|
|
}
|
2020-12-31 03:06:16 +09:00
|
|
|
|
2023-06-01 14:18:53 +09:00
|
|
|
export async function down(knex: Knex) {
|
|
|
|
throw new Error("Downward migrations are not supported. Restore from backup.");
|
|
|
|
}
|