initprovider: check if the provider is already initialized

exit with code 0 if no initialization is required
This commit is contained in:
Nicola Murino 2020-08-30 13:50:43 +02:00
parent 2746c0b0f1
commit 600a107699
9 changed files with 22 additions and 15 deletions

View file

@ -46,6 +46,8 @@ Please take a look at the usage below to customize the options.`,
err = dataprovider.InitializeDatabase(providerConf, configDir)
if err == nil {
logger.DebugToConsole("Data provider successfully initialized")
} else if err == dataprovider.ErrNoInitRequired {
logger.DebugToConsole("%v", err.Error())
} else {
logger.WarnToConsole("Unable to initialize data provider: %v", err)
os.Exit(1)

View file

@ -703,7 +703,7 @@ func (p BoltProvider) reloadConfig() error {
// initializeDatabase does nothing, no initilization is needed for bolt provider
func (p BoltProvider) initializeDatabase() error {
return errNoInitRequired
return ErrNoInitRequired
}
func (p BoltProvider) migrateDatabase() error {

View file

@ -96,6 +96,8 @@ var (
ErrNoAuthTryed = errors.New("no auth tryed")
// ValidProtocols defines all the valid protcols
ValidProtocols = []string{"SSH", "FTP", "DAV"}
// ErrNoInitRequired defines the error returned by InitProvider if no inizialization is required
ErrNoInitRequired = errors.New("Data provider initialization is not required")
config Config
provider Provider
sqlPlaceholders []string
@ -108,7 +110,6 @@ var (
availabilityTicker *time.Ticker
availabilityTickerDone chan bool
errWrongPassword = errors.New("password does not match")
errNoInitRequired = errors.New("initialization is not required for this data provider")
credentialsDirPath string
sqlTableUsers = "users"
sqlTableFolders = "folders"
@ -422,7 +423,7 @@ func InitializeDatabase(cnf Config, basePath string) error {
config = cnf
if config.Driver == BoltDataProviderName || config.Driver == MemoryDataProviderName {
return errNoInitRequired
return ErrNoInitRequired
}
err := createProvider(basePath)
if err != nil {

View file

@ -667,7 +667,7 @@ func (p MemoryProvider) reloadConfig() error {
// initializeDatabase does nothing, no initilization is needed for memory provider
func (p MemoryProvider) initializeDatabase() error {
return errNoInitRequired
return ErrNoInitRequired
}
func (p MemoryProvider) migrateDatabase() error {

View file

@ -196,7 +196,7 @@ func (p MySQLProvider) initializeDatabase() error {
}
func (p MySQLProvider) migrateDatabase() error {
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle)
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, true)
if err != nil {
return err
}

View file

@ -195,7 +195,7 @@ func (p PGSQLProvider) initializeDatabase() error {
}
func (p PGSQLProvider) migrateDatabase() error {
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle)
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, true)
if err != nil {
return err
}

View file

@ -766,7 +766,7 @@ func sqlCommonRollbackTransaction(tx *sql.Tx) {
}
}
func sqlCommonGetDatabaseVersion(dbHandle *sql.DB) (schemaVersion, error) {
func sqlCommonGetDatabaseVersion(dbHandle *sql.DB, showInitWarn bool) (schemaVersion, error) {
var result schemaVersion
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
defer cancel()
@ -774,7 +774,7 @@ func sqlCommonGetDatabaseVersion(dbHandle *sql.DB) (schemaVersion, error) {
stmt, err := dbHandle.PrepareContext(ctx, q)
if err != nil {
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
if strings.Contains(err.Error(), sqlTableSchemaVersion) {
if showInitWarn && strings.Contains(err.Error(), sqlTableSchemaVersion) {
logger.WarnToConsole("database query error, did you forgot to run the \"initprovider\" command?")
}
return result, err

View file

@ -194,6 +194,10 @@ func (p SQLiteProvider) reloadConfig() error {
// initializeDatabase creates the initial database structure
func (p SQLiteProvider) initializeDatabase() error {
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, false)
if err == nil && dbVersion.Version > 0 {
return ErrNoInitRequired
}
sqlUsers := strings.Replace(sqliteUsersTableSQL, "{{users}}", sqlTableUsers, 1)
tx, err := p.dbHandle.Begin()
if err != nil {
@ -218,7 +222,7 @@ func (p SQLiteProvider) initializeDatabase() error {
}
func (p SQLiteProvider) migrateDatabase() error {
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle)
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, true)
if err != nil {
return err
}