At Codemancers, we believe every day is an opportunity to grow. This section is where our team shares bite-sized discoveries, technical breakthroughs and fascinating nuggets of wisdom we've stumbled upon in our work.
Published
Author
Syed Sibtain
System Analyst
The dig method in Ruby is used to safely extract nested values from arrays or hashes. It allows us to traverse a data structure without worrying about whether each level of the structure exists, thus avoiding NoMethodError exceptions that occur when trying to call methods on nil.
PostgreSQL's Foreign Data Wrapper (FDW) extension and its capabilities for accessing remote databases. FDW provides a standard method for connecting to and querying tables in different databases as if they were part of the local database. This feature is particularly useful for integrating data across multiple PostgreSQL instances or accessing data from different servers.
#### What is FDW?
Foreign Data Wrapper (FDW) is an extension in PostgreSQL designed to connect to external data sources. With FDW, users can perform operations on remote tables seamlessly, making it easier to integrate and analyze data across various systems.
#### How to Use FDW
Here's a concise guide on setting up and using FDW:
1. Install the FDW Extension: To enable FDW, the extension must be installed in the PostgreSQL database:
Code
sqlCREATEEXTENSIONIFNOTEXISTS postgres_fdw;
2. Create a Foreign Server: Define the remote database connection details, including the host, port, and database name:
Code
sqlCREATESERVER my_foreign_serverFOREIGNDATAWRAPPER postgres_fdwOPTIONS (host 'your_host', port '5432', dbname 'remote_db');
3. Set Up User Mapping: Configure authentication details for accessing the remote server:
5. Query the Foreign Tables: Query the foreign tables as if they were local tables:
Code
sqlSELECT*FROM foreign_table_name;
#### Example Use Case
For instance, if there are two databases on the same PostgreSQL server—sales_db and hr_db—FDW can be used to access employee data from hr_db while working within sales_db. This setup simplifies data integration and reporting across different databases.
FDW streamlines data access and management, providing a powerful tool for integrating and analyzing data from multiple sources within PostgreSQL.
#postgres #database #fdw
Published
Author
Adithya Hebbar
System Analyst
To make your Django application ASGI-compatible and run it with Daphne, follow these steps:
1. Install Daphne: Install Daphne if you haven't already:
Code
pip install daphne
2. Ensure ASGI Configuration: Your asgi.py file should look like this:
Replace 'your_project_name.settings' with the path to your Django settings module.
4. Run Daphne: Start Daphne with your ASGI application using:
Code
daphne -u your_project_name.asgi:application
Alternatively, run it on a specific port:
Code
daphne -p 8000your_project_name.asgi:application
This setup will get your Django application running with Daphne as the ASGI server.
#python #django #daphne
Published
Author
Ashwani Kumar Jha
Senior System Analyst
A Lambda layer in AWS Lambda is a feature that lets us manage and share common code and libraries across multiple Lambda functions. By creating a Lambda layer, we can package common code in its own module, which can then be used by our Lambda functions. This reduces duplication and simplifies the management of shared code.
Published
Author
Adithya Hebbar
System Analyst
We can generate a base64-encoded random key easily using either openssl or a combination of dd and base64. Here's how:
Migration: Remove old image columns if switching from direct storage to Active Storage. #CU6U0R822 #activestorage #fileupload
Published
Author
Adithya Hebbar
System Analyst
To logout from Keycloak using the signOut function in NextAuth, you need to override the default behavior to ensure that the user is properly logged out from Keycloak as well. Here's how you can update your signOut function:
I encountered a scenario where I needed to retrieve the scope of one policy and use it within another policy. Specifically, I wanted to delegate permissions from one policy to another.
To address this issue, I learned to use Pundit's methods for manually retrieving policies and scopes: • Retrieving a Policy
Code
Pundit.policy(user, record) # Returns nil if the policy does not existPundit.policy!(user, record) # Raises an exception if the policy does not exist
• Retrieving a Policy Scope:
Code
Pundit.policy_scope(user, ModelClass) # Returns nil if the policy scope does not existPundit.policy_scope!(user, ModelClass) # Raises an exception if the policy scope does not exist
These methods allowed me to delegate permissions effectively by retrieving and applying the appropriate scopes and policies
#rails #pundit #pundit-policy #authorization
Published
Author
Syed Sibtain
System Analyst
Quick Tip: How can we open a PR from one repository to another repository
3. Push the new branch to the destination repository
Code
git push destination new-branch
4. To check all the remote repositories added, we can do:
Code
git remote -v
Then, we create a PR in the destination repository from the new branch. This process effectively copies the changes from the original PR into a new PR in a different repository.
#github #git
Showing 17 to 19 of 82 results
Ready to Build Something Amazing?
Codemancers can bring your vision to life and help you achieve your goals