Skip to content

Commit

Permalink
Added schemaCreator and comments in workflow
Browse files Browse the repository at this point in the history
Signed-off-by: Alok Kumar Singh <[email protected]>
  • Loading branch information
akstron committed Nov 28, 2024
1 parent 0167cb6 commit 07b99da
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-e2e-cassandra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
major: 5.x
schema: v004
exclude:
# Exclude v1 as create schema on fly is available for v2 only
- jaeger-version: v1
skip-apply-schema: true
name: ${{ matrix.version.distribution }} ${{ matrix.version.major }} ${{ matrix.jaeger-version }}
Expand Down
7 changes: 6 additions & 1 deletion pkg/cassandra/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ func (c *Configuration) newSessionPrerequisites() error {
return err
}

Check warning on line 204 in pkg/cassandra/config/config.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/config/config.go#L198-L204

Added lines #L198 - L204 were not covered by tests

return generateSchemaIfNotPresent(session, &c.Schema)
sc := schemaCreator{
session: session,
schema: c.Schema,
}

return sc.createSchemaIfNotPresent()

Check warning on line 211 in pkg/cassandra/config/config.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/config/config.go#L206-L211

Added lines #L206 - L211 were not covered by tests
}

// NewSession creates a new Cassandra session
Expand Down
45 changes: 26 additions & 19 deletions pkg/cassandra/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,22 @@ type TemplateParams struct {
DependenciesTTLInSeconds int64
}

func constructTemplateParams(cfg Schema) TemplateParams {
type schemaCreator struct {
session cassandra.Session
schema Schema
}

func (sc *schemaCreator) constructTemplateParams() TemplateParams {
return TemplateParams{
Keyspace: cfg.Keyspace,
Replication: fmt.Sprintf("{'class': 'NetworkTopologyStrategy', 'replication_factor': '%v' }", cfg.ReplicationFactor),
CompactionWindowInMinutes: int64(cfg.CompactionWindow / time.Minute),
TraceTTLInSeconds: int64(cfg.TraceTTL / time.Second),
DependenciesTTLInSeconds: int64(cfg.DependenciesTTL / time.Second),
Keyspace: sc.schema.Keyspace,
Replication: fmt.Sprintf("{'class': 'NetworkTopologyStrategy', 'replication_factor': '%v' }", sc.schema.ReplicationFactor),
CompactionWindowInMinutes: int64(sc.schema.CompactionWindow / time.Minute),
TraceTTLInSeconds: int64(sc.schema.TraceTTL / time.Second),
DependenciesTTLInSeconds: int64(sc.schema.DependenciesTTL / time.Second),
}

Check warning on line 45 in pkg/cassandra/config/schema.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/config/schema.go#L38-L45

Added lines #L38 - L45 were not covered by tests
}

func getQueryFileAsBytes(fileName string, params TemplateParams) ([]byte, error) {
func (sc *schemaCreator) getQueryFileAsBytes(fileName string, params TemplateParams) ([]byte, error) {

Check failure on line 48 in pkg/cassandra/config/schema.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'sc' is not referenced in method's body, consider removing or renaming it as _ (revive)
tmpl, err := template.ParseFS(schemaFile, fileName)
if err != nil {
return nil, err
Expand All @@ -55,7 +60,7 @@ func getQueryFileAsBytes(fileName string, params TemplateParams) ([]byte, error)
return result.Bytes(), nil

Check warning on line 60 in pkg/cassandra/config/schema.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/config/schema.go#L60

Added line #L60 was not covered by tests
}

func getQueriesFromBytes(queryFile []byte) ([]string, error) {
func (sc *schemaCreator) getQueriesFromBytes(queryFile []byte) ([]string, error) {

Check failure on line 63 in pkg/cassandra/config/schema.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'sc' is not referenced in method's body, consider removing or renaming it as _ (revive)
lines := bytes.Split(queryFile, []byte("\n"))

var extractedLines [][]byte
Expand All @@ -68,11 +73,13 @@ func getQueriesFromBytes(queryFile []byte) ([]string, error) {
line = line[0:commentIndex]
}

if len(line) == 0 {
trimmedLine := bytes.TrimSpace(line)

if len(trimmedLine) == 0 {
continue
}

extractedLines = append(extractedLines, bytes.TrimSpace(line))
extractedLines = append(extractedLines, trimmedLine)
}

var queries []string
Expand All @@ -94,36 +101,36 @@ func getQueriesFromBytes(queryFile []byte) ([]string, error) {
return queries, nil
}

func getCassandraQueriesFromQueryStrings(session cassandra.Session, queries []string) []cassandra.Query {
func (sc *schemaCreator) getCassandraQueriesFromQueryStrings(queries []string) []cassandra.Query {
var casQueries []cassandra.Query

for _, query := range queries {
casQueries = append(casQueries, session.Query(query))
casQueries = append(casQueries, sc.session.Query(query))
}

Check warning on line 109 in pkg/cassandra/config/schema.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/config/schema.go#L104-L109

Added lines #L104 - L109 were not covered by tests

return casQueries

Check warning on line 111 in pkg/cassandra/config/schema.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/config/schema.go#L111

Added line #L111 was not covered by tests
}

func contructSchemaQueries(session cassandra.Session, cfg *Schema) ([]cassandra.Query, error) {
params := constructTemplateParams(*cfg)
func (sc *schemaCreator) contructSchemaQueries() ([]cassandra.Query, error) {
params := sc.constructTemplateParams()

queryFile, err := getQueryFileAsBytes(`v004-go-tmpl.cql.tmpl`, params)
queryFile, err := sc.getQueryFileAsBytes(`v004-go-tmpl.cql.tmpl`, params)
if err != nil {
return nil, err
}

Check warning on line 120 in pkg/cassandra/config/schema.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/config/schema.go#L114-L120

Added lines #L114 - L120 were not covered by tests

queryStrings, err := getQueriesFromBytes(queryFile)
queryStrings, err := sc.getQueriesFromBytes(queryFile)
if err != nil {
return nil, err
}

Check warning on line 125 in pkg/cassandra/config/schema.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/config/schema.go#L122-L125

Added lines #L122 - L125 were not covered by tests

casQueries := getCassandraQueriesFromQueryStrings(session, queryStrings)
casQueries := sc.getCassandraQueriesFromQueryStrings(queryStrings)

return casQueries, nil

Check warning on line 129 in pkg/cassandra/config/schema.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/config/schema.go#L127-L129

Added lines #L127 - L129 were not covered by tests
}

func generateSchemaIfNotPresent(session cassandra.Session, cfg *Schema) error {
casQueries, err := contructSchemaQueries(session, cfg)
func (sc *schemaCreator) createSchemaIfNotPresent() error {
casQueries, err := sc.contructSchemaQueries()
if err != nil {
return err
}

Check warning on line 136 in pkg/cassandra/config/schema.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/config/schema.go#L132-L136

Added lines #L132 - L136 were not covered by tests
Expand Down
7 changes: 4 additions & 3 deletions pkg/cassandra/config/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ query1-finished;
`,
}

sc := schemaCreator{}
queriesAsBytes := []byte(queriesAsString)
queries, err := getQueriesFromBytes(queriesAsBytes)
queries, err := sc.getQueriesFromBytes(queriesAsBytes)
require.NoError(t, err)

require.Equal(t, len(expGeneratedQueries), len(queries))
Expand All @@ -51,8 +52,8 @@ func TestInvalidQueryTemplate(t *testing.T) {
query2;
query-3 query-3-continue query-3-finished -- missing semicolon
`

sc := schemaCreator{}
queriesAsBytes := []byte(queriesAsString)
_, err := getQueriesFromBytes(queriesAsBytes)
_, err := sc.getQueriesFromBytes(queriesAsBytes)
require.ErrorContains(t, err, "invalid template")
}

0 comments on commit 07b99da

Please sign in to comment.