Files
usenet-indexer/database/migrations/1768620764910_readable_name_any_extension.ts
T

24 lines
840 B
TypeScript

import { BaseSchema } from '@adonisjs/lucid/schema'
/**
* is_readable_name(): also judge the stem without an unknown short
* extension, so `326678d1d07ef12a.bin` counts as random (CONCEPT.md D22).
*/
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,}$')
FROM s
$$`)
})
}
async down() {}
}