48 lines
1.6 KiB
SQL
48 lines
1.6 KiB
SQL
-- +goose Up
|
|
-- +goose StatementBegin
|
|
CREATE TABLE devices (
|
|
slug VARCHAR(255) PRIMARY KEY COLLATE NOCASE,
|
|
disabled TINYINT NOT NULL DEFAULT 0,
|
|
name TEXT NOT NULL COLLATE NOCASE,
|
|
resolution_x DOUBLE NOT NULL,
|
|
resolution_y DOUBLE NOT NULL,
|
|
aspect_ratio_tolerance DOUBLE NOT NULL DEFAULT 0.2,
|
|
min_x INTEGER NOT NULL DEFAULT 0,
|
|
min_y INTEGER NOT NULL DEFAULT 0,
|
|
max_x INTEGER NOT NULL DEFAULT 0,
|
|
max_y INTEGER NOT NULL DEFAULT 0,
|
|
nsfw TINYINT NOT NULL DEFAULT 0,
|
|
single_folder_mode TINYINT NOT NULL DEFAULT 0,
|
|
created_at BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
|
updated_at BIGINT NOT NULL DEFAULT (strftime('%s', 'now'))
|
|
);
|
|
|
|
INSERT INTO metrics(name) VALUES ('devices.count');
|
|
|
|
CREATE UNIQUE INDEX idx_devices_unique_slug ON devices(slug);
|
|
|
|
CREATE TRIGGER devices_update_timestamp AFTER UPDATE ON devices FOR EACH ROW
|
|
BEGIN
|
|
UPDATE devices SET updated_at = strftime('%s', 'now') WHERE slug = old.slug;
|
|
END;
|
|
|
|
CREATE TRIGGER devices_insert_create_total_image_metrics AFTER INSERT on devices FOR EACH ROW
|
|
BEGIN
|
|
INSERT INTO metrics (name, value)
|
|
VALUES (CONCAT('devices.', NEW.slug, '.total_images'), 0);
|
|
UPDATE metrics SET value = value + 1 WHERE name = 'devices.count';
|
|
END;
|
|
|
|
CREATE TRIGGER devices_delete_total_image_metrics AFTER DELETE on devices FOR EACH ROW
|
|
BEGIN
|
|
DELETE FROM metrics WHERE name = CONCAT('devices.', OLD.slug, '.total_images');
|
|
UPDATE metrics SET value = value - 1 WHERE name = 'devices.count';
|
|
END;
|
|
-- +goose StatementEnd
|
|
|
|
-- +goose Down
|
|
-- +goose StatementBegin
|
|
DROP TABLE devices;
|
|
DELETE FROM metrics WHERE name = 'devices.count';
|
|
-- +goose StatementEnd
|