Skip to content

richkuz/schema-tools-sample-app

Repository files navigation

Schema Tools Sample App

This app demonstrates using the schema-tools gem to migrate an OpenSearch index.

The app itself is a minimal Rails application with PostgreSQL and a Books model that replicates changes to OpenSearch. It also implements a soft-delete capability in OpenSearch.

Setup

  1. Start PostgreSQL with Docker:

    docker compose up -d
  2. Install dependencies:

    bundle install

    Note: This app uses the schema-tools gem from the local ../schema-tools directory for development and testing.

  3. Create and migrate the database.

    rails db:create
    rails db:migrate
  4. Create and migrate the OpenSearch schema.

    export OPENSEARCH_URL=http://localhost:9200
    rails schema:migrate

Usage

Demo Script

Run the demonstration script to see all CRUD operations and OpenSearch schema migration:

./demo_script.rb

Books Model

The Books model includes:

  • title (string, required)
  • isbn (string, required, unique)
  • description (text)
  • author (string)
  • pages (integer)
  • created_at (timestamp)
  • updated_at (timestamp)
  • deleted_at (timestamp)

OpenSearch Integration

The Books model automatically syncs with OpenSearch when books are created or updated:

Environment Variables

Set these environment variables to enable OpenSearch integration:

export OPENSEARCH_URL=http://localhost:9200
export OPENSEARCH_USERNAME=your_username  # Optional
export OPENSEARCH_PASSWORD=your_password  # Optional

Automatic Sync

  • Books are automatically saved to OpenSearch when created (after_create callback)
  • Books are automatically updated in OpenSearch when modified (after_update callback)
  • Books are automatically deleted from OpenSearch when destroyed (after_destroy callback)
  • The OpenSearch document uses the same schema as defined in schemas/books/mappings.json

Manual Operations

  • Bulk index all books: Book.bulk_index_to_opensearch
  • Delete by query: Book.delete_by_query({ term: { "author.keyword" => "Author Name" } })
  • Check if OpenSearch is configured: Book.opensearch_client_available?

CRUD Operations

  • Create: Book.create!(title: "Book Title", isbn: "1234567890")
  • Read: Book.find_by(title: "Book Title")
  • Update: book.update!(title: "New Title")
  • Delete: book.destroy!
  • List all: Book.order(:updated_at)

About

Example app to demonstrate using the schema-tools Ruby gem

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages