Hardcode gocoder db schema

This commit is contained in:
2025-11-02 18:21:26 +01:00
parent 5827cc32e8
commit f1ddc7e7b9
5 changed files with 15 additions and 31 deletions

View File

@@ -34,10 +34,6 @@ POSTGRES_SERVER=
POSTGRES_PORT=5432
# can also be "require" ("prefer" is not supported)
POSTGRES_SSLMODE="disable"
# Default is gocoder, you can specify "disabled" to use the default search_path of the user.
# If this is not "disabled", the schema will be created (if it does not exists) and
# the search_path of the user will be ignored (only the schema specified will be used).
POSTGRES_SCHEMA=gocoder
# Storage backend
# There are two currently supported backends: local filesystem and s3.

View File

@@ -27,7 +27,7 @@ func (s *MetadataService) ExtractSubs(ctx context.Context, info *MediaInfo) (any
log.Printf("Couldn't extract subs: %v", err)
return set(nil, err)
}
_, err = s.database.Exec(`update info set ver_extract = $2 where sha = $1`, info.Sha, ExtractVersion)
_, err = s.database.Exec(`update gocoder.info set ver_extract = $2 where sha = $1`, info.Sha, ExtractVersion)
return set(nil, err)
}

View File

@@ -134,10 +134,10 @@ func (s *MetadataService) GetKeyframes(info *MediaInfo, isVideo bool, idx uint32
var table string
var err error
if isVideo {
table = "videos"
table = "gocoder.videos"
err = getVideoKeyframes(info.Path, idx, kf)
} else {
table = "audios"
table = "gocoder.audios"
err = getAudioKeyframes(info, idx, kf)
}
@@ -154,7 +154,7 @@ func (s *MetadataService) GetKeyframes(info *MediaInfo, isVideo bool, idx uint32
idx,
pq.Array(kf.Keyframes),
)
tx.Exec(`update info set ver_keyframes = $2 where sha = $1`, info.Sha, KeyframeVersion)
tx.Exec(`update gocoder.info set ver_keyframes = $2 where sha = $1`, info.Sha, KeyframeVersion)
err = tx.Commit()
if err != nil {
log.Printf("Couldn't store keyframes on database: %v", err)

View File

@@ -78,8 +78,6 @@ func (s *MetadataService) Close() error {
}
func (s *MetadataService) setupDb() (*sql.DB, error) {
schema := GetEnvOr("POSTGRES_SCHEMA", "gocoder")
connectionString := os.Getenv("POSTGRES_URL")
if connectionString == "" {
connectionString = fmt.Sprintf(
@@ -91,9 +89,6 @@ func (s *MetadataService) setupDb() (*sql.DB, error) {
url.QueryEscape(os.Getenv("POSTGRES_DB")),
url.QueryEscape(GetEnvOr("POSTGRES_SSLMODE", "disable")),
)
if schema != "disabled" {
connectionString = fmt.Sprintf("%s&search_path=%s", connectionString, url.QueryEscape(schema))
}
}
db, err := sql.Open("postgres", connectionString)
@@ -102,13 +97,6 @@ func (s *MetadataService) setupDb() (*sql.DB, error) {
return nil, err
}
if schema != "disabled" {
_, err = db.Exec(fmt.Sprintf("create schema if not exists %s", schema))
if err != nil {
return nil, err
}
}
driver, err := postgres.WithInstance(db, &postgres.Config{})
if err != nil {
return nil, err
@@ -169,9 +157,9 @@ func (s *MetadataService) GetMetadata(ctx context.Context, path string, sha stri
if err != nil {
return nil, err
}
tx.Exec(`update videos set keyframes = null where sha = $1`, sha)
tx.Exec(`update audios set keyframes = null where sha = $1`, sha)
tx.Exec(`update info set ver_keyframes = 0 where sha = $1`, sha)
tx.Exec(`update gocoder.videos set keyframes = null where sha = $1`, sha)
tx.Exec(`update gocoder.audios set keyframes = null where sha = $1`, sha)
tx.Exec(`update gocoder.info set ver_keyframes = 0 where sha = $1`, sha)
err = tx.Commit()
if err != nil {
fmt.Printf("error deleting old keyframes from database: %v", err)
@@ -187,7 +175,7 @@ func (s *MetadataService) getMetadata(path string, sha string) (*MediaInfo, erro
err := s.database.QueryRow(
`select i.sha, i.path, i.extension, i.mime_codec, i.size, i.duration, i.container,
i.fonts, i.ver_info, i.ver_extract, i.ver_thumbs, i.ver_keyframes
from info as i where i.sha=$1`,
from gocoder.info as i where i.sha=$1`,
sha,
).Scan(
&ret.Sha, &ret.Path, &ret.Extension, &ret.MimeCodec, &ret.Size, &ret.Duration, &ret.Container,
@@ -307,9 +295,9 @@ func (s *MetadataService) storeFreshMetadata(path string, sha string) (*MediaInf
// it needs to be a delete instead of a on conflict do update because we want to trigger delete casquade for
// videos/audios & co.
tx.Exec(`delete from info where path = $1`, path)
tx.Exec(`delete from gocoder.info where path = $1`, path)
tx.Exec(`
insert into info(sha, path, extension, mime_codec, size, duration, container,
insert into gocoder.info(sha, path, extension, mime_codec, size, duration, container,
fonts, ver_info, ver_extract, ver_thumbs, ver_keyframes)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
`,
@@ -319,7 +307,7 @@ func (s *MetadataService) storeFreshMetadata(path string, sha string) (*MediaInf
)
for _, v := range ret.Videos {
tx.Exec(`
insert into videos(sha, idx, title, language, codec, mime_codec, width, height, is_default, bitrate)
insert into gocoder.videos(sha, idx, title, language, codec, mime_codec, width, height, is_default, bitrate)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
on conflict (sha, idx) do update set
sha = excluded.sha,
@@ -338,7 +326,7 @@ func (s *MetadataService) storeFreshMetadata(path string, sha string) (*MediaInf
}
for _, a := range ret.Audios {
tx.Exec(`
insert into audios(sha, idx, title, language, codec, mime_codec, is_default, bitrate)
insert into gocoder.audios(sha, idx, title, language, codec, mime_codec, is_default, bitrate)
values ($1, $2, $3, $4, $5, $6, $7, $8)
on conflict (sha, idx) do update set
sha = excluded.sha,
@@ -355,7 +343,7 @@ func (s *MetadataService) storeFreshMetadata(path string, sha string) (*MediaInf
}
for _, s := range ret.Subtitles {
tx.Exec(`
insert into subtitles(sha, idx, title, language, codec, mime_codec, extension, is_default, is_forced, is_hearing_impaired)
insert into gocoder.subtitles(sha, idx, title, language, codec, mime_codec, extension, is_default, is_forced, is_hearing_impaired)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
on conflict (sha, idx) do update set
sha = excluded.sha,
@@ -374,7 +362,7 @@ func (s *MetadataService) storeFreshMetadata(path string, sha string) (*MediaInf
}
for _, c := range ret.Chapters {
tx.Exec(`
insert into chapters(sha, start_time, end_time, name, type)
insert into gocoder.chapters(sha, start_time, end_time, name, type)
values ($1, $2, $3, $4, $5)
on conflict (sha, start_time) do update set
sha = excluded.sha,

View File

@@ -77,7 +77,7 @@ func (s *MetadataService) ExtractThumbs(ctx context.Context, path string, sha st
if err != nil {
return set(nil, err)
}
_, err = s.database.Exec(`update info set ver_thumbs = $2 where sha = $1`, sha, ThumbsVersion)
_, err = s.database.Exec(`update gocoder.info set ver_thumbs = $2 where sha = $1`, sha, ThumbsVersion)
return set(nil, err)
}