Module: SafeMigrations::MigrationHelper::InstanceMethods

Defined in:
lib/safe_migrations/migration_helper.rb

Instance Method Summary collapse

Instance Method Details

#check_constraint_exists?(table_name, **options) ⇒ Boolean



81
82
83
84
85
86
87
# File 'lib/safe_migrations/migration_helper.rb', line 81

def check_constraint_exists?(table_name, **options)
  if !options.key?(:name) && !options.key?(:expression)
    raise ArgumentError, 'At least one of :name or :expression must be supplied'
  end

  check_constraint_for(table_name, **options).present?
end

#safe_add_check_constraint(table, condition, name:) ⇒ Object



89
90
91
92
93
# File 'lib/safe_migrations/migration_helper.rb', line 89

def safe_add_check_constraint(table, condition, name:, **)
  return unless table_exists?(table)

  check_constraint_exists?(table, name:) || add_check_constraint(table, condition, name:, **)
end

#safe_add_column(table, column, type, **options) ⇒ Object



8
9
10
11
12
13
# File 'lib/safe_migrations/migration_helper.rb', line 8

def safe_add_column(table, column, type, **options)
  return unless table_exists?(table)
  return if column_exists?(table, column)

  add_column(table, column, type, **options)
end

#safe_add_column_and_index(table, column, type, column_options = {}, index_options = {}) ⇒ Object



34
35
36
37
# File 'lib/safe_migrations/migration_helper.rb', line 34

def safe_add_column_and_index(table, column, type, column_options = {}, index_options = {})
  safe_add_column(table, column, type, **column_options)
  safe_add_index(table, column, **index_options)
end

#safe_add_foreign_key(from_table, to_table, **options) ⇒ Object



60
61
62
63
64
65
# File 'lib/safe_migrations/migration_helper.rb', line 60

def safe_add_foreign_key(from_table, to_table, **options)
  return unless table_exists?(from_table) && table_exists?(to_table)
  return if foreign_key_exists?(from_table, to_table, **options)

  add_foreign_key(from_table, to_table, **options)
end

#safe_add_index(table, column, **options) ⇒ Object



23
24
25
26
27
28
# File 'lib/safe_migrations/migration_helper.rb', line 23

def safe_add_index(table, column, **options)
  return unless table_exists?(table)
  return if index_exists?(table, column, **options)

  add_index(table, column, **options)
end

#safe_add_reference(table, ref_name) ⇒ Object



71
72
73
74
75
# File 'lib/safe_migrations/migration_helper.rb', line 71

def safe_add_reference(table, ref_name, **)
  return unless table_exists?(table)

  column_exists?(table, "#{ref_name.to_s.singularize}_id") || add_reference(table, ref_name, **)
end

#safe_change_column(table, column, type, **options) ⇒ Object



44
45
46
# File 'lib/safe_migrations/migration_helper.rb', line 44

def safe_change_column(table, column, type, **options)
  column_exists?(table, column) ? change_column(table, column, type, **options) : add_column(table, column, type, **options)
end

#safe_change_column_default(table, column, default_or_changes) ⇒ Object



99
100
101
102
103
# File 'lib/safe_migrations/migration_helper.rb', line 99

def safe_change_column_default(table, column, default_or_changes)
  return unless table_exists?(table)

  column_exists?(table, column) && change_column_default(table, column, default_or_changes)
end

#safe_change_column_null(table, column, null, default = nil) ⇒ Object



48
49
50
# File 'lib/safe_migrations/migration_helper.rb', line 48

def safe_change_column_null(table, column, null, default = nil)
  column_exists?(table, column) && change_column_null(table, column, null, default)
end

#safe_create_table(table, **options, &block) ⇒ Object



52
53
54
# File 'lib/safe_migrations/migration_helper.rb', line 52

def safe_create_table(table, **options, &block)
  create_table(table, **options, &block) unless table_exists?(table)
end

#safe_drop_table(table) ⇒ Object



56
57
58
# File 'lib/safe_migrations/migration_helper.rb', line 56

def safe_drop_table(table, **)
  drop_table(table, if_exists: true, **) if table_exists?(table)
end

#safe_remove_check_constraint(table, condition, name:) ⇒ Object



95
96
97
# File 'lib/safe_migrations/migration_helper.rb', line 95

def safe_remove_check_constraint(table, condition, name:, **)
  table_exists?(table) && check_constraint_exists?(table, name:) && remove_check_constraint(table, condition, name:, **)
end

#safe_remove_column(table, column, type = nil, **options) ⇒ Object



15
16
17
# File 'lib/safe_migrations/migration_helper.rb', line 15

def safe_remove_column(table, column, type = nil, **options)
  table_exists?(table) && column_exists?(table, column) && remove_column(table, column, type, **options)
end

#safe_remove_column_and_index(table, column, column_options = {}, index_options = {}) ⇒ Object



39
40
41
42
# File 'lib/safe_migrations/migration_helper.rb', line 39

def safe_remove_column_and_index(table, column, column_options = {}, index_options = {})
  safe_remove_index(table, column, **index_options)
  safe_remove_column(table, column, **column_options)
end

#safe_remove_foreign_key(from_table, to_table, **options) ⇒ Object



67
68
69
# File 'lib/safe_migrations/migration_helper.rb', line 67

def safe_remove_foreign_key(from_table, to_table, **options)
  table_exists?(from_table) && table_exists?(to_table) && foreign_key_exists?(from_table, to_table, **options) && remove_foreign_key(from_table, to_table, **options)
end

#safe_remove_index(table, column_name = nil, **options) ⇒ Object



30
31
32
# File 'lib/safe_migrations/migration_helper.rb', line 30

def safe_remove_index(table, column_name = nil, **options)
  index_exists?(table, column_name, **options) && remove_index(table, column_name, **options)
end

#safe_remove_reference(table, ref_name) ⇒ Object



77
78
79
# File 'lib/safe_migrations/migration_helper.rb', line 77

def safe_remove_reference(table, ref_name, **)
  table_exists?(table) && column_exists?(table, "#{ref_name.to_s.singularize}_id") && remove_reference(table, ref_name, **)
end

#safe_rename_column(table_name, column_name, new_column_name) ⇒ Object



19
20
21
# File 'lib/safe_migrations/migration_helper.rb', line 19

def safe_rename_column(table_name, column_name, new_column_name)
  column_exists?(table_name, column_name) && !column_exists?(table_name, new_column_name) && rename_column(table_name, column_name, new_column_name)
end