1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-18 22:48:02 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Bob Conan
56c2910252 Merge 5615da920f into 86972f41cc 2024-11-20 11:10:29 -05:00
jbranchaud
86972f41cc Add Set Default As SQL Function In Migration as a Rails TIL 2024-11-20 10:10:04 -06:00
jbranchaud
93a663cc9c Adjust formatting, add source link to pg TIL 2024-11-20 09:32:45 -06:00
Bob Conan
5615da920f Update README.md, fix typos 2024-11-15 16:16:31 -06:00
BobConanDev
c60c63f554 Updated README.md, fix typo(s) 2024-11-15 16:42:57 -05:00
4 changed files with 79 additions and 6 deletions

12
.vimrc
View File

@@ -9,3 +9,15 @@ function! CountTILs()
endfunction
nnoremap <leader>c :call CountTILs()<cr>
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',
" })

View File

@@ -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..._
---
@@ -190,7 +190,7 @@ _1511 TILs and counting..._
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
- [Check For Cached Site Assocation File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
- [Check For Cached Site Association File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
@@ -723,7 +723,7 @@ _1511 TILs and counting..._
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
- [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md)
- [Check Table For Any Orphaned Records](postgres/check-table-for-any-orphaned-records.md)
- [Checking Inequality](postgres/checking-inequality.md)
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md)
@@ -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)

View File

@@ -4,7 +4,9 @@ PostgreSQL's `between` construct allows you to make a comparison _between_
two values (numbers, timestamps, etc.).
```sql
> select * from generate_series(1,10) as numbers(a) where numbers.a between 3 and 6;
> select *
from generate_series(1,10) as numbers(a)
where numbers.a between 3 and 6;
a
---
3
@@ -17,7 +19,9 @@ If you supply an empty range by using the larger of the two values first, an
empty set will result.
```sql
> select * from generate_series(1,10) as numbers(a) where numbers.a between 6 and 3;
> select *
from generate_series(1,10) as numbers(a)
where numbers.a between 6 and 3;
a
---
```
@@ -26,7 +30,9 @@ Tacking `symmetric` onto the `between` construct is one way to avoid this
issue.
```sql
> select * from generate_series(1,10) as numbers(a) where numbers.a between symmetric 6 and 3;
> select *
from generate_series(1,10) as numbers(a)
where numbers.a between symmetric 6 and 3;
a
---
3
@@ -39,3 +45,5 @@ issue.
> that the argument to the left of AND be less than or equal to the argument
> on the right. If it is not, those two arguments are automatically swapped,
> so that a nonempty range is always implied.
[source](https://www.postgresql.org/docs/current/functions-comparison.html#:~:text=BETWEEN%20SYMMETRIC%20is%20like%20BETWEEN,nonempty%20range%20is%20always%20implied.)

View File

@@ -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)