2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-02-02 21:53:16 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jmoiron/sqlx"
|
2018-02-13 16:31:57 +00:00
|
|
|
_ "github.com/lib/pq" //postgres driver
|
2018-02-02 21:53:16 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2018-02-02 21:53:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DB struct {
|
|
|
|
*sqlx.DB
|
2018-02-13 16:31:57 +00:00
|
|
|
Node core.Node
|
|
|
|
NodeID int64
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDB(databaseConfig config.Database, node core.Node) (*DB, error) {
|
|
|
|
connectString := config.DbConnectionString(databaseConfig)
|
2019-03-19 19:11:26 +00:00
|
|
|
db, connectErr := sqlx.Connect("postgres", connectString)
|
|
|
|
if connectErr != nil {
|
|
|
|
return &DB{}, ErrDBConnectionFailed(connectErr)
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
pg := DB{DB: db, Node: node}
|
2019-03-19 19:11:26 +00:00
|
|
|
nodeErr := pg.CreateNode(&node)
|
|
|
|
if nodeErr != nil {
|
|
|
|
return &DB{}, ErrUnableToSetNode(nodeErr)
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
return &pg, nil
|
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
|
|
|
|
func (db *DB) CreateNode(node *core.Node) error {
|
2019-10-18 16:16:19 +00:00
|
|
|
var nodeID int64
|
2018-02-12 16:54:05 +00:00
|
|
|
err := db.QueryRow(
|
2018-03-21 14:53:20 +00:00
|
|
|
`INSERT INTO eth_nodes (genesis_block, network_id, eth_node_id, client_name)
|
2018-02-12 16:54:05 +00:00
|
|
|
VALUES ($1, $2, $3, $4)
|
2018-03-21 14:53:20 +00:00
|
|
|
ON CONFLICT (genesis_block, network_id, eth_node_id)
|
2018-02-12 16:54:05 +00:00
|
|
|
DO UPDATE
|
|
|
|
SET genesis_block = $1,
|
|
|
|
network_id = $2,
|
2018-03-21 14:53:20 +00:00
|
|
|
eth_node_id = $3,
|
2018-02-12 16:54:05 +00:00
|
|
|
client_name = $4
|
|
|
|
RETURNING id`,
|
2019-10-18 16:16:19 +00:00
|
|
|
node.GenesisBlock, node.NetworkID, node.ID, node.ClientName).Scan(&nodeID)
|
2018-02-12 16:54:05 +00:00
|
|
|
if err != nil {
|
2019-03-19 19:11:26 +00:00
|
|
|
return ErrUnableToSetNode(err)
|
2018-02-12 16:54:05 +00:00
|
|
|
}
|
2019-10-18 16:16:19 +00:00
|
|
|
db.NodeID = nodeID
|
2018-02-12 16:54:05 +00:00
|
|
|
return nil
|
|
|
|
}
|