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 typeint
will be created. - If no
created_at
is specified, a column namecreated_at
of typetimestamptz
is created. create_table
requires acomment:
attribute that explains the purpose of the table.create_table
accepts anexternal_id: true
attribute that will create a uniquecitext
field namedexternal_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 tokey
.
Instance Method Summary collapse
-
#add_column(table, *args) ⇒ Object
Overrides Sequel's
add_column
to defaultnull: false
. -
#add_key(fields) ⇒ Object
Specifies a non-primary key based on the fields given.
-
#create_table(*args) ⇒ Object
Overrides Sequel's
create_table
. -
#create_table_from_generator(name, generator, options) ⇒ Object
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) = args.last if .is_a?(Hash) if !.key?(:null) [: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
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
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, ) 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 |