Skip to content

sciencesakura/dbsetup-csv

Repository files navigation

dbsetup-csv: Import CSV into database with DbSetup

Maven Central

A DbSetup extension for importing CSV files into the database.

Requirements

  • Java 11+

Installation

The dbsetup-csv library is available on Maven Central. You can install it using your build system of choice.

Gradle

testImplementation 'com.sciencesakura:dbsetup-csv:3.0.1'

If you are using Kotlin, you can use the Kotlin module for a more concise DSL:

testImplementation 'com.sciencesakura:dbsetup-csv-kt:3.0.1'

Maven

<dependency>
  <groupId>com.sciencesakura</groupId>
  <artifactId>dbsetup-csv</artifactId>
  <version>3.0.1</version>
  <scope>test</scope>
</dependency>

If you are using Kotlin, you can use the Kotlin module for a more concise DSL:

<dependency>
  <groupId>com.sciencesakura</groupId>
  <artifactId>dbsetup-csv-kt</artifactId>
  <version>3.0.1</version>
  <scope>test</scope>
</dependency>

Usage

Import CSV/TSV file

import static com.sciencesakura.dbsetup.csv.Import.csv;
import com.ninja_squad.dbsetup.DbSetup;

// The operation to import a CSV file into a table
var operation = csv("test-items.csv").into("items").build();
// when importing a TSV file:
// var operation = csv("test-items.tsv").into("items").withDelimiter('\t').build();

// Create a `DbSetup` instance with the operation and execute it
var dbSetup = new DbSetup(destination, operation);
dbSetup.launch();

Clear table before import

import static com.ninja_squad.dbsetup.Operations.*;
import static com.sciencesakura.dbsetup.csv.Import.csv;
import com.ninja_squad.dbsetup.DbSetup;

// The operations to clear the table and then import the CSV file
var operations = sequenceOf(
    deleteAllFrom("items"),
    csv("test-items.csv").into("items").build()
);

var dbSetup = new DbSetup(destination, operations);
dbSetup.launch();

Use generated values and fixed values

import static com.sciencesakura.dbsetup.csv.Import.csv;
import com.ninja_squad.dbsetup.generator.ValueGenerators;

var operation = csv("test-items.csv").into("items")
    // Generate a random UUID for the `id` column
    .withGeneratedValue("id", () -> UUID.randomUUID().toString())
    // Generate a sequential string for the `name` column, starting with "item-001"
    .withGeneratedValue("name", ValueGenerators.stringSequence("item-").withLeftPadding(3))
    // Set a fixed value for the `created_at` column
    .withDefaultValue("created_at", "2023-01-01 10:20:30")
    .build();

Use Kotlin DSL

import com.ninja_squad.dbsetup_kotlin.dbSetup
import com.sciencesakura.dbsetup.csv.csv

dbSetup(destination) {
  csv("test-items.csv") {
    into("items")
    withGeneratedValue("id") { UUID.randomUUID().toString() }
  }
}.launch()

See API reference for more details.

Prefer Excel?

dbsetup-spreadsheet

License

This library is licensed under the MIT License.

Copyright (c) 2019 sciencesakura

About

A DbSetup extension for importing CSV files into the database.

Topics

Resources

License

Stars

Watchers

Forks