Class: DbSchema::Migrator::BodyYielder::AlterTableYielder

Inherits:
Object
  • Object
show all
Defined in:
lib/db_schema/migrator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name) ⇒ AlterTableYielder

Returns a new instance of AlterTableYielder.



61
62
63
64
# File 'lib/db_schema/migrator.rb', line 61

def initialize(table_name)
  @alter_table = Operations::AlterTable.new(table_name)
  @fkey_operations = []
end

Instance Attribute Details

#alter_tableObject (readonly)

Returns the value of attribute alter_table.



59
60
61
# File 'lib/db_schema/migrator.rb', line 59

def alter_table
  @alter_table
end

#fkey_operationsObject (readonly)

Returns the value of attribute fkey_operations.



59
60
61
# File 'lib/db_schema/migrator.rb', line 59

def fkey_operations
  @fkey_operations
end

Instance Method Details

#add_check(name, condition) ⇒ Object



121
122
123
124
125
# File 'lib/db_schema/migrator.rb', line 121

def add_check(name, condition)
  alter_table.changes << Operations::CreateCheckConstraint.new(
    Definitions::CheckConstraint.new(name: name, condition: condition)
  )
end

#add_column(name, type, **options) ⇒ Object



72
73
74
75
76
# File 'lib/db_schema/migrator.rb', line 72

def add_column(name, type, **options)
  alter_table.changes << Operations::CreateColumn.new(
    Definitions::Field.build(name, type, options)
  )
end

#add_foreign_key(*fkey_fields, **fkey_options) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/db_schema/migrator.rb', line 131

def add_foreign_key(*fkey_fields, **fkey_options)
  fkey_operations << Operations::CreateForeignKey.new(
    alter_table.table_name,
    DSL::TableYielder.build_foreign_key(
      fkey_fields,
      table_name: alter_table.table_name,
      **fkey_options
    )
  )
end

#add_index(*columns, **index_options) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/db_schema/migrator.rb', line 107

def add_index(*columns, **index_options)
  alter_table.changes << Operations::CreateIndex.new(
    DSL::TableYielder.build_index(
      columns,
      table_name: alter_table.table_name,
      **index_options
    )
  )
end

#allow_null(name) ⇒ Object



95
96
97
# File 'lib/db_schema/migrator.rb', line 95

def allow_null(name)
  alter_table.changes << Operations::AllowNull.new(name)
end

#alter_column_default(name, new_default) ⇒ Object



103
104
105
# File 'lib/db_schema/migrator.rb', line 103

def alter_column_default(name, new_default)
  alter_table.changes << Operations::AlterColumnDefault.new(name, new_default: new_default)
end

#alter_column_type(name, new_type, using: nil, **new_attributes) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/db_schema/migrator.rb', line 86

def alter_column_type(name, new_type, using: nil, **new_attributes)
  alter_table.changes << Operations::AlterColumnType.new(
    name,
    new_type: new_type,
    using: using,
    **new_attributes
  )
end

#disallow_null(name) ⇒ Object



99
100
101
# File 'lib/db_schema/migrator.rb', line 99

def disallow_null(name)
  alter_table.changes << Operations::DisallowNull.new(name)
end

#drop_check(name) ⇒ Object



127
128
129
# File 'lib/db_schema/migrator.rb', line 127

def drop_check(name)
  alter_table.changes << Operations::DropCheckConstraint.new(name)
end

#drop_column(name) ⇒ Object



78
79
80
# File 'lib/db_schema/migrator.rb', line 78

def drop_column(name)
  alter_table.changes << Operations::DropColumn.new(name)
end

#drop_foreign_key(fkey_name) ⇒ Object



142
143
144
145
146
147
# File 'lib/db_schema/migrator.rb', line 142

def drop_foreign_key(fkey_name)
  fkey_operations << Operations::DropForeignKey.new(
    alter_table.table_name,
    fkey_name
  )
end

#drop_index(name) ⇒ Object



117
118
119
# File 'lib/db_schema/migrator.rb', line 117

def drop_index(name)
  alter_table.changes << Operations::DropIndex.new(name)
end

#rename_column(from, to:) ⇒ Object



82
83
84
# File 'lib/db_schema/migrator.rb', line 82

def rename_column(from, to:)
  alter_table.changes << Operations::RenameColumn.new(old_name: from, new_name: to)
end

#run(block) ⇒ Object



66
67
68
69
70
# File 'lib/db_schema/migrator.rb', line 66

def run(block)
  block.call(self)

  [alter_table, *fkey_operations]
end