Compare commits

..

No commits in common. "0312a8266abc12a5e47a5d31a315bb4083fdcf3c" and "07ad091168f7f873fe5449fb335f710eb5ae6156" have entirely different histories.

8 changed files with 12 additions and 371 deletions

View File

@ -1 +0,0 @@
cmd/database/config.json

View File

@ -1,2 +0,0 @@
dbup:
go run cmd/database/main.go

View File

@ -1,32 +0,0 @@
package main
import (
"encoding/json"
"log"
"os"
"sevenkeys/database"
"github.com/go-sql-driver/mysql"
)
func readConfigFromFile(filename string) mysql.Config {
bytes, err := os.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
var config mysql.Config
err = json.Unmarshal(bytes, &config)
if err != nil {
log.Fatal(err)
}
return config
}
func main() {
config := readConfigFromFile("config.json")
db := database.GetDatabase(config)
database.CreateDatabaseSchema(db)
}

View File

@ -1,8 +0,0 @@
{
"User": "<your username>",
"Passwd": "<your password>",
"Net": "tcp",
"Addr": "127.0.0.1:3306",
"DBName": "<your database name>",
"AllowNativePasswords": true
}

View File

@ -1,8 +0,0 @@
{
"User": "root",
"Passwd": "o7MS6CIn660jIApSP",
"Net": "tcp",
"Addr": "127.0.0.1:3306",
"DBName": "sevenkeys",
"AllowNativePasswords": true
}

View File

@ -1,283 +0,0 @@
package database
import (
"database/sql"
"log"
"github.com/go-sql-driver/mysql"
)
var db *sql.DB
func GetDatabase(config mysql.Config) *sql.DB {
db, err := sql.Open("mysql", config.FormatDSN())
if err != nil {
log.Fatal(err)
}
if err = db.Ping(); err != nil {
log.Fatal(err)
}
return db
}
func createGamepieceTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS Gamepiece (
Id INT AUTO_INCREMENT PRIMARY KEY,
ArenaId INT NULL,
ScryfallId VARCHAR(36) NOT NULL,
OracleId VARCHAR(36) NOT NULL,
Name VARCHAR(141) NOT NULL,
Layout ENUM("normal",
"split",
"flip",
"transform",
"modal_dfc",
"meld",
"leveler",
"class",
"case",
"saga",
"adventure",
"mutate",
"prototype",
"battle",
"planar",
"scheme",
"vanguard",
"token",
"double_faced_token",
"emblem",
"augment",
"host",
"art_series",
"reversible_card") NOT NULL,
ManaCost VARCHAR(8) NOT NULL,
ManaValue FLOAT(7,2) NOT NULL,
TypeLine VARCHAR(50) NOT NULL,
OracleText VARCHAR(800) NOT NULL,
Power VARCHAR(5) NULL,
Toughness VARCHAR(5) NULL,
ReserveList BOOLEAN NOT NULL
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func createColorTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS Color (
Id INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(5) NOT NULL,
Abbreviation VARCHAR(1) NOT NULL
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func populateColorTable(db *sql.DB) {
query := `INSERT INTO Color (Name, Abbreviation) VALUES (?, ?);`
insert, err := db.Prepare(query)
defer insert.Close()
if err != nil {
log.Fatal(err)
}
colors := map[string]string{
"White": "W",
"Blue": "U",
"Black": "B",
"Red": "R",
"Green": "G",
}
for name, abbrev := range colors {
result, err := insert.Exec(name, abbrev)
rowsAffected, err := result.RowsAffected()
if err != nil || rowsAffected != 1 {
log.Fatal(err)
}
}
}
func createGamepieceColorTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS GamepieceColor (
ColorId INT,
GamepieceId INT,
PRIMARY KEY (ColorId, GamepieceId),
FOREIGN KEY (ColorId) REFERENCES Color(Id),
FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id)
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func createGamepieceColorIdentityTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS GamepieceColorIdentity (
ColorId INT,
GamepieceId INT,
PRIMARY KEY (ColorId, GamepieceId),
FOREIGN KEY (ColorId) REFERENCES Color(Id),
FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id)
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func createGamepieceColorIndicatorTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS GamepieceColorIndicator (
ColorId INT,
GamepieceId INT,
PRIMARY KEY (ColorId, GamepieceId),
FOREIGN KEY (ColorId) REFERENCES Color(Id),
FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id)
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func createKeywordTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS Keyword (
Id INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(20) NOT NULL
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func createGamepieceKeywordTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS GamepieceKeyword (
GamepieceId INT NOT NULL,
KeywordId INT NOT NULL,
PRIMARY KEY (GamepieceId, KeywordId),
FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id),
FOREIGN KEY (KeywordId) REFERENCES Keyword(Id)
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func createFormatTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS Format (
Id INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(50)
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func createGamepieceFormatLegalityTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS GamepieceFormatLegality (
GamepieceId INT NOT NULL,
FormatId INT NOT NULL,
PRIMARY KEY (GamepieceId, FormatId),
FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id),
FOREIGN KEY (FormatId) REFERENCES Format(Id),
Legality ENUM("legal", "not_legal", "restricted", "banned") NOT NULL
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func createSetTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS ExpansionSet (
Id INT AUTO_INCREMENT PRIMARY KEY,
SetCode VARCHAR(4) NOT NULL,
Name VARCHAR(40) NOT NULL,
Digital BOOLEAN NOT NULL
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func createArtistTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS Artist (
Id INT AUTO_INCREMENT PRIMARY KEY,
ScryfallId VARCHAR(36) NOT NULL,
Name VARCHAR(100) NOT NULL
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func createCardPrintingTable(db *sql.DB) {
query := `CREATE TABLE IF NOT EXISTS CardPrinting (
Id INT AUTO_INCREMENT PRIMARY KEY,
GamepieceId INT,
FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id),
Language VARCHAR(3) NOT NULL,
ReleasedAt DATETIME NOT NULL,
Reprint BOOLEAN NOT NULL,
ExpansionSetId INT NOT NULL,
FOREIGN KEY (ExpansionSetId) REFERENCES ExpansionSet(Id),
CollectorNumber VARCHAR(10) NOT NULL,
Rarity ENUM("common", "uncommon", "rare", "special", "mythic", "bonus") NOT NULL,
ArtistId INT NULL,
FOREIGN KEY (ArtistId) REFERENCES Artist(Id),
BorderColor ENUM("black", "white", "borderless", "silver", "gold") NOT NULL,
Frame ENUM("1993", "1997", "2003", "2015", "future") NOT NULL,
FullArt BOOLEAN NOT NULL,
Textless BOOLEAN NOT NULL,
StorySpotlight BOOLEAN NOT NULL,
FlavorName VARCHAR(40) NULL,
FlavorText VARCHAR(300) NULL,
DigitalOnly BOOLEAN NOT NULL,
Variation BOOLEAN NOT NULL,
VariationId VARCHAR(36) NULL,
SecurityStamp ENUM("oval", "triangle", "acorn", "circle", "arena", "heart") NULL
);`
_, err := db.Exec(query)
if err != nil {
log.Fatal(err)
}
}
func CreateDatabaseSchema(db *sql.DB) {
createGamepieceTable(db)
createColorTable(db)
populateColorTable(db)
createGamepieceColorTable(db)
createGamepieceColorIdentityTable(db)
createGamepieceColorIndicatorTable(db)
createKeywordTable(db)
createGamepieceKeywordTable(db)
createFormatTable(db)
createGamepieceFormatLegalityTable(db)
createSetTable(db)
createArtistTable(db)
createCardPrintingTable(db)
}

View File

@ -1,7 +1,8 @@
module sevenkeys module main
go 1.22.1 go 1.22.1
require github.com/go-sql-driver/mysql v1.8.1 require (
filippo.io/edwards25519 v1.1.0 // indirect
require filippo.io/edwards25519 v1.1.0 // indirect github.com/go-sql-driver/mysql v1.8.1 // indirect
)

View File

@ -2,8 +2,6 @@ USE DATABASE sevenkeys;
CREATE TABLE IF NOT EXISTS Gamepiece ( CREATE TABLE IF NOT EXISTS Gamepiece (
Id INT AUTO_INCREMENT PRIMARY KEY, Id INT AUTO_INCREMENT PRIMARY KEY,
ArenaId INT NULL,
ScryfallId VARCHAR(36) NOT NULL,
OracleId VARCHAR(36) NOT NULL, OracleId VARCHAR(36) NOT NULL,
Name VARCHAR(141) NOT NULL, Name VARCHAR(141) NOT NULL,
Layout ENUM("normal", Layout ENUM("normal",
@ -36,6 +34,7 @@ CREATE TABLE IF NOT EXISTS Gamepiece (
OracleText VARCHAR(800) NOT NULL, OracleText VARCHAR(800) NOT NULL,
Power VARCHAR(5) NULL, Power VARCHAR(5) NULL,
Toughness VARCHAR(5) NULL, Toughness VARCHAR(5) NULL,
Legalities, -- TODO: many-to-many table
ReserveList BOOLEAN NOT NULL, ReserveList BOOLEAN NOT NULL,
); );
@ -66,20 +65,12 @@ CREATE TABLE IF NOT EXISTS GamepieceColorIdentity (
FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id) FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id)
); );
CREATE TABLE IF NOT EXISTS GamepieceColorIndicator (
ColorId INT,
GamepieceId INT,
PRIMARY KEY (ColorId, GamepieceId),
FOREIGN KEY (ColorId) REFERENCES Color(Id),
FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id)
);
CREATE TABLE IF NOT EXISTS Keyword ( CREATE TABLE IF NOT EXISTS Keyword (
Id INT AUTO_INCREMENT PRIMARY KEY, Id INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(20) NOT NULL Name VARCHAR(20) NOT NULL
); );
CREATE TABLE IF NOT EXISTS GamepieceKeyword ( CREATE TABLE IF NOT EXISTS GamepieceKeywords (
GamepieceId INT NOT NULL, GamepieceId INT NOT NULL,
KeywordId INT NOT NULL, KeywordId INT NOT NULL,
PRIMARY KEY (GamepieceId, KeywordId), PRIMARY KEY (GamepieceId, KeywordId),
@ -101,19 +92,6 @@ CREATE TABLE IF NOT EXISTS GamepieceFormatLegality (
Legality ENUM("legal", "not_legal", "restricted", "banned") NOT NULL Legality ENUM("legal", "not_legal", "restricted", "banned") NOT NULL
); );
CREATE TABLE IF NOT EXISTS ExpansionSet (
Id INT AUTO_INCREMENT NOT NULL,
SetCode VARCHAR(4) NOT NULL,
Name VARCHAR(40) NOT NULL,
Digital BOOLEAN NOT NULL,
);
CREATE TABLE IF NOT EXISTS Artist (
Id INT AUTO_INCREMENT NOT NULL,
ScryfallId VARCHAR(36) NOT NULL,
Name VARCHAR(100) NOT NULL
);
CREATE TABLE IF NOT EXISTS CardPrinting ( CREATE TABLE IF NOT EXISTS CardPrinting (
Id INT AUTO_INCREMENT NOT NULL, Id INT AUTO_INCREMENT NOT NULL,
GamepieceId INT, GamepieceId INT,
@ -121,21 +99,17 @@ CREATE TABLE IF NOT EXISTS CardPrinting (
Language VARCHAR(3) NOT NULL, Language VARCHAR(3) NOT NULL,
ReleasedAt DATETIME NOT NULL, ReleasedAt DATETIME NOT NULL,
Reprint BOOLEAN NOT NULL, Reprint BOOLEAN NOT NULL,
ExpansionSetId INT NOT NULL, SetId INT NOT NULL,
FOREIGN KEY (SetId) REFERENCES ExpansionSet(Id), FOREIGN KEY (SetId) REFERENCES Set(Id), -- TODO: Set table
CollectorNumber VARCHAR(10) NOT NULL, CollectorNumber VARCHAR(10) NOT NULL,
Rarity ENUM("common", "uncommon", "rare", "special", "mythic", "bonus") NOT NULL, Rarity ENUM("common", "uncommon", "rare", "special", "mythic", "bonus") NOT NULL,
ArtistId INT NULL, ArtistId INT NULL,
FOREIGN KEY (ArtistId) REFERENCES Artist(Id), FOREIGN KEY (ArtistId) REFERENCES Artist(Id), -- TODO: Artist table
BorderColor ENUM("black", "white", "borderless", "silver", "gold") NOT NULL, BorderColor ENUM("black", "white", "borderless", "silver", "gold") NOT NULL,
Frame ENUM("1993", "1997", "2003", "2015", "future") NOT NULL, Frame ENUM("1993", "1997", "2003", "2015", "future") NOT NULL,
FullArt BOOLEAN NOT NULL, FullArt BOOLEAN NOT NULL,
Textless BOOLEAN NOT NULL, Textless BOOLEAN NOT NULL,
StorySpotlight BOOLEAN NOT NULL, StorySpotlight BOOLEAN NOT NULL,
FlavorName VARCHAR(40) NULL, FlavorName VARCHAR(40) NULL, -- TODO: check length
FlavorText VARCHAR(300) NULL, FlavorText VARCHAR(300) NULL, -- TODO: check length
DigitalOnly BOOLEAN NOT NULL,
Variation BOOLEAN NOT NULL,
VariationId VARCHAR(36) NULL,
SecurityStamp ENUM("oval", "triangle", "acorn", "circle", "arena", "heart") NULL,
); );