| Conditions | 16 |
| Total Lines | 76 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like factories.DatabaseFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package factories |
||
| 35 | func DatabaseFactory(conf config.Database) (db database.Database, err error) { |
||
| 36 | switch conf.Engine { |
||
| 37 | case database.POSTGRES.String(): |
||
| 38 | |||
| 39 | opts := []PQDatabase.Option{ |
||
| 40 | PQDatabase.MaxConnectionIdleTime(conf.MaxConnectionIdleTime), |
||
| 41 | PQDatabase.MaxConnectionLifeTime(conf.MaxConnectionLifetime), |
||
| 42 | PQDatabase.WatchBufferSize(conf.WatchBufferSize), |
||
| 43 | PQDatabase.MaxDataPerWrite(conf.MaxDataPerWrite), |
||
| 44 | PQDatabase.MaxRetries(conf.MaxRetries), |
||
| 45 | } |
||
| 46 | |||
| 47 | // Add MinConns if set (takes precedence over MaxIdleConnections for backward compatibility) |
||
| 48 | if conf.MinConns > 0 { |
||
| 49 | opts = append(opts, PQDatabase.MinConns(conf.MinConns)) |
||
| 50 | } |
||
| 51 | |||
| 52 | // Use MaxConns if set, otherwise fall back to MaxOpenConnections for backward compatibility |
||
| 53 | // Note: MaxConns defaults to 0 in config, so we check MaxOpenConnections if MaxConns is 0 |
||
| 54 | if conf.MaxConns > 0 { |
||
| 55 | opts = append(opts, PQDatabase.MaxConns(conf.MaxConns)) |
||
| 56 | } else { |
||
| 57 | // Backward compatibility: if MaxConns is not set, use MaxOpenConnections |
||
| 58 | opts = append(opts, PQDatabase.MaxOpenConnections(conf.MaxOpenConnections)) |
||
| 59 | } |
||
| 60 | |||
| 61 | // Kept for backward compatibility |
||
| 62 | if conf.MaxIdleConnections > 0 { |
||
| 63 | opts = append(opts, PQDatabase.MaxIdleConnections(conf.MaxIdleConnections)) |
||
| 64 | } |
||
| 65 | |||
| 66 | // Add MinIdleConns if set (takes precedence over MaxIdleConnections) |
||
| 67 | if conf.MinIdleConns > 0 { |
||
| 68 | opts = append(opts, PQDatabase.MinIdleConns(conf.MinIdleConns)) |
||
| 69 | } |
||
| 70 | |||
| 71 | // Add optional pool configuration options if set |
||
| 72 | if conf.HealthCheckPeriod > 0 { |
||
| 73 | opts = append(opts, PQDatabase.HealthCheckPeriod(conf.HealthCheckPeriod)) |
||
| 74 | } |
||
| 75 | if conf.MaxConnLifetimeJitter > 0 { |
||
| 76 | opts = append(opts, PQDatabase.MaxConnLifetimeJitter(conf.MaxConnLifetimeJitter)) |
||
| 77 | } |
||
| 78 | if conf.ConnectTimeout > 0 { |
||
| 79 | opts = append(opts, PQDatabase.ConnectTimeout(conf.ConnectTimeout)) |
||
| 80 | } |
||
| 81 | |||
| 82 | if conf.URI == "" { |
||
| 83 | db, err = PQDatabase.NewWithSeparateURIs(conf.Writer.URI, conf.Reader.URI, opts...) |
||
| 84 | if err != nil { |
||
| 85 | return nil, err |
||
| 86 | } |
||
| 87 | } else { |
||
| 88 | db, err = PQDatabase.New(conf.URI, opts...) |
||
| 89 | if err != nil { |
||
| 90 | return nil, err |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | // Verify postgres version compatibility |
||
| 95 | // Check postgres version compatibility with the database |
||
| 96 | _, err = utils.EnsureDBVersion(db.(*PQDatabase.Postgres).ReadPool) |
||
| 97 | if err != nil { // Version check failed |
||
| 98 | return nil, err // Return version error |
||
| 99 | } |
||
| 100 | // Return database instance |
||
| 101 | |||
| 102 | return db, err |
||
| 103 | case database.MEMORY.String(): |
||
| 104 | db, err = IMDatabase.New(migrations.Schema) |
||
| 105 | if err != nil { |
||
| 106 | return nil, err |
||
| 107 | } |
||
| 108 | return db, err |
||
| 109 | default: |
||
| 110 | return nil, fmt.Errorf("%s connection is unsupported", conf.Engine) |
||
| 111 | } |
||
| 113 |