...

Source file src/github.com/fergusstrange/embedded-postgres/config.go

Documentation: github.com/fergusstrange/embedded-postgres

     1  package embeddedpostgres
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"time"
     8  )
     9  
    10  // Config maintains the runtime configuration for the Postgres process to be created.
    11  type Config struct {
    12  	version             PostgresVersion
    13  	port                uint32
    14  	database            string
    15  	username            string
    16  	password            string
    17  	cachePath           string
    18  	runtimePath         string
    19  	dataPath            string
    20  	binariesPath        string
    21  	locale              string
    22  	startParameters     map[string]string
    23  	binaryRepositoryURL string
    24  	startTimeout        time.Duration
    25  	logger              io.Writer
    26  }
    27  
    28  // DefaultConfig provides a default set of configuration to be used "as is" or modified using the provided builders.
    29  // The following can be assumed as defaults:
    30  // Version:      14
    31  // Port:         5432
    32  // Database:     postgres
    33  // Username:     postgres
    34  // Password:     postgres
    35  // StartTimeout: 15 Seconds
    36  func DefaultConfig() Config {
    37  	return Config{
    38  		version:             V15,
    39  		port:                5432,
    40  		database:            "postgres",
    41  		username:            "postgres",
    42  		password:            "postgres",
    43  		startTimeout:        15 * time.Second,
    44  		logger:              os.Stdout,
    45  		binaryRepositoryURL: "https://repo1.maven.org/maven2",
    46  	}
    47  }
    48  
    49  // Version will set the Postgres binary version.
    50  func (c Config) Version(version PostgresVersion) Config {
    51  	c.version = version
    52  	return c
    53  }
    54  
    55  // Port sets the runtime port that Postgres can be accessed on.
    56  func (c Config) Port(port uint32) Config {
    57  	c.port = port
    58  	return c
    59  }
    60  
    61  // Database sets the database name that will be created.
    62  func (c Config) Database(database string) Config {
    63  	c.database = database
    64  	return c
    65  }
    66  
    67  // Username sets the username that will be used to connect.
    68  func (c Config) Username(username string) Config {
    69  	c.username = username
    70  	return c
    71  }
    72  
    73  // Password sets the password that will be used to connect.
    74  func (c Config) Password(password string) Config {
    75  	c.password = password
    76  	return c
    77  }
    78  
    79  // RuntimePath sets the path that will be used for the extracted Postgres runtime directory.
    80  // If Postgres data directory is not set with DataPath(), this directory is also used as data directory.
    81  func (c Config) RuntimePath(path string) Config {
    82  	c.runtimePath = path
    83  	return c
    84  }
    85  
    86  // CachePath sets the path that will be used for storing Postgres binaries archive.
    87  // If this option is not set, ~/.go-embedded-postgres will be used.
    88  func (c Config) CachePath(path string) Config {
    89  	c.cachePath = path
    90  	return c
    91  }
    92  
    93  // DataPath sets the path that will be used for the Postgres data directory.
    94  // If this option is set, a previously initialized data directory will be reused if possible.
    95  func (c Config) DataPath(path string) Config {
    96  	c.dataPath = path
    97  	return c
    98  }
    99  
   100  // BinariesPath sets the path of the pre-downloaded postgres binaries.
   101  // If this option is left unset, the binaries will be downloaded.
   102  func (c Config) BinariesPath(path string) Config {
   103  	c.binariesPath = path
   104  	return c
   105  }
   106  
   107  // Locale sets the default locale for initdb
   108  func (c Config) Locale(locale string) Config {
   109  	c.locale = locale
   110  	return c
   111  }
   112  
   113  // StartParameters sets run-time parameters when starting Postgres (passed to Postgres via "-c").
   114  //
   115  // These parameters can be used to override the default configuration values in postgres.conf such
   116  // as max_connections=100. See https://www.postgresql.org/docs/current/runtime-config.html
   117  func (c Config) StartParameters(parameters map[string]string) Config {
   118  	c.startParameters = parameters
   119  	return c
   120  }
   121  
   122  // StartTimeout sets the max timeout that will be used when starting the Postgres process and creating the initial database.
   123  func (c Config) StartTimeout(timeout time.Duration) Config {
   124  	c.startTimeout = timeout
   125  	return c
   126  }
   127  
   128  // Logger sets the logger for postgres output
   129  func (c Config) Logger(logger io.Writer) Config {
   130  	c.logger = logger
   131  	return c
   132  }
   133  
   134  // BinaryRepositoryURL set BinaryRepositoryURL to fetch PG Binary in case of Maven proxy
   135  func (c Config) BinaryRepositoryURL(binaryRepositoryURL string) Config {
   136  	c.binaryRepositoryURL = binaryRepositoryURL
   137  	return c
   138  }
   139  
   140  func (c Config) GetConnectionURL() string {
   141  	return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s", c.username, c.password, "localhost", c.port, c.database)
   142  }
   143  
   144  // PostgresVersion represents the semantic version used to fetch and run the Postgres process.
   145  type PostgresVersion string
   146  
   147  // Predefined supported Postgres versions.
   148  const (
   149  	V15 = PostgresVersion("15.3.0")
   150  	V14 = PostgresVersion("14.8.0")
   151  	V13 = PostgresVersion("13.11.0")
   152  	V12 = PostgresVersion("12.15.0")
   153  	V11 = PostgresVersion("11.20.0")
   154  	V10 = PostgresVersion("10.23.0")
   155  	V9  = PostgresVersion("9.6.24")
   156  )
   157  

View as plain text