Automating creation of custom Object Attributes

Hey @abelxluck - we recommend to use a zpm package for what you’re planning. However, addon migrations are kind of special. You need to place them in db/addon/your_package_name and need to define self.up and/or self.down on them. Rails change method will not work! A migration would look like this:

# db/addon/your_package_name/20200121171523_your_migration_name.rb
class YourMigrationName < ActiveRecord::Migration[5.2]
  def self.up
    ObjectManager::Attribute.add(
      # ...
    )
    ObjectManager::Attribute.migration_execute
  end

  def self.down
   ObjectManager::Attribute.remove(
      object: '...',
      name:   '...',
    )
    ObjectManager::Attribute.migration_execute
  end
end

With an entry in your zpm file like this:

    {
      "location": "db/addon/your_package_name/20200121171523_your_migration_name.rb",
      "permission": 644
    },

For testing purposes you can use Package::Migration.migrate('YourPackageName') or Package::Migration.migrate('YourPackageName', 'reverse') on the Zammad rails console.

Hope this helps :+1:

2 Likes