How to write a migration from Rails datetime type to date type

** "It looks good with date instead of this column datetime" **

I used datetime because I deal with dates, but when I think about it carefully, I don't need" time ", so I want to convert it to date.

This goal

--Book models have a release date released_at (datetime) --I want to migrate and make it released_on (date)

environment

Datetime → date conversion using change

def change
  rename_column :books, :released_at, :released_on
  change_column :books, :released_on, 'date USING CAST(released_on AS date)'
end

Can you db: rollback?

That's okay, just wait a minute with git commi .., **! ** **

Keep in mind that migrations roll back.

Let's see if we can roll back the migration mentioned above.

$ rails db:rollback

== ..... : reverting ========
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:


This migration uses change_column, which is not automatically reversible.
To make the migration reversible you can either:
1. Define #up and #down methods in place of the #change method.
2. Use the #reversible method to define reversible behavior.

What caused the error

--When you roll back with change_column, you can't restore it because you don't know the previous type information.

I was able to convert the type with 'date USING CAST (released_on AS date)', but the rollback failed because ** "type information before conversion" ** was missing.

As one way to solve this, let's rewrite it as ** up / down method **.

Method using up / down

def up
  rename_column :books, :published_at, :release_on
  change_column :books, :release_on, 'date USING CAST(release_on AS date)'
end

def down
  rename_column :books, :release_on, :published_at
  change_column :books, :published_at, 'timestamp USING CAST(published_at AS timestamp)'
end

By doing this

--db: migrate executes datetime-> date of ʻup --db: rollback executes down date-> datetime`

Bonus: datetime USING CAST gives an error in PostgreSQL

change_column :books, :released_at, 'datetime USING CAST(released_at AS datetime)'
PG::UndefinedObject: ERROR:  type "datetime" does not exist
LINE 1: ...released_at" TYPE datetime USING CAST(published_at AS datetime)

It was said that datetime in the Rails world becomestimestamp in the PostgreSQL world: sweat_smile:

Recommended Posts

How to write a migration from Rails datetime type to date type
How to write a date comparison search in Rails
How to write Rails
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
How to write Rails validation
How to write Rails seed
How to write Rails routing
java: How to write a generic type list [Note]
[Rails] How to write in Japanese
How to create a form to select a date from the calendar
[Java] How to convert a character string from String type to byte type
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
A memorandum when I investigated how to convert from Epoch Time to Date type in Scala
[Reading impression] "How to learn Rails, how to write a book, and how to teach"
[Basic] How to write a Dockerfile Self-learning ②
How to insert a video in Rails
[Introduction to Java] How to write a Java program
[rails] How to create a partial template
[SpringBoot] How to write a controller test
[Rails] How to create a table, add a column, and change the column type
[Rails] How to create a graph using lazy_high_charts
How to link Rails6 Vue (from environment construction)
How to get a heapdump from a Docker container
[Validation] rails How to specify after today's date
How to implement a like feature in Rails
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
How to make a follow function in Rails
Rails "How to delete NO FILE migration files"
[Rails] How to read the XML file uploaded from the screen with Hash type
How to get the date from the JavaScript Date type that C # developers are addicted to
How to write dockerfile
How to uninstall Rails
How to write docker-compose
How to write Mockito
How to write migrationfile
How to type backslash \
[Rails] How to create a signed URL for CloudFront
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
How to jump from Eclipse Java to a SQL file
How to write a unit test for Spring Boot 2
How to deploy to Heroku from a local docker image
How to delete a new_record object built with Rails
How to manually generate a JWT with Rails Knock
[How to insert a video in haml with Rails]
[Java] How to erase a specific character from a character string
How to download images from AWS S3 (rails, carrierwave)
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
How to write a core mod in Minecraft Forge 1.15.2
How to get started with creating a Rails app
[Rails] How to install a decorator using gem draper
[rails] How to post images
Let's write how to make API with SpringBoot + Docker from 0
Migration from Cobol to JAVA
How to write good code
[Rails] How to use enum