{{{
  exports({ to: app.migrationsPath(`${new Date().getTime()}_create_media_upload_sessions_table.ts`) })
}}}
import { BaseSchema } from '@adonisjs/lucid/schema'

/**
 * Tables for the @adonis-agora/media resumable (TUS) 'lucid' session store. 'media_upload_sessions'
 * persists offset/length/metadata/expiry so a dropped upload resumes; 'media_upload_parts' is the
 * per-part ETag side-index used to assemble native S3 multipart uploads. JSON 'metadata' is stored as
 * TEXT and timestamps as epoch-ms integers, so the schema is portable across SQLite / Postgres / MySQL.
 * Only needed if you enable 'uploads.resumable' with the 'lucid' session store.
 */
/**
 * The database schema both tables live in: the same value as 'schema' in uploadSessions.lucid().
 * Leave it undefined to use the connection's current schema. On MySQL a schema is a database.
 */
const schema: string | undefined = undefined

export default class extends BaseSchema {
  protected sessionsTable = 'media_upload_sessions'
  protected partsTable = 'media_upload_parts'

  async up() {
    if (schema) {
      const create = this.db.dialect.name === 'mysql' ? 'CREATE DATABASE' : 'CREATE SCHEMA'
      this.schema.raw(this.db.knexRawQuery(create + ' IF NOT EXISTS ??', [schema]).toQuery())
    }
    this.builder().createTable(this.sessionsTable, (table) => {
      table.string('id').primary()
      table.string('disk').notNullable()
      table.text('key').notNullable()
      table.string('content_type').nullable()
      table.bigInteger('size').nullable()
      table.bigInteger('offset').notNullable().defaultTo(0)
      table.integer('parts').notNullable().defaultTo(0)
      table.string('multipart_upload_id').nullable()
      table.text('metadata').nullable()
      table.bigInteger('created_at').notNullable()
      table.bigInteger('updated_at').nullable()
      table.bigInteger('expires_at').nullable()
      table.index(['disk'], 'media_upload_sessions_disk_idx')
    })

    this.builder().createTable(this.partsTable, (table) => {
      table.string('session_id').notNullable()
      table.integer('part_number').notNullable()
      table.string('etag').notNullable()
      table.primary(['session_id', 'part_number'])
    })
  }

  async down() {
    this.builder().dropTable(this.partsTable)
    this.builder().dropTable(this.sessionsTable)
  }

  /** The schema builder, pointed at 'schema' when it is set. */
  private builder() {
    return schema ? this.schema.withSchema(schema) : this.schema
  }
}
