data provider: try to automatically initialize it if required

This commit is contained in:
Nicola Murino 2020-10-05 12:55:49 +02:00
parent c65dd86d5e
commit 3e2afc35ba
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB
4 changed files with 15 additions and 14 deletions

View file

@ -43,10 +43,6 @@ jobs:
$DATE_TIME = ([datetime]::Now.ToUniversalTime().toString("yyyy-MM-ddTHH:mm:ssZ")) | Out-String
go build -ldflags "-s -w -X github.com/drakkan/sftpgo/version.commit=$GIT_COMMIT -X github.com/drakkan/sftpgo/version.date=$DATE_TIME" -o sftpgo.exe
- name: Initialize data provider
run: ./sftpgo initprovider
shell: bash
- name: Run test cases using SQLite provider
run: go test -v -p 1 -timeout 10m ./... -coverprofile=coverage.txt -covermode=atomic
@ -177,7 +173,6 @@ jobs:
- name: Run tests using PostgreSQL provider
run: |
./sftpgo initprovider
go test -v -p 1 -timeout 10m ./... -covermode=atomic
env:
SFTPGO_DATA_PROVIDER__DRIVER: postgresql
@ -189,7 +184,6 @@ jobs:
- name: Run tests using MySQL provider
run: |
./sftpgo initprovider
go test -v -p 1 -timeout 10m ./... -covermode=atomic
env:
SFTPGO_DATA_PROVIDER__DRIVER: mysql

View file

@ -111,10 +111,6 @@ jobs:
$DATE_TIME = ([datetime]::Now.ToUniversalTime().toString("yyyy-MM-ddTHH:mm:ssZ")) | Out-String
go build -ldflags "-s -w -X github.com/drakkan/sftpgo/version.commit=$GIT_COMMIT -X github.com/drakkan/sftpgo/version.date=$DATE_TIME" -o sftpgo.exe
- name: Initialize data provider
run: ./sftpgo initprovider
shell: bash
- name: Get SFTPGo version
id: get_version
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}

View file

@ -96,9 +96,11 @@ Before starting the SFTPGo server, please ensure that the configured data provid
SQL based data providers (SQLite, MySQL, PostgreSQL) require the creation of a database containing the required tables. Memory and bolt data providers do not require an initialization.
After configuring the data provider using the configuration file, you can create the required database structure using the `initprovider` command.
For SQLite provider, the `initprovider` command will auto create the database file, if missing, and the required tables.
For PostgreSQL and MySQL providers, you need to create the configured database, and the `initprovider` command will create the required tables.
For PostgreSQL and MySQL providers, you need to create the configured database.
SFTPGo will attempt to automatically detect if the data privider has been initialized and if not, initialize it on startup.
Alternately, you can create the required data provider structure yourself using the `initprovider` command.
For example, you can simply execute the following command from the configuration directory:
@ -112,7 +114,7 @@ Take a look at the CLI usage to learn how to specify a different configuration f
sftpgo initprovider --help
```
After the initialization, the database structure will be automatically checked and updated, if required, at startup.
After the first initialization (manual or automatic), the database structure will be automatically checked and updated, if required, at startup.
## Tutorials

View file

@ -383,6 +383,15 @@ func Initialize(cnf Config, basePath string) error {
if err != nil {
return err
}
err = provider.initializeDatabase()
if err != nil && err != ErrNoInitRequired {
logger.WarnToConsole("Unable to initialize data provider: %v", err)
providerLog(logger.LevelWarn, "Unable to initialize data provider: %v", err)
return err
}
if err == nil {
logger.DebugToConsole("Data provider successfully initialized")
}
err = provider.migrateDatabase()
if err != nil {
providerLog(logger.LevelWarn, "database migration error: %v", err)