Skip to content

Instantly share code, notes, and snippets.

@zerosign
Last active September 17, 2020 00:58
Show Gist options
  • Save zerosign/5d641b5a5d223728a4a5a5811b7e9eef to your computer and use it in GitHub Desktop.
Save zerosign/5d641b5a5d223728a4a5a5811b7e9eef to your computer and use it in GitHub Desktop.
psql (12.4, server 9.5.0)
Type "help" for help.
sample=# create table sample (id integer, metadata jsonb) ;
CREATE TABLE
sample=# insert into sample values (1, '{"test" : { "test" : 1 }}'::jsonb);
INSERT 0 1
sample=# select * from sample;
id | metadata
----+-----------------------
1 | {"test": {"test": 1}}
(1 row)
sample=# select * from sample;
id | metadata
----+-----------------------------------------------------------
1 | {"test": {"test": {"test": "hello''world'", "value": 1}}}
(1 row)
package main
import (
"encoding/json"
"github.com/rs/zerolog/log"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
dsn := "user=root dbname=sample port=5432 host=127.0.0.1 sslmode=disable"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal().Err(err).Send()
}
key := []string{"test", "test"}
value := map[string]interface{}{
"test" : "hello''world'",
"value" : 1,
}
data, err := json.Marshal(value)
if err != nil {
log.Fatal().Err(err).Send()
}
tx := db.Exec(
"update sample set metadata = jsonb_set(metadata, array(?), ?::jsonb) where id = ?",
key,
string(data),
1,
)
if tx.Error != nil {
log.Fatal().Err(tx.Error).Send()
}
statement := tx.Statement
log.Info().
Str("statement", statement.SQL.String()).
Send()
}
@zerosign
Copy link
Author

docker container run -p 5432:26257 -p 8080:8080 cockroachdb/cockroach:latest start-single-node --insecure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment