From a5753c425798a787f4d8aec888f16e4a23d567d9 Mon Sep 17 00:00:00 2001 From: rashad Date: Wed, 23 Nov 2022 11:09:27 -0500 Subject: [PATCH] Fix fk name bug (#355) Co-authored-by: Rashad Alston --- examples/simple-wasm/simple/src/main.rs | 6 +- fuel-indexer-schema/src/db/models.rs | 7 ++- fuel-indexer-schema/src/db/tables.rs | 73 +++++++++++++++++++++++-- 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/examples/simple-wasm/simple/src/main.rs b/examples/simple-wasm/simple/src/main.rs index 48ca40c26..ed4ddd0d6 100644 --- a/examples/simple-wasm/simple/src/main.rs +++ b/examples/simple-wasm/simple/src/main.rs @@ -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) { diff --git a/fuel-indexer-schema/src/db/models.rs b/fuel-indexer-schema/src/db/models.rs index 7c298f088..f4e9193f6 100644 --- a/fuel-indexer-schema/src/db/models.rs +++ b/fuel-indexer-schema/src/db/models.rs @@ -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 ) } } diff --git a/fuel-indexer-schema/src/db/tables.rs b/fuel-indexer-schema/src/db/tables.rs index 1fe769421..1eb87dc18 100644 --- a/fuel-indexer-schema/src/db/tables.rs +++ b/fuel-indexer-schema/src/db/tables.rs @@ -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] @@ -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] @@ -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()); + } }