import { BaseSchema } from '@adonisjs/lucid/schema' export default class extends BaseSchema { protected tableName = 'web_pages' async up() { this.schema.raw('DROP TYPE IF EXISTS web_page_status_enum') this.schema.createTable(this.tableName, (table) => { table.increments('id') table .enu('status', ['draft', 'published', 'unpublished'], { useNative: true, enumName: 'web_page_status_enum', }) .notNullable() .defaultTo('draft') table .integer('draft_of_id') .unsigned() .nullable() .references('id') .inTable(this.tableName) .onDelete('CASCADE') table.string('locale', 16).notNullable() table.string('path').notNullable() table.string('group_key').notNullable() table.string('page_type').notNullable() table.string('title').notNullable() table.jsonb('hero').nullable() table.string('seo_title').notNullable().defaultTo('') table.text('seo_description').notNullable().defaultTo('') table.timestamp('published_at', { useTz: true }).nullable() table.timestamp('created_at', { useTz: true }).notNullable() table.timestamp('updated_at', { useTz: true }).notNullable() table.index(['group_key', 'status']) table.index(['locale', 'path', 'status']) }) this.schema.raw(` CREATE UNIQUE INDEX web_pages_published_route_unique ON ${this.tableName} (locale, path) WHERE status = 'published' `) this.schema.raw(` CREATE UNIQUE INDEX web_pages_one_draft_per_page_unique ON ${this.tableName} (draft_of_id) WHERE draft_of_id IS NOT NULL `) } async down() { this.schema.dropTable(this.tableName) this.schema.raw('DROP TYPE IF EXISTS web_page_status_enum') } }