import { BaseSchema } from '@adonisjs/lucid/schema' /** * is_readable_name(): also hex with a Unix time, UUIDs, digit-only names * (and rclone's "83111763 n"), lowercase base32/36 tokens and short tokens * with capitals all over. The same rules as looksRandom() in ArchiveService. */ export default class extends BaseSchema { async up() { this.defer(async (db) => { await db.rawQuery(` CREATE OR REPLACE FUNCTION is_readable_name(name text) RETURNS boolean IMMUTABLE LANGUAGE sql AS $$ WITH s AS (SELECT regexp_replace(name_stem(name), '\\.[A-Za-z0-9]{1,4}$', '') AS stem) SELECT name IS NOT NULL AND NOT ( (stem ~ '^[A-Za-z0-9]{16,}$' AND stem ~ '[a-z]' AND stem ~ '[A-Z]' AND (stem ~ '\\d' OR length(regexp_replace(stem, '[^A-Z]', '', 'g')) >= 6)) OR stem ~* '^[0-9a-f]{16,}$' OR stem ~* '^[0-9a-f]{16,}\\.\\d{9,}$' OR stem ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' OR stem ~ '^\\d{6,}( n){0,1}$' OR (stem ~ '^[a-z0-9]{12,}$' AND length(regexp_replace(stem, '\\D', '', 'g')) >= 2 AND length(regexp_replace(stem, '[^a-z]', '', 'g')) >= 4) OR (stem ~ '^[A-Za-z0-9]{8,15}$' AND stem ~ '\\d' AND stem ~ '[a-z]' AND length(regexp_replace(substr(stem, 2), '[^A-Z]', '', 'g')) >= 3)) FROM s $$`) }) } async down() { this.defer(async (db) => { await db.rawQuery(` CREATE OR REPLACE FUNCTION is_readable_name(name text) RETURNS boolean IMMUTABLE LANGUAGE sql AS $$ WITH s AS (SELECT regexp_replace(name_stem(name), '\\.[A-Za-z0-9]{1,4}$', '') AS stem) SELECT name IS NOT NULL AND NOT ( (stem ~ '^[A-Za-z0-9]{16,}$' AND stem ~ '[a-z]' AND stem ~ '[A-Z]' AND (stem ~ '\\d' OR length(regexp_replace(stem, '[^A-Z]', '', 'g')) >= 6)) OR stem ~* '^[0-9a-f]{16,}$') FROM s $$`) }) } }