sky-uk / cqlmigrate

Cassandra schema migration library
BSD 3-Clause "New" or "Revised" License
47 stars 29 forks source link

Example results in Keyspace cqlmigrate does not exist #52

Closed samthebest closed 7 years ago

samthebest commented 7 years ago

Followed example in the README, and fails with the following.

uk.sky.cqlmigrate.exception.CannotAcquireLockException: Query to prepare locks queries failed
[info]   at uk.sky.cqlmigrate.CassandraLockingMechanism.init(CassandraLockingMechanism.java:43)
[info]   at uk.sky.cqlmigrate.Lock.lock(Lock.java:38)
[info]   at uk.sky.cqlmigrate.CqlMigratorImpl.migrate(CqlMigratorImpl.java:77)
[info]   ...

[info]   Cause: com.datastax.driver.core.exceptions.InvalidQueryException: Keyspace cqlmigrate does not exist
[info]   at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:50)
[info]   at com.datastax.driver.core.DriverThrowables.propagateCause(DriverThrowables.java:37)
[info]   at com.datastax.driver.core.AbstractSession.prepare(AbstractSession.java:98)
[info]   at uk.sky.cqlmigrate.CassandraLockingMechanism.init(CassandraLockingMechanism.java:39)
[info]   at uk.sky.cqlmigrate.Lock.lock(Lock.java:38)
[info]   at uk.sky.cqlmigrate.CqlMigratorImpl.migrate(CqlMigratorImpl.java:77)

Code:

import java.io.{FileNotFoundException, File}
import java.nio.file.{Paths, Path}
import java.time.Duration

import com.datastax.driver.core.Session
import uk.sky.cqlmigrate.{CqlMigratorFactory, CqlMigrator, CassandraLockConfig}
import scala.collection.JavaConversions._

...

val lockConfig: CassandraLockConfig =
      CassandraLockConfig.builder()
      .withTimeout(Duration.ofSeconds(3))
      .withPollingInterval(Duration.ofMillis(100))
      .build()

    val migrator: CqlMigrator = CqlMigratorFactory.create(lockConfig)
    val schemas: Path = Paths.get(
      Option(ClassLoader.getSystemResource(toSchemaResourcePath.stripPrefix("/")))
      .getOrElse(throw new FileNotFoundException(toSchemaResourcePath))
      .toURI())
    migrator.migrate(session, keyspace, List(schemas))
oliverlockwood commented 7 years ago

Hi @samthebest, thanks for getting in touch.

This behaviour is expected - as per https://github.com/sky-uk/cqlmigrate#cassandra-prerequisites:

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

Creating the locks keyspace and table should be a one-off that applies regardless of how many apps you're using cqlmigrate for, but it has to be done outside the scope of cqlmigrate otherwise there's a chicken-and-egg problem: how can you perform locking around creating the locking mechanism? 😃

Hope that makes sense. I'm going to close this issue now as the behaviour you've experienced is working as designed.

samthebest commented 7 years ago

Thanks @oliverlockwood this thread ought to serve future googlers well.

oliverlockwood commented 7 years ago

Agreed :+1: