I have a micronaut app that starts up with a Scylla database and I want initialize a keyspace and a columnfamily so that people can run the app locally and have it ready to use.
For my initial approach I created a singleton cqlSession and then I create those things there.
@Singleton
public CqlSession cqlSession(ScyllaConfig scyllaConfig) throws UnknownHostException
{
CqlSessionBuilder sessionBuilder = CqlSession.builder()
.addContactPoints(scyllaConfig.contactPoints().stream().map(host -> new InetSocketAddress(host, scyllaConfig.port())).toList())
.withLocalDatacenter(scyllaConfig.datacenter());
CqlSession session = sessionBuilder.build();
session.execute("CREATE KEYSPACE IF NOT EXISTS " + scyllaConfig.keyspaceName() + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};");
session.execute("CREATE COLUMNFAMILY IF NOT EXISTS " + scyllaConfig.keyspaceName() + ".payment_data (id uuid PRIMARY KEY, account_id int, person_id int, payment_id text);");
return session;
}
This doesn't seem like the best approach. I was hoping there would be someway to plugging a cql file or something like that where I could run a bunch of cql and initialize the database, but I can't find anything on how to do that. If worse comes to worse, I could just right a bash script or something like that. Would I just call that from a docker compose file? Is there a better way?
What approaches have you taken? This seems like a common use case so I wanted to get thoughts on best approaches for doing this.