db: rework schema and infrastructure
This commit is contained in:
parent
fbcaea8bfc
commit
13a6d287af
7 changed files with 86 additions and 85 deletions
|
@ -1,62 +0,0 @@
|
|||
#!/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)
|
||||
);
|
||||
|
||||
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
|
22
bin/migrate.pl
Normal file
22
bin/migrate.pl
Normal file
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env perl
|
||||
use v5.36;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use DBIx::Class::Schema::Loader qw(make_schema_at);
|
||||
use DBIx::Migration;
|
||||
use Mojo::File qw(curfile);
|
||||
|
||||
my $parent = curfile->dirname;
|
||||
my $dsn = "dbi:SQLite:" . $parent->sibling("db", "PoorBooru.db")->to_string;
|
||||
|
||||
DBIx::Migration->new({
|
||||
dsn => $dsn,
|
||||
dir => $parent->sibling("migrations")->to_string,
|
||||
})->migrate or die "couldn't run migration";
|
||||
|
||||
make_schema_at(
|
||||
"PoorBooru::Schema",
|
||||
{ dump_directory => $parent->sibling("lib")->to_string },
|
||||
[ $dsn ],
|
||||
);
|
Reference in a new issue