dbutil: RewriteTable, generalize table.RewriteRows to copy from source table to destination table with row transformation
This commit is contained in:
committed by
Michal Jan Matczuk
parent
13ef8ceaf1
commit
69f6f201f2
@@ -1,31 +0,0 @@
|
||||
// Copyright (C) 2017 ScyllaDB
|
||||
// Use of this source code is governed by a ALv2-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package table
|
||||
|
||||
import (
|
||||
"github.com/scylladb/gocqlx/v2"
|
||||
)
|
||||
|
||||
// RewriteRows performs a sequential rewrite of all rows in a table.
|
||||
func RewriteRows(session gocqlx.Session, t *Table, options ...func(q *gocqlx.Queryx)) error {
|
||||
insert := t.InsertQuery(session)
|
||||
defer insert.Release()
|
||||
|
||||
// Apply query options
|
||||
for _, o := range options {
|
||||
o(insert)
|
||||
}
|
||||
|
||||
// Iterate over all rows and reinsert them
|
||||
iter := session.Query(t.SelectAll()).Iter()
|
||||
m := make(map[string]interface{})
|
||||
for iter.MapScan(m) {
|
||||
if err := insert.BindMap(m).Exec(); err != nil {
|
||||
return err
|
||||
}
|
||||
m = make(map[string]interface{})
|
||||
}
|
||||
return iter.Close()
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
// Copyright (C) 2017 ScyllaDB
|
||||
// Use of this source code is governed by a ALv2-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build all integration
|
||||
|
||||
package table_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/scylladb/gocqlx/v2/gocqlxtest"
|
||||
"github.com/scylladb/gocqlx/v2/qb"
|
||||
"github.com/scylladb/gocqlx/v2/table"
|
||||
)
|
||||
|
||||
func TestRewriteRows(t *testing.T) {
|
||||
session := CreateSession(t)
|
||||
defer session.Close()
|
||||
|
||||
if err := session.ExecStmt(`CREATE TABLE gocqlx_test.rewrite_table (testtext text PRIMARY KEY)`); err != nil {
|
||||
t.Fatal("create table:", err)
|
||||
}
|
||||
|
||||
tbl := table.New(table.Metadata{
|
||||
Name: "gocqlx_test.rewrite_table",
|
||||
Columns: []string{"testtext"},
|
||||
PartKey: []string{"testtext"},
|
||||
})
|
||||
|
||||
// Insert data with 500ms TTL
|
||||
q := tbl.InsertBuilder().TTL(500 * time.Millisecond).Query(session)
|
||||
if err := q.Bind("a").Exec(); err != nil {
|
||||
t.Fatal("insert:", err)
|
||||
}
|
||||
if err := q.Bind("b").Exec(); err != nil {
|
||||
t.Fatal("insert:", err)
|
||||
}
|
||||
if err := q.Bind("c").Exec(); err != nil {
|
||||
t.Fatal("insert:", err)
|
||||
}
|
||||
|
||||
// Rewrite data without TTL
|
||||
if err := table.RewriteRows(session, tbl); err != nil {
|
||||
t.Fatal("rewrite:", err)
|
||||
}
|
||||
|
||||
// Wait and check if data persisted
|
||||
time.Sleep(time.Second)
|
||||
|
||||
var n int
|
||||
if err := qb.Select(tbl.Name()).CountAll().Query(session).Scan(&n); err != nil {
|
||||
t.Fatal("scan:", err)
|
||||
}
|
||||
if n != 3 {
|
||||
t.Fatal("expected 3 entries")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user