Ver Fonte

data provider: try to automatically initialize it if required

Nicola Murino há 4 anos atrás
pai
commit
3e2afc35ba

+ 0 - 6
.github/workflows/development.yml

@@ -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

+ 0 - 4
.github/workflows/release.yml

@@ -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\//}

+ 6 - 4
README.md

@@ -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
 

+ 9 - 0
dataprovider/dataprovider.go

@@ -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)