Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rails 6 migrations to upgrade SQLite booleans from t/f to 1/0
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