sql: added devices table migration

This commit is contained in:
Tigor Hutasuhut 2024-04-09 22:37:26 +07:00
parent 7fd9d17f40
commit ba35d7329f
4 changed files with 51 additions and 2 deletions

View file

@ -0,0 +1,12 @@
package api
import "context"
type DownloadSubredditParams struct {
Countback int
NSFW bool
}
func (api *API) DownloadSubredditImages(ctx context.Context, subredditName string, params DownloadSubredditParams) error {
return nil
}

View file

@ -3,8 +3,9 @@
CREATE TABLE subreddits ( CREATE TABLE subreddits (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name VARCHAR(30) NOT NULL, name VARCHAR(30) NOT NULL,
subtype INT NOT NULL, subtype INT NOT NULL DEFAULT 0,
schedule VARCHAR(20) NOT NULL, schedule VARCHAR(20) NOT NULL DEFAULT '0 0 * * *',
countback INT NOT NULL DEFAULT 100,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
); );

View file

@ -0,0 +1,29 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE devices(
id INTEGER PRIMARY KEY,
name VARCHAR(255) NOT NULL,
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 INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
CREATE INDEX idx_devices_name ON devices(name);
CREATE TRIGGER update_devices_timestamp AFTER UPDATE ON devices FOR EACH ROW
BEGIN
UPDATE devices SET updated_at = CURRENT_TIMESTAMP WHERE id = old.id;
END;
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS devices;
-- +goose StatementEnd

View file

@ -3,6 +3,7 @@
CREATE TABLE images( CREATE TABLE images(
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
subreddit_id INTEGER NOT NULL, subreddit_id INTEGER NOT NULL,
device_id INTEGER NOT NULL,
title VARCHAR(255) NOT NULL, title VARCHAR(255) NOT NULL,
post_id VARCHAR(50) NOT NULL, post_id VARCHAR(50) NOT NULL,
poster VARCHAR(50) NOT NULL, poster VARCHAR(50) NOT NULL,
@ -10,11 +11,16 @@ CREATE TABLE images(
thumbnail_relative_path VARCHAR(255) NOT NULL DEFAULT '', thumbnail_relative_path VARCHAR(255) NOT NULL DEFAULT '',
image_original_url VARCHAR(255) NOT NULL, image_original_url VARCHAR(255) NOT NULL,
thumbnail_original_url VARCHAR(255) NOT NULL DEFAULT '', thumbnail_original_url VARCHAR(255) NOT NULL DEFAULT '',
nsfw INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT fk_subreddit_id CONSTRAINT fk_subreddit_id
FOREIGN KEY (subreddit_id) FOREIGN KEY (subreddit_id)
REFERENCES subreddits(id) REFERENCES subreddits(id)
ON DELETE CASCADE,
CONSTRAINT fk_devices_id
FOREIGN KEY (device_id)
REFERENCES devices(id)
ON DELETE CASCADE ON DELETE CASCADE
); );
@ -24,6 +30,7 @@ BEGIN
END; END;
CREATE INDEX idx_subreddit_id ON images(subreddit_id); CREATE INDEX idx_subreddit_id ON images(subreddit_id);
CREATE INDEX idx_nsfw ON images(nsfw);
-- +goose StatementEnd -- +goose StatementEnd
-- +goose Down -- +goose Down