This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
/
Migration030.go
150 lines (138 loc) · 4.35 KB
/
Migration030.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package migrations
import (
"database/sql"
"fmt"
"os"
"path"
"strings"
_ "github.com/mutecomm/go-sqlcipher"
)
var (
am03_up_create_utxos = "create table utxos (outpoint text primary key not null, value text, height integer, scriptPubKey text, watchOnly integer, coin text);"
am03_down_create_utxos = "create table utxos (outpoint text primary key not null, value integer, height integer, scriptPubKey text, watchOnly integer, coin text);"
am03_temp_utxos = "ALTER TABLE utxos RENAME TO temp_utxos;"
am03_insert_utxos = "INSERT INTO utxos SELECT outpoint, value, height, scriptPubKey, watchOnly, coin FROM temp_utxos;"
am03_drop_temp_utxos = "DROP TABLE temp_utxos;"
am03_up_create_stxos = "create table stxos (outpoint text primary key not null, value text, height integer, scriptPubKey text, watchOnly integer, spendHeight integer, spendTxid text, coin text);"
am03_down_create_stxos = "create table stxos (outpoint text primary key not null, value integer, height integer, scriptPubKey text, watchOnly integer, spendHeight integer, spendTxid text, coin text);"
am03_temp_stxos = "ALTER TABLE stxos RENAME TO temp_stxos;"
am03_insert_stxos = "INSERT INTO stxos SELECT outpoint, value, height, scriptPubKey, watchOnly, spendHeight, spendTxid, coin FROM temp_stxos;"
am03_drop_temp_stxos = "DROP TABLE temp_stxos;"
am03_up_create_txns = "create table txns (txid text primary key not null, value text, height integer, timestamp integer, watchOnly integer, tx blob, coin text);"
am03_down_create_txns = "create table txns (txid text primary key not null, value integer, height integer, timestamp integer, watchOnly integer, tx blob, coin text);"
am03_temp_txns = "ALTER TABLE txns RENAME TO temp_txns;"
am03_insert_txns = "INSERT INTO txns SELECT txid, value, height, timestamp, watchOnly, tx, coin FROM temp_txns;"
am03_drop_temp_txns = "DROP TABLE temp_txns;"
)
type Migration030 struct{ AM03 }
type AM03 struct{}
func (AM03) Up(repoPath string, dbPassword string, testnet bool) error {
var dbPath string
if testnet {
dbPath = path.Join(repoPath, "datastore", "testnet.db")
} else {
dbPath = path.Join(repoPath, "datastore", "mainnet.db")
}
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return err
}
if dbPassword != "" {
p := "pragma key='" + dbPassword + "';"
if _, err := db.Exec(p); err != nil {
return err
}
}
upSequence := strings.Join([]string{
am03_temp_utxos,
am03_up_create_utxos,
am03_insert_utxos,
am03_drop_temp_utxos,
am03_temp_stxos,
am03_up_create_stxos,
am03_insert_stxos,
am03_drop_temp_stxos,
am03_temp_txns,
am03_up_create_txns,
am03_insert_txns,
am03_drop_temp_txns,
}, " ")
tx, err := db.Begin()
if err != nil {
return err
}
if _, err = tx.Exec(upSequence); err != nil {
if rErr := tx.Rollback(); rErr != nil {
return fmt.Errorf("rollback failed: (%s) due to (%s)", rErr.Error(), err.Error())
}
return err
}
if err = tx.Commit(); err != nil {
return err
}
f1, err := os.Create(path.Join(repoPath, "repover"))
if err != nil {
return err
}
_, err = f1.Write([]byte("31"))
if err != nil {
return err
}
f1.Close()
return nil
}
func (AM03) Down(repoPath string, dbPassword string, testnet bool) error {
var dbPath string
if testnet {
dbPath = path.Join(repoPath, "datastore", "testnet.db")
} else {
dbPath = path.Join(repoPath, "datastore", "mainnet.db")
}
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return err
}
if dbPassword != "" {
p := "pragma key='" + dbPassword + "';"
if _, err := db.Exec(p); err != nil {
return err
}
}
downSequence := strings.Join([]string{
am03_temp_utxos,
am03_down_create_utxos,
am03_insert_utxos,
am03_drop_temp_utxos,
am03_temp_stxos,
am03_down_create_stxos,
am03_insert_stxos,
am03_drop_temp_stxos,
am03_temp_txns,
am03_down_create_txns,
am03_insert_txns,
am03_drop_temp_txns,
}, " ")
tx, err := db.Begin()
if err != nil {
return err
}
if _, err = tx.Exec(downSequence); err != nil {
if rErr := tx.Rollback(); rErr != nil {
return fmt.Errorf("rollback failed: (%s) due to (%s)", rErr.Error(), err.Error())
}
return err
}
if err = tx.Commit(); err != nil {
return err
}
f1, err := os.Create(path.Join(repoPath, "repover"))
if err != nil {
return err
}
_, err = f1.Write([]byte("30"))
if err != nil {
return err
}
f1.Close()
return nil
}