Skip to content

Commit 483b887

Browse files
committed
Update to ChirpStack v4.7 db schema.
1 parent 44c529f commit 483b887

File tree

2 files changed

+18
-40
lines changed

2 files changed

+18
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ database will be removed!
4545

4646
## Notes
4747

48-
* This utility is compatible with the ChirpStack v4.6.0 database schema.
48+
* This utility is compatible with the ChirpStack v4.7.0 database schema.
4949
* This utility does not support [environment variables](https://www.chirpstack.io/docs/chirpstack/configuration.html#environment-variables) in configuration files, like ChirpStack does.
5050

5151
## Building from source

main.go

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,8 @@ func migrateDevicesFn() {
17421742
"tags",
17431743
"variables",
17441744
"join_eui",
1745+
"secondary_dev_addr",
1746+
"device_session",
17451747
))
17461748
if err != nil {
17471749
log.Fatal("Prepare device statement error", err)
@@ -1764,10 +1766,23 @@ func migrateDevicesFn() {
17641766

17651767
var devEUI lorawan.EUI64
17661768
var appSKey lorawan.AES128Key
1769+
var deviceSession *[]byte
1770+
17671771
copy(devEUI[:], asDev.DevEUI)
17681772
copy(appSKey[:], asDev.AppSKey)
17691773
appSKeys[devEUI] = appSKey
17701774

1775+
ds, err := getDeviceSession(devEUI[:], appSKey[:])
1776+
if err == nil {
1777+
// Make sure we have the DevAddr from the device-session
1778+
copy(asDev.DevAddr[:], ds.DevAddr)
1779+
1780+
b, err := proto.Marshal(&ds)
1781+
if err == nil {
1782+
deviceSession = &b
1783+
}
1784+
}
1785+
17711786
_, err = stmt.Exec(
17721787
nsDev.DevEUI,
17731788
intToUUID(asDev.ApplicationID),
@@ -1792,6 +1807,8 @@ func migrateDevicesFn() {
17921807
hstoreToJSON(asDev.Tags),
17931808
hstoreToJSON(asDev.Variables),
17941809
[]byte{0, 0, 0, 0, 0, 0, 0, 0},
1810+
nil,
1811+
deviceSession,
17951812
)
17961813
if err != nil {
17971814
log.Fatal("Execute device statement error", err)
@@ -1827,11 +1844,6 @@ func migrateDevicesFn() {
18271844
}
18281845
}
18291846

1830-
log.Println("Migrating device-sessions")
1831-
for devEUI, appSKey := range appSKeys {
1832-
migrateDeviceSessionFn(devEUI[:], appSKey[:])
1833-
}
1834-
18351847
log.Println("Migrate device <> gateway")
18361848
for devEUI := range appSKeys {
18371849
migrateDeviceGatewayFn(devEUI[:])
@@ -2010,15 +2022,6 @@ func hstoreToJSON(h hstore.Hstore) string {
20102022
return string(b)
20112023
}
20122024

2013-
func migrateDeviceSessionFn(devEUI []byte, appSKey []byte) {
2014-
ds, err := getDeviceSession(devEUI, appSKey)
2015-
if err != nil {
2016-
// We don't know if there is a device-session
2017-
return
2018-
}
2019-
saveDeviceSession(ds)
2020-
}
2021-
20222025
func getDeviceSession(devEUI []byte, appSKey []byte) (pbnew.DeviceSession, error) {
20232026
key := fmt.Sprintf("%slora:ns:device:%s", nsPrefix, hex.EncodeToString(devEUI))
20242027
var dsOld pbold.DeviceSessionPB
@@ -2117,31 +2120,6 @@ func getDeviceSession(devEUI []byte, appSKey []byte) (pbnew.DeviceSession, error
21172120
return dsNew, nil
21182121
}
21192122

2120-
func saveDeviceSession(ds pbnew.DeviceSession) {
2121-
devAddrKey := fmt.Sprintf("%sdevaddr:{%s}", csPrefix, hex.EncodeToString(ds.DevAddr))
2122-
devSessKey := fmt.Sprintf("%sdevice:{%s}:ds", csPrefix, hex.EncodeToString(ds.DevEui))
2123-
2124-
b, err := proto.Marshal(&ds)
2125-
if err != nil {
2126-
panic(err)
2127-
}
2128-
2129-
pipe := csRedis.TxPipeline()
2130-
pipe.SAdd(context.Background(), devAddrKey, ds.DevEui)
2131-
pipe.PExpire(context.Background(), devAddrKey, time.Duration(csSessionTTL)*time.Hour*24)
2132-
if _, err := pipe.Exec(context.Background()); err != nil {
2133-
panic(err)
2134-
}
2135-
2136-
// On purpose, this is not part of the above pipeline as the devAddrKey and
2137-
// devSessKey do not share the same hash tags and are therefore not
2138-
// guaranteed to be on the same server / shard.
2139-
err = csRedis.Set(context.Background(), devSessKey, b, time.Duration(csSessionTTL)*time.Hour*24).Err()
2140-
if err != nil {
2141-
panic(err)
2142-
}
2143-
}
2144-
21452123
func migrateDeviceGatewayFn(devEUI []byte) {
21462124
devGW, err := getDeviceGateway(devEUI)
21472125
if err != nil {

0 commit comments

Comments
 (0)