1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-05 16:18:01 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
jbranchaud
f394ecc055 Add Make Remove Column Migration Reversible as a Rails TIL 2022-07-07 11:43:15 -05:00
jbranchaud
5b5756b714 Add Generate A V4 UUID In The Browser as a JavaScript TIL 2022-06-16 10:06:50 -05:00
3 changed files with 59 additions and 1 deletions

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).
_1225 TILs and counting..._
_1227 TILs and counting..._
---
@@ -408,6 +408,7 @@ _1225 TILs and counting..._
- [for...in Iterates Over Object Properties](javascript/for-in-iterates-over-object-properties.md)
- [Formatting Values With Units For Display](javascript/formatting-values-with-units-for-display.md)
- [Freeze An Object, Sorta](javascript/freeze-an-object-sorta.md)
- [Generate A V4 UUID In The Browser](javascript/generate-a-v4-uuid-in-the-browser.md)
- [Generate Random Integers](javascript/generate-random-integers.md)
- [Get The Location And Size Of An Element](javascript/get-the-location-and-size-of-an-element.md)
- [Get The Response Status From An Axios Error](javascript/get-the-response-status-from-an-axios-error.md)
@@ -766,6 +767,7 @@ _1225 TILs and counting..._
- [Log SQL Queries Executed By ActiveRecord](rails/log-sql-queries-executed-by-activerecord.md)
- [Mark A Migration As Irreversible](rails/mark-a-migration-as-irreversible.md)
- [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md)
- [Make Remove Column Migration Reversible](rails/make-remove-column-migration-reversible.md)
- [Manually Run A Migration From Rails Console](rails/manually-run-a-migration-from-rails-console.md)
- [Mark For Destruction](rails/mark-for-destruction.md)
- [Mask An ActiveRecord Attribute](rails/mask-an-activerecord-attribute.md)

View File

@@ -0,0 +1,24 @@
# Generate A V4 UUID In The Browser
The [Web Crypto
API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) provides
a
[`randomUUID`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID)
function for generating v4 UUID values which are cryptographically random.
It works like this:
```javascript
> crypto.randomUUID()
'f2050c5e-af52-4ca2-b4d6-23758b3396c9'
> crypto.randomUUID()
'079d5186-84d4-41d6-a660-edafb6a74c86'
```
No two UUIDs will be the same. This is a great way to generate IDs that are
guaranteed to be unique. Or if you need a value that is practically impossible
to guess.
I was surprised to see that as of writing this, this function has great browser
support. Modern versions of all browsers except Internet Explorer have
implemented `randomUUID`.

View File

@@ -0,0 +1,32 @@
# Make Remove Column Migration Reversible
The Rails migration DSL includes a
[`#remove_column`](https://edgeapi.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-remove_column)
method for removing a column from a table.
The two required values are the table name and the column name.
```ruby
class RemoveNotesColumnFromTodo < ActiveRecord::Migration[5.2]
def change
remove_column :todos, :notes
end
end
```
This on it's own is not reversible though. If you were to run a `db:rollback`
with this migration, Rails wouldn't know what type to use when re-adding the
column.
To make it reversible, you need to add in the column type as a third argument.
```ruby
remove_column :todos, :notes, :string
```
Note: perhaps more importantly, this column isn't reversible in the sense that
when you remove the column, you are throwing away all the data associated with
that column. Be sure you're ready to part with this data before deploying a
migration like this.
[source](https://stackoverflow.com/questions/24520550/how-do-you-make-remove-column-reversible)