github.com/RevenueMonster/sqlike@v1.0.6/examples/sqldump.go (about)

     1  package examples
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"encoding/json"
     7  	"io/ioutil"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"cloud.google.com/go/civil"
    13  	sqldump "github.com/RevenueMonster/sqlike/sql/dump"
    14  	"github.com/RevenueMonster/sqlike/sql/expr"
    15  	"github.com/RevenueMonster/sqlike/sqlike"
    16  	"github.com/RevenueMonster/sqlike/sqlike/actions"
    17  	"github.com/RevenueMonster/sqlike/sqlike/options"
    18  	"github.com/RevenueMonster/sqlike/types"
    19  	"github.com/brianvoe/gofakeit"
    20  	"github.com/google/uuid"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  type dumpStruct struct {
    25  	UUID    uuid.UUID
    26  	String  string
    27  	Bool    bool
    28  	Int64   int64
    29  	Int     int
    30  	Uint64  uint64
    31  	Uint    uint
    32  	Byte    []byte
    33  	JSONRaw json.RawMessage
    34  	JSON    struct{}
    35  	Array   []string
    36  	// Point        orb.Point
    37  	// LineString   orb.LineString
    38  	Enum        Enum      `sqlike:",enum=SUCCESS|FAILED|UNKNOWN"`
    39  	Set         types.Set `sqlike:",set=A|B|C"`
    40  	Date        civil.Date
    41  	DateTime    time.Time
    42  	PtrString   *string
    43  	PtrBool     *bool
    44  	PtrInt64    *int64
    45  	PtrUint64   *uint64
    46  	PtrJSONRaw  *json.RawMessage
    47  	PtrDate     *civil.Date
    48  	PtrTime     *civil.Time
    49  	PtrDateTime *time.Time
    50  }
    51  
    52  // SQLDumpExamples :
    53  func SQLDumpExamples(ctx context.Context, t *testing.T, client *sqlike.Client) {
    54  
    55  	db := client.Database("sqlike")
    56  	table := db.Table("sqldump")
    57  
    58  	if err := table.DropIfExists(ctx); err != nil {
    59  		require.NoError(t, err)
    60  	}
    61  	table.MustUnsafeMigrate(ctx, dumpStruct{})
    62  
    63  	// generate data
    64  	data := make([]dumpStruct, 10)
    65  	for i := 0; i < len(data); i++ {
    66  		data[i] = newDumpStruct()
    67  	}
    68  
    69  	{
    70  		if _, err := table.Insert(
    71  			ctx, &data,
    72  			options.Insert().SetDebug(true),
    73  		); err != nil {
    74  			require.NoError(t, err)
    75  		}
    76  	}
    77  
    78  	{
    79  		// zip all sql files
    80  
    81  		file, err := ioutil.TempFile("", ".sql")
    82  		if err != nil {
    83  			panic(err)
    84  		}
    85  		defer os.Remove(file.Name())
    86  
    87  		// utcNow := time.Now().UTC().Add(-1 * time.Hour * 48)
    88  		zero := time.Time{}
    89  		filter := expr.And(
    90  			expr.GreaterThan("DateTime", zero.Format("2006-01-02 15:04:05")),
    91  		)
    92  
    93  		offset := uint(0)
    94  		limit := uint(10)
    95  		dumper := sqldump.NewDumper("mysql", client)
    96  
    97  		// backup 100 records per time
    98  		for {
    99  			// check how many records return or backup
   100  			affected, err := dumper.BackupTo(
   101  				ctx,
   102  				actions.Find().
   103  					From("sqlike", "sqldump").
   104  					Where(filter).
   105  					Offset(offset).
   106  					Limit(limit),
   107  				file,
   108  			)
   109  			if err != nil {
   110  				if err != sql.ErrNoRows {
   111  					require.NoError(t, err)
   112  				}
   113  				break
   114  			}
   115  
   116  			if affected < int64(limit) {
   117  				break
   118  			}
   119  
   120  			offset += limit
   121  		}
   122  
   123  		file.Close()
   124  
   125  		err = table.Truncate(ctx)
   126  		require.NoError(t, err)
   127  
   128  		// b, err := ioutil.ReadFile(file.Name())
   129  		// if err != nil {
   130  		// 	panic(err)
   131  		// }
   132  
   133  		// log.Println(string(b))
   134  		// result, err := client.Exec(string(b))
   135  		// require.NoError(t, err)
   136  		// affected, err := result.RowsAffected()
   137  		// require.NoError(t, err)
   138  		// log.Println(affected)
   139  	}
   140  }
   141  
   142  func newDumpStruct() (o dumpStruct) {
   143  	date := gofakeit.Date()
   144  	o.UUID = uuid.New()
   145  	o.String = gofakeit.Name()
   146  	o.Int = int(gofakeit.Int32())
   147  	o.Int64 = gofakeit.Int64()
   148  	o.Uint = uint(gofakeit.Uint32())
   149  	o.Uint64 = gofakeit.Uint64()
   150  	o.JSONRaw = []byte(`{"id": 100, "message": "hello world"}`)
   151  	o.Date = civil.Date{Year: date.Year(), Month: date.Month(), Day: date.Day()}
   152  	o.Enum = Enum(gofakeit.RandString([]string{
   153  		"SUCCESS",
   154  		"FAILED",
   155  		"UNKNOWN",
   156  	}))
   157  	o.Set = []string{"A", "C"}
   158  	o.DateTime = gofakeit.Date()
   159  	// o.Timestamp = types.Timestamp(gofakeit.Date())
   160  	// o.Point = orb.Point{gofakeit.Longitude(), gofakeit.Latitude()}
   161  	// o.LineString = orb.LineString{
   162  	// 	orb.Point{gofakeit.Longitude(), gofakeit.Latitude()},
   163  	// 	orb.Point{gofakeit.Longitude(), gofakeit.Latitude()},
   164  	// 	orb.Point{gofakeit.Longitude(), gofakeit.Latitude()},
   165  	// }
   166  
   167  	str := gofakeit.Address().Address
   168  	num := gofakeit.Int64()
   169  	ts := gofakeit.Date()
   170  
   171  	o.PtrString = &str
   172  	o.PtrInt64 = &num
   173  	o.PtrDateTime = &ts
   174  	return
   175  }