(t TBSubset, opts ...OpenOption)
| 33 | } |
| 34 | |
| 35 | func (b *Broker) Create(t TBSubset, opts ...OpenOption) (ConnectionParams, error) { |
| 36 | if err := b.init(t); err != nil { |
| 37 | return ConnectionParams{}, err |
| 38 | } |
| 39 | openOptions := OpenOptions{} |
| 40 | for _, opt := range opts { |
| 41 | opt(&openOptions) |
| 42 | } |
| 43 | |
| 44 | var ( |
| 45 | username = defaultConnectionParams.Username |
| 46 | password = defaultConnectionParams.Password |
| 47 | host = defaultConnectionParams.Host |
| 48 | port = defaultConnectionParams.Port |
| 49 | ) |
| 50 | packageName := getTestPackageName(t) |
| 51 | testName := t.Name() |
| 52 | |
| 53 | // Use a time-based prefix to make it easier to find the database |
| 54 | // when debugging. |
| 55 | now := time.Now().Format("test_2006_01_02_15_04_05") |
| 56 | dbSuffix, err := cryptorand.StringCharset(cryptorand.Lower, 10) |
| 57 | if err != nil { |
| 58 | return ConnectionParams{}, xerrors.Errorf("generate db suffix: %w", err) |
| 59 | } |
| 60 | dbName := now + "_" + dbSuffix |
| 61 | |
| 62 | _, err = b.coderTestingDB.Exec( |
| 63 | "INSERT INTO test_databases (name, process_uuid, test_package, test_name) VALUES ($1, $2, $3, $4)", |
| 64 | dbName, b.uuid, packageName, testName) |
| 65 | if err != nil { |
| 66 | return ConnectionParams{}, xerrors.Errorf("insert test_database row: %w", err) |
| 67 | } |
| 68 | |
| 69 | // if empty createDatabaseFromTemplate will create a new template db |
| 70 | templateDBName := os.Getenv("DB_FROM") |
| 71 | if openOptions.DBFrom != nil { |
| 72 | templateDBName = *openOptions.DBFrom |
| 73 | } |
| 74 | if err = createDatabaseFromTemplate(t, defaultConnectionParams, b.coderTestingDB, dbName, templateDBName); err != nil { |
| 75 | return ConnectionParams{}, xerrors.Errorf("create database: %w", err) |
| 76 | } |
| 77 | |
| 78 | testDBParams := ConnectionParams{ |
| 79 | Username: username, |
| 80 | Password: password, |
| 81 | Host: host, |
| 82 | Port: port, |
| 83 | DBName: dbName, |
| 84 | } |
| 85 | |
| 86 | // Optionally log the DSN to help connect to the test database. |
| 87 | if openOptions.LogDSN { |
| 88 | _, _ = fmt.Fprintf(os.Stderr, "Connect to the database for %s using: psql '%s'\n", t.Name(), testDBParams.DSN()) |
| 89 | } |
| 90 | t.Cleanup(b.clean(t, dbName)) |
| 91 | return testDBParams, nil |
| 92 | } |
nothing calls this directly
no test coverage detected