# New columns in directory (/users)

**URL:** https://meta.discourse.org/t/new-columns-in-directory-users/77814
**Category:** Development
**Created:** [January 12, 2018, 7:57am UTC](https://meta.discourse.org/t/new-columns-in-directory-users/77814 "2018-01-12T07:57:18Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![gdpelican](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gdpelican/32/81308_2.png) [@gdpelican](https://meta.discourse.org/u/gdpelican)
#### Post date: [August 8, 2018, 5:40am UTC](https://meta.discourse.org/t/new-columns-in-directory-users/77814/5 "2018-08-08T05:40:55Z")

</div>

FWIW I’ve decided to go this route with a plugin of mine, where PluginStoreRow and CustomFields didn’t qqquite cut it on their own. ⚠ **Caveat emptor** ⚠ You should be trying really hard not to do this - It’s not recommended behaviour to modify the database from within a plugin, but it can be done unobtrusively in small, additive doses (I’d say you _never for any reason_ want to remove or change existing columns)

Here’s how I did it:

- Write a migration in the plugin which modifies the DB

```plaintext
# plugins/my_plugin/migrations/add_column.rb
class AddColumn < ActiveRecord::Migration[5.1]
  def change
    add_column :post_custom_fields, :my_column, :jsonb, default: {}, index: true
  end
end

```

_NB: it’s a good idea to namespace your column with the name of your plugin, since adding a column with a generic name like ‘value’ could end up actually existing in the future and then you’d have a **real** problem._

- When the plugin is loaded, check to see if the migration has been run, and if not, run it

```plaintext
# plugin.rb
if !PostCustomField.new.respond_to?(:my_column)
  require Rails.root.join('plugins', 'my_plugin', 'migrations', 'add_column')
  AddColumns.new.up # <-- this runs the migration
end

```

- The first time you run `rails s` or `rails c` after this, you should see the following:

```plaintext
add_column(:post_custom_fields, :my_column, :jsonb, {:default=>{}, :index=>true})

```

that means it worked! Subsequent boots of the app shouldn’t display this message.

- **Important, don’t skip plz** - Provide a method for users to clean up your database changes if they don’t want your plugin anymore. I did it with a rake task.

```plaintext
# plugins/my_plugin/tasks/remove_column.rake

# (I think there's a better way to do this, but this line works.)
# Essentially, we need the environment loaded so that ActionRecord::Migration is defined.
require File.expand_path(File.dirname( __FILE__ ) + '../../../config/environment'

desc "remove my_column from the database"
task "my_column:remove" do
  require Rails.root.join('plugins', 'my_plugin', 'migrations', 'add_column')
  AddColumn.new.down
end

```

Then, in your README, instructions on how to use it

```plaintext
This plugin adds a column to the database. If you want to remove this plugin and don't plan on using it again, perform the following steps:

- Enter your discourse container 
./launcher enter app
- Run the following command to reverse the database changes
bundle exec rake -f plugins/my_plugin/tasks/remove_column.rake -T
- Remove the 'git clone' line from your app.yml
- Rebuild your container
./launcher rebuild app

Note that dropping this column is permanent and irreversible for the data stored there..

```

As always, let me know if I’ve missed something or if this can be improved.

---

_[View the full topic](https://meta.discourse.org/t/new-columns-in-directory-users/77814)._
