Skip to content

Commit cceeed5

Browse files
committed
Add uniqueness constraints to multiple sql tables.
1 parent 219bf31 commit cceeed5

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

internal/database/mariadb.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (m *MariaDB) InitializeSchema() error {
4848
4949
CONSTRAINT pk_source PRIMARY KEY (id),
5050
51-
UNIQUE unique_source_idx (name, auth_token)
51+
UNIQUE unique_source (name, auth_token)
5252
)`)
5353
if err != nil {
5454
return fmt.Errorf("Unable to create sources table: %v", err)
@@ -63,7 +63,8 @@ func (m *MariaDB) InitializeSchema() error {
6363
6464
CONSTRAINT pk_asset PRIMARY KEY (id),
6565
66-
UNIQUE unique_asset_idx (type, value),
66+
UNIQUE unique_asset (type, value),
67+
6768
INDEX value_idx (value),
6869
INDEX type_idx (type))`)
6970
if err != nil {
@@ -81,6 +82,8 @@ func (m *MariaDB) InitializeSchema() error {
8182
CONSTRAINT fk_from FOREIGN KEY (from_id) REFERENCES assets (id),
8283
CONSTRAINT fk_to FOREIGN KEY (to_id) REFERENCES assets (id),
8384
85+
UNIQUE unique_relation (from_id, to_id, type),
86+
8487
INDEX full_relation_type_from_to_idx (type, from_id, to_id),
8588
INDEX full_relation_type_to_from_idx (type, to_id, from_id),
8689
INDEX full_relation_from_type_to_idx (from_id, type, to_id),
@@ -97,6 +100,8 @@ func (m *MariaDB) InitializeSchema() error {
97100
source_id INT NOT NULL,
98101
relation_id BIGINT UNSIGNED NOT NULL,
99102
update_time TIMESTAMP,
103+
104+
UNIQUE unique_relation_by_source (source_id, relation_id),
100105
101106
CONSTRAINT pk_relations_by_source PRIMARY KEY (id),
102107
CONSTRAINT fk_relations_by_source_source_id FOREIGN KEY (source_id) REFERENCES sources (id) ON DELETE CASCADE,
@@ -113,6 +118,8 @@ func (m *MariaDB) InitializeSchema() error {
113118
source_id INT NOT NULL,
114119
asset_id BIGINT UNSIGNED NOT NULL,
115120
update_time TIMESTAMP,
121+
122+
UNIQUE unique_asset_by_source (source_id, asset_id),
116123
117124
CONSTRAINT pk_assets_by_source PRIMARY KEY (id),
118125
CONSTRAINT fk_asset_by_source_source_id FOREIGN KEY (source_id) REFERENCES sources (id) ON DELETE CASCADE,

0 commit comments

Comments
 (0)