A CLI tool for managing Elasticsearch schema migrations.
elasticmate [flags]
Flags:
-url string Elasticsearch URL (default "http://localhost:9200")
-file string Optional path to text file for version management
- Automatic version generation based on migration content
- Tracks migrations in a dedicated Elasticsearch index (
.elasticmate_migrations
) or optionally in a text file - Automatically runs migrations in version order
- Skips already applied migrations
- Stores migration history with timestamps and function names
- Create your migration function:
func createUsersIndex(client *elasticsearch.Client) error {
mapping := `{
"mappings": {
"properties": {
"name": { "type": "text" },
"email": { "type": "keyword" },
"created_at": { "type": "date" }
}
}
}`
res, err := client.Indices.Create(
"users",
client.Indices.Create.WithBody(strings.NewReader(mapping)),
)
if err != nil {
return fmt.Errorf("error creating users index: %w", err)
}
defer res.Body.Close()
return nil
}
- Register the migration (version is computed automatically):
mm.Register(migration.NewMigration(
"Create users index",
createUsersIndex,
))
- Run the migrations:
$ elasticmate -url http://localhost:9200
The tool will:
- Connect to your Elasticsearch instance
- Create a
.elasticmate_migrations
index if it doesn't exist - Generate unique versions based on migration content
- Run any pending migrations in order
- Store migration history with timestamps and function names
The version for each migration is automatically computed using:
- The function name of the migration
- The description text
- These are combined and hashed (SHA-256), with the first 8 characters used as the version
This means:
- No need to manually specify versions
- If you change the migration function or description, it gets a new version
- Consistent versions across different runs
- Easy to track in version control
If you prefer not to create an additional Elasticsearch index for tracking migrations, you can use a text file instead:
# Run migrations using a text file to track versions
elasticmate -file ./migrations.json
The file will be created automatically if it doesn't exist. It stores migration versions in a simple JSON format:
{
"abcd1234": true,
"efgh5678": true
}
This is particularly useful for development environments or when you want to keep migration tracking separate from Elasticsearch.
- Docker and Docker Compose
- Go 1.21 or later
- Start the test Elasticsearch instance:
docker-compose up -d
- Run the tests:
go test ./... -v
- Clean up:
docker-compose down -v
The test suite includes:
- Migration registration and execution
- Idempotency checks (migrations run only once)
- Version generation verification
- Error handling
The test suite uses:
- Elasticsearch 8.12.1 in Docker
- Automated test setup and teardown
- Isolated test indices
- Comprehensive test cases for all major features