Skip to content

Commit ba3be56

Browse files
committed
Maria: set columns charsets
The columns were using the default DB charset and collation. This can cause issues with special characters. Asset values are now using utf8mb4, with binary collation for case sensitiveness and speed. Other columns are set to ascii.
1 parent 1e5235f commit ba3be56

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

internal/database/mariadb.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ func NewMariaDB(username string, password string, host string, databaseName stri
4343
// InitializeSchema initialize the schema of the database
4444
func (m *MariaDB) InitializeSchema() error {
4545
// type must be part of the primary key to be a partition key
46-
q, err := m.db.QueryContext(context.Background(), `
46+
_, err := m.db.ExecContext(context.Background(), `
4747
CREATE TABLE IF NOT EXISTS assets (
4848
id INT NOT NULL AUTO_INCREMENT,
49-
value VARCHAR(255) NOT NULL,
50-
type VARCHAR(64) NOT NULL,
49+
value VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
50+
type VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
5151
5252
CONSTRAINT pk_asset PRIMARY KEY (id),
5353
UNIQUE unique_asset_idx (type, value),
@@ -56,21 +56,20 @@ CREATE TABLE IF NOT EXISTS assets (
5656
if err != nil {
5757
return err
5858
}
59-
defer q.Close()
6059

6160
// type must be part of the primary key to be a partition key
62-
q, err = m.db.QueryContext(context.Background(), `
61+
_, err = m.db.ExecContext(context.Background(), `
6362
CREATE TABLE IF NOT EXISTS relations (
6463
id INT NOT NULL AUTO_INCREMENT,
6564
from_id INT NOT NULL,
6665
to_id INT NOT NULL,
67-
type VARCHAR(64) NOT NULL,
68-
source VARCHAR(64) NOT NULL,
66+
type VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
67+
source VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
6968
7069
CONSTRAINT pk_relation PRIMARY KEY (id),
7170
CONSTRAINT fk_from FOREIGN KEY (from_id) REFERENCES assets (id),
7271
CONSTRAINT fk_to FOREIGN KEY (to_id) REFERENCES assets (id),
73-
72+
7473
INDEX full_relation_type_from_to_idx (type, from_id, to_id),
7574
INDEX full_relation_type_to_from_idx (type, to_id, from_id),
7675
INDEX full_relation_from_type_to_idx (from_id, type, to_id),
@@ -80,24 +79,22 @@ CREATE TABLE IF NOT EXISTS relations (
8079
if err != nil {
8180
return err
8281
}
83-
defer q.Close()
8482

8583
// Create the table storing the schema graphs
86-
q, err = m.db.QueryContext(context.Background(), `
84+
_, err = m.db.ExecContext(context.Background(), `
8785
CREATE TABLE IF NOT EXISTS graph_schema (
8886
id INTEGER AUTO_INCREMENT NOT NULL,
89-
importer VARCHAR(64) NOT NULL,
87+
importer VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
9088
graph TEXT NOT NULL,
9189
timestamp TIMESTAMP,
92-
90+
9391
CONSTRAINT pk_schema PRIMARY KEY (id))`)
9492
if err != nil {
9593
return err
9694
}
97-
defer q.Close()
9895

9996
// Create the table storing importers tokens
100-
q, err = m.db.QueryContext(context.Background(), `
97+
_, err = m.db.ExecContext(context.Background(), `
10198
CREATE TABLE IF NOT EXISTS importers (
10299
id INTEGER AUTO_INCREMENT NOT NULL,
103100
name VARCHAR(64) NOT NULL,
@@ -109,10 +106,9 @@ CREATE TABLE IF NOT EXISTS importers (
109106
if err != nil {
110107
return err
111108
}
112-
defer q.Close()
113109

114110
// Create the table storing importers tokens
115-
q, err = m.db.QueryContext(context.Background(), `
111+
_, err = m.db.ExecContext(context.Background(), `
116112
CREATE TABLE IF NOT EXISTS query_history (
117113
id INTEGER AUTO_INCREMENT NOT NULL,
118114
timestamp TIMESTAMP,
@@ -126,7 +122,7 @@ CREATE TABLE IF NOT EXISTS importers (
126122
if err != nil {
127123
return err
128124
}
129-
defer q.Close()
125+
130126
return nil
131127
}
132128

0 commit comments

Comments
 (0)