Skip to content

Commit 90e6060

Browse files
committed
Merge branch 'master' into guestupload
2 parents 0872801 + 14b2f7b commit 90e6060

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

docs/advanced.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ To switch to a different database, Gokapi provides a migration tool. By running:
144144

145145
::
146146

147-
gokapi --migrate [old Database URL] [new Database URL]
147+
gokapi migrate-database --source [old Database URL] --destination [new Database URL]
148148
149149
all existing data, except for user sessions, will be transferred to the new database. After the migration, you will need to rerun the setup and specify the new database location. For details on the correct database URL format, refer to the section :ref:`databaseUrl`.
150150

151151
For Docker users, the command is:
152152
::
153153

154-
docker run --rm -v gokapi-data:/app/data f0rc3/gokapi:latest /app/run.sh [old Database URL] [new Database URL]
154+
docker run --rm -v gokapi-data:/app/data f0rc3/gokapi:latest /app/run.sh migrate-database --source [old Database URL] --destination [new Database URL]
155155

156156

157157
.. _databaseUrl:
@@ -190,20 +190,20 @@ Migrating SQLite (``/app/data/gokapi.sqlite``) to Redis (``127.0.0.1:6379``):
190190

191191
::
192192

193-
gokapi --migrate sqlite:///app/data/gokapi.sqlite redis://127.0.0.1:6379
193+
gokapi migrate-database --source sqlite:///app/data/gokapi.sqlite --destination redis://127.0.0.1:6379
194194

195195
Migrating SQLite (``/app/data/gokapi.sqlite``) to SQLite (``./data/gokapi.sqlite``):
196196

197197
::
198198

199-
gokapi --migrate sqlite:///app/data/gokapi.sqlite sqlite://./data/gokapi.sqlite
199+
gokapi migrate-database --source sqlite:///app/data/gokapi.sqlite --destination sqlite://./data/gokapi.sqlite
200200
201201
Migrating Redis (``127.0.0.1:6379, User: test, Password: 1234, Prefix: gokapi_, using SSL``) to SQLite (``./data/gokapi.sqlite``):
202202

203203

204204
::
205205

206-
gokapi --migrate "redis://test:1234@127.0.0.1:6379?prefix=gokapi_&ssl=true" sqlite://./data/gokapi.sqlite
206+
gokapi migrate-database --source "redis://test:1234@127.0.0.1:6379?prefix=gokapi_&ssl=true" --destination sqlite://./data/gokapi.sqlite
207207

208208

209209

docs/setup.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ During the first start, a new configuration file will be created and you will be
147147

148148
Database
149149
""""""""""""""
150-
By default, Gokapi stores its data in a database located in the ``data`` directory. You can specify a different database location in this menu. If no changes are needed, you can proceed as is. Redis is recommended for servers with a high frequency of downloads.
150+
By default, Gokapi stores its data in a SQLite database located in the ``data`` directory. You can specify a different database location in this menu. If you expect a high frequency of downloads or uploads or if your instance has a slow disk, using **Redis is strongly recommended instead of SQLite**.
151151

152152
You can configure the following settings:
153153

@@ -159,6 +159,10 @@ You can configure the following settings:
159159
- **Password (optional)** Enter the password for database connection.
160160
- **Use SSL** Select this option to establish an SSL connection.
161161

162+
163+
.. danger::
164+
When using Redis, make sure that you enable persistence (e.g. by setting ``save 1 1`` in the redis server configuration file). Otherwise all data will be lost after a restart!
165+
162166
.. warning::
163167
The Redis password will be stored in plain text and can be viewed when re-running the setup.
164168

internal/configuration/database/provider/redis/Redis.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package redis
33
import (
44
"errors"
55
"fmt"
6-
"github.com/forceu/gokapi/internal/environment"
7-
"github.com/forceu/gokapi/internal/helper"
8-
"github.com/forceu/gokapi/internal/models"
9-
redigo "github.com/gomodule/redigo/redis"
106
"os"
117
"strconv"
128
"strings"
139
"time"
10+
11+
"github.com/forceu/gokapi/internal/helper"
12+
"github.com/forceu/gokapi/internal/models"
13+
redigo "github.com/gomodule/redigo/redis"
1414
)
1515

1616
// DatabaseProvider contains the database instance
@@ -47,6 +47,15 @@ func (p DatabaseProvider) init(config models.DbConnection) (DatabaseProvider, er
4747
if err != nil {
4848
return DatabaseProvider{}, err
4949
}
50+
isPersistenceEnabled, err := p.isPersistenceEnabled()
51+
if err == nil {
52+
if !isPersistenceEnabled {
53+
fmt.Println("WARNING! Redis persistence is disabled. ALL DATA WILL BE LOST after a database restart.")
54+
}
55+
} else {
56+
fmt.Println("Unable to check if Redis has persistence enabled.")
57+
}
58+
5059
// If DB version is 0, the DB is new and therefore set version to latest one.
5160
// Otherwise, Upgrade() would be called after loading
5261
if p.GetDbVersion() == 0 {
@@ -55,6 +64,18 @@ func (p DatabaseProvider) init(config models.DbConnection) (DatabaseProvider, er
5564
return p, nil
5665
}
5766

67+
func (p DatabaseProvider) isPersistenceEnabled() (bool, error) {
68+
output, err := redigo.Values(p.getConfigRaw("save"))
69+
if err != nil {
70+
return false, err
71+
}
72+
if len(output) < 2 {
73+
return false, nil
74+
}
75+
saveVal, _ := redigo.String(output[1], nil)
76+
return len(saveVal) > 0, nil
77+
}
78+
5879
func getDialOptions(config models.DbConnection) []redigo.DialOption {
5980
dialOptions := []redigo.DialOption{redigo.DialClientName("gokapi")}
6081
if config.Username != "" {
@@ -211,6 +232,11 @@ func (p DatabaseProvider) getKeyRaw(id string) (any, error) {
211232
return conn.Do("GET", p.dbPrefix+id)
212233
}
213234

235+
func (p DatabaseProvider) getConfigRaw(id string) (any, error) {
236+
conn := p.pool.Get()
237+
defer conn.Close()
238+
return conn.Do("CONFIG", "GET", id)
239+
}
214240
func (p DatabaseProvider) getKeyString(id string) (string, bool) {
215241
result, err := redigo.String(p.getKeyRaw(id))
216242
if result == "" {

internal/configuration/setup/templates/setup.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113

114114
{{ if not .IsInitialSetup }}
115115
<p>
116-
<span style="color:red"><b>Warning:</b> When changing database parameters, ensure you first migrate the database to the new location using <code>gokapi --migrate-database</code> before finalising the setup.</span>
116+
<span style="color:red"><b>Warning:</b> When changing database parameters, ensure you first migrate the database to the new location using <code>gokapi migrate-database</code> before finalising the setup.</span>
117117
</p><br>
118118
{{ end }}
119119

0 commit comments

Comments
 (0)