Skip to content

arkuksin/flyway-multitenancy

Repository files navigation

Car Management API

This project is a Spring Boot application designed to manage car data. It supports multi-tenancy, allowing different tenants (e.g., VW and BMW) to manage their car inventories independently. Each tenant has its own dedicated H2 in-memory database.

This README will guide you through setting up, running, and interacting with the application.

Getting Started

Prerequisites

  • Java 11 or higher
  • Gradle (the project uses the Gradle wrapper, so a local installation is not strictly required)

Building the Project

To build the project, navigate to the root directory and run:

./gradlew build

This command will compile the code and package it into a JAR file.

Running the Application

You can run the application in a few ways:

  1. Using Gradle:
    ./gradlew bootRun
  2. Running the JAR file: After building the project, you can find the JAR file in the build/libs/ directory. Run it using:
    java -jar build/libs/multi-tenant-datasource-0.0.1-SNAPSHOT.jar 
    (Replace multi-tenant-datasource-0.0.1-SNAPSHOT.jar with the actual JAR file name if it differs).

Testing with run.sh

The project includes a run.sh script that demonstrates basic API interactions. Once the application is running, you can execute this script from the root directory:

./run.sh

This script will:

  • List cars for the 'vw' tenant.
  • Add a new car ('tiguan') for the 'vw' tenant.
  • List cars for the 'vw' tenant again to show the newly added car.
  • List cars for the 'bmw' tenant.
  • Add a new car ('X5') for the 'bmw' tenant.
  • List cars for the 'bmw' tenant again.

API Endpoints

The application exposes the following RESTful API endpoints for managing cars:

List Cars

  • URL: /cars
  • Method: GET
  • Headers:
    • X-Tenant: Specifies the tenant whose cars you want to list (e.g., vw or bmw).
  • Description: Retrieves a list of all cars for the specified tenant.
  • Example:
    curl --request GET --header "X-tenant: vw" localhost:8080/cars

Add Car

  • URL: /cars
  • Method: POST
  • Headers:
    • X-Tenant: Specifies the tenant for whom you are adding the car (e.g., vw or bmw).
    • Content-Type: application/json
  • Body: A JSON object representing the car to be added.
    {
        "name": "car_name",
        "color": "car_color"
    }
  • Description: Adds a new car to the inventory of the specified tenant.
  • Example:
    curl --request POST --header "X-Tenant: vw" --header "Content-Type: application/json"  --data '{"name":"tiguan","color":"black"}' localhost:8080/cars

Database Setup

This application uses a multi-tenant database architecture, where each tenant has its own isolated H2 in-memory database. This ensures data separation and security between different tenants.

Configuration

The database configurations for each tenant are defined in the src/main/resources/application.yaml file. Here's an example snippet:

spring:
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
  flyway:
    enabled: false # Flyway is currently disabled
tenants:
  datasources:
    vw: # Tenant 'vw'
      jdbcUrl: jdbc:h2:mem:vw # In-memory H2 database named 'vw'
      driverClassName: org.h2.Driver
      username: sa
      password: password
    bmw: # Tenant 'bmw'
      jdbcUrl: jdbc:h2:mem:bmw # In-memory H2 database named 'bmw'
      driverClassName: org.h2.Driver
      username: sa
      password: password

Key points about the database setup:

  • In-Memory: The H2 databases are in-memory, meaning data will be lost when the application stops. This is suitable for development and testing. For production, you would typically switch to a persistent database.
  • Tenant-Specific Schemas: Each tenant (vw, bmw) has its own JDBC URL (jdbc:h2:mem:vw, jdbc:h2:mem:bmw), effectively creating separate database instances.
  • Flyway: Database migrations with Flyway are currently disabled (flyway: enabled: false). If you enable it, you would typically manage schema changes under src/main/resources/db/migration/ for each tenant.

Contributing

Contributions to this project are welcome! If you have suggestions for improvements or find any issues, please feel free to:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make your changes.
  4. Commit your changes and push them to your fork.
  5. Submit a pull request.

Please ensure your code follows the existing style and that any new features are appropriately tested.

About

Flyway Migration in a Spring Boot Application with Multitenancy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages