From 7d10ad48b9207ca4ef16baafba4ec950e230b70e Mon Sep 17 00:00:00 2001 From: Tigor Hutasuhut Date: Fri, 16 Aug 2024 14:38:57 +0700 Subject: [PATCH] sqlite: added create images_fts5 table --- Makefile | 1 + flake.nix | 13 ++++++++-- .../20240808143100_create_images_table.sql | 26 ++++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index c3ff567..96e774d 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ export PATH := $(shell pwd)/bin:$(shell pwd)/web/node_modules/.bin:$(PATH) export GOOSE_DRIVER ?= sqlite3 export GOOSE_DBSTRING ?= ./go/data.db export GOOSE_MIGRATION_DIR ?= schemas/migrations +export GOFLAGS=-tags=fts5 build: generate-go generate-web go build -o bin/bluemage ./go/cmd/bluemage/main.go diff --git a/flake.nix b/flake.nix index 08ded18..17b5cbc 100644 --- a/flake.nix +++ b/flake.nix @@ -3,10 +3,19 @@ nixpkgs.url = "nixpkgs/nixpkgs-unstable"; }; - outputs = inputs@{ nixpkgs, ... }: + outputs = { nixpkgs, ... }: let system = "x86_64-linux"; - pkgs = inputs.nixpkgs.legacyPackages.${system}; + + # Go-Jet binary in nixos repo is not built with fts5 sqlite enabled + overlays = [ + (final: prev: { + go-jet = prev.go-jet.overrideAttrs (oldAttrs: { + tags = oldAttrs.tags ++ [ "fts5" ]; + }); + }) + ]; + pkgs = import nixpkgs { inherit system overlays; }; goverter = pkgs.buildGoModule rec { name = "goverter"; version = "1.5.0"; diff --git a/schemas/migrations/20240808143100_create_images_table.sql b/schemas/migrations/20240808143100_create_images_table.sql index 1d927bb..01c4533 100644 --- a/schemas/migrations/20240808143100_create_images_table.sql +++ b/schemas/migrations/20240808143100_create_images_table.sql @@ -1,7 +1,7 @@ -- +goose Up -- +goose StatementBegin CREATE TABLE images( - id INTEGER PRIMARY KEY, + id INTEGER PRIMARY KEY AUTOINCREMENT, subreddit VARCHAR(255) NOT NULL COLLATE NOCASE, device VARCHAR(250) NOT NULL COLLATE NOCASE, post_title VARCHAR(255) NOT NULL, @@ -33,6 +33,17 @@ CREATE TABLE images( REFERENCES subreddits(cover_image_id) ); +CREATE VIRTUAL TABLE images_fts5 USING fts5( + post_title, + post_name, + post_author, + image_relative_path, + image_original_url, + thumbnail_relative_path, + content='images', + content_rowid='id' +); + CREATE INDEX idx_subreddit_images_blacklisted ON images(subreddit, blacklisted); CREATE INDEX idx_subreddit_device_images_blacklisted ON images(device, subreddit, blacklisted); CREATE INDEX idx_images_nsfw_blacklisted ON images(nsfw, blacklisted); @@ -54,9 +65,22 @@ CREATE TRIGGER images_update_subreddit_cover_on_delete AFTER DELETE ON images FO BEGIN UPDATE subreddits SET cover_image_id = NULL WHERE cover_image_id = old.id; END; + +CREATE TRIGGER images_update_fts_insert AFTER INSERT ON images FOR EACH ROW +BEGIN + INSERT INTO images_fts5(rowid, post_title, post_name, post_author, image_relative_path, image_original_url, thumbnail_relative_path) + VALUES (new.id, new.post_title, new.post_name, new.post_author, new.image_relative_path, new.image_original_url, new.thumbnail_relative_path); +END; + +CREATE TRIGGER images_update_fts_delete AFTER DELETE ON images FOR EACH ROW +BEGIN + DELETE FROM images_fts5 WHERE rowid = old.id; +END; + -- +goose StatementEnd -- +goose Down -- +goose StatementBegin +DROP TABLE images_fts5; DROP TABLE images; -- +goose StatementEnd