SQLite: Rename dialect constant to SQLite3 for consistency #319 #1791

This commit is contained in:
Michael Mayer 2021-12-09 07:47:23 +01:00
parent 9f3ebf671f
commit 3ae3fe29bc
15 changed files with 34 additions and 32 deletions

View file

@ -129,7 +129,7 @@ func backupAction(ctx *cli.Context) error {
"-p"+conf.DatabasePassword(),
conf.DatabaseName(),
)
case config.SQLite:
case config.SQLite3:
cmd = exec.Command(
conf.SqliteBin(),
conf.DatabaseDsn(),

View file

@ -148,7 +148,7 @@ func restoreAction(ctx *cli.Context) error {
"-f",
conf.DatabaseName(),
)
case config.SQLite:
case config.SQLite3:
log.Infoln("dropping existing tables")
tables.Drop(conf.Db())
cmd = exec.Command(

View file

@ -475,8 +475,8 @@ func (c *Config) Workers() int {
cores = cpuid.CPU.PhysicalCores
}
// Limit number of workers when using SQLite to avoid database locking issues.
if c.DatabaseDriver() == SQLite && (cores >= 8 && c.options.Workers <= 0 || c.options.Workers > 4) {
// Limit number of workers when using SQLite3 to avoid database locking issues.
if c.DatabaseDriver() == SQLite3 && (cores >= 8 && c.options.Workers <= 0 || c.options.Workers > 4) {
return 4
}

View file

@ -29,15 +29,15 @@ func (c *Config) DatabaseDriver() string {
switch strings.ToLower(c.options.DatabaseDriver) {
case MySQL, MariaDB:
c.options.DatabaseDriver = MySQL
case SQLite, "sqlite", "sqllite", "test", "file", "":
c.options.DatabaseDriver = SQLite
case SQLite3, "sqlite", "sqllite", "test", "file", "":
c.options.DatabaseDriver = SQLite3
case "tidb":
log.Warnf("config: database driver 'tidb' is deprecated, using sqlite")
c.options.DatabaseDriver = SQLite
c.options.DatabaseDriver = SQLite3
c.options.DatabaseDsn = ""
default:
log.Warnf("config: unsupported database driver %s, using sqlite", c.options.DatabaseDriver)
c.options.DatabaseDriver = SQLite
c.options.DatabaseDriver = SQLite3
c.options.DatabaseDsn = ""
}
@ -65,7 +65,7 @@ func (c *Config) DatabaseDsn() string {
c.DatabaseHost(),
c.DatabasePort(),
)
case SQLite:
case SQLite3:
return filepath.Join(c.StoragePath(), "index.db")
default:
log.Errorf("config: empty database dsn")
@ -227,7 +227,7 @@ func (c *Config) SetDbOptions() {
c.Db().Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci")
case Postgres:
// Ignore for now.
case SQLite:
case SQLite3:
// Not required as unicode is default.
}
}

View file

@ -10,7 +10,7 @@ func TestConfig_DatabaseDriver(t *testing.T) {
c := NewConfig(CliTestContext())
driver := c.DatabaseDriver()
assert.Equal(t, SQLite, driver)
assert.Equal(t, SQLite3, driver)
}
func TestConfig_ParseDatabaseDsn(t *testing.T) {
@ -73,7 +73,7 @@ func TestConfig_DatabaseDsn(t *testing.T) {
c := NewConfig(CliTestContext())
dsn := c.DatabaseDriver()
assert.Equal(t, SQLite, dsn)
assert.Equal(t, SQLite3, dsn)
c.options.DatabaseDsn = ""
c.options.DatabaseDriver = "MariaDB"
assert.Equal(t, "photoprism:@tcp(localhost)/photoprism?charset=utf8mb4,utf8&collation=utf8mb4_unicode_ci&parseTime=true", c.DatabaseDsn())

View file

@ -8,16 +8,18 @@ import (
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
"github.com/photoprism/photoprism/pkg/fs"
)
// Database drivers (sql dialects).
const (
MySQL = "mysql"
MariaDB = "mariadb"
SQLite = "sqlite3"
SQLite3 = "sqlite3"
Postgres = "postgres" // TODO: Requires GORM 2.0 for generic column data types
)

View file

@ -48,7 +48,7 @@ func NewTestOptions() *Options {
// dbDsn = "photoprism:photoprism@tcp(mariadb:4001)/photoprism?parseTime=true",
if dbDriver == "test" || dbDriver == "sqlite" || dbDriver == "" || dbDsn == "" {
dbDriver = SQLite
dbDriver = SQLite3
dbDsn = ".test.db"
}
@ -94,7 +94,7 @@ func NewTestOptionsError() *Options {
OriginalsPath: testDataPath + "/originals",
ImportPath: testDataPath + "/import",
TempPath: testDataPath + "/temp",
DatabaseDriver: SQLite,
DatabaseDriver: SQLite3,
DatabaseDsn: ".test-error.db",
}

View file

@ -96,7 +96,7 @@ func UpdateSubjectCounts() (err error) {
SET subjects.file_count = CASE WHEN b.subj_files IS NULL THEN 0 ELSE b.subj_files END,
subjects.photo_count = CASE WHEN b.subj_photos IS NULL THEN 0 ELSE b.subj_photos END
WHERE ?`, gorm.Expr(subjTable), gorm.Expr(filesTable), gorm.Expr(markerTable), condition)
case SQLite:
case SQLite3:
// Update files count.
res = Db().Table(subjTable).
UpdateColumn("file_count", gorm.Expr("(SELECT COUNT(DISTINCT f.id) FROM files f "+
@ -147,7 +147,7 @@ func UpdateLabelCounts() (err error) {
) p2 GROUP BY p2.label_id
) b ON b.label_id = labels.id
SET photo_count = CASE WHEN b.label_photos IS NULL THEN 0 ELSE b.label_photos END`)
} else if IsDialect(SQLite) {
} else if IsDialect(SQLite3) {
res = Db().
Table("labels").
UpdateColumn("photo_count",

View file

@ -12,8 +12,8 @@ import (
// Database drivers (sql dialects).
const (
MySQL = "mysql"
SQLite = "sqlite3"
MySQL = "mysql"
SQLite3 = "sqlite3"
)
var dbProvider DbProvider

View file

@ -101,7 +101,7 @@ func (m *Photo) EstimateLocation(force bool) {
Where("taken_src <> '' AND taken_at BETWEEN CAST(? AS DATETIME) AND CAST(? AS DATETIME)", rangeMin, rangeMax).
Order(gorm.Expr("ABS(TIMESTAMPDIFF(SECOND, taken_at, ?))", m.TakenAt)).Limit(2).
Preload("Place").Find(&mostRecent).Error
case SQLite:
case SQLite3:
err = UnscopedDb().
Where("photo_lat <> 0 AND photo_lng <> 0").
Where("place_src <> '' AND place_src <> ? AND place_id IS NOT NULL AND place_id <> '' AND place_id <> 'zz'", SrcEstimate).

View file

@ -109,7 +109,7 @@ func (m *Photo) Merge(mergeMeta, mergeUuid bool) (original Photo, merged Photos,
logResult(UnscopedDb().Exec("UPDATE IGNORE `photos_keywords` SET `photo_id` = ? WHERE photo_id = ?", original.ID, merge.ID))
logResult(UnscopedDb().Exec("UPDATE IGNORE `photos_labels` SET `photo_id` = ? WHERE photo_id = ?", original.ID, merge.ID))
logResult(UnscopedDb().Exec("UPDATE IGNORE `photos_albums` SET `photo_uid` = ? WHERE photo_uid = ?", original.PhotoUID, merge.PhotoUID))
case SQLite:
case SQLite3:
logResult(UnscopedDb().Exec("UPDATE OR IGNORE `photos_keywords` SET `photo_id` = ? WHERE photo_id = ?", original.ID, merge.ID))
logResult(UnscopedDb().Exec("UPDATE OR IGNORE `photos_labels` SET `photo_id` = ? WHERE photo_id = ?", original.ID, merge.ID))
logResult(UnscopedDb().Exec("UPDATE OR IGNORE `photos_albums` SET `photo_uid` = ? WHERE photo_uid = ?", original.PhotoUID, merge.PhotoUID))

View file

@ -33,7 +33,7 @@ func UpdateAlbumDefaultCovers() (err error) {
GROUP BY pa.album_uid) p2 WHERE p2.photo_id = f.photo_id AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
) b ON b.album_uid = albums.album_uid
SET thumb = b.file_hash WHERE ?`, condition)
case SQLite:
case SQLite3:
res = Db().Table(entity.Album{}.TableName()).
UpdateColumn("thumb", gorm.Expr(`(
SELECT f.file_hash FROM files f
@ -80,7 +80,7 @@ func UpdateAlbumFolderCovers() (err error) {
GROUP BY p.photo_path) p2 WHERE p2.photo_id = f.photo_id AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
) b ON b.photo_path = albums.album_path
SET thumb = b.file_hash WHERE ?`, condition)
case SQLite:
case SQLite3:
res = Db().Table(entity.Album{}.TableName()).UpdateColumn("thumb", gorm.Expr(`(
SELECT f.file_hash FROM files f,(
SELECT p.photo_path, max(p.id) AS photo_id FROM photos p
@ -128,7 +128,7 @@ func UpdateAlbumMonthCovers() (err error) {
GROUP BY p.photo_year, p.photo_month) p2 WHERE p2.photo_id = f.photo_id AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg'
) b ON b.photo_year = albums.album_year AND b.photo_month = albums.album_month
SET thumb = b.file_hash WHERE ?`, condition)
case SQLite:
case SQLite3:
res = Db().Table(entity.Album{}.TableName()).UpdateColumn("thumb", gorm.Expr(`(
SELECT f.file_hash FROM files f,(
SELECT p.photo_year, p.photo_month, max(p.id) AS photo_id FROM photos p
@ -204,7 +204,7 @@ func UpdateLabelCovers() (err error) {
) p2 WHERE p2.photo_id = f.photo_id AND f.file_primary = 1 AND f.file_error = '' AND f.file_type = 'jpg' AND f.file_missing = 0
) b ON b.label_id = labels.id
SET thumb = b.file_hash WHERE ?`, condition)
case SQLite:
case SQLite3:
res = Db().Table(entity.Label{}.TableName()).UpdateColumn("thumb", gorm.Expr(`(
SELECT f.file_hash FROM files f
JOIN photos_labels pl ON pl.label_id = labels.id AND pl.photo_id = f.photo_id AND pl.uncertainty < 100
@ -269,7 +269,7 @@ func UpdateSubjectCovers() (err error) {
GROUP BY m.subj_uid, m.q
) b ON b.subj_uid = subjects.subj_uid
SET thumb = marker_thumb WHERE ?`, gorm.Expr(subjTable), gorm.Expr(markerTable), condition)
case SQLite:
case SQLite3:
from := gorm.Expr(fmt.Sprintf("%s m WHERE m.subj_uid = %s.subj_uid ", markerTable, subjTable))
res = Db().Table(entity.Subject{}.TableName()).UpdateColumn("thumb", gorm.Expr(`(
SELECT m.thumb FROM ? AND m.thumb <> '' ORDER BY m.subj_src DESC, m.q DESC LIMIT 1

View file

@ -176,7 +176,7 @@ func FlagHiddenPhotos() error {
if res.RowsAffected > 0 {
log.Infof("index: flagged %s as hidden or missing [%s]", english.Plural(int(res.RowsAffected), "photo", "photos"), time.Since(start))
}
case SQLite:
case SQLite3:
if res.RowsAffected > 0 {
log.Debugf("index: flagged %s as hidden or missing [%s]", english.Plural(int(res.RowsAffected), "photo", "photos"), time.Since(start))
}

View file

@ -19,7 +19,7 @@ func PhotoSelection(f form.Selection) (results entity.Photos, err error) {
switch DbDialect() {
case MySQL:
concat = "CONCAT(a.path, '/%')"
case SQLite:
case SQLite3:
concat = "a.path || '/%'"
default:
return results, fmt.Errorf("unknown sql dialect: %s", DbDialect())
@ -59,7 +59,7 @@ func FileSelection(f form.Selection) (results entity.Files, err error) {
switch DbDialect() {
case MySQL:
concat = "CONCAT(a.path, '/%')"
case SQLite:
case SQLite3:
concat = "a.path || '/%'"
default:
return results, fmt.Errorf("unknown sql dialect: %s", DbDialect())

View file

@ -40,8 +40,8 @@ import (
var log = event.Log
const (
MySQL = "mysql"
SQLite = "sqlite3"
MySQL = "mysql"
SQLite3 = "sqlite3"
)
// Cols represents a list of database columns.