2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-08-02 05:28:45 +02:00
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package database // import "miniflux.app/database"
|
2018-08-02 05:28:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-12-21 03:24:04 +01:00
|
|
|
"fmt"
|
2021-05-24 04:32:34 +02:00
|
|
|
"time"
|
2018-08-02 05:28:45 +02:00
|
|
|
|
|
|
|
// Postgresql driver import
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewConnectionPool configures the database connection pool.
|
2021-05-24 04:32:34 +02:00
|
|
|
func NewConnectionPool(dsn string, minConnections, maxConnections int, connectionLifetime time.Duration) (*sql.DB, error) {
|
2018-08-02 05:28:45 +02:00
|
|
|
db, err := sql.Open("postgres", dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db.SetMaxOpenConns(maxConnections)
|
|
|
|
db.SetMaxIdleConns(minConnections)
|
2021-05-24 04:32:34 +02:00
|
|
|
db.SetConnMaxLifetime(connectionLifetime)
|
2018-08-02 05:28:45 +02:00
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
2020-12-21 03:24:04 +01:00
|
|
|
|
|
|
|
// Migrate executes database migrations.
|
2021-01-03 01:33:41 +01:00
|
|
|
func Migrate(db *sql.DB) error {
|
2020-12-21 03:24:04 +01:00
|
|
|
var currentVersion int
|
|
|
|
db.QueryRow(`SELECT version FROM schema_version`).Scan(¤tVersion)
|
|
|
|
|
|
|
|
fmt.Println("-> Current schema version:", currentVersion)
|
|
|
|
fmt.Println("-> Latest schema version:", schemaVersion)
|
|
|
|
|
|
|
|
for version := currentVersion; version < schemaVersion; version++ {
|
|
|
|
newVersion := version + 1
|
|
|
|
fmt.Println("* Migrating to version:", newVersion)
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
2021-01-03 01:33:41 +01:00
|
|
|
return fmt.Errorf("[Migration v%d] %v", newVersion, err)
|
2020-12-21 03:24:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := migrations[version](tx); err != nil {
|
|
|
|
tx.Rollback()
|
2021-01-03 01:33:41 +01:00
|
|
|
return fmt.Errorf("[Migration v%d] %v", newVersion, err)
|
2020-12-21 03:24:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := tx.Exec(`DELETE FROM schema_version`); err != nil {
|
|
|
|
tx.Rollback()
|
2021-01-03 01:33:41 +01:00
|
|
|
return fmt.Errorf("[Migration v%d] %v", newVersion, err)
|
2020-12-21 03:24:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := tx.Exec(`INSERT INTO schema_version (version) VALUES ($1)`, newVersion); err != nil {
|
|
|
|
tx.Rollback()
|
2021-01-03 01:33:41 +01:00
|
|
|
return fmt.Errorf("[Migration v%d] %v", newVersion, err)
|
2020-12-21 03:24:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2021-01-03 01:33:41 +01:00
|
|
|
return fmt.Errorf("[Migration v%d] %v", newVersion, err)
|
2020-12-21 03:24:04 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-03 01:33:41 +01:00
|
|
|
|
|
|
|
return nil
|
2020-12-21 03:24:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsSchemaUpToDate checks if the database schema is up to date.
|
|
|
|
func IsSchemaUpToDate(db *sql.DB) error {
|
|
|
|
var currentVersion int
|
|
|
|
db.QueryRow(`SELECT version FROM schema_version`).Scan(¤tVersion)
|
|
|
|
if currentVersion < schemaVersion {
|
|
|
|
return fmt.Errorf(`the database schema is not up to date: current=v%d expected=v%d`, currentVersion, schemaVersion)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|