Skip to content

TILs

Fueling Curiosity, One Insight at a Time

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.

  1. When we rewrite git history (like squashing commits, interactive rebase), our local branch diverges from the remote branch.
    A regular git push will be rejected because the histories don't match. The first option is git push --force, which overwrites the remote branch with our local version.

    The Usual Approach: git push --force

    Code

    # DANGEROUS - can overwrite other people's work
    # We do it everyday though
    git push --force origin <feature-branch>


    The Safer Alternative: git push --force-with-lease

    Code

    # SAFER - includes safety checks
    git push --force-with-lease origin feature-branch


    How --force-with-lease works:
    • Checks if the remote branch has moved since we last fetched it.
    • Only overwrites if the remote is exactly where we expect it to be.
    • Fails safely if someone else has pushed commits we don't have locally.
    • Prevents accidental overwrites of other people's work.
    PS: Might sound simple, but helps a lot when working with bigger teams.

    #git #github
  2. If a port (say 3000) is already in use, you can check what’s running on it with:

    Code

    lsof -i :3000


    Here, lsof stands for “List Open Files” — and in Unix/Linux, everything is a file, including network sockets.

    So this command lists all processes that have files (or ports) open.
    Example output:

    Code

    COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    node     1234  nived  22u  IPv6  0x...      0t0  TCP *:3000 (LISTEN)


    Now you can see the PID (Process ID) of the program using the port.

    To stop it, run:

    Code

    kill 1234


    The kill command sends a signal to a process — by default, it’s SIGTERM (Signal Terminate), which politely asks the process to shut down.
    If the process refuses to die, you can forcefully stop it with:

    Code

    kill -9 1234


    Here, -9 represents the SIGKILL (Signal Kill) — which immediately ends the process without cleanup.

    #unix
  3. Mohammad
    The JavaScript console isn’t just console.log. It provides specialized methods for different scenarios:
    • console.error() → logs errors in red.
    • console.warn() → highlights warnings.
    • console.table() → formats arrays or objects as tables.
    • console.group() / console.groupEnd() → groups related logs for better organization.
    💡 Using these methods makes debugging clearer and more structured than relying only on console.log.
    #Javascript
  4. How to deploy to fly.

    1. Initial Setup

    Code

    # Initialize the app
    
    fly launch
    
    # This creates:
    # - fly.toml (basic configuration)
    # - .dockerignore
    # - Dockerfile


    2. App Creation

    Code

    # Create app in specific organization
    
    fly apps create ui-delta-c9s --org c9s-staging


    3. Configuration Setup

    Code

    # Set required secrets
    # Example
    
    fly secrets set RAILS_MASTER_KEY=$(cat config/master.key) --app ui-delta-c9s
    fly secrets set SECRET_KEY_BASE=$(openssl rand -hex 64) --app ui-delta-c9s
    fly secrets set RAILS_ENV=production --app ui-delta-c9s


    4. Database Setup

    Code

    # Option 1: Create new database
    fly postgres create --name ui-delta-db --region sin
    
    # Option 2: Attach to shared cluster
    fly postgres attach postgres-fire-9606 -a ui-delta-c9s


    5. Customize fly.toml as required for the project

    6. Deploy

    Code

    fly deploy --app ui-delta-c9s


    #fly #deploy
  5. The Problem:
    https://Fly.io|Fly.io databases use internal networks (.flycast domains) that aren't publicly accessible, so we can't connect directly from external tools like DBeaver.

    Solution:
    Use https://Fly.io|Fly.io's proxy to create a secure tunnel:

    Code

    fly proxy 5433:5432 --app <cluster_name>


    How It Works:
    • Local port 5433 → Remote database port 5432
    • Creates a secure tunnel through https://Fly.io|Fly.io's network
    • Keep the proxy running while using external tools
    • Allows external tools like DBeaver to connect via localhost:5433
    #database #fly
  6. The Problem: When comparing images for UI testing, we needed to implement a fuzz factor to ignore small colour differences (like anti-aliasing, compression artifacts, or minor rendering variations). This is crucial for UI screenshots where a 1-2 pixel difference shouldn't count as a "failure."

    Command Line approach

    Code

    # This works but is fragile
    magick compare -metric AE -fuzz 10% image1.png image2.png NULL: 2>&1
    
    # Problems:
    # - Shell command parsing issues
    # - Error handling is difficult
    # - Output parsing is brittle
    # - Cross-platform compatibility issues
    # - Hard to debug when things go wrong


    The Mini Magick Solution

    Code

    # Step 1: Create difference image
    difference_image = first_image.composite(second_image) do |c|
      c.compose "difference"
    end
    
    # Step 2: Apply fuzz factor using threshold
    thresholded_diff = difference_image.dup
    thresholded_diff.combine_options do |c|
      c.normalize
      c.threshold "10%"
    # This acts as our fuzz factor!
    end
    
    # Step 3: Get statistics
    mean_value = thresholded_diff.identify do |c|
      c.format "%[fx:mean]"
    end
    
    # Step 4: Convert to percentage
    percentage = mean_value.to_f * 100


    Key Insight: Fuzz Factor = Threshold
    - In ImageMagick: fuzz 10% tells it to ignore differences below 10%
    - In MiniMagick: threshold10% does the same thing by setting pixels below 10% to black

    #ruby #image
  7. * The Problem:* I had an existing Elixir app with a users table that used password_digest field, but Devise expects encrypted_password by default.

    Devise's Default Password Field:

    Code

    # Devise expects this by default:
    class User < ApplicationRecord
      devise :database_authenticatable
      # Uses 'encrypted_password' column automatically
    end


    Our Existing Schema:

    Code

    # Uses password_digest
    create_table :users do |t|
      t.string :password_digest, null: false  # ← Different field name!
      # ... other fields
    end


    The Override Solution:

    Code

    # Devise calls: user.encrypted_password
    # Our override returns: user.password_digest
    
    class User < ApplicationRecord
      devise :database_authenticatable, :registerable, :validatable
    
      # Tell Devise to use our existing password_digest field
      def self.encrypted_password_column
        :password_digest
      end
    
      # Override the getter method
      def encrypted_password
        read_attribute(:password_digest)
      end
    
      # Override the setter method  
      def encrypted_password=(value)
        self.password_digest = value
      end
    end


    So we can simply override Devise's password field by:
    1. Telling Devise which column to use (encrypted_password_column)
    2. Creating getter/setter bridges (encrypted_password methods)
    3. Using read_attribute to avoid method conflicts
    #devise #auth
  8. What does model: resource actually do in form_with?

    model: resource is the bridge between our form and the data object. It makes forms smart - they remember values, show errors, and handle the complex Rails form lifecycle automatically.

    Example:

    Code

    <%= form_with model: resource, as: resource_name, url: session_path(resource_name), local: true, class: "space-y-6" do |f| %>


    model: resource tells Rails:
    • Which object to bind the form to
    • Where to get field values from
    • Where to send validation errors to
    • What HTTP method to use (POST for new, PATCH for existing)
    In Devise Context:
    resource is a Devise helper that returns:
    • New User object (for signup) - User.new
    • New User object (for signin) - User.new (usually empty, not pre-populated)
    Without model: resource:
    • Form fields are always empty
    • No automatic error handling
    • Manual parameter naming required
    • No automatic HTTP method detection
    #CU6U0R822 #devise
  9. To understand network sockets locally, you can try this simple demo with two terminal tabs:
    1. In the first tab run ns -l 1234 : This starts a small server process that listens for TCP connections on your computer’s address (localhost / 127.0.0.1) at port 1234.
    2. In the second tab run ns localhost 1234 : This connects a client to the server on 127.0.0.1:1234 , creating a TCP connection between the two processes.
    Now type text in one terminal and press Enter you’ll see the same text appear in the other terminal and vice-versa. 🚀

    #sockets
  10. Ashwani Kumar JhaSenior System Analyst
    Filtering API logs with path params in OpenSearch

    When querying logs in OpenSearch Dashboards, paths with dynamic segments (like IDs) often don’t match with the usual search bar syntax because the field is mapped as text instead of keyword.

    Problem:
    • Queries like

    Code

    json.req.url: "/api/bankAccounts/*/debit"


    return no results.
    • .keyword may not exist (json.req.url.keyword) if the field wasn’t mapped that way at ingestion.
    Solution:
    Use Query DSL with a wildcard or regexp query.

    ✅ Wildcard query (matches any value in the middle):

    Code

    {
      "query": {
        "wildcard": {
          "json.req.url": {
            "value": "/api/bankAccounts*/debit"
          }
        }
      }
    }


    ✅ Regex query (restricts the middle part to numbers):

    Code

    {
      "query": {
        "regexp": {
          "json.req.url": "/api/bankAccounts/[0-9]+/debit"
        }
      }
    }


    #opensearch #logs
  11. Mohammad
    You can mark methods as deprecated in Rails using ActiveSupport::Deprecation. This warns developers that a method will be removed in future versions.


    Code

    class User < ApplicationRecord
      def full_name
        "#{first_name} #{last_name}"
      end
    
      # Mark full_name as deprecated
      deprecate :full_name, deprecator: ActiveSupport::Deprecation.new("2.0", "MyApp")
    end



    Code

    DEPRECATION WARNING: full_name is deprecated and will be removed from MyApp 2.0


    💡 Might be especially useful in larger codebases, where many developers might be using the same method and you want to safely signal that it will be removed without breaking things immediately.
    #Rails
  12. In Prisma, there’s a big difference between directly setting a foreign key and using connect when creating related records.

    Code

    // ❌ Directly setting the foreign key
    users: {
      create: {
        userId: testUser.id,
      },
    }
    
    // ✅ Using relation API
    users: {
      create: {
        user: {
          connect: { id: testUser.id },
        },
      },
    }


    • Direct assignment just writes the raw foreign key value — Prisma doesn’t check if the user actually exists.
    • connect uses Prisma’s relation API, validates that the record exists, and safely links them.

    #prisma
  13. Ashwani Kumar JhaSenior System Analyst
    Nx has a built-in command to remove libraries

    Nx provides the @nx/workspace:remove generator that can remove a library from our workspace.

    The command:

    Code

    npx nx g @nx/workspace:remove <library-name>


    What it does automatically:
    • Deletes the library folder and all its files
    • Removes the project from nx.json and workspace configuration
    • Checks for dependencies and warns if other projects still use it
    We can use --forceRemove flag if we want to remove a library even when other projects depend on it (though this can break the build).
    #nx #monorepo
  14. WITH (NOLOCK) is often used to speed up queries by avoiding shared locks, which means the query won’t block other reads or writes. This can improve performance on busy systems, but it comes at the cost of data accuracy.

    When using NOLOCK, SQL Server may:
    • Read uncommitted (dirty) data that could later be rolled back.
    • Return missing or duplicate rows due to page splits and concurrent writes.
    • Show inconsistent values within the same row if columns are updated mid-read.
    In short: NOLOCK trades reliability for speed. It’s fine for dashboards, reports, or monitoring where approximate numbers are acceptable, but it should be avoided for financial or critical business logic where accuracy is essential.

    #sql
  15. Prisma --create-only

    npx prisma migrate dev --create-only --name <migration_name>

    Creates a migration file with the SQL changes but does not apply them to the database.

    Why this is useful:

    ✅ Safe inspection – lets you review the generated SQL before running it, especially helpful for destructive operations like drops or PK changes.

    ✅ Manual adjustments – you can tweak the SQL (e.g., add a USING clause for type casting or backfill data before dropping a column).

    ✅ Separation of responsibilities – you can generate migrations while DBAs/ops review and apply them in controlled environments.

    Key takeaway:

    👉 --create-only is like a dry run for migration generation – you get the recipe, but the dish isn't cooked yet 🍳.

    #postgres
  16. Mohammad
    Rails has a built-in way to reduce repetition in associations—with_options.
    When multiple has_many relationships share the same option (like dependent: :destroy), repeating it clutters your model:


    Ruby

    class Account < ApplicationRecord
      has_many :customers, dependent: :destroy
      has_many :products,  dependent: :destroy
      has_many :invoices,  dependent: :destroy
      has_many :expenses,  dependent: :destroy
    end


    Using with_options, you can group them under one block:


    Ruby

    class Account < ApplicationRecord
      with_options dependent: :destroy do
        has_many :customers
        has_many :products
        has_many :invoices
        has_many :expenses
      end
    end


    This makes the intent clearer—all these associations share the same rule.
    It's easier to read, less error-prone, and keeps your model DRY ✨
    #Rails
  17. Ashwani Kumar JhaSenior System Analyst
    Next.js Typed Routes

    Next.js now provides TypeScript support for route parameters and navigation, helping catch routing errors at compile time rather than runtime.

    Next.js automatically generates route types based on our file structure in the app directory.

    For example, if we have:

    TypeScript

    app/
      users/
        [id]/
          page.tsx
      posts/
        [slug]/
          page.tsx


    The generated types will understand routes like /users/123 and /posts/my-post-slug.


    TypeScript

    import { useRouter } from 'next/navigation'
    import Link from 'next/link'
    
    // TypeScript knows about our routes
    const router = useRouter()
    router.push('/users/123') // ✅ Valid
    router.push('/invalid-route') // ❌ TypeScript error
    
    // Link component is also typed
    <Link href="/posts/my-slug">My Post</Link> // ✅ Valid
    <Link href="/wrong-path">Invalid</Link> // ❌ TypeScript error


    #nextjs #typescript
  18. Adithya HebbarSystem Analyst
    How LLM's temperature affects AI output:

    LLM models use a parameter called temperature to control randomness in generated responses.
    temperature: 0 → deterministic output (same input = same output)
    temperature: 1 → default setting, balanced creativity
    temperature > 1 → more random, creative, but less reliable

    Tip: Use lower temperatures when you need reliable, consistent responses, and higher temperatures for creative or exploratory tasks.
    #llm
  19. Mohammad
    Traits in FactoryBot allow you to define reusable pieces of attributes that can be mixed into factories to create variations of objects without duplicating code.


    Code

    FactoryBot.define do
      factory :user do
        name { "John Doe" }
    
        trait :admin do
          role { "admin" }
        end
      end
    end
    
    create(:user, :admin)


    Here, :admin is a trait that overrides or adds attributes (role: "admin").
    You can pass traits to create, build, or attributes_for to easily generate variations.
    #CU6U0R822 #RSpec
  20. Swasthik
    Boost Query Performance with Prisma's Rust-free Engine(v6.7.0+)

    Why?
    • Quick Setup
    • Faster queries
    • 85–90% bundle size reduce
    • Smoother & Better DX
    Setup Steps :

    1. Update Your schema.prisma

    Code

    generator client {
      provider        = "prisma-client-js" // or `prisma-client`
      previewFeatures = ["queryCompiler", "driverAdapters"]
      output          = "../generated/prisma"
    }


    2. Re-Generate Prisma Client

    Code

    npx prisma generate


    3. Install Driver Adapter
    • For Postgres: npm install @prisma/adapter-pg
    • For other DBs: https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/no-rust-engine#3-install-the-driver-adapter|Prisma Adapter Docs
    4. Instantiate Prisma Client
    • For Postgres:

    Code

    import { PrismaPg } from '@prisma/adapter-pg'
    import { PrismaClient } from './generated/prisma'
    
    const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
    const prisma = new PrismaClient({ adapter })


    • For other DBs: https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/no-rust-engine#4-instantiate-prisma-client|Prisma Client Docs
    5. All done!🎉 Now your can query as usual.

    🔗 https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/no-rust-engine|Official Rust-free Prisma Docs

Showing page 2 of 42

Your competitors are already using AI. The question is how fast you want to unlock the value.

  • Don't know where to start?

    AI is everywhere but it's unclear which investments will actually move your metrics and which are expensive experiments.

  • Your data isn't ready

    Most AI projects fail at the data layer. Pipelines, quality, access all need work before LLMs can deliver value.

  • Internal teams are stretched

    Your engineers are shipping product. They don't have capacity to also become AI specialists with production-grade experience.

  • Legacy systems block everything

    Aging, undocumented codebases make AI integration slow, risky, and expensive. They need to move first.

Don't worry. We've got you covered.

Start with the audit.

Book a free discovery call