Skip to content

Commit

Permalink
Added a keyspace is set field
Browse files Browse the repository at this point in the history
  • Loading branch information
aswinbennyofficial committed Nov 16, 2024
1 parent 10c9494 commit aa5a333
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cassandra:
- "cassandra-node1"
port: 9042
keyspace: "myapp_keyspace"
keyspace_isset: false
username: "myapp_user"
password: "your_secure_password"
consistency: "QUORUM"
Expand Down
24 changes: 14 additions & 10 deletions src/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (

func NewCassandraConnection(config config.CassandraConfig, log zerolog.Logger) (*gocql.Session,error){
cluster:=gocql.NewCluster(config.Hosts...)
// cluster.Keyspace=config.Keyspace
if(config.IsKeySpaceSet){
cluster.Keyspace=config.Keyspace
}
cluster.Port=config.Port
cluster.Consistency=gocql.Quorum
if config.Username!="" && config.Password!="" {
Expand All @@ -32,15 +34,17 @@ func NewCassandraConnection(config config.CassandraConfig, log zerolog.Logger) (
session, err = cluster.CreateSession()
if err == nil {
log.Info().Msgf("Connected to Cassandra on attempt %d", attempt)
if err=ensureKeyspace(session, config, log);err!=nil{
return nil,err
}
session.Close()

cluster.Keyspace = config.Keyspace
session, err = cluster.CreateSession()
if err!=nil{
return nil,err
if(!config.IsKeySpaceSet){
if err=ensureKeyspace(session, config, log);err!=nil{
return nil,err
}
session.Close()

cluster.Keyspace = config.Keyspace
session, err = cluster.CreateSession()
if err!=nil{
return nil,err
}
}


Expand Down
1 change: 1 addition & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type CassandraConfig struct {
Replication CasandraReplication `mapstructure:"replication"`
ProtoVersion int `mapstructure:"proto_version"`
Migrations MigrationsConfig `mapstructure:"migrations"`
IsKeySpaceSet bool `mapstructure:"keyspace_isset"`
}

type CasandraReplication struct{
Expand Down

0 comments on commit aa5a333

Please sign in to comment.