Today I Learned Notes to self about software development

    Amending Git Commits

    So one way or another, you hecked up and now GitHub says one user authored the commit while a different user committed the commit.

    git-commit-by-two-users.png

    How to fix?

    The only way I know is to amend or rebase, which will change commit Hashes and mess up other users commit History.

    If you don’t care about that, proceed.

    Change Committer

    Pass GIT_COMMITTER_EMAIL and GIT_COMMITTER_NAME to amend with email and GitHub username respectively.

    GIT_COMMITTER_EMAIL=joemama@example.com GIT_COMMITTER_NAME=joemama git commit --amend --no-edit
    

    Change Author

    Pass GIT_AUTHOR_EMAIL and GIT_AUTHOR_NAME to amend with email and namerespectively.

    GIT_AUTHOR_EMAIL=joemama@example.com GIT_AUTHOR_NAME=joemama git commit --amend --no-edit
    

    You can always find out the name by running git show ... on the commit that they made:

    commit b65f3160e773ff2a18cc6e4993c278d50cedea94
    Author: Joe Mama <joemama@example.com>
    Date:   Thu Mar 2 11:55:40 2023 -0600
    
        Joe Mama's commit message 😎
    

    Change Date

    Useful if you editing several commits at once and want the origninal dates to not get updated to the current time.

    Prepend GIT_COMMITTER_DATE env variable to your git ammend --no-edit command

    GIT_COMMITTER_DATE="Wed Mar 29 20:32:01 2023 -0600" git commit --amend --no-edit --date="Wed Mar 29 20:32 2023 -0600"
    

    --date will set the author date, which should also be the same as the committer date.

    Amending a commit that’s not the most recent commit

    You’ll need to rebase, git rebase -i HEAD~3 to edit the third most recent commit.

    pick 93ef79f Dynamically resize textarea
    pick cca2dd2 Display full titles on menu link hover
    pick a372bc3 Use JS autosize for query field
    

    You’ll want to choose edit for the commit you want to amend.

    Note, if you only amend the author or committer for a past commit, all dates will be updated to current time.

    If you want to keep the existing dates for these commits, it’s helpful to keep another Terminal tab open with the current git log and choose edit for each commit. Amend the author or committer for the desired commit and edit the date for the rest to be what the were before.

    Reset primary key sequence SQLite3

    I was trying to reset the primary key sequence in a development Rails environment and realized the usual:

    ActiveRecord::Base.connection.reset_pk_sequence!('users')
    

    did not work. (I think that is a postgres exclusive method)

    I did some digging and found that executing this raw SQL did work:

    ActiveRecord::Base.connection.execute("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='table_name'")
    

    I learned from this post.

    Moving hidden files in the command line

    I was attempting to move all files from a subfolder into the root folder in the command line and ran:

    mv subfolder/* .
    

    but to my surprise, files beginning with ., like .gitignore, did not move.

    It turns out hidden files are exlcuded by default I guess?

    If you want to move dotfiles, you can do this instead:

    mv subfolder/{,.}* .
    

    See this answer.

    Docker WSL2 - failed to solve with frontend dockerfile.v0

    I was getting this error when I tried building an image:

    : failed to create LLB definition: rpc error: code = Unknown desc = error getting credentials - err: exit status 255, out: ``
    

    Restarting WSL didn’t help, but I found out that disabling the buildkit does fix it.

    DOCKER_BUILDKIT=0 docker build ...
    

    See SO answer.

    Rails migration shortcuts you might not know of!

    These are definitely listed in the Rails guide, but I rarely see anyone use these so I wasn’t aware they existed!

    Specifying modifiers

    Type Modifiers are listed here. Things like, setting default values, null constraints, and character limits. Apparently, you can specify some of them in the generator command.

    Of course, they don’t just TELL you which ones you can use 😩 that would be too easy. After searching for a long while I found the source code and I think these are the only currently supported modifiers:

    • limit: Sets the maximum number of characters for a string column and the maximum number of bytes for string/text/binary/integer columns.
    • precision: Specifies the precision for decimal/numeric/datetime/time columns.
    • scale: Specifies the scale for the decimal and numeric columns, representing the number of digits after the decimal point.
    • polymorphic: When generating with references, this option will create two columns which can be used for polymorphic associations: <column_name>_type and <column_name>_id.

    To specify these modifiers, you need to pass values enclosed in curly braces after the field type like this:

    Limit:

    rails g migration AddNameToUsers name:string{40}
    

    Precision and Scale:

    rails g migrationAddAmountToProducts amount:decimal{10.2}
    

    You must set both precision and scale at once.

    Polymorphic

    rails g migration add_supplier_to_products supplier:references{polymorphic}
    

    Specifying indexes

    A third value can be specified using another : after the column name and type, to configure the index of the column. This could also be the second option if the column type is a String.

    :index: will just add an index

      add_column :products, :amount, :string
      add_index :products, :amount
    

    :uniq: will add unique: true in the migration.

    Creating join tables

    Migration names containing JoinTable will generate join tables for use with has_and_belongs_to_many associations.

    rails g migration CreateJoinTableCustomerProduct customer product
    

    will create the migration:

    def change
      create_join_table :customers, :products do |t|
        # t.index [:customer_id, :product_id]
        # t.index [:product_id, :customer_id]
      end
    end
    

    Nice.