Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
feat: create fk for tx_* (#28)
Browse files Browse the repository at this point in the history
closes #20 

We also remove the `USING HASH` index method because even if it creates
smaller index, doing INSERT cost more with this method. `btree` the
default is faster in this case.
  • Loading branch information
rllola authored Oct 17, 2023
1 parent 1d8d423 commit 1e70877
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ impl Database {

query(
format!(
"CREATE INDEX x_source_transfer ON {}.tx_transfer USING HASH (source);",
"ALTER TABLE {0}.tx_transfer ADD CONSTRAINT fk_tx_id FOREIGN KEY (tx_id) REFERENCES {0}.transactions (hash);",
self.network
)
.as_str(),
Expand All @@ -709,7 +709,17 @@ impl Database {

query(
format!(
"CREATE INDEX x_target_transfer ON {}.tx_transfer USING HASH (target);",
"CREATE INDEX x_source_transfer ON {}.tx_transfer (source);",
self.network
)
.as_str(),
)
.execute(&*self.pool)
.await?;

query(
format!(
"CREATE INDEX x_target_transfer ON {}.tx_transfer (target);",
self.network
)
.as_str(),
Expand All @@ -727,6 +737,16 @@ impl Database {
.execute(&*self.pool)
.await?;

query(
format!(
"ALTER TABLE {0}.tx_bond ADD CONSTRAINT fk_tx_id FOREIGN KEY (tx_id) REFERENCES {0}.transactions (hash);",
self.network
)
.as_str(),
)
.execute(&*self.pool)
.await?;

query(
format!(
"ALTER TABLE {}.tx_bridge_pool ADD CONSTRAINT pk_tx_id_bridge PRIMARY KEY (tx_id);",
Expand All @@ -739,7 +759,17 @@ impl Database {

query(
format!(
"CREATE INDEX x_validator_bond ON {}.tx_bond USING HASH (validator);",
"ALTER TABLE {0}.tx_bridge_pool ADD CONSTRAINT fk_tx_id FOREIGN KEY (tx_id) REFERENCES {0}.transactions (hash);",
self.network
)
.as_str(),
)
.execute(&*self.pool)
.await?;

query(
format!(
"CREATE INDEX x_validator_bond ON {}.tx_bond (validator);",
self.network
)
.as_str(),
Expand All @@ -749,7 +779,7 @@ impl Database {

query(
format!(
"CREATE INDEX x_source_bond ON {}.tx_bond USING HASH (source);",
"CREATE INDEX x_source_bond ON {}.tx_bond (source);",
self.network
)
.as_str(),
Expand Down

0 comments on commit 1e70877

Please sign in to comment.