Friday, July 19, 2024

Add non-null modifier for migrations, default script folder and generator, sessions generator and much more!

Posted by vipulnsward

Hey everyone, Happy Friday! Vipul here with the latest updates for This Week in Rails. Let’s dive in.

Add a basic sessions generator
This change adds a new sessions generator to give a basic start to an authentication system using database-tracked sessions.

# Generate with...
bin/rails generate session

# Generated files
app/models/current.rb
app/models/user.rb
app/models/session.rb
app/controllers/sessions_controller.rb
app/views/sessions/new.html.erb
db/migrate/xxxxxxx_create_users.rb
db/migrate/xxxxxxx_create_sessions.rb

Add script folder and generator
This Pull Request adds a new script default folder to hold one-off or general purpose scripts, such as data migration scripts, cleanup scripts, etc.

The new script generator allows us to create such scripts:

rails generate script my_script

We can also specify a folder, when generating scripts:

rails generate script cleanup/my_script

We can then run the generated scripts using:

ruby script/my_script.rb

Remove channels from default app/ structure
Now that Hotwire is the default in Rails, this change drops the channels folder from default app/ structure. The folder still gets created when using the channel generator if needed.

Drop default permissions policy initializer
This change drops the rarely used default permissions_policy configuration files. The configuration can be added back as needed referring to the documentation of permissions_policy instead.

Add not-null modifier to migrations
This change adds a not-null modifier to migrations, which we can now specify using a ! after column types:

# Generating with...
bin/rails generate migration CreateUsers email_address:string!:uniq password_digest:string!

# Produces:
class CreateUsers < ActiveRecord::Migration[8.0]
  def change
    create_table :users do |t|
      t.string :email_address, null: false
      t.string :password_digest, null: false
      t.timestamps
    end
    add_index :users, :email_address, unique: true
  end
end

Raise specific exception when a connection is not defined
This change makes it easier to provide programmatic access to details about requested shard/role. The new ConnectionNotDefined exception provides connection name, shard and role accessors indicating the details of the connection that was requested.

You can view the whole list of changes here.
We had 20 contributors to the Rails codebase this past week!

Until next time!

Subscribe to get these updates mailed to you.