Skip to content

Add migration helper to create table with CHECKs

Patrick Bair requested to merge pb-check-constraint-on-table-create into master

What does this MR do?

Related issue: #227610 (closed)

Add a new migration helper create_table_with_constraints that can be used to create CHECK constraints at the time of table creation. This prevents a common pattern of having to disable_ddl_transaction! to use the add_check_constraint or add_text_limit helpers, allowing us to use transactional migrations.

Since the migration is no longer atomic, we have to test whether the table exists:

  disable_ddl_transaction!

  def up
    unless table_exists?(:my_table)
      create_table :my_table do |t|
        t.text :my_column
      end
    end

    add_text_limit :my_table, :my_column, 100
  end

This goes away with the new helper, where constraints/text limit can be defined inline:

  def up
    create_table_with_constraints :some_table do |t|
      t.integer :thing, null: false
      t.text :other_thing
      
      t.check_constraint :thing_is_not_null, 'thing IS NOT NULL'
      t.text_limit :other_thing, 255
    end
  end

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by Patrick Bair

Merge request reports