Today I Learned Notes to self about software development

    Naming DB indexes

    Rails 7 apps have a limit on autogenerated index names of 63 characters.

    ArgumentError: Index name
    'index_codeblock_submission_on_user_id_and_lesson_id_and_block_id_and_internal_submission_id' on table
    'codeblock_submission' is too long; the limit is 63 characters.
    

    To work around this you can choose a name to use instead of the autogenerated one.

    update the migration with the name key and value:

        add_index :codeblock_submissions, [:user_id, :lesson_id, :block_id, :internal_submission_id], unique: true, name: "index_codeblock_submissions_on_block_lesson_user_submission"
      end
    

    Source

    WSL - Syncing system clock

    WSL2 clock sometimes becomes out of sync with native windows clock. This can apparently happen after resuming from sleep/hibernate but not always.

    To re-sync run:

    sudo hwclock -s
    

    Rails - Extracting controller logic

    In situations where one controller action is responsible for a lot, I’ve usually been able to extract the complicated code into a model method or helper. This works fine when the complicated logic has to do with the model instead of it being a 5 branch if statement on how to decide what to show the user.

    In those situations some people might say that you hecked up and designed your routen poorly. And maybe they’re right.

    Anyway, I was able to turn this complicated controller action:

      def show
        if params.has_key?(:token)
          invite_link = @forum.invite_links.find_by(token: params[:token])
          if invite_link.active?
            if current_user.present? && current_user.is_member_of?(@forum)
              redirect_to forum_posts_path(@forum), alert: "You're already a member of #{@forum.name}."
            elsif current_user.present? && !current_user.is_member_of?(@forum)
              current_user.memberships.create(invite_link: invite_link, forum: @forum)
              redirect_to forum_posts_path(@forum), notice: "You're now a member of #{@forum.name}."
            else
              render "invite_links/sign_up"
            end
          else
            redirect_back_or_to root_path, alert: "Invite link has not been activated."
          end
        else
          redirect_back_or_to root_path, alert: "You don't have access to view that page."
        end
      end
    

    into this:

      def show
        Forums::HandleInviteService.new(@forum, self).process
      end
    

    after realizing that I can just pass the instance of the controller class to a separate Ruby object and then call render, redirect_to, and params on the controller object.

    I’m not the biggest fan of service objects but this seemed to work nicely.

    Heroku - Copying Databases

    Heroku has the CLI command

    heroku pg:copy SOURCE TARGET -a <value>
    

    Which takes a source database and copies the contents to the target database. The problem is that apparently the only way I can specify the database of a specific app is to use the key word DATABASE for both apps.

    copying production data to staging

    heroku pg:copy my-app-production::DATABASE DATABASE --app my-app-staging       
    

    Doing this makes the confirmation message extremely confusing:

    css-float-docs-demo.gif

    It’s not really clear from the warning message which DATABASE will be deleted. You just kinda have to trust that it’s not the source db. The docs seem out of date since they suggested using things like CRIMSON and ORANGE as identifiers for the database but it looks like Heroku stopped that naming convention for database add-ons, or it’s optional and the app pipeline I was working wasn’t using it.

    Either way, this command does do what is should do and when it says “This command will remove everything from DATABASE” it means from the target db, not the source db.

    Internet Basics

    Brushing up on the fundamentals that I was taught a while ago and haven’t really thought too deeply about since!

    The Internet is a global network of computers connected to each other which communicate through a standardized set of protocols.

    When referring to The Internet™️, a network means a “group of computers that are connected to each other”.

    For example, at your house the network could consist of a laptop, desktop, smart phone, and game console. Together, the networks from everyone’s home makes up the internet. Consider it a “network of networks”.

    Overview

    The core of the internet is a global network of interconnected routers, which are responsible for directing traffic between different devices and systems. When you send data over the internet, it is broken up into small packets that are sent from your device to a router. The router examines the packet and forwards it to the next router in the path towards its destination. This process continues until the packet reaches its final destination.

    Terms

    • Packet: A small unit of data transmitted over the internet.
    • Router: Device that directs packets between different networks.
    • IP Address: Unique ID assigned to each device on a network. It’s used to route data to the correct destination.
    • Domain Name: Human-readable name used to identify websites in place of an IP Address.
    • DNS: Doman Name System, responsible for converting domain name into an IP address.
    • HTTP: Hypertext Transfer Protocol, used to transfer data between client (browser) and server (web app/computer hosting it).
    • HTTPS: An encrypted verison of HTTP. Used to provide secure communication between client and server.
    • SSL/TLS: Secure Sockets Layer and Transport Layer Security protocols, used to provide secure communication over the network*.

    Protocols

    A protocol is a set of rules and standards that define how information is exchanged between devices and systems.

    Aside from HTTP, HTTPS, SSL, TLS, and DNS mentioned earlier, other important ones include:

    • IP: (Internet Protocol) responsible for routing packets to their correct destination.
    • TCP: (Transmission Control Protocol) responsible for transmitting packets reliably and in correct order.
    • UDP: (User Datagram Protocal) responsible for sending messages to other devices on an IP network.

    Having these “rules” (standards and protocols) is what allows for devices and systems to be created from different manufacturers and vendors and they can still work and function together properly.

    IP Addresses and Domain Names

    An IP address is a unique identifier assigned to each device on a network. It’s used to route data to the correct destination, ensuring that information is sent to the intended recipient. IP addresses are typically represented as a series of four numbers separated by periods, such as “192.168.1.1”.

    Domain names, on the other hand, are human-readable names used to identify websites and other internet resources. They’re typically composed of two or more parts, separated by periods. For example, “google.com” is a domain name. Domain names are translated into IP addresses using the Domain Name System (DNS).