add truncate operation

This commit is contained in:
Simon Vieille 2024-03-19 13:19:44 +01:00
commit 0de68ae32d
Signed by untrusted user: deblan
GPG key ID: 579388D585F70417
2 changed files with 114 additions and 80 deletions

View file

@ -19,17 +19,25 @@ type App struct {
Db *sql.DB Db *sql.DB
} }
func (a *App) ApplyRule(c config.SchemaConfigData, globalColumns map[string]string, generators map[string][]string) error { func (a *App) DoAction(c config.SchemaConfigAction, globalColumns map[string]string, generators map[string][]string) error {
var query string var query string
if c.Table == "" { if c.Table == "" {
return errors.New("Table must be defined") return errors.New("Table must be defined")
} }
if c.Query != "" { if c.Truncate {
query = c.Query if c.Query != "" {
query = c.Query
} else {
return a.TruncateTable(c.Table)
}
} else { } else {
query = fmt.Sprintf("SELECT * FROM %s", c.Table) if c.Query != "" {
query = c.Query
} else {
query = fmt.Sprintf("SELECT * FROM %s", c.Table)
}
} }
if len(c.PrimaryKey) == 0 { if len(c.PrimaryKey) == 0 {
@ -37,40 +45,56 @@ func (a *App) ApplyRule(c config.SchemaConfigData, globalColumns map[string]stri
} }
rows := database.GetRows(a.Db, query) rows := database.GetRows(a.Db, query)
var scan any
for key, row := range rows { if c.Truncate {
if len(c.VirtualColumns) > 0 { for _, row := range rows {
for col, faker := range c.VirtualColumns { pkeys := []string{}
rows[key][col] = data.Data{ pCounter := 1
Value: "",
Faker: faker, for _, col := range c.PrimaryKey {
IsVirtual: true, pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
} pCounter = pCounter + 1
} }
}
if len(c.Columns) > 0 { sql := fmt.Sprintf(
for col, faker := range c.Columns { "DELETE FROM %s WHERE %s",
r := row[col] c.Table,
r.Faker = faker strings.Join(pkeys, " AND "),
rows[key][col] = r )
stmt := nq.NewNamedParameterQuery(sql)
pCounter = 1
for _, col := range c.PrimaryKey {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), row[col].Value)
pCounter = pCounter + 1
} }
}
if len(globalColumns) > 0 { a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan)
for col, faker := range globalColumns { }
if value, exists := row[col]; exists { } else {
if value.Faker == "" { for key, row := range rows {
value.Faker = faker if len(c.VirtualColumns) > 0 {
rows[key][col] = value for col, faker := range c.VirtualColumns {
rows[key][col] = data.Data{
Value: "",
Faker: faker,
IsVirtual: true,
} }
} }
} }
}
if len(generators) > 0 { if len(c.Columns) > 0 {
for faker, columns := range generators { for col, faker := range c.Columns {
for _, col := range columns { r := row[col]
r.Faker = faker
rows[key][col] = r
}
}
if len(globalColumns) > 0 {
for col, faker := range globalColumns {
if value, exists := row[col]; exists { if value, exists := row[col]; exists {
if value.Faker == "" { if value.Faker == "" {
value.Faker = faker value.Faker = faker
@ -79,68 +103,71 @@ func (a *App) ApplyRule(c config.SchemaConfigData, globalColumns map[string]stri
} }
} }
} }
}
for _, col := range c.PrimaryKey { if len(generators) > 0 {
value := row[col] for faker, columns := range generators {
value.IsPrimaryKey = true for _, col := range columns {
rows[key][col] = value if value, exists := row[col]; exists {
} if value.Faker == "" {
value.Faker = faker
rows[key] = a.UpdateRow(rows[key]) rows[key][col] = value
} }
}
var scan any }
}
x := 1
t := len(rows)
for _, row := range rows {
fmt.Printf("%+v/%+v\n", x, t)
x = x + 1
updates := []string{}
pkeys := []string{}
pCounter := 1
for col, value := range row {
if value.IsUpdated {
updates = append(updates, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
pCounter = pCounter + 1
} }
for _, col := range c.PrimaryKey {
value := row[col]
value.IsPrimaryKey = true
rows[key][col] = value
}
rows[key] = a.UpdateRow(rows[key])
} }
for _, col := range c.PrimaryKey { for _, row := range rows {
pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter))) updates := []string{}
pCounter = pCounter + 1 pkeys := []string{}
} pCounter := 1
if len(updates) > 0 { for col, value := range row {
sql := fmt.Sprintf(
"UPDATE %s SET %s WHERE %s",
c.Table,
strings.Join(updates, ", "),
strings.Join(pkeys, " AND "),
)
stmt := nq.NewNamedParameterQuery(sql)
pCounter = 1
for _, value := range row {
if value.IsUpdated { if value.IsUpdated {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), value.Value) updates = append(updates, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
pCounter = pCounter + 1 pCounter = pCounter + 1
} }
} }
for _, col := range c.PrimaryKey { for _, col := range c.PrimaryKey {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), row[col].Value) pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
pCounter = pCounter + 1 pCounter = pCounter + 1
} }
a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan) if len(updates) > 0 {
sql := fmt.Sprintf(
"UPDATE %s SET %s WHERE %s",
c.Table,
strings.Join(updates, ", "),
strings.Join(pkeys, " AND "),
)
// fmt.Printf("%+v\n", r) stmt := nq.NewNamedParameterQuery(sql)
pCounter = 1
for _, value := range row {
if value.IsUpdated {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), value.Value)
pCounter = pCounter + 1
}
}
for _, col := range c.PrimaryKey {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), row[col].Value)
pCounter = pCounter + 1
}
a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan)
}
} }
} }
@ -179,11 +206,17 @@ func (a *App) UpdateRow(row map[string]data.Data) map[string]data.Data {
return row return row
} }
func (a *App) TruncateTable(table string) error {
_, err := a.Db.Exec(fmt.Sprintf("TRUNCATE %s", table))
return err
}
func (a *App) Run(db *sql.DB, c config.SchemaConfig) error { func (a *App) Run(db *sql.DB, c config.SchemaConfig) error {
a.Db = db a.Db = db
for _, data := range c.Rules.Datas { for _, data := range c.Rules.Actions {
err := a.ApplyRule(data, c.Rules.Columns, c.Rules.Generators) err := a.DoAction(data, c.Rules.Columns, c.Rules.Generators)
logger.LogFatalExitIf(err) logger.LogFatalExitIf(err)
} }

View file

@ -6,19 +6,20 @@ import (
"os" "os"
) )
type SchemaConfigData struct { type SchemaConfigAction struct {
Table string `yaml:"table"` Table string `yaml:"table"`
Query string `yaml:"query"` Query string `yaml:"query"`
VirtualColumns map[string]string `yaml:"virtual_columns"` VirtualColumns map[string]string `yaml:"virtual_columns"`
Generators map[string][]string `yaml:"generators"` Generators map[string][]string `yaml:"generators"`
Columns map[string]string `yaml:"columns"` Columns map[string]string `yaml:"columns"`
PrimaryKey []string `yaml:"primary_key"` PrimaryKey []string `yaml:"primary_key"`
Truncate bool `yaml:"truncate"`
} }
type SchemaConfigRules struct { type SchemaConfigRules struct {
Columns map[string]string `yaml:"columns"` Columns map[string]string `yaml:"columns"`
Generators map[string][]string `yaml:"generators"` Generators map[string][]string `yaml:"generators"`
Datas []SchemaConfigData `yaml:"datas"` Actions []SchemaConfigAction `yaml:"actions"`
} }
type SchemaConfig struct { type SchemaConfig struct {