Class: MigrationManager::MigrationBuilderController

Inherits:
ApplicationController show all
Defined in:
app/controllers/migration_manager/migration_builder_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/migration_manager/migration_builder_controller.rb', line 7

def create
  operation = params[:operation] # "create_table" or "alter_table"
  table_name = params[:table_name].presence
  columns = params[:columns] || []

  return redirect_to request.referer, alert: "Table name is required" if table_name.blank?

  migration_name = generate_migration_name(operation, table_name, columns)
  timestamp = Time.now.strftime('%Y%m%d%H%M%S')
  formatted_file_name = migration_name.underscore

  file_name = "#{timestamp}_#{formatted_file_name}.rb"
  file_path = Rails.root.join("db/migrate/#{file_name}")

  migration_content = generate_migration_content(migration_name, operation, table_name, columns)

  File.open(file_path, "w") { |file| file.write(migration_content) }

  redirect_to "#{request.base_url}/migration_manager/home", notice: "Migration #{file_name} created successfully!"
end

#newObject



3
4
5
# File 'app/controllers/migration_manager/migration_builder_controller.rb', line 3

def new
  @tables = ActiveRecord::Base.connection.tables.reject { |t| t == 'schema_migrations' }
end