Skip to content

Commit

Permalink
revert sqlite changes with migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Feb 22, 2024
1 parent 3d995cc commit 57e460d
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions tools/walletextension/storage/database/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package sqlite
import (
"database/sql"
"fmt"
"os"
"path/filepath"
"runtime"

_ "github.com/mattn/go-sqlite3" // sqlite driver for sql.Open()
obscurocommon "github.com/ten-protocol/go-ten/go/common"
"github.com/ten-protocol/go-ten/go/common/errutil"
"github.com/ten-protocol/go-ten/tools/walletextension/storage/database"

_ "github.com/mattn/go-sqlite3" // sqlite driver for sql.Open()
common "github.com/ten-protocol/go-ten/tools/walletextension/common"
"os"
"path/filepath"
)

type Database struct {
Expand All @@ -33,15 +29,31 @@ func NewSqliteDatabase(dbPath string) (*Database, error) {
return nil, err
}

// get the path to the migrations (they are always in the same directory as file containing connection function)
_, filename, _, ok := runtime.Caller(0)
if !ok {
return nil, fmt.Errorf("failed to get current directory")
// enable foreign keys in sqlite
_, err = db.Exec("PRAGMA foreign_keys = ON;")
if err != nil {
return nil, err
}

// create users table
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS users (
user_id binary(20) PRIMARY KEY,
private_key binary(32)
);`)

if err != nil {
return nil, err
}
migrationsDir := filepath.Dir(filename)

// apply migrations
if err = database.ApplyMigrations(db, migrationsDir); err != nil {
// create accounts table
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS accounts (
user_id binary(20),
account_address binary(20),
signature binary(65),
FOREIGN KEY(user_id) REFERENCES users(user_id) ON DELETE CASCADE
);`)

if err != nil {
return nil, err
}

Expand Down

0 comments on commit 57e460d

Please sign in to comment.