Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

ClockworkConsulting/postgresql-fixture

Repository files navigation

Build Status

Intro

postgresql-fixture provides a simple way to create temporary PostgreSQL databases for testing purposes in your Scala projects.

To use it, you'll need the credentials for user who can create databases on the PostgreSQL server you're targeting.

Usage (JDBC)

Example:

    // Create the temporary database
    val temporaryDatabase = PostgreSQLFixtureSupport.createTemporaryDatabase(
      DatabaseSettings(
        databaseName = "postgres",
        userName = "my-user",
        password = "my-password"),
      "template1")
    // Try-finally to ensure resources are freed.
    try {
      // Temporary database settings are now available
      // to use for establishing a connection, etc.
      Console.println(s"temporary database settings: ${temporaryDatabase.databaseSettings}")
    } finally {
      // Drop the temporary database
      temporaryDatabase.close()
    }

Usage (ScalikeJDBC)

Example:

    // Create the temporary database
    val temporaryDatabase = ScalikeJDBCTemporaryDatabaseFixture.setup(
      'MyConnectionPool,
      ScalikeJDBCTemporaryDatabaseSettings(
        templateDatabaseName = "template1",
        databaseSettings = DatabaseSettings(
          databaseName = "postgres",
          userName = "my-username",
          password = "my-password"),
        connectionPoolSize = 16,
        postInitialize = connectionPool => {
          // Do any post-initialization setup you want here; such as
          // applying migrations, etc.
        }))
    // Try-finally to ensure resources are freed.
    try {
      // Temporary database connections are now available
      // from the 'MyConnectionPool connection pool.
      val connection = ConnectionPool.borrow('MyConnectionPool)
      // ...
    } finally {
      // Drop the temporary database
      temporaryDatabase.close()
    }

Copyright and License

This code is provided under the BSD 2-clause license

About

Scala library for creating temporary PostgreSQL databases

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages