Sequel.migration do
  up do
    alter_table :visualizations do
      set_column_allow_null :permission_id, false
      add_foreign_key [:permission_id], :permissions, null: false
    end
  end

  down do
    alter_table :visualizations do
      drop_constraint :visualizations_permission_id_fkey
      set_column_allow_null :permission_id, true
    end
  end
end

Sequel.migration do
  up do
    Rails::Sequel.connection.transaction do
      load File.join(Rails.root, 'Rakefile')
      Rake::Task['cartodb:permissions:fill_missing_permissions'].invoke(true)
      alter_table :visualizations do
        set_column_allow_null :permission_id, false
        add_foreign_key [:permission_id], :permissions, null: false
      end
    end
  end

  down do
    alter_table :visualizations do
      drop_constraint :visualizations_permission_id_fkey
      set_column_allow_null :permission_id, true
    end
  end
end

Call permissions rake from migration
Call permissions rake from migration
