|
| 1 | +/* eslint newline-per-chained-call: ["error", { "ignoreChainWithDepth": 10 }] */ |
| 2 | +/* eslint-disable prettier/prettier, max-len, arrow-body-style */ |
| 3 | + |
| 4 | +const createAuthorsTable = table => { |
| 5 | + table.increments('id'); |
| 6 | + table.string('name').notNullable(); |
| 7 | + table.string('image_url').notNullable(); |
| 8 | + table.string('role').notNullable(); |
| 9 | +}; |
| 10 | + |
| 11 | +const createUsersTable = table => { |
| 12 | + table.increments('id'); |
| 13 | + table.string('username').notNullable().unique(); |
| 14 | + table.string('email').notNullable().unique(); |
| 15 | + table.string('first_name').notNullable(); |
| 16 | + table.string('last_name').notNullable(); |
| 17 | + table.string('password').notNullable(); |
| 18 | + table.integer('author_id').references('id').inTable('authors'); |
| 19 | +}; |
| 20 | + |
| 21 | +const createArticlesTable = (table, knex) => { |
| 22 | + table.increments('id'); |
| 23 | + table.integer('author').notNullable(); |
| 24 | + table.string('title').notNullable(); |
| 25 | + table.string('subtitle').notNullable(); |
| 26 | + table.string('slug').notNullable().unique(); |
| 27 | + table.string('category').notNullable(); |
| 28 | + table.date('posted_on').notNullable().defaultTo(knex.fn.now()); |
| 29 | + table.boolean('hidden').defaultTo(false); |
| 30 | +}; |
| 31 | + |
| 32 | +const createArticleContentTable = table => { |
| 33 | + table.integer('article_id').notNullable().unique().references('id').inTable('articles'); |
| 34 | + table.string('summary').notNullable(); |
| 35 | + table.string('image_url').notNullable(); |
| 36 | + table.text('markdown_content').notNullable(); |
| 37 | +}; |
| 38 | + |
| 39 | +const createRelatedArticlesTable = table => { |
| 40 | + table.integer('article_id').notNullable().unique().references('id').inTable('articles'); |
| 41 | + table.integer('related_article_id').notNullable().references('id').inTable('articles'); |
| 42 | +}; |
| 43 | + |
| 44 | +exports.up = (knex, Promise) => { |
| 45 | + return Promise.all([ |
| 46 | + knex.schema.createTable('users', table => createUsersTable(table)), |
| 47 | + knex.schema.createTable('authors', table => createAuthorsTable(table)), |
| 48 | + knex.schema.createTable('articles', table => createArticlesTable(table, knex)), |
| 49 | + knex.schema.createTable('article_content', table => createArticleContentTable(table)), |
| 50 | + knex.schema.createTable('related_articles', table => createRelatedArticlesTable(table)), |
| 51 | + ]); |
| 52 | +}; |
| 53 | + |
| 54 | +exports.down = (knex, Promise) => { |
| 55 | + return Promise.all([ |
| 56 | + knex.schema.dropTable('related_articles'), |
| 57 | + knex.schema.dropTable('article_content'), |
| 58 | + knex.schema.dropTable('articles'), |
| 59 | + knex.schema.dropTable('users'), |
| 60 | + knex.schema.dropTable('authors'), |
| 61 | + ]); |
| 62 | +}; |
0 commit comments