fix: Sink

This commit is contained in:
Prad Nukala 2024-12-19 00:48:42 +00:00
parent 6072f6ecfa
commit 265aec187e
20 changed files with 138 additions and 43 deletions

View File

@ -314,7 +314,7 @@ gen-pkl: init-env
pkl-gen-go pkl/sonr.net/Motr.pkl pkl-gen-go pkl/sonr.net/Motr.pkl
gen-sqlc: init-env gen-sqlc: init-env
@cd internal/models && sqlc generate @sqlc generate -f deploy/sqlc.yaml
gen-templ: init-env gen-templ: init-env
@templ generate @templ generate

View File

@ -7,17 +7,33 @@ vars:
sh: git rev-parse --short HEAD sh: git rev-parse --short HEAD
ROOT_DIR: ROOT_DIR:
sh: git rev-parse --show-toplevel sh: git rev-parse --show-toplevel
OS:
sh: uname -s
TASKS:
sh: task -l
tasks: tasks:
default: default:
cmds: cmds:
- echo "{{.VERSION}}" - gh run ls -L 3
- echo "{{.COMMIT}}" - gum format -- "# Sonr ({{.OS}}-{{.VERSION}})" "({{.COMMIT}}) {{.ROOT_DIR}}" "### {{ .TASKS }}"
- echo "{{.ROOT_DIR}}" silent: true
clean:
desc: Clean build artifacts
cmds:
- sh ./scripts/init_env.sh
- rm -rf ./build
- rm -rf ./dist
- rm -rf ./static
silent: true silent: true
build: build:
desc: Build all binaries
silent: true silent: true
cmds: cmds:
- task: clean
- mkdir -p ./build
- mkdir -p ./static/wasm
- task: build:motr - task: build:motr
- task: build:sonr - task: build:sonr
- task: build:hway - task: build:hway
@ -36,3 +52,25 @@ tasks:
internal: true internal: true
silent: true silent: true
cmd: goreleaser build --snapshot --id hway --single-target --clean -o ./build/hway 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

View File

@ -13,8 +13,8 @@ import (
_ "github.com/ncruces/go-sqlite3/driver" _ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed" _ "github.com/ncruces/go-sqlite3/embed"
"github.com/onsonr/sonr/cmd/motr/wasm" "github.com/onsonr/sonr/cmd/motr/wasm"
sink "github.com/onsonr/sonr/deploy/sink"
"github.com/onsonr/sonr/internal/config/motr" "github.com/onsonr/sonr/internal/config/motr"
sink "github.com/onsonr/sonr/internal/models/sink/sqlite"
vault "github.com/onsonr/sonr/pkg/vault/routes" vault "github.com/onsonr/sonr/pkg/vault/routes"
) )
@ -58,15 +58,15 @@ func main() {
wasm.ServeFetch(e) wasm.ServeFetch(e)
} }
// NewDB initializes and returns a configured database connection // createDB initializes and returns a configured database connection
func NewDB() (*sql.DB, error) { func createDB() (*sql.DB, error) {
db, err := sql.Open("sqlite3", ":memory:") db, err := sql.Open("sqlite3", ":memory:")
if err != nil { if err != nil {
return nil, err return nil, err
} }
// create tables // 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 nil, err
} }
return db, nil return db, nil

29
deploy/sink/db_init.sql Normal file
View File

@ -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
deploy/sink/db_reset.sql Normal file
View File

@ -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;

View File

@ -4,6 +4,9 @@
this schema before using the database to index events. 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 blocks table records metadata about each block.
-- The block record does not include its events or transactions (see tx_results). -- The block record does not include its events or transactions (see tx_results).
CREATE TABLE blocks ( CREATE TABLE blocks (
@ -83,3 +86,9 @@ CREATE VIEW tx_events AS
FROM blocks JOIN tx_results ON (blocks.rowid = tx_results.block_id) FROM blocks JOIN tx_results ON (blocks.rowid = tx_results.block_id)
JOIN event_attributes ON (tx_results.rowid = event_attributes.tx_id) JOIN event_attributes ON (tx_results.rowid = event_attributes.tx_id)
WHERE event_attributes.tx_id IS NOT NULL; 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;

8
deploy/sink/sink.go Normal file
View File

@ -0,0 +1,8 @@
package sink
import (
_ "embed"
)
//go:embed schema_vault.sql
var SchemaVaultSQL string

19
deploy/sqlc.yaml Normal file
View File

@ -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"

View File

@ -1,8 +0,0 @@
package sqlite
import (
_ "embed"
)
//go:embed schema.sql
var SchemaMotrSQL string

View File

@ -1,19 +0,0 @@
version: "2"
sql:
- engine: "sqlite"
queries: "./sink/sqlite/query.sql"
schema: "./sink/sqlite/schema.sql"
gen:
go:
package: "motrorm"
out: "drivers/motrorm"
- engine: "postgresql"
queries: "./sink/postgres/query.sql"
schema: "./sink/postgres/schema.sql"
gen:
go:
package: "hwayorm"
out: "drivers/hwayorm"
sql_package: "pgx/v5"

View File

@ -2,7 +2,7 @@
// versions: // versions:
// sqlc v1.27.0 // sqlc v1.27.0
package hwayorm package orm
import ( import (
"context" "context"

View File

@ -2,7 +2,7 @@
// versions: // versions:
// sqlc v1.27.0 // sqlc v1.27.0
package hwayorm package orm
import ( import (
"github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgtype"

View File

@ -1,9 +1,9 @@
// Code generated by sqlc. DO NOT EDIT. // Code generated by sqlc. DO NOT EDIT.
// versions: // versions:
// sqlc v1.27.0 // sqlc v1.27.0
// source: query.sql // source: query_highway.sql
package hwayorm package orm
import ( import (
"context" "context"

View File

@ -2,7 +2,7 @@
// versions: // versions:
// sqlc v1.27.0 // sqlc v1.27.0
package motrorm package orm
import ( import (
"context" "context"

View File

@ -2,7 +2,7 @@
// versions: // versions:
// sqlc v1.27.0 // sqlc v1.27.0
package motrorm package orm
import ( import (
"database/sql" "database/sql"

View File

@ -1,9 +1,9 @@
// Code generated by sqlc. DO NOT EDIT. // Code generated by sqlc. DO NOT EDIT.
// versions: // versions:
// sqlc v1.27.0 // sqlc v1.27.0
// source: query.sql // source: query_vault.sql
package motrorm package orm
import ( import (
"context" "context"