Skip to content

Commit

Permalink
fix: Sink
Browse files Browse the repository at this point in the history
  • Loading branch information
prnk28 committed Dec 19, 2024
1 parent 6072f6e commit 265aec1
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ gen-pkl: init-env
pkl-gen-go pkl/sonr.net/Motr.pkl

gen-sqlc: init-env
@cd internal/models && sqlc generate
@sqlc generate -f deploy/sqlc.yaml

gen-templ: init-env
@templ generate
Expand Down
44 changes: 41 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@ vars:
sh: git rev-parse --short HEAD
ROOT_DIR:
sh: git rev-parse --show-toplevel
OS:
sh: uname -s
TASKS:
sh: task -l
tasks:
default:
cmds:
- echo "{{.VERSION}}"
- echo "{{.COMMIT}}"
- echo "{{.ROOT_DIR}}"
- gh run ls -L 3
- gum format -- "# Sonr ({{.OS}}-{{.VERSION}})" "({{.COMMIT}}) {{.ROOT_DIR}}" "### {{ .TASKS }}"
silent: true

clean:
desc: Clean build artifacts
cmds:
- sh ./scripts/init_env.sh
- rm -rf ./build
- rm -rf ./dist
- rm -rf ./static
silent: true

build:
desc: Build all binaries
silent: true
cmds:
- task: clean
- mkdir -p ./build
- mkdir -p ./static/wasm
- task: build:motr
- task: build:sonr
- task: build:hway
Expand All @@ -36,3 +52,25 @@ tasks:
internal: true
silent: true
cmd: goreleaser build --snapshot --id hway --single-target --clean -o ./build/hway

init:db:
desc: Initialize the database
silent: true
platforms:
- linux
cmds:
- sudo -u postgres psql -f ./deploy/sink/db_seed.sql
- sudo -u postgres psql -d chainindex -f ./deploy/sink/schema_indexer.sql

reset:db:
desc: Reset the database
silent: true
platforms:
- linux
cmd: gum confirm "Reset chainindex, highway, and matrixhs?" --default=false --affirmative "Yes" && sudo -u postgres psql -f ./deploy/sink/db_reset.sql|| echo "No selected"

init:ipfs:
desc: Initialize the ipfs node
silent: true
cmds:
- sh ./scripts/ipfs_config.sh
8 changes: 4 additions & 4 deletions cmd/motr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/onsonr/sonr/cmd/motr/wasm"
sink "github.com/onsonr/sonr/deploy/sink"
"github.com/onsonr/sonr/internal/config/motr"
sink "github.com/onsonr/sonr/internal/models/sink/sqlite"
vault "github.com/onsonr/sonr/pkg/vault/routes"
)

Expand Down Expand Up @@ -58,15 +58,15 @@ func main() {
wasm.ServeFetch(e)
}

// NewDB initializes and returns a configured database connection
func NewDB() (*sql.DB, error) {
// createDB initializes and returns a configured database connection
func createDB() (*sql.DB, error) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
return nil, err
}

// create tables
if _, err := db.ExecContext(context.Background(), sink.SchemaMotrSQL); err != nil {
if _, err := db.ExecContext(context.Background(), sink.SchemaVaultSQL); err != nil {
return nil, err
}
return db, nil
Expand Down
29 changes: 29 additions & 0 deletions deploy/sink/db_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- Connect to postgres default database
\c postgres;

-- Create databases
CREATE DATABASE chainindex;
CREATE DATABASE highway;
CREATE DATABASE matrixhs;

-- Create users with passwords
CREATE USER chainindex_user WITH PASSWORD 'chainindex_password123';
CREATE USER highway_user WITH PASSWORD 'highway_password123';
CREATE USER matrixhs_user WITH PASSWORD 'matrixhs_password123';

-- Grant privileges for each database to their respective users
\c chainindex;
GRANT ALL PRIVILEGES ON DATABASE chainindex TO chainindex_user;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO chainindex_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TABLES TO chainindex_user;

\c highway;
GRANT ALL PRIVILEGES ON DATABASE highway TO highway_user;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO highway_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TABLES TO highway_user;

\c matrixhs;
GRANT ALL PRIVILEGES ON DATABASE matrixhs TO matrixhs_user;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO matrixhs_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TABLES TO matrixhs_user;

19 changes: 19 additions & 0 deletions deploy/sink/db_reset.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Connect to a different database first (postgres) since we can't drop a database while connected to it
\c postgres;

-- Terminate all connections to the databases
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname IN ('chainindex', 'highway', 'matrixhs')
AND pid <> pg_backend_pid();

-- Drop the databases if they exist
DROP DATABASE IF EXISTS chainindex;
DROP DATABASE IF EXISTS highway;
DROP DATABASE IF EXISTS matrixhs;

-- Drop the users if they exist
DROP USER IF EXISTS chainindex_user;
DROP USER IF EXISTS highway_user;
DROP USER IF EXISTS matrixhs_user;

File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions deploy/indexer/schema.sql → deploy/sink/schema_indexer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
this schema before using the database to index events.
*/

-- First, ensure we're connected to the chainindex database
\c chainindex;

-- The blocks table records metadata about each block.
-- The block record does not include its events or transactions (see tx_results).
CREATE TABLE blocks (
Expand Down Expand Up @@ -83,3 +86,9 @@ CREATE VIEW tx_events AS
FROM blocks JOIN tx_results ON (blocks.rowid = tx_results.block_id)
JOIN event_attributes ON (tx_results.rowid = event_attributes.tx_id)
WHERE event_attributes.tx_id IS NOT NULL;

-- Grant privileges for each database to their respective users
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO chainindex_user;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO chainindex_user;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO chainindex_user;

File renamed without changes.
8 changes: 8 additions & 0 deletions deploy/sink/sink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sink

import (
_ "embed"
)

//go:embed schema_vault.sql
var SchemaVaultSQL string
19 changes: 19 additions & 0 deletions deploy/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "2"
sql:
- engine: "sqlite"
queries: "./sink/query_vault.sql"
schema: "./sink/schema_vault.sql"
gen:
go:
package: "orm"
out: "../pkg/vault/orm"

- engine: "postgresql"
queries: "./sink/query_highway.sql"
schema: "./sink/schema_highway.sql"
gen:
go:
package: "orm"
out: "../pkg/gateway/orm"
sql_package: "pgx/v5"

8 changes: 0 additions & 8 deletions internal/models/sink/sqlite/embed.go

This file was deleted.

19 changes: 0 additions & 19 deletions internal/models/sqlc.yaml

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 265aec1

Please sign in to comment.