| Conditions | 1 |
| Total Lines | 70 |
| Code Lines | 49 |
| 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:
| 1 | package testinstance |
||
| 19 | func PostgresDB(postgresVersion string) database.Database { |
||
| 20 | ctx := context.Background() |
||
| 21 | |||
| 22 | image := fmt.Sprintf("postgres:%s-alpine", postgresVersion) |
||
| 23 | |||
| 24 | postgres, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
||
| 25 | ContainerRequest: testcontainers.ContainerRequest{ |
||
| 26 | Image: image, |
||
| 27 | ExposedPorts: []string{"5432/tcp"}, |
||
| 28 | Env: map[string]string{"POSTGRES_USER": "postgres", "POSTGRES_PASSWORD": "postgres", "POSTGRES_DB": "permify"}, |
||
| 29 | WaitingFor: wait.ForAll( |
||
| 30 | wait.ForLog("database system is ready to accept connections"), |
||
| 31 | wait.ForListeningPort("5432/tcp"), |
||
| 32 | ), |
||
| 33 | }, |
||
| 34 | Started: true, |
||
| 35 | }) |
||
| 36 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
||
| 37 | |||
| 38 | // Execute the command in the container |
||
| 39 | _, _, execErr := postgres.Exec(ctx, []string{"psql", "-U", "postgres", "-c", "ALTER SYSTEM SET track_commit_timestamp = on;"}) |
||
| 40 | gomega.Expect(execErr).ShouldNot(gomega.HaveOccurred()) |
||
| 41 | |||
| 42 | stopTimeout := 2 * time.Second |
||
| 43 | err = postgres.Stop(context.Background(), &stopTimeout) |
||
| 44 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
||
| 45 | |||
| 46 | err = postgres.Start(context.Background()) |
||
| 47 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
||
| 48 | |||
| 49 | cmd := []string{"sh", "-c", "export PGPASSWORD=postgres" + "; psql -U postgres -d permify -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public;'"} |
||
| 50 | |||
| 51 | _, _, err = postgres.Exec(ctx, cmd) |
||
| 52 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
||
| 53 | |||
| 54 | host, err := postgres.Host(ctx) |
||
| 55 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
||
| 56 | |||
| 57 | port, err := postgres.MappedPort(ctx, "5432") |
||
| 58 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
||
| 59 | |||
| 60 | dbAddr := fmt.Sprintf("%s:%s", host, port.Port()) |
||
| 61 | postgresDSN := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", "postgres", "postgres", dbAddr, "permify") |
||
| 62 | |||
| 63 | cfg := config.Database{ |
||
| 64 | Engine: "postgres", |
||
| 65 | URI: postgresDSN, |
||
| 66 | AutoMigrate: true, |
||
| 67 | MaxOpenConnections: 20, |
||
| 68 | MaxIdleConnections: 1, |
||
| 69 | MaxConnectionLifetime: 300, |
||
| 70 | MaxConnectionIdleTime: 60, |
||
| 71 | } |
||
| 72 | |||
| 73 | err = storage.Migrate(cfg) |
||
| 74 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
||
| 75 | |||
| 76 | var db database.Database |
||
| 77 | db, err = PQDatabase.New(cfg.URI, |
||
| 78 | PQDatabase.MaxOpenConnections(cfg.MaxOpenConnections), |
||
| 79 | PQDatabase.MaxIdleConnections(cfg.MaxIdleConnections), |
||
| 80 | PQDatabase.MaxConnectionIdleTime(cfg.MaxConnectionIdleTime), |
||
| 81 | PQDatabase.MaxConnectionLifeTime(cfg.MaxConnectionLifetime), |
||
| 82 | ) |
||
| 83 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
||
| 84 | |||
| 85 | _, err = utils.EnsureDBVersion(db.(*PQDatabase.Postgres).WritePool) |
||
| 86 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
||
| 87 | |||
| 88 | return db |
||
| 89 | } |
||
| 90 |