From 86972f41cc34e9835f4d077282b93c74426c151c Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 20 Nov 2024 10:10:04 -0600 Subject: [PATCH] Add Set Default As SQL Function In Migration as a Rails TIL --- .vimrc | 12 +++++ README.md | 3 +- ...et-default-as-sql-function-in-migration.md | 52 +++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 rails/set-default-as-sql-function-in-migration.md diff --git a/.vimrc b/.vimrc index 6989dbc..f4beaf4 100644 --- a/.vimrc +++ b/.vimrc @@ -9,3 +9,15 @@ function! CountTILs() endfunction nnoremap c :call CountTILs() + +augroup DisableMarkdownFormattingForTILReadme + autocmd! + autocmd BufRead ~/code/til/README.md autocmd! Format +augroup END + +" local til_readme_group = vim.api.nvim_create_augroup('DisableMarkdownFormattingForTILReadme', { clear = true }) +" vim.api.nvim_create_autocmd('BufRead', { +" command = 'autocmd! Format', +" group = til_readme_group, +" pattern = vim.fn.expand '~/code/til/README.md', +" }) diff --git a/README.md b/README.md index 9129e6d..26257a7 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1511 TILs and counting..._ +_1512 TILs and counting..._ --- @@ -1020,6 +1020,7 @@ _1511 TILs and counting..._ - [Serialize With fast_jsonapi In A Rails App](rails/serialize-with-fast-jsonapi-in-a-rails-app.md) - [Set A Timestamp Field To The Current Time](rails/set-a-timestamp-field-to-the-current-time.md) - [Set DateTime To Include Time Zone In Migrations](rails/set-datetime-to-include-time-zone-in-migrations.md) +- [Set Default As SQL Function In Migration](rails/set-default-as-sql-function-in-migration.md) - [Set default_url_options For Entire Application](rails/set-default-url-options-for-entire-application.md) - [Set Schema Search Path](rails/set-schema-search-path.md) - [Set Statement Timeout For All Postgres Connections](rails/set-statement-timeout-for-all-postgres-connections.md) diff --git a/rails/set-default-as-sql-function-in-migration.md b/rails/set-default-as-sql-function-in-migration.md new file mode 100644 index 0000000..a6fa7bf --- /dev/null +++ b/rails/set-default-as-sql-function-in-migration.md @@ -0,0 +1,52 @@ +# Set Default As SQL Function In Migration + +With static default values, like `0`, `true`, or `'pending'`, we can set them +directly as the value of `default`. + +```ruby +class CreateActionsTable < ActiveRecord::Migration[7.2] + def change + create_table :actions do |t| + t.string :status, default: 'pending' + end + end +end +``` + +However, if we want our default value to be a SQL function like `now()`, we +have to use a lambda. + +Let's extend the above example to see what that looks like: + +```ruby +class CreateActionsTable < ActiveRecord::Migration[7.2] + def change + create_table :actions do |t| + t.string :status, default: 'pending' + + t.column :created_at, :timestamptz, default: -> { 'now()' }, null: false + end + end +end +``` + +If we need to alter the default of an existing table's column, we can do +something like this: + +```ruby +class AddDefaultTimestampsToActions < ActiveRecord::Migration[7.2] + def up + change_column_default :actions, :created_at, -> { "now()" } + change_column_default :actions, :updated_at, -> { "now()" } + end + + def down + change_column_default :actions, :created_at, nil + change_column_default :actions, :updated_at, nil + end +end +``` + +I believe this functionality is available to Rails 5.0 and later. + +[source](https://github.com/rails/rails/issues/27077#issuecomment-261155826)