providers
SQL

SQL

There are various SQL providers available, which all share a common DSL.

  • postgres : Declares an image using the postgres:13 image by default
  • mysql: Declares an image using the mysql:9 image by default
// use the default image.
postgres {
    // definition goes here
}
 
// custom image
postgres(imageName = "postgres:12") {
    // definition goes here
}

Defining tables

You can run DDL to create tables, and populate them with data by calling the table() function:

postgres {
    table(
        "users", """
        CREATE TABLE users (
            id UUID PRIMARY KEY,
            username VARCHAR(100) NOT NULL,
            created_at TIMESTAMP WITH TIME ZONE,
            is_active BOOLEAN,
            login_count INTEGER,
            balance DECIMAL(10, 2)
        )
    """,
    data = listOf(
            mapOf(
                "id" to UUID.randomUUID(),
                "username" to "john_doe",
                "created_at" to now,
                "is_active" to true,
                "login_count" to 5,
                "balance" to BigDecimal("100.50")
            ),
            mapOf(
                "id" to UUID.randomUUID(),
                "username" to "jane_smith",
                "created_at" to now.minusSeconds(3600),
                "is_active" to false,
                "login_count" to 2,
                "balance" to BigDecimal("75.25")
            )
        )
    )
 
    table(
        "products", """
        CREATE TABLE products (
            id SERIAL PRIMARY KEY,
            name VARCHAR(100) NOT NULL,
            description TEXT,
            price DECIMAL(10, 2) NOT NULL,
            created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
        )
    """, data = listOf(
 
            mapOf(
                "name" to "Widget",
                "description" to "A fantastic widget",
                "price" to BigDecimal("9.99")
            ),
            mapOf(
                "name" to "Gadget",
                "description" to "An amazing gadget",
                "price" to BigDecimal("24.99")
            )
        )
    )
}

Returned values

When any database component is declared, the following data is returned:

  • databaseName
  • jdbcUrl
  • username
  • password
  • port