Skip to content

Commit

Permalink
fix: add support for MariaDB as a data source option
Browse files Browse the repository at this point in the history
  • Loading branch information
eddydecena committed Jul 31, 2024
1 parent 92333bf commit 4e8b00a
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Once the Docker installation is complete, go to [http://localhost:8888/v1/swagge

* PostgreSQL
* MySQL
* MariaDB

<!-- CONTRIBUTING -->
## Contributing
Expand Down
8 changes: 0 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,3 @@ services:
CATALOG_MANAGEMENT: dynamic
ports:
- "8080:8080"

db:
image: mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: poligono
9 changes: 6 additions & 3 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ const docTemplate = `{
"type": {
"enum": [
"PostgreSQL",
"MySQL"
"MySQL",
"MariaDB"
],
"allOf": [
{
Expand All @@ -394,11 +395,13 @@ const docTemplate = `{
"type": "string",
"enum": [
"PostgreSQL",
"MySQL"
"MySQL",
"MariaDB"
],
"x-enum-varnames": [
"PostgreSQL",
"MySQL"
"MySQL",
"MariaDB"
]
},
"models.GenerateQueryActivity": {
Expand Down
9 changes: 6 additions & 3 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@
"type": {
"enum": [
"PostgreSQL",
"MySQL"
"MySQL",
"MariaDB"
],
"allOf": [
{
Expand All @@ -385,11 +386,13 @@
"type": "string",
"enum": [
"PostgreSQL",
"MySQL"
"MySQL",
"MariaDB"
],
"x-enum-varnames": [
"PostgreSQL",
"MySQL"
"MySQL",
"MariaDB"
]
},
"models.GenerateQueryActivity": {
Expand Down
3 changes: 3 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ definitions:
enum:
- PostgreSQL
- MySQL
- MariaDB
required:
- name
- secret
Expand All @@ -20,10 +21,12 @@ definitions:
enum:
- PostgreSQL
- MySQL
- MariaDB
type: string
x-enum-varnames:
- PostgreSQL
- MySQL
- MariaDB
models.GenerateQueryActivity:
properties:
data:
Expand Down
3 changes: 2 additions & 1 deletion internal/models/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type DataSourceType string
const (
PostgreSQL DataSourceType = "PostgreSQL"
MySQL DataSourceType = "MySQL"
MariaDB DataSourceType = "MariaDB"
)

type EngineType string
Expand All @@ -25,7 +26,7 @@ type DataSource struct {
Name string `json:"name" bson:"name" validate:"required"`
OrganizationId string `json:"organization_id" bson:"organization_id" validate:"required" swaggerignore:"true"`
CreatedBy string `json:"-" bson:"created_by" validate:"required"`
Type DataSourceType `json:"type" bson:"type" validate:"required,oneof=PostgreSQL MySQL"`
Type DataSourceType `json:"type" bson:"type" validate:"required,oneof=PostgreSQL MySQL MariaDB"`
Secret string `json:"secret,omitempty" bson:"-" validate:"required"`
CreatedAt time.Time `json:"created_at" bson:"created_at" validate:"required" swaggerignore:"true"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at" validate:"required" swaggerignore:"true"`
Expand Down
7 changes: 6 additions & 1 deletion internal/services/ds.service.impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ func (self *DataSourceServiceImpl) CreateCatalog(catalogName string, dataSourceT
case models.MySQL:
mysql := NewMySQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
err = mysql.CreateCatalog(catalogName, dataSourceType, secret)
case models.MariaDB:
mariadb := NewMariaDBDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
err = mariadb.CreateCatalog(catalogName, dataSourceType, secret)
default:
return errors.New("Invalid Data Source Type")
}
Expand All @@ -310,12 +313,14 @@ func (self *DataSourceServiceImpl) Sync(dataSourceName string, organizationId st

switch dataSourceType {
case models.PostgreSQL:
logger.Info.Println("Sync for POSTGRESQL")
psql := NewPostgreSQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
err = psql.Sync(dataSourceName, organizationId)
case models.MySQL:
mysql := NewMySQLDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
err = mysql.Sync(dataSourceName, organizationId)
case models.MariaDB:
mariadb := NewMariaDBDataSourceDatabase(self.ctx, self.engineService, self.schemaService)
err = mariadb.Sync(dataSourceName, organizationId)
default:
return errors.New("Invalid Data Source Type")
}
Expand Down
103 changes: 103 additions & 0 deletions internal/services/ds.type.mariadb.impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package services

import (
"context"
"encoding/json"
"fmt"
"strconv"

"github.com/poligonoio/vega-core/internal/models"
)

type MariaDBDataSourceTypeImpl struct {
ctx context.Context
engineService EngineService
schemaService SchemaService
}

func NewMariaDBDataSourceDatabase(ctx context.Context, engineService EngineService, schemaService SchemaService) DataSourceTypeInter {
return &MariaDBDataSourceTypeImpl{
ctx: ctx,
engineService: engineService,
schemaService: schemaService,
}
}

func (self *MariaDBDataSourceTypeImpl) Sync(dataSourceName string, organizationId string) error {
catalogName := self.engineService.GetCatalogName(dataSourceName, organizationId)

var sqlSchemas []models.SQLSchema
err := self.engineService.Query(fmt.Sprintf("SELECT schema_name AS name FROM %s.information_schema.schemata WHERE schema_name NOT IN ('sys', 'performance_schema')", catalogName), &sqlSchemas)
if err != nil {
return err
}

for _, sqlSchema := range sqlSchemas {
var sqlTables []models.SQLSchema

err := self.engineService.Query(fmt.Sprintf("SELECT table_name AS name FROM %s.information_schema.tables WHERE table_schema = '%s'", catalogName, sqlSchema.Name), &sqlTables)
if err != nil {
return err
}

var tables []models.Table
for _, sqlTable := range sqlTables {
var sqlFields []models.SQLField
err := self.engineService.Query(fmt.Sprintf("SELECT column_name AS name FROM %s.information_schema.columns WHERE table_name = '%s'", catalogName, sqlTable.Name), &sqlFields)
if err != nil {
return err
}

var fields []models.Field
for _, sqlField := range sqlFields {
field := models.Field{
Name: sqlField.Name,
}
fields = append(fields, field)
}

table := models.Table{
Name: sqlTable.Name,
Fields: fields,
}
tables = append(tables, table)
}

schema := models.Schema{
Name: sqlSchema.Name,
Tables: tables,
OrganizationId: organizationId,
DataSourceName: dataSourceName,
}

err = self.schemaService.Create(schema)
if err != nil {
return err
}
}

return nil
}

func (self *MariaDBDataSourceTypeImpl) CreateCatalog(catalogName string, dataSourceType models.DataSourceType, secret string) error {
mysql := models.MySQLSecret{}
if err := json.Unmarshal([]byte(secret), &mysql); err != nil {
return err
}

var mysqlString string

if mysql.SSL {
mysqlString = fmt.Sprintf("jdbc:mariadb://%s:%s/%s?sslMode=REQUIRED", mysql.Host, strconv.Itoa(mysql.Port), mysql.Database)
} else {
mysqlString = fmt.Sprintf("jdbc:mariadb://%s:%s/%s", mysql.Host, strconv.Itoa(mysql.Port), mysql.Database)
}

query := fmt.Sprintf("CREATE CATALOG %s USING mariadb WITH (\"connection-url\" = '%s', \"connection-user\" = '%s', \"connection-password\" = '%s', \"case-insensitive-name-matching\" = 'true')", catalogName, mysqlString, mysql.User, mysql.Password)

if err := self.engineService.Query(query, nil); err != nil {
return err
}

return nil
}
1 change: 0 additions & 1 deletion internal/services/infisical.service.impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func (self *InfisicalServiceImpl) GetSecret(key string) (string, error) {
}

func (self *InfisicalServiceImpl) CreateSecret(key string, secret string) error {
logger.Info.Println(self.token)
url := fmt.Sprintf("https://app.infisical.com/api/v3/secrets/raw/%s", key)

requestBody := models.InfisicalCreateSecretRequestBody{
Expand Down

0 comments on commit 4e8b00a

Please sign in to comment.