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


132
133
134
135
136
# File 'lib/db_schema/migrator.rb', line 132

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


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

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


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

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

#add_primary_key(*columns) ⇒ Object


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

def add_primary_key(*columns)
  alter_table.changes << Operations::CreateIndex.new(
    DSL::TableYielder.build_index(columns, table_name: alter_table.table_name, primary: true)
  )
end

#allow_null(name) ⇒ Object


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

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

#alter_column_default(name, new_default) ⇒ Object


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

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
94
# 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,
    old_type: nil,
    new_type: new_type,
    using: using,
    **new_attributes
  )
end

#disallow_null(name) ⇒ Object


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

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

#drop_check(name) ⇒ Object


138
139
140
# File 'lib/db_schema/migrator.rb', line 138

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


153
154
155
156
157
158
# File 'lib/db_schema/migrator.rb', line 153

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

#drop_index(name) ⇒ Object


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

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

#drop_primary_keyObject


114
115
116
# File 'lib/db_schema/migrator.rb', line 114

def drop_primary_key
  alter_table.changes << Operations::DropIndex.new(:"#{alter_table.table_name}_pkey", true)
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