Created
April 30, 2019 09:52
-
-
Save rbq/d1e1010971c24dfc0b0dc965c30cde4c to your computer and use it in GitHub Desktop.
Rails 6 migrations to upgrade SQLite booleans from t/f to 1/0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConvertSqliteBooleans < ActiveRecord::Migration[6.0] | |
BOOLEANS = [ | |
[ 'table_name', 'column_name' ], | |
[ 'table_name', 'column_name' ], | |
] | |
def up | |
BOOLEANS.each do |table, column| | |
execute "UPDATE #{table} SET #{column} = 1 WHERE #{column} = 't';" | |
execute "UPDATE #{table} SET #{column} = 0 WHERE #{column} = 'f' OR #{column} IS NULL;" | |
end | |
end | |
def down | |
BOOLEANS.each do |table, column| | |
execute "UPDATE #{table} SET #{column} = 't' WHERE #{column} = 1;" | |
execute "UPDATE #{table} SET #{column} = 'f' WHERE #{column} = 0;" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment