Giter Club home page Giter Club logo

cqlmigrate's Introduction

Build Status Download

Cassandra CQL migration tool

cqlmigrate is a library for performing schema migrations on a cassandra cluster.

It is best used as an application dependency, but can also be used standalone.

Adding as a Gradle dependency

repositories {
    jcenter()
}

compile 'uk.sky:cqlmigrate:0.9.8'

Adding as a Maven dependency

<repositories>
    <repository>
        <id>jcenter</id>
        <name>jcenter</name>
        <url>http://jcenter.bintray.com</url>
    </repository>
</repositories>

<dependency>
  <groupId>uk.sky</groupId>
  <artifactId>cqlmigrate</artifactId>
  <version>0.9.8</version>
</dependency>

Cassandra Prerequisites

The locks keyspace and table needs to be created before running any migrations.

CREATE KEYSPACE IF NOT EXISTS cqlmigrate WITH replication = {'class': 'REPLICATION_CLASS', 'replication_factor': REPLICATION_FACTOR };
CREATE TABLE IF NOT EXISTS cqlmigrate.locks (name text PRIMARY KEY, client text);

Library usage

To apply all .cql files located in /cql in the classpath:

import com.datastax.driver.core.*;

// Configure locking for coordination of multiple nodes
CassandraLockConfig lockConfig = CassandraLockConfig.builder()
        .withTimeout(Duration.standardSeconds(3))
        .withPollingInterval(Duration.millis(500))
        .build();

// Create a Cassandra session
Cluster cluster = Cluster.builder.addContactPoint("localhost").build();
Session session = cluster.connect();

// Create a migrator and run it
CqlMigrator migrator = CqlMigratorFactory.create(lockConfig);
Path schemas = Paths.get(ClassLoader.getSystemResource("/cql").toURI());
migrator.migrate(session, "my_keyspace", asList(schemas));

The migrator will look for a bootstrap.cql file for setting up the keyspace.

Standalone usage

$ java -Dhosts=localhost,192.168.1.1 -Dkeyspace=my_keyspace -Ddirectories=cql-common,cql-local -jar cqlmigrate.jar

Specify credentials, if required, using -Dusername=<username> and -Dpassword=<password>.

What it does

  1. Checks all nodes are up and their schemas are in agreement.

  2. Tries to acquire a lock for the keyspace. If it can't initially be acquired it will continue to retry at a set polling time until the timeout is reached.

  3. Looks for a bootstrap.cql file and runs it first. This file should contain the keyspace definition:

    CREATE KEYSPACE my_keyspace
     WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1 };
    
  4. Applies .cql files one by one, sorted by filename in ascending order. It is suggested to prefix the files with a datetime to order them:

    /cql/2015-05-02-19:20-create-airplanes-table.cql
    /cql/2015-05-03-14:19-add-manufacturer-column.cql
    

    Any previously applied files will be skipped.

  5. Releases the lock.

schema_updates table

This table is used to determine what has been previously applied.

SELECT * FROM schema_updates;

 filename                         | applied_on               | checksum
----------------------------------+--------------------------+------------------------------------------
           0001-create-tables.cql | 2015-04-08 12:10:04+0100 | ec19dfac7ede62b2a40c0f39706b237cd5c30da6
                 0002-dataset.cql | 2015-04-08 12:10:04+0100 | 4fa2d6c4fae9950f0c9140ae2eb57fe689192b4a
            0003-initial-date.cql | 2015-04-08 12:10:04+0100 | 19d0c9522b6464a06b18192c6e04233f83e78a84

(3 rows)

It also maintains a checksum to ensure the script hasn't changed since it was last applied.

locks keyspace and table

The locks keyspace replication class and factor can be configured using the LocksConfig. This table is used to keep track of what locks are currently in place, and relies on Cassandra's lightweight transactions.

SELECT * FROM locks;

 name                                | client
-------------------------------------+--------------------------------------
 airplanes_keyspace.schema_migration | 2a4ec2ae-d3d1-4b33-86a9-eb844e35eeeb

(1 rows)

Each lock will be deleted by cqlmigrate once the migration is complete.

Supported Cassandra versions

This project has been tested against the following versions:

  • DSE 5.0.2 (3.0.8)
  • 2.1.7
  • DSE 4.7.3 (2.1.8)
  • 2.2.2
  • 2.2.3

Caveats

Cassandra is an eventually consistent, AP database, and so applying schema updates are not as simple as a traditional relational database.

  • Certain schema changes can cause data corruption on cassandra. Be very careful changing a schema for a table that is being actively written to. Generally, adding columns is safe (but be careful with collection types). Test the migration before rolling out to production.

  • AP properties of Cassandra also apply to schema updates - so it is possible for a cluster to have an inconsistent schema across nodes in case of split brain or other situation. cqlmigrate tries to alleviate this with appropriate consistency levels.

Cql File Comments

There are a number of ways to add comments to your cql files.

For inline comments prepend -- to your comment, e.g:

-- Select Queries
SELECT * FROM schema_updates;

For multiline comments wrap them with /* and */, e.g:

/*
    Added by John Smith
    19th September 2017
*/
SELECT * FROM schema_updates;

Contributors

Originally developed by the Cirrus team at Sky.

  • Adam Dougal
  • James Booth
  • James Ravn
  • Adrian Ng
  • Malinda Rajapakse
  • Ashutosh Gawande
  • Dominic Mullings
  • Yoseph Sultan
  • David Sale
  • Supreeth Rao
  • Jose Taboada

cqlmigrate's People

Contributors

adrianmkng avatar baddapuri9 avatar balooo avatar bitsetd4d avatar chbatey avatar chuckydev avatar dogan-sky avatar howardburgess avatar joseblas avatar jsravn avatar marktickner avatar oliverlockwood avatar razvanth avatar saley89 avatar sebduff avatar ssserj avatar supreethrao avatar totahuanocotl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.