sqlite: added create images_fts5 table

This commit is contained in:
Tigor Hutasuhut 2024-08-16 14:38:57 +07:00
parent f98e5dc063
commit 7d10ad48b9
3 changed files with 37 additions and 3 deletions

View file

@ -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

View file

@ -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";

View file

@ -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