Refatoring : Working state
This commit is contained in:
67
database/migrations/002_create_posts_search.php
Normal file
67
database/migrations/002_create_posts_search.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Migration 002 — Création de l'index de recherche FTS5 et de ses triggers.
|
||||
*/
|
||||
return [
|
||||
'up' => "
|
||||
CREATE VIRTUAL TABLE IF NOT EXISTS posts_fts USING fts5(
|
||||
title,
|
||||
content,
|
||||
author_username,
|
||||
tokenize = 'unicode61 remove_diacritics 1'
|
||||
);
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS posts_fts_insert
|
||||
AFTER INSERT ON posts BEGIN
|
||||
INSERT INTO posts_fts(rowid, title, content, author_username)
|
||||
VALUES (
|
||||
NEW.id,
|
||||
NEW.title,
|
||||
COALESCE(strip_tags(NEW.content), ''),
|
||||
COALESCE((SELECT username FROM users WHERE id = NEW.author_id), '')
|
||||
);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS posts_fts_update
|
||||
AFTER UPDATE ON posts BEGIN
|
||||
DELETE FROM posts_fts WHERE rowid = OLD.id;
|
||||
INSERT INTO posts_fts(rowid, title, content, author_username)
|
||||
VALUES (
|
||||
NEW.id,
|
||||
NEW.title,
|
||||
COALESCE(strip_tags(NEW.content), ''),
|
||||
COALESCE((SELECT username FROM users WHERE id = NEW.author_id), '')
|
||||
);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS posts_fts_delete
|
||||
AFTER DELETE ON posts BEGIN
|
||||
DELETE FROM posts_fts WHERE rowid = OLD.id;
|
||||
END;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS posts_fts_users_update
|
||||
AFTER UPDATE OF username ON users BEGIN
|
||||
DELETE FROM posts_fts
|
||||
WHERE rowid IN (SELECT id FROM posts WHERE author_id = NEW.id);
|
||||
|
||||
INSERT INTO posts_fts(rowid, title, content, author_username)
|
||||
SELECT p.id,
|
||||
p.title,
|
||||
COALESCE(strip_tags(p.content), ''),
|
||||
NEW.username
|
||||
FROM posts p
|
||||
WHERE p.author_id = NEW.id;
|
||||
END;
|
||||
",
|
||||
|
||||
'down' => "
|
||||
DROP TRIGGER IF EXISTS posts_fts_users_update;
|
||||
DROP TRIGGER IF EXISTS posts_fts_delete;
|
||||
DROP TRIGGER IF EXISTS posts_fts_update;
|
||||
DROP TRIGGER IF EXISTS posts_fts_insert;
|
||||
DROP TABLE IF EXISTS posts_fts;
|
||||
",
|
||||
];
|
||||
Reference in New Issue
Block a user