mirror of
https://github.com/stashapp/stash.git
synced 2025-12-18 04:44:37 +03:00
Add Studio to movie and fix movie schema (#458)
* Add movie migration * Update server and UI code for type changes * Add studio to movies * Movie blobs to end * Document movie duration * Add filtering on movie studio
This commit is contained in:
@@ -4,8 +4,10 @@ import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/stashapp/stash/pkg/database"
|
||||
"github.com/stashapp/stash/pkg/logger"
|
||||
)
|
||||
|
||||
type migrateData struct {
|
||||
@@ -47,20 +49,44 @@ func doMigrateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, fmt.Sprintf("error: %s", err), 500)
|
||||
}
|
||||
|
||||
backupPath := r.Form.Get("backuppath")
|
||||
formBackupPath := r.Form.Get("backuppath")
|
||||
|
||||
// always backup so that we can roll back to the previous version if
|
||||
// migration fails
|
||||
backupPath := formBackupPath
|
||||
if formBackupPath == "" {
|
||||
backupPath = database.DatabaseBackupPath()
|
||||
}
|
||||
|
||||
// perform database backup
|
||||
if backupPath != "" {
|
||||
if err = database.Backup(backupPath); err != nil {
|
||||
http.Error(w, fmt.Sprintf("error backing up database: %s", err), 500)
|
||||
return
|
||||
}
|
||||
if err = database.Backup(backupPath); err != nil {
|
||||
http.Error(w, fmt.Sprintf("error backing up database: %s", err), 500)
|
||||
return
|
||||
}
|
||||
|
||||
err = database.RunMigrations()
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("error performing migration: %s", err), 500)
|
||||
errStr := fmt.Sprintf("error performing migration: %s", err)
|
||||
|
||||
// roll back to the backed up version
|
||||
restoreErr := database.RestoreFromBackup(backupPath)
|
||||
if restoreErr != nil {
|
||||
errStr = fmt.Sprintf("ERROR: unable to restore database from backup after migration failure: %s\n%s", restoreErr.Error(), errStr)
|
||||
} else {
|
||||
errStr = "An error occurred migrating the database to the latest schema version. The backup database file was automatically renamed to restore the database.\n" + errStr
|
||||
}
|
||||
|
||||
http.Error(w, errStr, 500)
|
||||
return
|
||||
}
|
||||
|
||||
// if no backup path was provided, then delete the created backup
|
||||
if formBackupPath == "" {
|
||||
err = os.Remove(backupPath)
|
||||
if err != nil {
|
||||
logger.Warnf("error removing unwanted database backup (%s): %s", backupPath, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/", 301)
|
||||
}
|
||||
|
||||
@@ -29,9 +29,10 @@ func (r *movieResolver) Aliases(ctx context.Context, obj *models.Movie) (*string
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (r *movieResolver) Duration(ctx context.Context, obj *models.Movie) (*string, error) {
|
||||
func (r *movieResolver) Duration(ctx context.Context, obj *models.Movie) (*int, error) {
|
||||
if obj.Duration.Valid {
|
||||
return &obj.Duration.String, nil
|
||||
rating := int(obj.Duration.Int64)
|
||||
return &rating, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
@@ -44,13 +45,23 @@ func (r *movieResolver) Date(ctx context.Context, obj *models.Movie) (*string, e
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (r *movieResolver) Rating(ctx context.Context, obj *models.Movie) (*string, error) {
|
||||
func (r *movieResolver) Rating(ctx context.Context, obj *models.Movie) (*int, error) {
|
||||
if obj.Rating.Valid {
|
||||
return &obj.Rating.String, nil
|
||||
rating := int(obj.Rating.Int64)
|
||||
return &rating, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (r *movieResolver) Studio(ctx context.Context, obj *models.Movie) (*models.Studio, error) {
|
||||
qb := models.NewStudioQueryBuilder()
|
||||
if obj.StudioID.Valid {
|
||||
return qb.Find(int(obj.StudioID.Int64), nil)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (r *movieResolver) Director(ctx context.Context, obj *models.Movie) (*string, error) {
|
||||
if obj.Director.Valid {
|
||||
return &obj.Director.String, nil
|
||||
|
||||
@@ -119,10 +119,17 @@ func (r *sceneResolver) Movies(ctx context.Context, obj *models.Scene) ([]*model
|
||||
}
|
||||
|
||||
sceneIdx := sm.SceneIndex
|
||||
ret = append(ret, &models.SceneMovie{
|
||||
Movie: movie,
|
||||
SceneIndex: &sceneIdx,
|
||||
})
|
||||
sceneMovie := &models.SceneMovie{
|
||||
Movie: movie,
|
||||
}
|
||||
|
||||
if sceneIdx.Valid {
|
||||
var idx int
|
||||
idx = int(sceneIdx.Int64)
|
||||
sceneMovie.SceneIndex = &idx
|
||||
}
|
||||
|
||||
ret = append(ret, sceneMovie)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
@@ -51,7 +51,8 @@ func (r *mutationResolver) MovieCreate(ctx context.Context, input models.MovieCr
|
||||
newMovie.Aliases = sql.NullString{String: *input.Aliases, Valid: true}
|
||||
}
|
||||
if input.Duration != nil {
|
||||
newMovie.Duration = sql.NullString{String: *input.Duration, Valid: true}
|
||||
duration := int64(*input.Duration)
|
||||
newMovie.Duration = sql.NullInt64{Int64: duration, Valid: true}
|
||||
}
|
||||
|
||||
if input.Date != nil {
|
||||
@@ -59,9 +60,10 @@ func (r *mutationResolver) MovieCreate(ctx context.Context, input models.MovieCr
|
||||
}
|
||||
|
||||
if input.Rating != nil {
|
||||
newMovie.Rating = sql.NullString{String: *input.Rating, Valid: true}
|
||||
}
|
||||
|
||||
rating := int64(*input.Rating)
|
||||
newMovie.Rating = sql.NullInt64{Int64: rating, Valid: true}
|
||||
}
|
||||
|
||||
if input.Director != nil {
|
||||
newMovie.Director = sql.NullString{String: *input.Director, Valid: true}
|
||||
}
|
||||
@@ -94,57 +96,71 @@ func (r *mutationResolver) MovieCreate(ctx context.Context, input models.MovieCr
|
||||
func (r *mutationResolver) MovieUpdate(ctx context.Context, input models.MovieUpdateInput) (*models.Movie, error) {
|
||||
// Populate movie from the input
|
||||
movieID, _ := strconv.Atoi(input.ID)
|
||||
updatedMovie := models.Movie{
|
||||
|
||||
updatedMovie := models.MoviePartial{
|
||||
ID: movieID,
|
||||
UpdatedAt: models.SQLiteTimestamp{Timestamp: time.Now()},
|
||||
UpdatedAt: &models.SQLiteTimestamp{Timestamp: time.Now()},
|
||||
}
|
||||
if input.FrontImage != nil {
|
||||
_, frontimageData, err := utils.ProcessBase64Image(*input.FrontImage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updatedMovie.FrontImage = frontimageData
|
||||
updatedMovie.FrontImage = &frontimageData
|
||||
}
|
||||
if input.BackImage != nil {
|
||||
_, backimageData, err := utils.ProcessBase64Image(*input.BackImage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updatedMovie.BackImage = backimageData
|
||||
updatedMovie.BackImage = &backimageData
|
||||
}
|
||||
|
||||
if input.Name != nil {
|
||||
// generate checksum from movie name rather than image
|
||||
checksum := utils.MD5FromString(*input.Name)
|
||||
updatedMovie.Name = sql.NullString{String: *input.Name, Valid: true}
|
||||
updatedMovie.Checksum = checksum
|
||||
updatedMovie.Name = &sql.NullString{String: *input.Name, Valid: true}
|
||||
updatedMovie.Checksum = &checksum
|
||||
}
|
||||
|
||||
if input.Aliases != nil {
|
||||
updatedMovie.Aliases = sql.NullString{String: *input.Aliases, Valid: true}
|
||||
updatedMovie.Aliases = &sql.NullString{String: *input.Aliases, Valid: true}
|
||||
}
|
||||
if input.Duration != nil {
|
||||
updatedMovie.Duration = sql.NullString{String: *input.Duration, Valid: true}
|
||||
duration := int64(*input.Duration)
|
||||
updatedMovie.Duration = &sql.NullInt64{Int64: duration, Valid: true}
|
||||
}
|
||||
|
||||
if input.Date != nil {
|
||||
updatedMovie.Date = models.SQLiteDate{String: *input.Date, Valid: true}
|
||||
updatedMovie.Date = &models.SQLiteDate{String: *input.Date, Valid: true}
|
||||
}
|
||||
|
||||
if input.Rating != nil {
|
||||
updatedMovie.Rating = sql.NullString{String: *input.Rating, Valid: true}
|
||||
rating := int64(*input.Rating)
|
||||
updatedMovie.Rating = &sql.NullInt64{Int64: rating, Valid: true}
|
||||
} else {
|
||||
// rating must be nullable
|
||||
updatedMovie.Rating = &sql.NullInt64{Valid: false}
|
||||
}
|
||||
|
||||
if input.StudioID != nil {
|
||||
studioID, _ := strconv.ParseInt(*input.StudioID, 10, 64)
|
||||
updatedMovie.StudioID = &sql.NullInt64{Int64: studioID, Valid: true}
|
||||
} else {
|
||||
// studio must be nullable
|
||||
updatedMovie.StudioID = &sql.NullInt64{Valid: false}
|
||||
}
|
||||
|
||||
if input.Director != nil {
|
||||
updatedMovie.Director = sql.NullString{String: *input.Director, Valid: true}
|
||||
updatedMovie.Director = &sql.NullString{String: *input.Director, Valid: true}
|
||||
}
|
||||
|
||||
if input.Synopsis != nil {
|
||||
updatedMovie.Synopsis = sql.NullString{String: *input.Synopsis, Valid: true}
|
||||
updatedMovie.Synopsis = &sql.NullString{String: *input.Synopsis, Valid: true}
|
||||
}
|
||||
|
||||
if input.URL != nil {
|
||||
updatedMovie.URL = sql.NullString{String: *input.URL, Valid: true}
|
||||
updatedMovie.URL = &sql.NullString{String: *input.URL, Valid: true}
|
||||
}
|
||||
|
||||
// Start the transaction and save the movie
|
||||
|
||||
@@ -153,16 +153,19 @@ func (r *mutationResolver) sceneUpdate(input models.SceneUpdateInput, tx *sqlx.T
|
||||
for _, movie := range input.Movies {
|
||||
|
||||
movieID, _ := strconv.Atoi(movie.MovieID)
|
||||
sceneIdx := ""
|
||||
if movie.SceneIndex != nil {
|
||||
sceneIdx = *movie.SceneIndex
|
||||
}
|
||||
|
||||
movieJoin := models.MoviesScenes{
|
||||
MovieID: movieID,
|
||||
SceneID: sceneID,
|
||||
SceneIndex: sceneIdx,
|
||||
MovieID: movieID,
|
||||
SceneID: sceneID,
|
||||
}
|
||||
|
||||
if movie.SceneIndex != nil {
|
||||
movieJoin.SceneIndex = sql.NullInt64{
|
||||
Int64: int64(*movie.SceneIndex),
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
|
||||
movieJoins = append(movieJoins, movieJoin)
|
||||
}
|
||||
if err := jqb.UpdateMoviesScenes(sceneID, movieJoins, tx); err != nil {
|
||||
|
||||
@@ -13,12 +13,12 @@ func (r *queryResolver) FindMovie(ctx context.Context, id string) (*models.Movie
|
||||
return qb.Find(idInt, nil)
|
||||
}
|
||||
|
||||
func (r *queryResolver) FindMovies(ctx context.Context, filter *models.FindFilterType) (*models.FindMoviesResultType, error) {
|
||||
func (r *queryResolver) FindMovies(ctx context.Context, movieFilter *models.MovieFilterType, filter *models.FindFilterType) (*models.FindMoviesResultType, error) {
|
||||
qb := models.NewMovieQueryBuilder()
|
||||
movies, total := qb.Query(filter)
|
||||
movies, total := qb.Query(movieFilter, filter)
|
||||
return &models.FindMoviesResultType{
|
||||
Count: total,
|
||||
Movies: movies,
|
||||
Count: total,
|
||||
Movies: movies,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -5,11 +5,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
sqlite3mig "github.com/golang-migrate/migrate/v4/database/sqlite3"
|
||||
"github.com/golang-migrate/migrate/v4/source"
|
||||
"github.com/jmoiron/sqlx"
|
||||
sqlite3 "github.com/mattn/go-sqlite3"
|
||||
@@ -19,14 +19,14 @@ import (
|
||||
|
||||
var DB *sqlx.DB
|
||||
var dbPath string
|
||||
var appSchemaVersion uint = 7
|
||||
var appSchemaVersion uint = 8
|
||||
var databaseSchemaVersion uint
|
||||
|
||||
const sqlite3Driver = "sqlite3_regexp"
|
||||
const sqlite3Driver = "sqlite3ex"
|
||||
|
||||
func init() {
|
||||
// register custom driver with regexp function
|
||||
registerRegexpFunc()
|
||||
registerCustomDriver()
|
||||
}
|
||||
|
||||
func Initialize(databasePath string) {
|
||||
@@ -55,14 +55,25 @@ func Initialize(databasePath string) {
|
||||
}
|
||||
}
|
||||
|
||||
const disableForeignKeys = false
|
||||
DB = open(databasePath, disableForeignKeys)
|
||||
}
|
||||
|
||||
func open(databasePath string, disableForeignKeys bool) *sqlx.DB {
|
||||
// https://github.com/mattn/go-sqlite3
|
||||
conn, err := sqlx.Open(sqlite3Driver, "file:"+databasePath+"?_fk=true")
|
||||
url := "file:" + databasePath
|
||||
if !disableForeignKeys {
|
||||
url += "?_fk=true"
|
||||
}
|
||||
|
||||
conn, err := sqlx.Open(sqlite3Driver, url)
|
||||
conn.SetMaxOpenConns(25)
|
||||
conn.SetMaxIdleConns(4)
|
||||
if err != nil {
|
||||
logger.Fatalf("db.Open(): %q\n", err)
|
||||
}
|
||||
DB = conn
|
||||
|
||||
return conn
|
||||
}
|
||||
|
||||
func Reset(databasePath string) error {
|
||||
@@ -97,6 +108,10 @@ func Backup(backupPath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func RestoreFromBackup(backupPath string) error {
|
||||
return os.Rename(backupPath, dbPath)
|
||||
}
|
||||
|
||||
// Migrate the database
|
||||
func NeedsMigration() bool {
|
||||
return databaseSchemaVersion != appSchemaVersion
|
||||
@@ -123,10 +138,21 @@ func getMigrate() (*migrate.Migrate, error) {
|
||||
|
||||
databasePath := utils.FixWindowsPath(dbPath)
|
||||
s, _ := WithInstance(packrSource)
|
||||
return migrate.NewWithSourceInstance(
|
||||
|
||||
const disableForeignKeys = true
|
||||
conn := open(databasePath, disableForeignKeys)
|
||||
|
||||
driver, err := sqlite3mig.WithInstance(conn.DB, &sqlite3mig.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// use sqlite3Driver so that migration has access to durationToTinyInt
|
||||
return migrate.NewWithInstance(
|
||||
"packr2",
|
||||
s,
|
||||
fmt.Sprintf("sqlite3://%s", "file:"+databasePath),
|
||||
databasePath,
|
||||
driver,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -153,6 +179,8 @@ func RunMigrations() error {
|
||||
if stepNumber != 0 {
|
||||
err = m.Steps(int(stepNumber))
|
||||
if err != nil {
|
||||
// migration failed
|
||||
m.Close()
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -164,15 +192,23 @@ func RunMigrations() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func registerRegexpFunc() {
|
||||
regexFn := func(re, s string) (bool, error) {
|
||||
return regexp.MatchString(re, s)
|
||||
}
|
||||
|
||||
func registerCustomDriver() {
|
||||
sql.Register(sqlite3Driver,
|
||||
&sqlite3.SQLiteDriver{
|
||||
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
||||
return conn.RegisterFunc("regexp", regexFn, true)
|
||||
funcs := map[string]interface{}{
|
||||
"regexp": regexFn,
|
||||
"durationToTinyInt": durationToTinyIntFn,
|
||||
}
|
||||
|
||||
for name, fn := range funcs {
|
||||
if err := conn.RegisterFunc(name, fn, true); err != nil {
|
||||
return fmt.Errorf("Error registering function %s: %s", name, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
37
pkg/database/functions.go
Normal file
37
pkg/database/functions.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func regexFn(re, s string) (bool, error) {
|
||||
return regexp.MatchString(re, s)
|
||||
}
|
||||
|
||||
func durationToTinyIntFn(str string) (int64, error) {
|
||||
splits := strings.Split(str, ":")
|
||||
|
||||
if len(splits) > 3 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
seconds := 0
|
||||
factor := 1
|
||||
for len(splits) > 0 {
|
||||
// pop the last split
|
||||
var thisSplit string
|
||||
thisSplit, splits = splits[len(splits)-1], splits[:len(splits)-1]
|
||||
|
||||
thisInt, err := strconv.Atoi(thisSplit)
|
||||
if err != nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
seconds += factor * thisInt
|
||||
factor *= 60
|
||||
}
|
||||
|
||||
return int64(seconds), nil
|
||||
}
|
||||
106
pkg/database/migrations/8_movie_fix.up.sql
Normal file
106
pkg/database/migrations/8_movie_fix.up.sql
Normal file
@@ -0,0 +1,106 @@
|
||||
ALTER TABLE `movies` rename to `_movies_old`;
|
||||
ALTER TABLE `movies_scenes` rename to `_movies_scenes_old`;
|
||||
|
||||
DROP INDEX IF EXISTS `movies_checksum_unique`;
|
||||
DROP INDEX IF EXISTS `index_movie_id_scene_index_unique`;
|
||||
DROP INDEX IF EXISTS `index_movies_scenes_on_movie_id`;
|
||||
DROP INDEX IF EXISTS `index_movies_scenes_on_scene_id`;
|
||||
|
||||
-- recreate the movies table with fixed column types and constraints
|
||||
CREATE TABLE `movies` (
|
||||
`id` integer not null primary key autoincrement,
|
||||
-- add not null
|
||||
`name` varchar(255) not null,
|
||||
`aliases` varchar(255),
|
||||
-- varchar(6) -> integer
|
||||
`duration` integer,
|
||||
`date` date,
|
||||
-- varchar(1) -> tinyint
|
||||
`rating` tinyint,
|
||||
`studio_id` integer,
|
||||
`director` varchar(255),
|
||||
`synopsis` text,
|
||||
`checksum` varchar(255) not null,
|
||||
`url` varchar(255),
|
||||
`created_at` datetime not null,
|
||||
`updated_at` datetime not null,
|
||||
`front_image` blob not null,
|
||||
`back_image` blob,
|
||||
foreign key(`studio_id`) references `studios`(`id`) on delete set null
|
||||
);
|
||||
CREATE TABLE `movies_scenes` (
|
||||
`movie_id` integer,
|
||||
`scene_id` integer,
|
||||
-- varchar(2) -> tinyint
|
||||
`scene_index` tinyint,
|
||||
foreign key(`movie_id`) references `movies`(`id`) on delete cascade,
|
||||
foreign key(`scene_id`) references `scenes`(`id`) on delete cascade
|
||||
);
|
||||
|
||||
-- add unique index on movie name
|
||||
CREATE UNIQUE INDEX `movies_name_unique` on `movies` (`name`);
|
||||
CREATE UNIQUE INDEX `movies_checksum_unique` on `movies` (`checksum`);
|
||||
-- remove unique index on movies_scenes
|
||||
CREATE INDEX `index_movies_scenes_on_movie_id` on `movies_scenes` (`movie_id`);
|
||||
CREATE INDEX `index_movies_scenes_on_scene_id` on `movies_scenes` (`scene_id`);
|
||||
CREATE INDEX `index_movies_on_studio_id` on `movies` (`studio_id`);
|
||||
|
||||
-- custom functions cannot accept NULL values, so massage the old data
|
||||
UPDATE `_movies_old` set `duration` = 0 WHERE `duration` IS NULL;
|
||||
|
||||
-- now populate from the old tables
|
||||
INSERT INTO `movies`
|
||||
(
|
||||
`id`,
|
||||
`name`,
|
||||
`aliases`,
|
||||
`duration`,
|
||||
`date`,
|
||||
`rating`,
|
||||
`director`,
|
||||
`synopsis`,
|
||||
`front_image`,
|
||||
`back_image`,
|
||||
`checksum`,
|
||||
`url`,
|
||||
`created_at`,
|
||||
`updated_at`
|
||||
)
|
||||
SELECT
|
||||
`id`,
|
||||
`name`,
|
||||
`aliases`,
|
||||
durationToTinyInt(`duration`),
|
||||
`date`,
|
||||
CAST(`rating` as tinyint),
|
||||
`director`,
|
||||
`synopsis`,
|
||||
`front_image`,
|
||||
`back_image`,
|
||||
`checksum`,
|
||||
`url`,
|
||||
`created_at`,
|
||||
`updated_at`
|
||||
FROM `_movies_old`
|
||||
-- ignore null named movies
|
||||
WHERE `name` is not null;
|
||||
|
||||
-- durationToTinyInt returns 0 if it cannot parse the string
|
||||
-- set these values to null instead
|
||||
UPDATE `movies` SET `duration` = NULL WHERE `duration` = 0;
|
||||
|
||||
INSERT INTO `movies_scenes`
|
||||
(
|
||||
`movie_id`,
|
||||
`scene_id`,
|
||||
`scene_index`
|
||||
)
|
||||
SELECT
|
||||
`movie_id`,
|
||||
`scene_id`,
|
||||
CAST(`scene_index` as tinyint)
|
||||
FROM `_movies_scenes_old`;
|
||||
|
||||
-- drop old tables
|
||||
DROP TABLE `_movies_scenes_old`;
|
||||
DROP TABLE `_movies_old`;
|
||||
@@ -9,18 +9,18 @@ import (
|
||||
)
|
||||
|
||||
type Movie struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Aliases string `json:"aliases,omitempty"`
|
||||
Duration string `json:"duration,omitempty"`
|
||||
Date string `json:"date,omitempty"`
|
||||
Rating string `json:"rating,omitempty"`
|
||||
Director string `json:"director,omitempty"`
|
||||
Synopsis string `json:"sypnopsis,omitempty"`
|
||||
FrontImage string `json:"front_image,omitempty"`
|
||||
BackImage string `json:"back_image,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
CreatedAt models.JSONTime `json:"created_at,omitempty"`
|
||||
UpdatedAt models.JSONTime `json:"updated_at,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Aliases string `json:"aliases,omitempty"`
|
||||
Duration int `json:"duration,omitempty"`
|
||||
Date string `json:"date,omitempty"`
|
||||
Rating int `json:"rating,omitempty"`
|
||||
Director string `json:"director,omitempty"`
|
||||
Synopsis string `json:"sypnopsis,omitempty"`
|
||||
FrontImage string `json:"front_image,omitempty"`
|
||||
BackImage string `json:"back_image,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
CreatedAt models.JSONTime `json:"created_at,omitempty"`
|
||||
UpdatedAt models.JSONTime `json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
func LoadMovieFile(filePath string) (*Movie, error) {
|
||||
|
||||
@@ -31,7 +31,7 @@ type SceneFile struct {
|
||||
|
||||
type SceneMovie struct {
|
||||
MovieName string `json:"movieName,omitempty"`
|
||||
SceneIndex string `json:"scene_index,omitempty"`
|
||||
SceneIndex int `json:"scene_index,omitempty"`
|
||||
}
|
||||
|
||||
type Scene struct {
|
||||
|
||||
@@ -146,7 +146,7 @@ func (t *ExportTask) ExportScenes(ctx context.Context) {
|
||||
if movie.Name.Valid {
|
||||
sceneMovieJSON := jsonschema.SceneMovie{
|
||||
MovieName: movie.Name.String,
|
||||
SceneIndex: sceneMovie.SceneIndex,
|
||||
SceneIndex: int(sceneMovie.SceneIndex.Int64),
|
||||
}
|
||||
newSceneJSON.Movies = append(newSceneJSON.Movies, sceneMovieJSON)
|
||||
}
|
||||
@@ -381,10 +381,10 @@ func (t *ExportTask) ExportMovies(ctx context.Context) {
|
||||
newMovieJSON.Date = utils.GetYMDFromDatabaseDate(movie.Date.String)
|
||||
}
|
||||
if movie.Rating.Valid {
|
||||
newMovieJSON.Rating = movie.Rating.String
|
||||
newMovieJSON.Rating = int(movie.Rating.Int64)
|
||||
}
|
||||
if movie.Duration.Valid {
|
||||
newMovieJSON.Duration = movie.Duration.String
|
||||
newMovieJSON.Duration = int(movie.Duration.Int64)
|
||||
}
|
||||
|
||||
if movie.Director.Valid {
|
||||
|
||||
@@ -250,8 +250,6 @@ func (t *ImportTask) ImportMovies(ctx context.Context) {
|
||||
Name: sql.NullString{String: movieJSON.Name, Valid: true},
|
||||
Aliases: sql.NullString{String: movieJSON.Aliases, Valid: true},
|
||||
Date: models.SQLiteDate{String: movieJSON.Date, Valid: true},
|
||||
Duration: sql.NullString{String: movieJSON.Duration, Valid: true},
|
||||
Rating: sql.NullString{String: movieJSON.Rating, Valid: true},
|
||||
Director: sql.NullString{String: movieJSON.Director, Valid: true},
|
||||
Synopsis: sql.NullString{String: movieJSON.Synopsis, Valid: true},
|
||||
URL: sql.NullString{String: movieJSON.URL, Valid: true},
|
||||
@@ -259,6 +257,13 @@ func (t *ImportTask) ImportMovies(ctx context.Context) {
|
||||
UpdatedAt: models.SQLiteTimestamp{Timestamp: t.getTimeFromJSONTime(movieJSON.UpdatedAt)},
|
||||
}
|
||||
|
||||
if movieJSON.Rating != 0 {
|
||||
newMovie.Rating = sql.NullInt64{Int64: int64(movieJSON.Rating), Valid: true}
|
||||
}
|
||||
if movieJSON.Duration != 0 {
|
||||
newMovie.Duration = sql.NullInt64{Int64: int64(movieJSON.Duration), Valid: true}
|
||||
}
|
||||
|
||||
_, err = qb.Create(newMovie, tx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
@@ -712,11 +717,19 @@ func (t *ImportTask) getMoviesScenes(input []jsonschema.SceneMovie, sceneID int,
|
||||
if movie == nil {
|
||||
logger.Warnf("[scenes] movie %s does not exist", inputMovie.MovieName)
|
||||
} else {
|
||||
movies = append(movies, models.MoviesScenes{
|
||||
MovieID: movie.ID,
|
||||
SceneID: sceneID,
|
||||
SceneIndex: inputMovie.SceneIndex,
|
||||
})
|
||||
toAdd := models.MoviesScenes{
|
||||
MovieID: movie.ID,
|
||||
SceneID: sceneID,
|
||||
}
|
||||
|
||||
if inputMovie.SceneIndex != 0 {
|
||||
toAdd.SceneIndex = sql.NullInt64{
|
||||
Int64: int64(inputMovie.SceneIndex),
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
|
||||
movies = append(movies, toAdd)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package models
|
||||
|
||||
import "database/sql"
|
||||
|
||||
type PerformersScenes struct {
|
||||
PerformerID int `db:"performer_id" json:"performer_id"`
|
||||
SceneID int `db:"scene_id" json:"scene_id"`
|
||||
}
|
||||
|
||||
type MoviesScenes struct {
|
||||
MovieID int `db:"movie_id" json:"movie_id"`
|
||||
SceneID int `db:"scene_id" json:"scene_id"`
|
||||
SceneIndex string `db:"scene_index" json:"scene_index"`
|
||||
MovieID int `db:"movie_id" json:"movie_id"`
|
||||
SceneID int `db:"scene_id" json:"scene_id"`
|
||||
SceneIndex sql.NullInt64 `db:"scene_index" json:"scene_index"`
|
||||
}
|
||||
|
||||
type ScenesTags struct {
|
||||
|
||||
@@ -5,20 +5,39 @@ import (
|
||||
)
|
||||
|
||||
type Movie struct {
|
||||
ID int `db:"id" json:"id"`
|
||||
FrontImage []byte `db:"front_image" json:"front_image"`
|
||||
BackImage []byte `db:"back_image" json:"back_image"`
|
||||
Checksum string `db:"checksum" json:"checksum"`
|
||||
Name sql.NullString `db:"name" json:"name"`
|
||||
Aliases sql.NullString `db:"aliases" json:"aliases"`
|
||||
Duration sql.NullString `db:"duration" json:"duration"`
|
||||
Date SQLiteDate `db:"date" json:"date"`
|
||||
Rating sql.NullString `db:"rating" json:"rating"`
|
||||
Director sql.NullString `db:"director" json:"director"`
|
||||
Synopsis sql.NullString `db:"synopsis" json:"synopsis"`
|
||||
URL sql.NullString `db:"url" json:"url"`
|
||||
CreatedAt SQLiteTimestamp `db:"created_at" json:"created_at"`
|
||||
UpdatedAt SQLiteTimestamp `db:"updated_at" json:"updated_at"`
|
||||
ID int `db:"id" json:"id"`
|
||||
FrontImage []byte `db:"front_image" json:"front_image"`
|
||||
BackImage []byte `db:"back_image" json:"back_image"`
|
||||
Checksum string `db:"checksum" json:"checksum"`
|
||||
Name sql.NullString `db:"name" json:"name"`
|
||||
Aliases sql.NullString `db:"aliases" json:"aliases"`
|
||||
Duration sql.NullInt64 `db:"duration" json:"duration"`
|
||||
Date SQLiteDate `db:"date" json:"date"`
|
||||
Rating sql.NullInt64 `db:"rating" json:"rating"`
|
||||
StudioID sql.NullInt64 `db:"studio_id,omitempty" json:"studio_id"`
|
||||
Director sql.NullString `db:"director" json:"director"`
|
||||
Synopsis sql.NullString `db:"synopsis" json:"synopsis"`
|
||||
URL sql.NullString `db:"url" json:"url"`
|
||||
CreatedAt SQLiteTimestamp `db:"created_at" json:"created_at"`
|
||||
UpdatedAt SQLiteTimestamp `db:"updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
type MoviePartial struct {
|
||||
ID int `db:"id" json:"id"`
|
||||
FrontImage *[]byte `db:"front_image" json:"front_image"`
|
||||
BackImage *[]byte `db:"back_image" json:"back_image"`
|
||||
Checksum *string `db:"checksum" json:"checksum"`
|
||||
Name *sql.NullString `db:"name" json:"name"`
|
||||
Aliases *sql.NullString `db:"aliases" json:"aliases"`
|
||||
Duration *sql.NullInt64 `db:"duration" json:"duration"`
|
||||
Date *SQLiteDate `db:"date" json:"date"`
|
||||
Rating *sql.NullInt64 `db:"rating" json:"rating"`
|
||||
StudioID *sql.NullInt64 `db:"studio_id,omitempty" json:"studio_id"`
|
||||
Director *sql.NullString `db:"director" json:"director"`
|
||||
Synopsis *sql.NullString `db:"synopsis" json:"synopsis"`
|
||||
URL *sql.NullString `db:"url" json:"url"`
|
||||
CreatedAt *SQLiteTimestamp `db:"created_at" json:"created_at"`
|
||||
UpdatedAt *SQLiteTimestamp `db:"updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
var DefaultMovieImage string = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4wgVBQsJl1CMZAAAASJJREFUeNrt3N0JwyAYhlEj3cj9R3Cm5rbkqtAP+qrnGaCYHPwJpLlaa++mmLpbAERAgAgIEAEBIiBABERAgAgIEAEBIiBABERAgAgIEAHZuVflj40x4i94zhk9vqsVvEq6AsQqMP1EjORx20OACAgQRRx7T+zzcFBxcjNDfoB4ntQqTm5Awo7MlqywZxcgYQ+RlqywJ3ozJAQCSBiEJSsQA0gYBpDAgAARECACAkRAgAgIEAERECACAmSjUv6eAOSB8m8YIGGzBUjYbAESBgMkbBkDEjZbgITBAClcxiqQvEoatreYIWEBASIgJ4Gkf11ntXH3nS9uxfGWfJ5J9hAgAgJEQAQEiIAAERAgAgJEQAQEiIAAERAgAgJEQAQEiL7qBuc6RKLHxr0CAAAAAElFTkSuQmCC"
|
||||
|
||||
@@ -161,7 +161,7 @@ func (qb *JoinsQueryBuilder) CreateMoviesScenes(newJoins []MoviesScenes, tx *sql
|
||||
// if the movie already exists on the scene. It returns true if scene
|
||||
// movie was added.
|
||||
|
||||
func (qb *JoinsQueryBuilder) AddMoviesScene(sceneID int, movieID int, sceneIdx string, tx *sqlx.Tx) (bool, error) {
|
||||
func (qb *JoinsQueryBuilder) AddMoviesScene(sceneID int, movieID int, sceneIdx *int, tx *sqlx.Tx) (bool, error) {
|
||||
ensureTx(tx)
|
||||
|
||||
existingMovies, err := qb.GetSceneMovies(sceneID, tx)
|
||||
@@ -178,9 +178,15 @@ func (qb *JoinsQueryBuilder) AddMoviesScene(sceneID int, movieID int, sceneIdx s
|
||||
}
|
||||
|
||||
movieJoin := MoviesScenes{
|
||||
MovieID: movieID,
|
||||
SceneID: sceneID,
|
||||
SceneIndex: sceneIdx,
|
||||
MovieID: movieID,
|
||||
SceneID: sceneID,
|
||||
}
|
||||
|
||||
if sceneIdx != nil {
|
||||
movieJoin.SceneIndex = sql.NullInt64{
|
||||
Int64: int64(*sceneIdx),
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
movieJoins := append(existingMovies, movieJoin)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strconv"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/stashapp/stash/pkg/database"
|
||||
@@ -16,8 +17,8 @@ func NewMovieQueryBuilder() MovieQueryBuilder {
|
||||
func (qb *MovieQueryBuilder) Create(newMovie Movie, tx *sqlx.Tx) (*Movie, error) {
|
||||
ensureTx(tx)
|
||||
result, err := tx.NamedExec(
|
||||
`INSERT INTO movies (front_image, back_image, checksum, name, aliases, duration, date, rating, director, synopsis, url, created_at, updated_at)
|
||||
VALUES (:front_image, :back_image, :checksum, :name, :aliases, :duration, :date, :rating, :director, :synopsis, :url, :created_at, :updated_at)
|
||||
`INSERT INTO movies (front_image, back_image, checksum, name, aliases, duration, date, rating, studio_id, director, synopsis, url, created_at, updated_at)
|
||||
VALUES (:front_image, :back_image, :checksum, :name, :aliases, :duration, :date, :rating, :studio_id, :director, :synopsis, :url, :created_at, :updated_at)
|
||||
`,
|
||||
newMovie,
|
||||
)
|
||||
@@ -35,20 +36,17 @@ func (qb *MovieQueryBuilder) Create(newMovie Movie, tx *sqlx.Tx) (*Movie, error)
|
||||
return &newMovie, nil
|
||||
}
|
||||
|
||||
func (qb *MovieQueryBuilder) Update(updatedMovie Movie, tx *sqlx.Tx) (*Movie, error) {
|
||||
func (qb *MovieQueryBuilder) Update(updatedMovie MoviePartial, tx *sqlx.Tx) (*Movie, error) {
|
||||
ensureTx(tx)
|
||||
_, err := tx.NamedExec(
|
||||
`UPDATE movies SET `+SQLGenKeys(updatedMovie)+` WHERE movies.id = :id`,
|
||||
`UPDATE movies SET `+SQLGenKeysPartial(updatedMovie)+` WHERE movies.id = :id`,
|
||||
updatedMovie,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := tx.Get(&updatedMovie, `SELECT * FROM movies WHERE id = ? LIMIT 1`, updatedMovie.ID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &updatedMovie, nil
|
||||
return qb.Find(updatedMovie.ID, tx)
|
||||
}
|
||||
|
||||
func (qb *MovieQueryBuilder) Destroy(id string, tx *sqlx.Tx) error {
|
||||
@@ -113,10 +111,13 @@ func (qb *MovieQueryBuilder) AllSlim() ([]*Movie, error) {
|
||||
return qb.queryMovies("SELECT movies.id, movies.name FROM movies "+qb.getMovieSort(nil), nil, nil)
|
||||
}
|
||||
|
||||
func (qb *MovieQueryBuilder) Query(findFilter *FindFilterType) ([]*Movie, int) {
|
||||
func (qb *MovieQueryBuilder) Query(movieFilter *MovieFilterType, findFilter *FindFilterType) ([]*Movie, int) {
|
||||
if findFilter == nil {
|
||||
findFilter = &FindFilterType{}
|
||||
}
|
||||
if movieFilter == nil {
|
||||
movieFilter = &MovieFilterType{}
|
||||
}
|
||||
|
||||
var whereClauses []string
|
||||
var havingClauses []string
|
||||
@@ -125,6 +126,7 @@ func (qb *MovieQueryBuilder) Query(findFilter *FindFilterType) ([]*Movie, int) {
|
||||
body += `
|
||||
left join movies_scenes as scenes_join on scenes_join.movie_id = movies.id
|
||||
left join scenes on scenes_join.scene_id = scenes.id
|
||||
left join studios as studio on studio.id = movies.studio_id
|
||||
`
|
||||
|
||||
if q := findFilter.Q; q != nil && *q != "" {
|
||||
@@ -134,6 +136,16 @@ func (qb *MovieQueryBuilder) Query(findFilter *FindFilterType) ([]*Movie, int) {
|
||||
args = append(args, thisArgs...)
|
||||
}
|
||||
|
||||
if studiosFilter := movieFilter.Studios; studiosFilter != nil && len(studiosFilter.Value) > 0 {
|
||||
for _, studioID := range studiosFilter.Value {
|
||||
args = append(args, studioID)
|
||||
}
|
||||
|
||||
whereClause, havingClause := qb.getMultiCriterionClause("studio", "", "studio_id", studiosFilter)
|
||||
whereClauses = appendClause(whereClauses, whereClause)
|
||||
havingClauses = appendClause(havingClauses, havingClause)
|
||||
}
|
||||
|
||||
sortAndPagination := qb.getMovieSort(findFilter) + getPagination(findFilter)
|
||||
idsResult, countResult := executeFindQuery("movies", body, args, sortAndPagination, whereClauses, havingClauses)
|
||||
|
||||
@@ -146,6 +158,29 @@ func (qb *MovieQueryBuilder) Query(findFilter *FindFilterType) ([]*Movie, int) {
|
||||
return movies, countResult
|
||||
}
|
||||
|
||||
// returns where clause and having clause
|
||||
func (qb *MovieQueryBuilder) getMultiCriterionClause(table string, joinTable string, joinTableField string, criterion *MultiCriterionInput) (string, string) {
|
||||
whereClause := ""
|
||||
havingClause := ""
|
||||
if criterion.Modifier == CriterionModifierIncludes {
|
||||
// includes any of the provided ids
|
||||
whereClause = table + ".id IN " + getInBinding(len(criterion.Value))
|
||||
} else if criterion.Modifier == CriterionModifierIncludesAll {
|
||||
// includes all of the provided ids
|
||||
whereClause = table + ".id IN " + getInBinding(len(criterion.Value))
|
||||
havingClause = "count(distinct " + table + ".id) IS " + strconv.Itoa(len(criterion.Value))
|
||||
} else if criterion.Modifier == CriterionModifierExcludes {
|
||||
// excludes all of the provided ids
|
||||
if joinTable != "" {
|
||||
whereClause = "not exists (select " + joinTable + ".movie_id from " + joinTable + " where " + joinTable + ".movie_id = movies.id and " + joinTable + "." + joinTableField + " in " + getInBinding(len(criterion.Value)) + ")"
|
||||
} else {
|
||||
whereClause = "not exists (select m.id from movies as m where m.id = movies.id and m." + joinTableField + " in " + getInBinding(len(criterion.Value)) + ")"
|
||||
}
|
||||
}
|
||||
|
||||
return whereClause, havingClause
|
||||
}
|
||||
|
||||
func (qb *MovieQueryBuilder) getMovieSort(findFilter *FindFilterType) string {
|
||||
var sort string
|
||||
var direction string
|
||||
|
||||
Reference in New Issue
Block a user