Compare commits
8 commits
ae77b65281
...
11b676446d
Author | SHA1 | Date | |
---|---|---|---|
Simon Vieille | 11b676446d | ||
Simon Vieille | 801b11e33f | ||
Simon Vieille | 40d474b6eb | ||
Simon Vieille | 83682511b3 | ||
Simon Vieille | 60cbaf1530 | ||
Simon Vieille | 5be7927799 | ||
Simon Vieille | 038d0a2d6b | ||
Simon Vieille | 16bdb470dc |
|
@ -45,4 +45,4 @@ services:
|
|||
image: *mysql_image
|
||||
environment:
|
||||
- MYSQL_DATABASE=test
|
||||
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
|
||||
- MARIADB_ROOT_PASSWORD=root
|
||||
|
|
32
app/app.go
32
app/app.go
|
@ -60,8 +60,13 @@ func (a *App) TruncateTable(c config.SchemaConfigAction) error {
|
|||
pCounter := 1
|
||||
|
||||
for _, col := range c.PrimaryKey {
|
||||
pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
|
||||
pCounter = pCounter + 1
|
||||
if row[col].IsInteger {
|
||||
value, _ := strconv.Atoi(row[col].Value)
|
||||
pkeys = append(pkeys, fmt.Sprintf("%s=%d", col, value))
|
||||
} else {
|
||||
pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
|
||||
pCounter = pCounter + 1
|
||||
}
|
||||
}
|
||||
|
||||
sql := fmt.Sprintf(
|
||||
|
@ -74,8 +79,10 @@ func (a *App) TruncateTable(c config.SchemaConfigAction) error {
|
|||
pCounter = 1
|
||||
|
||||
for _, col := range c.PrimaryKey {
|
||||
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), row[col].Value)
|
||||
pCounter = pCounter + 1
|
||||
if !row[col].IsInteger {
|
||||
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), row[col].Value)
|
||||
pCounter = pCounter + 1
|
||||
}
|
||||
}
|
||||
|
||||
a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan)
|
||||
|
@ -148,19 +155,23 @@ func (a *App) UpdateRows(c config.SchemaConfigAction, globalColumns map[string]s
|
|||
for _, row := range rows {
|
||||
updates := []string{}
|
||||
pkeys := []string{}
|
||||
values := make(map[int]string)
|
||||
values := make(map[int][]string)
|
||||
pCounter := 1
|
||||
|
||||
for col, value := range row {
|
||||
if value.IsUpdated && !value.IsVirtual {
|
||||
values[pCounter] = value.Value
|
||||
if value.IsInteger {
|
||||
values[pCounter] = []string{value.Value, "int"}
|
||||
} else {
|
||||
values[pCounter] = []string{value.Value, "string"}
|
||||
}
|
||||
updates = append(updates, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
|
||||
pCounter = pCounter + 1
|
||||
}
|
||||
}
|
||||
|
||||
for _, col := range c.PrimaryKey {
|
||||
values[pCounter] = row[col].Value
|
||||
values[pCounter] = []string{row[col].Value, "string"}
|
||||
pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
|
||||
pCounter = pCounter + 1
|
||||
}
|
||||
|
@ -177,7 +188,12 @@ func (a *App) UpdateRows(c config.SchemaConfigAction, globalColumns map[string]s
|
|||
pCounter = 1
|
||||
|
||||
for i, value := range values {
|
||||
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(i)), value)
|
||||
if value[1] == "string" {
|
||||
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(i)), value[0])
|
||||
} else {
|
||||
newValue, _ := strconv.Atoi(value[0])
|
||||
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(i)), newValue)
|
||||
}
|
||||
}
|
||||
|
||||
a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan)
|
||||
|
|
|
@ -2,7 +2,9 @@ package app
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/lib/pq"
|
||||
"gitnet.fr/deblan/database-anonymizer/config"
|
||||
"gitnet.fr/deblan/database-anonymizer/faker"
|
||||
"testing"
|
||||
|
@ -47,30 +49,30 @@ func TestAppDoAction(t *testing.T) {
|
|||
func TestAppRun(t *testing.T) {
|
||||
schema, _ := config.LoadSchemaConfigFromFile("../tests/schema.yml")
|
||||
|
||||
dsns := []string{
|
||||
"mysql://root:root@tcp(service-mysql)/test",
|
||||
// "postgres://postgres@tcp(service-postgres)/test",
|
||||
}
|
||||
dsns := make(map[string]string)
|
||||
dsns["mysql"] = "mysql://root:root@tcp(service-mysql)/test"
|
||||
dsns["postgres"] = "postgres://postgres:postgres@service-postgres:5432/test?sslmode=disable"
|
||||
|
||||
for _, dsn := range dsns {
|
||||
var count int
|
||||
|
||||
for dbtype, dsn := range dsns {
|
||||
databaseConfig, _ := config.LoadDatabaseConfig(dsn)
|
||||
db, _ := sql.Open(databaseConfig.Type, databaseConfig.Dsn)
|
||||
app := App{}
|
||||
app.Run(db, schema, faker.NewFakeManager(), databaseConfig)
|
||||
|
||||
var count int
|
||||
row := db.QueryRow("SELECT COUNT(*) FROM table_truncate1")
|
||||
row.Scan(&count)
|
||||
|
||||
if count != 0 {
|
||||
t.Fatalf("TestAppRuny: table_truncate1 check failed")
|
||||
t.Fatalf(fmt.Sprintf("TestAppRuny: table_truncate1 check failed (%s)", dbtype))
|
||||
}
|
||||
|
||||
row = db.QueryRow("SELECT COUNT(*) FROM table_truncate2")
|
||||
row.Scan(&count)
|
||||
|
||||
if count != 1 {
|
||||
t.Fatalf("TestAppRuny: table_truncate2 check failed")
|
||||
t.Fatalf(fmt.Sprintf("TestAppRuny: table_truncate2 check failed (%s)", dbtype))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,12 @@ func LoadDatabaseConfig(dsn string) (DatabaseConfig, error) {
|
|||
|
||||
dbType := elements[0]
|
||||
|
||||
config.Dsn = strings.Replace(dsn, fmt.Sprintf("%s://", dbType), "", 1)
|
||||
if dbType == "postgres" {
|
||||
config.Dsn = dsn
|
||||
} else {
|
||||
config.Dsn = strings.Replace(dsn, fmt.Sprintf("%s://", dbType), "", 1)
|
||||
}
|
||||
|
||||
config.Type = elements[0]
|
||||
|
||||
return config, nil
|
||||
|
|
|
@ -16,22 +16,26 @@ type Data struct {
|
|||
IsVirtual bool
|
||||
IsPrimaryKey bool
|
||||
IsUpdated bool
|
||||
IsInteger bool
|
||||
}
|
||||
|
||||
func (d *Data) FromByte(v []byte) *Data {
|
||||
d.Value = string(v)
|
||||
d.IsInteger = false
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *Data) FromInt64(v int64) *Data {
|
||||
d.Value = strconv.FormatInt(v, 10)
|
||||
d.IsInteger = true
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *Data) FromString(v string) *Data {
|
||||
d.Value = v
|
||||
d.IsInteger = false
|
||||
|
||||
return d
|
||||
}
|
||||
|
|
|
@ -15,6 +15,10 @@ func TestDataFroms(t *testing.T) {
|
|||
t.Fatalf("TestDataFroms: FromInt64 check failed")
|
||||
}
|
||||
|
||||
if !d.IsInteger {
|
||||
t.Fatalf("TestDataFroms: FromInt64 + IsInteger check failed")
|
||||
}
|
||||
|
||||
v := []byte{'A', 'B', 'C'}
|
||||
|
||||
d.FromByte(v)
|
||||
|
@ -22,6 +26,10 @@ func TestDataFroms(t *testing.T) {
|
|||
if d.Value != "ABC" {
|
||||
t.Fatalf("TestDataFroms: FromByte check failed")
|
||||
}
|
||||
|
||||
if d.IsInteger {
|
||||
t.Fatalf("TestDataFroms FromByte + IsInteger check failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDataIsTwigExpression(t *testing.T) {
|
||||
|
|
25
tests/postgres_data.sql
Normal file
25
tests/postgres_data.sql
Normal file
|
@ -0,0 +1,25 @@
|
|||
DROP TABLE IF EXISTS "table_truncate1";
|
||||
DROP SEQUENCE IF EXISTS table_truncate1_id_seq;
|
||||
CREATE SEQUENCE table_truncate1_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;
|
||||
|
||||
CREATE TABLE "public"."table_truncate1" (
|
||||
"id" integer DEFAULT nextval('table_truncate1_id_seq') NOT NULL,
|
||||
CONSTRAINT "table_truncate1_pkey" PRIMARY KEY ("id")
|
||||
) WITH (oids = false);
|
||||
|
||||
INSERT INTO "table_truncate1" ("id") VALUES (1), (2), (3);
|
||||
|
||||
DROP TABLE IF EXISTS "table_truncate2";
|
||||
DROP SEQUENCE IF EXISTS table_truncate2_id_seq;
|
||||
CREATE SEQUENCE table_truncate2_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;
|
||||
|
||||
CREATE TABLE "public"."table_truncate2" (
|
||||
"id" integer DEFAULT nextval('table_truncate2_id_seq') NOT NULL,
|
||||
"delete_me" boolean NOT NULL,
|
||||
CONSTRAINT "table_truncate2_pkey" PRIMARY KEY ("id")
|
||||
) WITH (oids = false);
|
||||
|
||||
INSERT INTO "table_truncate2" ("id", "delete_me") VALUES
|
||||
(1, 't'),
|
||||
(2, 't'),
|
||||
(3, 'f');
|
|
@ -2,7 +2,6 @@ rules:
|
|||
actions:
|
||||
- table: table_truncate1
|
||||
truncate: true
|
||||
|
||||
- table: table_truncate2
|
||||
query: 'select * from table_truncate2 where delete_me=1'
|
||||
query: 'select * from table_truncate2 where delete_me=true'
|
||||
truncate: true
|
||||
|
|
Loading…
Reference in a new issue