Module: Sequel::Extensions::BrutMigrations

Defined in:
lib/sequel/extensions/brut_migrations.rb

Overview

Modifies and enhances Sequel’s migrations DSL to default to best practices.

  • If no primary key is specified, a primary key column named ‘id` of type `int` will be created.

  • If no ‘created_at` is specified, a column name `created_at` of type `timestamptz` is created.

  • ‘create_table` requires a `comment:` attribute that explains the purpose of the table.

  • ‘create_table` accepts an `external_id: true` attribute that will create a unique `citext` field named `external_id`. This is intended to be used with Plugins::ExternalId.

  • Columns are non-null by default. To make a nullable column, use ‘null: true`.

  • Foreign keys are non-null by default and an index is created by default.

  • The ‘key` method allows specifying additional keys on the table. This effecitvely creates a unique constraint on the fields given to `key`.

Instance Method Summary collapse

Instance Method Details

#add_column(table, *args) ⇒ Object

Overrides Sequel’s ‘add_column` to default `null: false`.



48
49
50
51
52
53
54
55
56
# File 'lib/sequel/extensions/brut_migrations.rb', line 48

def add_column(table,*args)
  options = args.last
  if options.is_a?(Hash)
    if !options.key?(:null)
      options[:null] = false
    end
  end
  super(table,*args)
end

#add_key(fields) ⇒ Object

Specifies a non-primary key based on the fields given. Effectively creates a unique index on these fields. Inside a ‘create_table` block, this can be called via `key`

Parameters:

  • fields (Array)

    fields that should form the key.



43
44
45
# File 'lib/sequel/extensions/brut_migrations.rb', line 43

def add_key(fields)
  add_index fields, unique: true
end

#create_table(*args) ⇒ Object

Overrides Sequel’s ‘create_table`

Parameters:

  • args (Object)

    the arguments to pass to Sequel’s ‘create_table`. If the last entry in `*args` is a `Hash`, new options are recognized:

Options Hash (*args):

  • :comment (String)

    String containing the table’s description, included in the table definition. Required.

  • :external_id (true|false)

    If true, adds a ‘:citext` column named `external_id` that has a unique index on it.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sequel/extensions/brut_migrations.rb', line 18

def create_table(*args)
  super


  if args.last.is_a?(Hash)
    name = args.first
    if name != "schema_migrations" && name != "schema_info"
      if args.last[:comment]
        run %{
          comment on table #{name} is #{literal args.last[:comment]}
        }
      else
        raise ArgumentError, "Table #{name} must have a comment"
      end
      if args.last[:external_id]
        add_column name, :external_id, :citext, unique: true
      end
    end
  end
end

#create_table_from_generator(name, generator, options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sequel/extensions/brut_migrations.rb', line 58

def create_table_from_generator(name, generator, options)
  if name != "schema_migrations" && name != "schema_info"
    if generator.columns.none? { |column| column[:primary_key] }
      generator.primary_key :id
    end
    if generator.columns.none? { |column| column[:name].to_s == "created_at" }
      generator.column :created_at, :timestamptz, null: false
    end
    generator.columns.each do |column|
      if !column.key?(:null)
        column[:null] = false
      end
      if column.key?(:table)
        if !column.key?(:index)
          column[:index] = true
          generator.index(column[:name])
        end
      end
    end
  end
  super
end