Hanami natively supports PostgreSQL data types.

Please check your PostgreSQL version for the available features.

UUID

Here’s how to use UUID for a column:

  1. # db/migrations/20161113184557_create_projects.rb
  2. Hanami::Model.migration do
  3. up do
  4. execute 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'
  5. create_table :projects do
  6. primary_key :id
  7. column :name, String
  8. column :token, 'uuid'
  9. end
  10. end
  11. down do
  12. drop_table :projects
  13. execute 'DROP EXTENSION IF EXISTS "uuid-ossp"'
  14. end
  15. end
  1. require "securerandom"
  2. ProjectRepository.new.create(name: "Hanami", token: SecureRandom.uuid)
  3. # => #<Project:0x007fbbc78f0a40 @attributes={:id=>1, :name=>"Hanami", :token=>"0aa7ecff-15e4-4aa4-8c00-0e699e2c66f0"}>

UUID as Primary Key

  1. Hanami::Model.migration do
  2. up do
  3. execute 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'
  4. create_table :project_files do
  5. primary_key :id, 'uuid', null: false, default: Hanami::Model::Sql.function(:uuid_generate_v4)
  6. column :name, String
  7. end
  8. end
  9. down do
  10. drop_table :project_files
  11. execute 'DROP EXTENSION IF EXISTS "uuid-ossp"'
  12. end
  13. end
  1. ProjectFileRepository.new.create(name: "source.rb")
  2. # => #<ProjectFile:0x007ff29c4b9740 @attributes={:id=>"239f8e0f-d764-4a76-aaa7-7b59b5301c72", :name=>"source.rb"}>

Array

  1. Hanami::Model.migration do
  2. change do
  3. create_table :articles do
  4. primary_key :id
  5. column :title, String
  6. column :tags, "text[]"
  7. end
  8. end
  9. end
  1. ArticleRepository.new.create(title: "Announcing Hanami 1.0", tags: ["announcements"])
  2. # => #<Article:0x007ffe1a5d9da8 @attributes={:id=>1, :title=>"Announcing Hanami 1.0", :tags=>["announcements"]}>

JSON(B)

  1. Hanami::Model.migration do
  2. change do
  3. create_table :commits do
  4. primary_key :id
  5. column :metadata, "jsonb"
  6. end
  7. end
  8. end
  1. CommitRepository.new.create(metadata: { sha: "8775b81" })
  2. # => #<Commit:0x007f8573dcbbf8 @attributes={:id=>1, :metadata=>{:sha=>"8775b81"}}>