Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
wdvxdr1123 committed Feb 15, 2022
0 parents commit ab3d655
Show file tree
Hide file tree
Showing 7 changed files with 487 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: release

on:
push:
tags:
- 'v*'

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2.3.4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.17'

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52 changes: 52 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
env:
- GO111MODULE=on
before:
hooks:
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
- GO111MODULE=on
goos:
- linux
- darwin
- windows
goarch:
- '386'
- amd64
- arm
- arm64
goarm:
- '7'
ignore:
- goos: darwin
goarch: arm
- goos: darwin
goarch: '386'
- goos: windows
goarch: arm64
mod_timestamp: '{{ .CommitTimestamp }}'
flags:
- -trimpath
ldflags:
- -s -w

checksum:
name_template: '{{ .ProjectName }}_checksums.txt'
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
- fix typo
- Merge pull request
- Merge branch
- Merge remote-tracking
- go mod tidy

archives:
- name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
format_overrides:
- goos: windows
format: zip
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
go-cqhttp leveldb v3 迁移工具
===============================

## 安装

你可以下载 [已经编译好的二进制文件](https://github.com/RomiChan/gocq-leveldb-migrate/releases).

从源码安装:
```bash
$ go install github.com/RomiChan/gocq-leveldb-migrate@latest
```

## 使用方法

```bash
./gocq-leveldb-migrate -from xxx -to yyy
```
默认值:
* from: `data/leveldb-v2`
* to: `data/leveldb-v3`
99 changes: 99 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cmd

import (
"bytes"
"encoding/gob"
"flag"
"fmt"
"os"

"github.com/Mrs4s/go-cqhttp/db"
"github.com/Mrs4s/go-cqhttp/global"
"github.com/pkg/errors"
"github.com/syndtr/goleveldb/leveldb"
)

func handle(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func Main() {
v2 := flag.String("from", "data/leveldb-v2", "leveldb v2 path")
v3 := flag.String("to", "data/leveldb-v3", "leveldb v3 path")
flag.Parse()
if flag.Arg(0) == "help" {
flag.Usage()
return
}
gob.Register(db.StoredMessageAttribute{}) // register gob types
gob.Register(db.StoredGuildMessageAttribute{})
gob.Register(db.QuotedInfo{})
gob.Register(global.MSG{})
gob.Register(db.StoredGroupMessage{})
gob.Register(db.StoredPrivateMessage{})
gob.Register(db.StoredGuildChannelMessage{})

v2db, err := leveldb.OpenFile(*v2, nil)
handle(err)
defer v2db.Close()
{
v3db, err := leveldb.OpenFile(*v3, nil)
handle(err)
_ = v3db.Close()
}
v3db, err := leveldb.OpenFile(*v3, nil)
handle(err)

iter := v2db.NewIterator(nil, nil)
var errs []error
for iter.Next() {
key := iter.Key()
value := iter.Value()
if len(value) == 0 {
continue
}
writer := newWriter()
data := value[1:]
writer.uvarint(uint64(value[0]))
switch value[0] {
case group:
x := &db.StoredGroupMessage{}
if err = gob.NewDecoder(bytes.NewReader(data)).Decode(x); err != nil {
errs = append(errs, errors.Wrap(err, "decode group message failed"))
continue
}
writer.writeStoredGroupMessage(x)
case private:
x := &db.StoredPrivateMessage{}
if err = gob.NewDecoder(bytes.NewReader(data)).Decode(x); err != nil {
errs = append(errs, errors.Wrap(err, "decode private message failed"))
continue
}
writer.writeStoredPrivateMessage(x)
case guildChannel:
x := &db.StoredGuildChannelMessage{}
if err = gob.NewDecoder(bytes.NewReader(data)).Decode(x); err != nil {
errs = append(errs, errors.Wrap(err, "decode guild channel message failed"))
continue
}
writer.writeStoredGuildChannelMessage(x)
}
err := v3db.Put(key, writer.bytes(), nil)
if err != nil {
errs = append(errs, errors.Wrap(err, "put to v3 failed"))
}
}
iter.Release()
if len(errs) > 0 {
for _, err := range errs {
fmt.Printf("%v\n", err)
}
}
tr, err := v3db.OpenTransaction()
handle(err)
handle(tr.Commit())
handle(v3db.Close())
}
Loading

0 comments on commit ab3d655

Please sign in to comment.