-
Notifications
You must be signed in to change notification settings - Fork 38
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 (#1768)
- Loading branch information
Showing
7 changed files
with
110 additions
and
30 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
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 |
---|---|---|
@@ -1,16 +1,12 @@ | ||
CREATE DATABASE ogdb; | ||
/* | ||
This file is used to create the database and set the necessary permissions for the user that will be used by the gateway. | ||
*/ | ||
|
||
USE ogdb; | ||
-- Create the database | ||
CREATE DATABASE IF NOT EXISTS ogdb; | ||
|
||
GRANT SELECT, INSERT, UPDATE, DELETE ON ogdb.* TO 'obscurouser'; | ||
-- Grant the necessary permissions | ||
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON ogdb.* TO 'obscurouser'; | ||
|
||
CREATE TABLE IF NOT EXISTS ogdb.users ( | ||
user_id varbinary(20) PRIMARY KEY, | ||
private_key varbinary(32) | ||
); | ||
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 | ||
); | ||
-- Reload the privileges from the grant tables in the mysql database | ||
FLUSH PRIVILEGES; |
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
/* | ||
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 TABLE IF NOT EXISTS ogdb.users ( | ||
user_id varbinary(20) PRIMARY KEY, | ||
private_key varbinary(32) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS ogdb.accounts ( | ||
user_id varbinary(20), | ||
account_address varbinary(20), | ||
signature varbinary(65), | ||
FOREIGN KEY(user_id) REFERENCES ogdb.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
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 | ||
} |
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