ionian/migrations/initial.ts

53 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-12-31 03:06:16 +09:00
import Knex from 'knex';
export async function up(knex:Knex) {
await knex.schema.createTable("users",(b)=>{
b.string("username").primary().comment("user's login id");
b.string("password_hash",64).notNullable();
b.string("password_salt",64).notNullable();
});
await knex.schema.createTable("contents",(b)=>{
b.increments("id").primary();
b.string("title").notNullable();
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();
b.timestamps();
b.index("content_type","content_type_index");
});
await knex.schema.createTable("tags", (b)=>{
b.string("name").primary();
b.text("description");
});
await knex.schema.createTable("content_tag_relation",(b)=>{
b.integer("content_id").unsigned().notNullable();
b.string("tag_name").notNullable();
b.foreign("content_id").references("contents.id");
b.foreign("tag_name").references("tags.name");
b.primary(["content_id","tag_name"]);
});
await knex.schema.createTable("permissions",b=>{
2021-01-10 04:27:50 +09:00
b.string('username').notNullable();
2020-12-31 03:06:16 +09:00
b.string("name").notNullable();
b.primary(["username","name"]);
b.foreign('username').references('users.username');
});
//create admin account.
await knex.insert({
username:"admin",
password_hash:"unchecked",
password_salt:"unchecked"
}).into('users');
};
export async function down(knex:Knex) {
//throw new Error('Downward migrations are not supported. Restore from backup.');
await knex.schema.dropTable("users");
await knex.schema.dropTable("contents");
await knex.schema.dropTable("tags");
await knex.schema.dropTable("content_tag_relation");
await knex.schema.dropTable("permissions");
};