Module: Hanami::Model

Defined in:
lib/hanami/model.rb,
lib/hanami/model/sql.rb,
lib/hanami/model/error.rb,
lib/hanami/model/types.rb,
lib/hanami/model/mapping.rb,
lib/hanami/model/plugins.rb,
lib/hanami/model/version.rb,
lib/hanami/model/migrator.rb,
lib/hanami/model/migration.rb,
lib/hanami/model/sql/types.rb,
lib/hanami/model/association.rb,
lib/hanami/model/entity_name.rb,
lib/hanami/model/sql/console.rb,
lib/hanami/model/configurator.rb,
lib/hanami/model/configuration.rb,
lib/hanami/model/relation_name.rb,
lib/hanami/model/plugins/schema.rb,
lib/hanami/model/mapped_relation.rb,
lib/hanami/model/migrator/logger.rb,
lib/hanami/model/plugins/mapping.rb,
lib/hanami/model/associations/dsl.rb,
lib/hanami/model/migrator/adapter.rb,
lib/hanami/model/sql/entity/schema.rb,
lib/hanami/model/plugins/timestamps.rb,
lib/hanami/model/sql/consoles/mysql.rb,
lib/hanami/model/migrator/connection.rb,
lib/hanami/model/sql/consoles/sqlite.rb,
lib/hanami/model/associations/has_one.rb,
lib/hanami/model/associations/has_many.rb,
lib/hanami/model/sql/consoles/abstract.rb,
lib/hanami/model/migrator/mysql_adapter.rb,
lib/hanami/model/associations/belongs_to.rb,
lib/hanami/model/migrator/sqlite_adapter.rb,
lib/hanami/model/sql/consoles/postgresql.rb,
lib/hanami/model/associations/many_to_many.rb,
lib/hanami/model/migrator/postgres_adapter.rb,
lib/hanami/model/sql/types/schema/coercions.rb

Overview

Hanami::Model migrations

Since:

  • 0.1.0

Defined Under Namespace

Modules: Associations, Plugins, Sql, Types Classes: Association, CheckConstraintViolationError, Configuration, Configurator, ConstraintViolationError, DatabaseError, EntityName, Error, ForeignKeyConstraintViolationError, InvalidCommandError, MappedRelation, Mapping, Migration, MigrationError, Migrator, MissingPrimaryKeyError, NotNullConstraintViolationError, RelationName, UniqueConstraintViolationError, UnknownAttributeError, UnknownDatabaseAdapterError, UnknownDatabaseTypeError

Constant Summary collapse

VERSION =

Defines the version

Since:

  • 0.1.0

'1.3.2'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.7.0



28
29
30
# File 'lib/hanami/model.rb', line 28

def config
  @config
end

.loadedObject (readonly) Also known as: loaded?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.7.0



32
33
34
# File 'lib/hanami/model.rb', line 32

def loaded
  @loaded
end

Class Method Details

.configurationObject

Current configuration

Since:

  • 0.1.0



60
61
62
# File 'lib/hanami/model.rb', line 60

def self.configuration
  @configuration ||= Configuration.new(config)
end

.configure(&block) ⇒ Object

Configure the framework

Examples:

require 'hanami/model'

Hanami::Model.configure do
  adapter :sql, ENV['DATABASE_URL']

  migrations 'db/migrations'
  schema     'db/schema.sql'
end

Since:

  • 0.1.0



52
53
54
55
# File 'lib/hanami/model.rb', line 52

def self.configure(&block)
  @config = Configurator.build(&block)
  self
end

.containerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.7.0



72
73
74
75
76
# File 'lib/hanami/model.rb', line 72

def self.container
  raise 'Not loaded' unless loaded?

  @container
end

.disconnectObject

Disconnect from the database

This is useful for rebooting applications in production and to ensure that the framework prunes stale connections.

Examples:

With Full Stack Hanami Project

# config/puma.rb
# ...
on_worker_boot do
  Hanami.boot
end

With Standalone Hanami::Model

# config/puma.rb
# ...
on_worker_boot do
  Hanami::Model.disconnect
  Hanami::Model.load!
end

Since:

  • 1.0.0



105
106
107
# File 'lib/hanami/model.rb', line 105

def self.disconnect
  configuration.connection&.disconnect
end

.load!(&blk) ⇒ Object

Since:

  • 0.1.0



79
80
81
82
# File 'lib/hanami/model.rb', line 79

def self.load!(&blk)
  @container = configuration.load!(repositories, &blk)
  @loaded    = true
end

.migration(&blk) ⇒ Object

Define a migration

It must define an up/down strategy to write schema changes (up) and to rollback them (down).

We can use up and down blocks for custom strategies, or only one change block that automatically implements “down” strategy.

Examples:

Use up/down blocks

Hanami::Model.migration do
  up do
    create_table :books do
      primary_key :id
      column :book, String
    end
  end

  down do
    drop_table :books
  end
end

Use change block

Hanami::Model.migration do
  change do
    create_table :books do
      primary_key :id
      column :book, String
    end
  end

  # DOWN strategy is automatically generated
end

Parameters:

  • blk (Proc)

    a block that defines up/down or change database migration

Since:

  • 0.4.0



48
49
50
# File 'lib/hanami/model/sql.rb', line 48

def self.migration(&blk)
  Migration.new(configuration.gateways[:default], &blk)
end

.repositoriesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.7.0



66
67
68
# File 'lib/hanami/model.rb', line 66

def self.repositories
  @__repositories__
end