From fb21e4a17e89f7e76e5dfa3df506db03f50f1a28 Mon Sep 17 00:00:00 2001 From: "pavel.voropaev" Date: Mon, 13 Dec 2021 01:22:15 +0000 Subject: [PATCH] Fixed cassandra tests --- .dockerignore | 4 +- build/Dockerfile-cassandra | 6 ++ build/cassandra-entrypoint.sh | 65 +++++++++++++++++++ .../io/getquill/CassandraCeContext.scala | 23 ++++--- 4 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 build/cassandra-entrypoint.sh diff --git a/.dockerignore b/.dockerignore index f59ec20aab..3ed725abfe 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,3 @@ -* \ No newline at end of file +* +!/quill-cassandra/src/test/cql/cassandra-schema.cql +!/build/cassandra-entrypoint.sh diff --git a/build/Dockerfile-cassandra b/build/Dockerfile-cassandra index eec7840e98..f934ab04a1 100644 --- a/build/Dockerfile-cassandra +++ b/build/Dockerfile-cassandra @@ -2,3 +2,9 @@ FROM cassandra:3.11.11 MAINTAINER deusaquilus@gmail.com RUN apt-get update; DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends default-jdk +ENV LANG C.UTF-8 +COPY ./build/cassandra-entrypoint.sh /entrypoint.sh +RUN chmod 755 /entrypoint.sh +COPY ./quill-cassandra/src/test/cql/*.cql /docker-entrypoint-initdb.d/ +ENTRYPOINT ["/entrypoint.sh"] +CMD ["cassandra", "-f"] \ No newline at end of file diff --git a/build/cassandra-entrypoint.sh b/build/cassandra-entrypoint.sh new file mode 100644 index 0000000000..d6ac88d314 --- /dev/null +++ b/build/cassandra-entrypoint.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +## +## This script will generate a patched docker-entrypoint.sh that: +## - executes any *.sh script found in /docker-entrypoint-initdb.d +## - boots cassandra up +## - executes any *.cql script found in docker-entrypoint-initdb.d +## +## It is compatible with any cassandra:* image +## + + +## Create script that executes files found in docker-entrypoint-initdb.d/ + +cat <<'EOF' >> /run-init-scripts.sh +#!/usr/bin/env bash + +LOCK=/var/lib/cassandra/_init.done +INIT_DIR=docker-entrypoint-initdb.d + +if [ -f "$LOCK" ]; then + echo "@@ Initialization already performed." + exit 0 +fi + +cd $INIT_DIR + +echo "@@ Executing bash scripts found in $INIT_DIR" + +# execute scripts found in INIT_DIR +for f in $(find . -type f -name "*.sh" -executable -print | sort); do + echo "$0: sourcing $f" + . "$f" + echo "$0: $f executed." +done + +# wait for cassandra to be ready and execute cql in background +( + while ! cqlsh -e 'describe cluster' > /dev/null 2>&1; do sleep 6; done + echo "$0: Cassandra cluster ready: executing cql scripts found in $INIT_DIR" + for f in $(find . -type f -name "*.cql" -print | sort); do + echo "$0: running $f" + cqlsh -f "$f" + echo "$0: $f executed" + done + # mark things as initialized (in case /var/lib/cassandra was mapped to a local folder) + touch $LOCK +) & + +EOF + +## Patch existing entrypoint to call our script in the background +# This has been inspired by https://www.thetopsites.net/article/51594713.shtml +EP=/patched-entrypoint.sh +sed '$ d' /docker-entrypoint.sh > $EP +cat <<'EOF' >> $EP +/run-init-scripts.sh & +exec "$@" +EOF + +# Make both scripts executable +chmod +x /run-init-scripts.sh +chmod +x $EP + +# Call the new entrypoint +$EP "$@" \ No newline at end of file diff --git a/quill-cassandra-ce/src/main/scala-2.13/io/getquill/CassandraCeContext.scala b/quill-cassandra-ce/src/main/scala-2.13/io/getquill/CassandraCeContext.scala index 0018679bff..91ba7e5fd5 100644 --- a/quill-cassandra-ce/src/main/scala-2.13/io/getquill/CassandraCeContext.scala +++ b/quill-cassandra-ce/src/main/scala-2.13/io/getquill/CassandraCeContext.scala @@ -3,13 +3,13 @@ package io.getquill import cats._ import cats.effect._ import cats.syntax.all._ -import fs2.{Chunk, Stream} +import fs2.{ Chunk, Stream } import com.typesafe.config.Config import com.datastax.oss.driver.api.core.cql.Row import com.datastax.oss.driver.api.core.CqlSession import com.datastax.oss.driver.api.core.cql.AsyncResultSet import io.getquill.context.cassandra.CqlIdiom -import io.getquill.util.{ContextLogger, LoadConfig} +import io.getquill.util.{ ContextLogger, LoadConfig } import io.getquill.context.ExecutionInfo import io.getquill.context.ce.CeContext @@ -17,12 +17,12 @@ import scala.jdk.CollectionConverters._ import scala.language.higherKinds class CassandraCeContext[N <: NamingStrategy, F[_]]( - naming: N, - session: CqlSession, - preparedStatementCacheSize: Long - )(implicit val af: Async[F]) + naming: N, + session: CqlSession, + preparedStatementCacheSize: Long +)(implicit val af: Async[F]) extends CassandraCqlSessionContext[N](naming, session, preparedStatementCacheSize) - with CeContext[CqlIdiom, N, F] { + with CeContext[CqlIdiom, N, F] { private val logger = ContextLogger(classOf[CassandraCeContext[_, F]]) @@ -39,8 +39,7 @@ class CassandraCeContext[N <: NamingStrategy, F[_]]( if (rem > 0) af.delay(Some(Chunk.iterable(rs.currentPage().asScala), rs.remaining())) else - af.pure(None) - ) + af.pure(None)) def streamQuery[T](cql: String, prepare: Prepare = identityPrepare, extractor: Extractor[T] = identityExtractor)(info: ExecutionInfo, dc: Runner): StreamResult[T] = { Stream @@ -71,13 +70,13 @@ class CassandraCeContext[N <: NamingStrategy, F[_]]( object CassandraCeContext { - def apply[N <: NamingStrategy, F[_] : Async : FlatMap](naming: N, config: CassandraContextConfig): CassandraCeContext[N, F] = + def apply[N <: NamingStrategy, F[_]: Async: FlatMap](naming: N, config: CassandraContextConfig): CassandraCeContext[N, F] = new CassandraCeContext(naming, config.session, config.preparedStatementCacheSize) - def apply[N <: NamingStrategy, F[_] : Async : FlatMap](naming: N, config: Config): CassandraCeContext[N, F] = + def apply[N <: NamingStrategy, F[_]: Async: FlatMap](naming: N, config: Config): CassandraCeContext[N, F] = CassandraCeContext(naming, CassandraContextConfig(config)) - def apply[N <: NamingStrategy, F[_] : Async : FlatMap](naming: N, configPrefix: String): CassandraCeContext[N, F] = + def apply[N <: NamingStrategy, F[_]: Async: FlatMap](naming: N, configPrefix: String): CassandraCeContext[N, F] = CassandraCeContext(naming, LoadConfig(configPrefix)) }