-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create database migration for the gateway
- Loading branch information
Showing
7 changed files
with
136 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
tools/walletextension/storage/database/mariadb/001_init.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
This is a migration file for MariaDB and is executed when the Gateway is started to make sure the database schema is up to date. | ||
*/ | ||
|
||
CREATE DATABASE ogdb; | ||
|
||
USE ogdb; | ||
|
||
GRANT SELECT, INSERT, UPDATE, DELETE ON ogdb.* TO 'obscurouser'; | ||
|
||
-- Create users table | ||
CREATE TABLE IF NOT EXISTS ogdb.users ( | ||
user_id varbinary(20) PRIMARY KEY, | ||
private_key varbinary(32) | ||
); | ||
|
||
-- Create accounts table | ||
CREATE TABLE IF NOT EXISTS ogdb.accounts ( | ||
user_id varbinary(20), | ||
account_address varbinary(20), | ||
signature varbinary(65), | ||
FOREIGN KEY(user_id) REFERENCES users(user_id) ON DELETE CASCADE | ||
); | ||
|
||
-- Create transactions table | ||
-- TODO @ziga: Add more fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
) | ||
|
||
func ApplyMigrations(db *sql.DB, migrationsPath string) error { | ||
files, err := os.ReadDir(migrationsPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var sqlFiles []string | ||
for _, file := range files { | ||
if filepath.Ext(file.Name()) == ".sql" { | ||
sqlFiles = append(sqlFiles, filepath.Join(migrationsPath, file.Name())) | ||
} | ||
} | ||
|
||
sort.Strings(sqlFiles) // Sort files lexicographically to apply migrations in order | ||
|
||
for _, file := range sqlFiles { | ||
fmt.Println("Executing db migration file: ", file) | ||
if err = executeSQLFile(db, file); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func executeSQLFile(db *sql.DB, filePath string) error { | ||
content, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = db.Exec(string(content)) | ||
if err != nil { | ||
return fmt.Errorf("failed to execute %s: %w", filePath, err) | ||
} | ||
|
||
return nil | ||
} |
16 changes: 16 additions & 0 deletions
16
tools/walletextension/storage/database/sqlite/001_init.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- Enable foreign keys in SQLite | ||
PRAGMA foreign_keys = ON; | ||
|
||
-- Create users table | ||
CREATE TABLE IF NOT EXISTS users ( | ||
user_id binary(20) PRIMARY KEY, | ||
private_key binary(32) | ||
); | ||
|
||
-- Create accounts table | ||
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters