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

Commit

Permalink
Fix fk name bug (#355)
Browse files Browse the repository at this point in the history
Co-authored-by: Rashad Alston <rashad@Rashads-MacBook-Pro.local>
  • Loading branch information
ra0x3 and Rashad Alston authored Nov 23, 2022
1 parent b062536 commit a5753c4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
6 changes: 2 additions & 4 deletions examples/simple-wasm/simple/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ async fn get_contract_id(wallet: &WalletUnlocked) -> Bech32ContractId {
Contract::load_contract("../contracts/out/debug/contracts.bin", &None).unwrap();

let bin_path = "../contracts/out/debug/contracts.bin".to_string();
let contract_id = Contract::deploy(
Contract::deploy(
&bin_path,
wallet,
tx_params(),
StorageConfiguration::default(),
)
.await
.unwrap();

contract_id
.unwrap()
}

async fn setup_provider_and_wallet(port: u16) -> (Provider, WalletUnlocked) {
Expand Down
7 changes: 5 additions & 2 deletions fuel-indexer-schema/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ impl ForeignKey {

pub fn name(&self) -> String {
format!(
"fk_{}_{}_{}",
self.table_name, self.reference_table_name, self.reference_column_name
"fk_{}_{}__{}_{}",
self.table_name,
self.column_name,
self.reference_table_name,
self.reference_column_name
)
}
}
Expand Down
73 changes: 69 additions & 4 deletions fuel-indexer-schema/src/db/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ mod tests {
let SchemaBuilder { foreign_keys, .. } = sb.build(graphql_schema);

assert_eq!(foreign_keys.len(), 2);
assert_eq!(foreign_keys[0].create_statement(), "ALTER TABLE namespace.lender ADD CONSTRAINT fk_lender_borrower_id FOREIGN KEY (borrower) REFERENCES namespace.borrower(id) ON DELETE NO ACTION ON UPDATE NO ACTION INITIALLY DEFERRED;".to_string());
assert_eq!(foreign_keys[1].create_statement(), "ALTER TABLE namespace.auditor ADD CONSTRAINT fk_auditor_borrower_id FOREIGN KEY (borrower) REFERENCES namespace.borrower(id) ON DELETE NO ACTION ON UPDATE NO ACTION INITIALLY DEFERRED;".to_string());
assert_eq!(foreign_keys[0].create_statement(), "ALTER TABLE namespace.lender ADD CONSTRAINT fk_lender_borrower__borrower_id FOREIGN KEY (borrower) REFERENCES namespace.borrower(id) ON DELETE NO ACTION ON UPDATE NO ACTION INITIALLY DEFERRED;".to_string());
assert_eq!(foreign_keys[1].create_statement(), "ALTER TABLE namespace.auditor ADD CONSTRAINT fk_auditor_borrower__borrower_id FOREIGN KEY (borrower) REFERENCES namespace.borrower(id) ON DELETE NO ACTION ON UPDATE NO ACTION INITIALLY DEFERRED;".to_string());
}

#[test]
Expand Down Expand Up @@ -659,8 +659,8 @@ mod tests {
let SchemaBuilder { foreign_keys, .. } = sb.build(graphql_schema);

assert_eq!(foreign_keys.len(), 2);
assert_eq!(foreign_keys[0].create_statement(), "ALTER TABLE namespace.lender ADD CONSTRAINT fk_lender_borrower_account FOREIGN KEY (borrower) REFERENCES namespace.borrower(account) ON DELETE NO ACTION ON UPDATE NO ACTION INITIALLY DEFERRED;".to_string());
assert_eq!(foreign_keys[1].create_statement(), "ALTER TABLE namespace.auditor ADD CONSTRAINT fk_auditor_borrower_account FOREIGN KEY (borrower) REFERENCES namespace.borrower(account) ON DELETE NO ACTION ON UPDATE NO ACTION INITIALLY DEFERRED;".to_string());
assert_eq!(foreign_keys[0].create_statement(), "ALTER TABLE namespace.lender ADD CONSTRAINT fk_lender_borrower__borrower_account FOREIGN KEY (borrower) REFERENCES namespace.borrower(account) ON DELETE NO ACTION ON UPDATE NO ACTION INITIALLY DEFERRED;".to_string());
assert_eq!(foreign_keys[1].create_statement(), "ALTER TABLE namespace.auditor ADD CONSTRAINT fk_auditor_borrower__borrower_account FOREIGN KEY (borrower) REFERENCES namespace.borrower(account) ON DELETE NO ACTION ON UPDATE NO ACTION INITIALLY DEFERRED;".to_string());
}

#[test]
Expand Down Expand Up @@ -702,4 +702,69 @@ mod tests {
assert_eq!(foreign_keys[0].create_statement(), "ALTER TABLE lender DROP COLUMN borrower; ALTER TABLE lender ADD COLUMN borrower TEXT REFERENCES borrower(account);");
assert_eq!(foreign_keys[1].create_statement(), "ALTER TABLE auditor DROP COLUMN borrower; ALTER TABLE auditor ADD COLUMN borrower TEXT REFERENCES borrower(account);");
}

#[test]
fn test_schema_builder_for_postgres_creates_fk_with_proper_column_names() {
let graphql_schema: &str = r#"
schema {
query: QueryRoot
}
type QueryRoot {
account: Account
message: Message
}
type Account {
id: ID!
account: Address! @indexed
}
type Message {
id: ID!
sender: Account!
receiver: Account!
}
"#;

let sb = SchemaBuilder::new("namespace", "v1", DbType::Postgres);

let SchemaBuilder { foreign_keys, .. } = sb.build(graphql_schema);

assert_eq!(foreign_keys.len(), 2);
assert_eq!(foreign_keys[0].create_statement(), "ALTER TABLE namespace.message ADD CONSTRAINT fk_message_sender__account_id FOREIGN KEY (sender) REFERENCES namespace.account(id) ON DELETE NO ACTION ON UPDATE NO ACTION INITIALLY DEFERRED;".to_string());
assert_eq!(foreign_keys[1].create_statement(), "ALTER TABLE namespace.message ADD CONSTRAINT fk_message_receiver__account_id FOREIGN KEY (receiver) REFERENCES namespace.account(id) ON DELETE NO ACTION ON UPDATE NO ACTION INITIALLY DEFERRED;".to_string());
}
#[test]
fn test_schema_builder_for_sqlite_creates_fk_with_proper_column_names() {
let graphql_schema: &str = r#"
schema {
query: QueryRoot
}
type QueryRoot {
account: Account
message: Message
}
type Account {
id: ID!
account: Address! @indexed
}
type Message {
id: ID!
sender: Account!
receiver: Account!
}
"#;

let sb = SchemaBuilder::new("namespace", "v1", DbType::Sqlite);

let SchemaBuilder { foreign_keys, .. } = sb.build(graphql_schema);

assert_eq!(foreign_keys.len(), 2);
assert_eq!(foreign_keys[0].create_statement(), "ALTER TABLE message DROP COLUMN sender; ALTER TABLE message ADD COLUMN sender INTEGER REFERENCES account(id);".to_string());
assert_eq!(foreign_keys[1].create_statement(), "ALTER TABLE message DROP COLUMN receiver; ALTER TABLE message ADD COLUMN receiver INTEGER REFERENCES account(id);".to_string());
}
}

0 comments on commit a5753c4

Please sign in to comment.