PoorBooru/bin/create-dbix-class-schemas

64 lines
1.2 KiB
Bash
Executable File

#!/bin/sh
err()
{
printf "%s: %s\n" "${0##*/}" "$*" >&2
exit 1
}
warn()
{
printf "%s: %s\n" "${0##*/}" "$*" >&2
}
check_required_programs()
{
_rc=0
for _prog; do
if ! command -v "$_prog" >/dev/null; then
_rc=1
warn "$_prog: not found"
fi
done
return $_rc;
}
set -eu
check_required_programs sqlite3 dbicdump || exit 1
sqlite3 "db/PoorBooru.db" <<'_SQL'
PRAGMA foreig_keys = ON;
CREATE TABLE IF NOT EXISTS media(
media_id INTEGER PRIMARY KEY,
content BLOB (10485760) NOT NULL,
filename TEXT (255) NOT NULL,
content_type TEXT (255),
upload_date INTEGER
);
CREATE TABLE IF NOT EXISTS tags(
tag_id INTEGER PRIMARY KEY,
name TEXT (255) NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS media_tags(
media_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
FOREIGN KEY (media_id) REFERENCES media (media_id) ON DELETE CASCADE,
FOREIGN KEY (tag_id) REFERENCES tags (tag_id) ON DELETE CASCADE,
PRIMARY KEY(media_id, tag_id)
);
CREATE VIEW IF NOT EXISTS tags_count_view AS SELECT
tags.tag_id AS tag_id,
tags.name AS name,
COUNT(media_tags.media_id) AS count
FROM tags
INNER JOIN media_tags USING (tag_id)
GROUP BY tag_id;
_SQL
dbicdump -o dump_directory=./lib PoorBooru::Schema dbi:SQLite:db/PoorBooru.db