import { BaseSchema } from '@adonisjs/lucid/schema' /** * Accounts with limits (config/access.ts): a Newznab key and a tier per user, * invites, and what each user used per day. */ export default class extends BaseSchema { async up() { this.schema.alterTable('users', (table) => { // Existing users get a key of their own (a volatile default is evaluated per row). table.string('api_key', 32).notNullable().unique().defaultTo(this.raw(`md5(random()::text || clock_timestamp()::text)`)) table.string('tier', 16).notNullable().defaultTo('free') // Null: the tier doesn't run out. table.timestamp('tier_until', { useTz: true }).nullable() table.boolean('suspended').notNullable().defaultTo(false) }) this.schema.createTable('invites', (table) => { table.string('code', 32).primary() // What the invite gives (e.g. a sold VIP invite); null: the free tier. table.string('tier', 16).nullable() table.integer('tier_days').nullable() table.string('note').nullable() table.integer('used_by').nullable().references('id').inTable('users').onDelete('SET NULL') table.timestamp('used_at', { useTz: true }).nullable() table.timestamp('created_at', { useTz: true }).notNullable().defaultTo(this.now()) }) this.schema.createTable('usage', (table) => { table.integer('user_id').notNullable().references('id').inTable('users').onDelete('CASCADE') // A UTC day. table.date('day').notNullable() table.integer('api_hits').notNullable().defaultTo(0) table.integer('grabs').notNullable().defaultTo(0) table.primary(['user_id', 'day']) }) } async down() { this.schema.dropTable('usage') this.schema.dropTable('invites') this.schema.alterTable('users', (table) => { table.dropColumn('api_key') table.dropColumn('tier') table.dropColumn('tier_until') table.dropColumn('suspended') }) } }