refactor: consolidate db package and flatten structure
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.31.1
|
||||||
|
|
||||||
package database
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package database
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package database
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.31.1
|
||||||
|
|
||||||
package database
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
@@ -43,9 +43,9 @@ type ContinueWatchingEntry struct {
|
|||||||
AnimeID int64 `json:"anime_id"`
|
AnimeID int64 `json:"anime_id"`
|
||||||
CurrentEpisode sql.NullInt64 `json:"current_episode"`
|
CurrentEpisode sql.NullInt64 `json:"current_episode"`
|
||||||
CurrentTimeSeconds float64 `json:"current_time_seconds"`
|
CurrentTimeSeconds float64 `json:"current_time_seconds"`
|
||||||
DurationSeconds sql.NullFloat64 `json:"duration_seconds"`
|
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
DurationSeconds sql.NullFloat64 `json:"duration_seconds"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type JikanCache struct {
|
type JikanCache struct {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.31.1
|
||||||
|
|
||||||
package database
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
// sqlc v1.31.1
|
// sqlc v1.31.1
|
||||||
// source: queries.sql
|
// source: queries.sql
|
||||||
|
|
||||||
package database
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -129,7 +129,7 @@ func (q *Queries) EnqueueAnimeFetchRetry(ctx context.Context, arg EnqueueAnimeFe
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getAnime = `-- name: GetAnime :one
|
const getAnime = `-- name: GetAnime :one
|
||||||
SELECT id, title_original, image_url, created_at, title_english, title_japanese, airing, status, relations_synced_at FROM anime WHERE id = ? LIMIT 1
|
SELECT id, title_original, image_url, created_at, title_english, title_japanese, airing, status, relations_synced_at, duration_seconds FROM anime WHERE id = ? LIMIT 1
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetAnime(ctx context.Context, id int64) (Anime, error) {
|
func (q *Queries) GetAnime(ctx context.Context, id int64) (Anime, error) {
|
||||||
@@ -145,6 +145,7 @@ func (q *Queries) GetAnime(ctx context.Context, id int64) (Anime, error) {
|
|||||||
&i.Airing,
|
&i.Airing,
|
||||||
&i.Status,
|
&i.Status,
|
||||||
&i.RelationsSyncedAt,
|
&i.RelationsSyncedAt,
|
||||||
|
&i.DurationSeconds,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -275,7 +276,7 @@ func (q *Queries) GetContinueWatchingEntries(ctx context.Context, userID string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getContinueWatchingEntry = `-- name: GetContinueWatchingEntry :one
|
const getContinueWatchingEntry = `-- name: GetContinueWatchingEntry :one
|
||||||
SELECT id, user_id, anime_id, current_episode, current_time_seconds, created_at, updated_at FROM continue_watching_entry
|
SELECT id, user_id, anime_id, current_episode, current_time_seconds, created_at, updated_at, duration_seconds FROM continue_watching_entry
|
||||||
WHERE user_id = ? AND anime_id = ? LIMIT 1
|
WHERE user_id = ? AND anime_id = ? LIMIT 1
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -295,6 +296,7 @@ func (q *Queries) GetContinueWatchingEntry(ctx context.Context, arg GetContinueW
|
|||||||
&i.CurrentTimeSeconds,
|
&i.CurrentTimeSeconds,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
|
&i.DurationSeconds,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -405,7 +407,7 @@ WITH RECURSIVE sequel_chain AS (
|
|||||||
WHERE r.relation_type = 'Sequel' AND sc.depth < 10
|
WHERE r.relation_type = 'Sequel' AND sc.depth < 10
|
||||||
)
|
)
|
||||||
SELECT DISTINCT
|
SELECT DISTINCT
|
||||||
related.id, related.title_original, related.image_url, related.created_at, related.title_english, related.title_japanese, related.airing, related.status, related.relations_synced_at,
|
related.id, related.title_original, related.image_url, related.created_at, related.title_english, related.title_japanese, related.airing, related.status, related.relations_synced_at, related.duration_seconds,
|
||||||
sc.root_title AS prequel_title
|
sc.root_title AS prequel_title
|
||||||
FROM sequel_chain sc
|
FROM sequel_chain sc
|
||||||
JOIN anime related ON sc.current_id = related.id
|
JOIN anime related ON sc.current_id = related.id
|
||||||
@@ -418,16 +420,17 @@ ORDER BY related.id DESC
|
|||||||
`
|
`
|
||||||
|
|
||||||
type GetUpcomingSeasonsRow struct {
|
type GetUpcomingSeasonsRow struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
TitleOriginal string `json:"title_original"`
|
TitleOriginal string `json:"title_original"`
|
||||||
ImageUrl string `json:"image_url"`
|
ImageUrl string `json:"image_url"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
TitleEnglish sql.NullString `json:"title_english"`
|
TitleEnglish sql.NullString `json:"title_english"`
|
||||||
TitleJapanese sql.NullString `json:"title_japanese"`
|
TitleJapanese sql.NullString `json:"title_japanese"`
|
||||||
Airing sql.NullBool `json:"airing"`
|
Airing sql.NullBool `json:"airing"`
|
||||||
Status sql.NullString `json:"status"`
|
Status sql.NullString `json:"status"`
|
||||||
RelationsSyncedAt sql.NullTime `json:"relations_synced_at"`
|
RelationsSyncedAt sql.NullTime `json:"relations_synced_at"`
|
||||||
PrequelTitle string `json:"prequel_title"`
|
DurationSeconds sql.NullFloat64 `json:"duration_seconds"`
|
||||||
|
PrequelTitle string `json:"prequel_title"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) GetUpcomingSeasons(ctx context.Context, userID string) ([]GetUpcomingSeasonsRow, error) {
|
func (q *Queries) GetUpcomingSeasons(ctx context.Context, userID string) ([]GetUpcomingSeasonsRow, error) {
|
||||||
@@ -449,6 +452,7 @@ func (q *Queries) GetUpcomingSeasons(ctx context.Context, userID string) ([]GetU
|
|||||||
&i.Airing,
|
&i.Airing,
|
||||||
&i.Status,
|
&i.Status,
|
||||||
&i.RelationsSyncedAt,
|
&i.RelationsSyncedAt,
|
||||||
|
&i.DurationSeconds,
|
||||||
&i.PrequelTitle,
|
&i.PrequelTitle,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -824,7 +828,7 @@ ON CONFLICT (user_id, anime_id) DO UPDATE SET
|
|||||||
current_time_seconds = excluded.current_time_seconds,
|
current_time_seconds = excluded.current_time_seconds,
|
||||||
duration_seconds = excluded.duration_seconds,
|
duration_seconds = excluded.duration_seconds,
|
||||||
updated_at = CURRENT_TIMESTAMP
|
updated_at = CURRENT_TIMESTAMP
|
||||||
RETURNING id, user_id, anime_id, current_episode, current_time_seconds, duration_seconds, created_at, updated_at
|
RETURNING id, user_id, anime_id, current_episode, current_time_seconds, created_at, updated_at, duration_seconds
|
||||||
`
|
`
|
||||||
|
|
||||||
type UpsertContinueWatchingEntryParams struct {
|
type UpsertContinueWatchingEntryParams struct {
|
||||||
@@ -852,9 +856,9 @@ func (q *Queries) UpsertContinueWatchingEntry(ctx context.Context, arg UpsertCon
|
|||||||
&i.AnimeID,
|
&i.AnimeID,
|
||||||
&i.CurrentEpisode,
|
&i.CurrentEpisode,
|
||||||
&i.CurrentTimeSeconds,
|
&i.CurrentTimeSeconds,
|
||||||
&i.DurationSeconds,
|
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
|
&i.DurationSeconds,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package sqlite
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"mal/internal/db"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Open(dbFile string) (*sql.DB, error) {
|
func Open(dbFile string) (*sql.DB, error) {
|
||||||
@@ -36,13 +35,13 @@ func GetMigrationsDir() (string, error) {
|
|||||||
return filepath.Join(wd, "migrations"), nil
|
return filepath.Join(wd, "migrations"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(db *sql.DB) (*database.Queries, error) {
|
func Init(db *sql.DB) (*Queries, error) {
|
||||||
migrationsDir, err := GetMigrationsDir()
|
migrationsDir, err := GetMigrationsDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := database.RunMigrations(db, migrationsDir); err != nil {
|
if err := RunMigrations(db, migrationsDir); err != nil {
|
||||||
return nil, fmt.Errorf("failed to run migrations: %w", err)
|
return nil, fmt.Errorf("failed to run migrations: %w", err)
|
||||||
}
|
}
|
||||||
return database.New(db), nil
|
return New(db), nil
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user