ruby on rails - रेल माइग्रेशन पर रुबी में डेटाबेस कॉलम का नाम कैसे बदल सकता हूं?
ruby-on-rails ruby-on-rails-3 (16)
मैंने गलत तरीके से hased_password
बजाय कॉलम hased_password
नाम दिया है।
इस कॉलम का नाम बदलने के लिए माइग्रेशन का उपयोग करके मैं डेटाबेस स्कीमा कैसे अपडेट करूं?
आईएमओ, इस मामले में, बेहतर उपयोग rake db:rollback
। फिर अपने माइग्रेशन को संपादित करें और फिर rake db:migrate
टाइप करें rake db:migrate
। हालांकि, अगर आपके पास उस कॉलम में डेटा है जिसे आप खोना नहीं चाहते हैं, तो rename_column
उपयोग rename_column
।
आदेश का उपयोग कर माइग्रेशन उत्पन्न करें
rails g migration rename_hased_password
उसके बाद माइग्रेशन परिवर्तन विधि में निम्न पंक्ति जोड़ें
rename_column :table, :hased_password, :hashed_password
यह काम कर जाना चाहिए।
ऐसा करने के आपके पास दो तरीके हैं:
रोलबैक होने पर इस प्रकार में यह स्वचालित रूप से इसके विपरीत कोड चलाता है।
def change rename_column :table_name, :old_column_name, :new_column_name end
इस प्रकार के लिए, यह
rake db:migrate
दौरान अप विधि को चलाता हैrake db:migrate
और डाउन विधिrake db:rollback
जबrake db:rollback
:def self.up rename_column :table_name, :old_column_name, :new_column_name end def self.down rename_column :table_name,:new_column_name,:old_column_name end
बस एक नया माइग्रेशन बनाएं, और एक ब्लॉक में, rename_column
उपयोग नीचे के रूप में करें।
rename_column :your_table_name, :hased_password, :hashed_password
मैन्युअल रूप से हम नीचे दी गई विधि का उपयोग कर सकते हैं:
हम माइग्रेशन को मैन्युअल रूप से संपादित कर सकते हैं जैसे:
ओपन
app/db/migrate/xxxxxxxxx_migration_file.rb
hased_password
कोhashed_password
अपडेट करेंनीचे आदेश चलाएं
$> rake db:migrate:down VERSION=xxxxxxxxx
फिर यह आपके प्रवास को हटा देगा:
$> rake db:migrate:up VERSION=xxxxxxxxx
यह आपके प्रवासन को अद्यतन परिवर्तन के साथ जोड़ देगा।
यदि आपका कोड किसी अन्य के साथ साझा नहीं किया गया है, तो सबसे अच्छा विकल्प केवल rake db:rollback
फिर माइग्रेशन और rake db:migrate
में अपना कॉलम नाम संपादित करें rake db:migrate
। बस
और आप कॉलम का नाम बदलने के लिए एक और माइग्रेशन लिख सकते हैं
def change
rename_column :table_name, :old_name, :new_name
end
बस।
यदि कॉलम पहले से ही डेटा के साथ आबादी में है और उत्पादन में रहता है, तो मैं चरण-दर-चरण दृष्टिकोण की अनुशंसा करता हूं, ताकि माइग्रेशन की प्रतीक्षा करते समय उत्पादन में डाउनटाइम से बचें।
सबसे पहले मैं नए नामों के साथ कॉलम जोड़ने के लिए एक डीबी माइग्रेशन तैयार करता हूं और उन्हें पुराने कॉलम नाम से मानों के साथ पॉप्युलेट करता हूं।
class AddCorrectColumnNames < ActiveRecord::Migration
def up
add_column :table, :correct_name_column_one, :string
add_column :table, :correct_name_column_two, :string
puts 'Updating correctly named columns'
execute "UPDATE table_name SET correct_name_column_one = old_name_column_one, correct_name_column_two = old_name_column_two"
end
end
def down
remove_column :table, :correct_name_column_one
remove_column :table, :correct_name_column_two
end
end
तो मैं बस उस बदलाव को प्रतिबद्ध करता हूं, और परिवर्तन को उत्पादन में डाल देता हूं।
git commit -m 'adding columns with correct name'
फिर एक बार प्रतिबद्धता को उत्पादन में धकेल दिया गया है, तो मैं दौड़ूंगा।
Production $ bundle exec rake db:migrate
फिर मैं उन सभी विचारों / नियंत्रकों को अपडेट करूंगा जो पुराने कॉलम नाम को नए कॉलम नाम पर संदर्भित करते हैं। मेरे परीक्षण सूट के माध्यम से भागो, और केवल उन परिवर्तनों को प्रतिबद्ध करें। (यह सुनिश्चित करने के बाद कि यह स्थानीय रूप से काम कर रहा था और पहले सभी परीक्षणों को पार कर रहा था!)
git commit -m 'using correct column name instead of old stinky bad column name'
तब मैं उस प्रतिबद्धता को उत्पादन में धक्का दूंगा।
इस बिंदु पर आप माइग्रेशन से जुड़े किसी भी प्रकार के डाउनटाइम के बारे में चिंता किए बिना मूल कॉलम को हटा सकते हैं।
class RemoveBadColumnNames < ActiveRecord::Migration
def up
remove_column :table, :old_name_column_one
remove_column :table, :old_name_column_two
end
def down
add_column :table, :old_name_column_one, :string
add_column :table, :old_name_column_two, :string
end
end
फिर उत्पादन के लिए इस नवीनतम माइग्रेशन को दबाएं और bundle exec rake db:migrate
चलाएं bundle exec rake db:migrate
पृष्ठभूमि में bundle exec rake db:migrate
करें।
मुझे एहसास है कि यह एक प्रक्रिया में थोड़ा अधिक शामिल है, लेकिन मैं अपने उत्पादन प्रवासन के साथ मुद्दों के बजाय ऐसा करना चाहता हूं।
यदि वर्तमान डेटा आपके लिए महत्वपूर्ण नहीं है, तो आप इसका उपयोग करके अपना मूल माइग्रेशन ले सकते हैं:
rake db:migrate:down VERSION='YOUR MIGRATION FILE VERSION HERE'
उद्धरण के बिना, फिर मूल माइग्रेशन में परिवर्तन करें और फिर से माइग्रेशन चलाएं:
rake db:migrate
रेल 5 प्रवास परिवर्तन
उदाहरण के लिए:
रेल जी मॉडल छात्र student_name: स्ट्रिंग आयु: पूर्णांक
यदि आप छात्र_नाम कॉलम को नाम के रूप में बदलना चाहते हैं
नोट: - यदि आप रेल डीबी नहीं चलाते हैं : माइग्रेट करें
आप निम्नलिखित कदम उठा सकते हैं
रेल डी मॉडल छात्र student_name: स्ट्रिंग आयु: पूर्णांक
यह जेनरेट माइग्रेशन फ़ाइल को हटा देगा, अब आप अपना कॉलम नाम सही कर सकते हैं
रेल जी मॉडल छात्र का नाम: स्ट्रिंग आयु: पूर्णांक
यदि आप कॉलम नाम बदलने के लिए विकल्पों के बाद माइग्रेट (रेल डीबी: माइग्रेट) माइग्रेट करते हैं
रेल जी माइग्रेशन RemoveStudentNameFromStudent student_name: स्ट्रिंग
रेल जी माइग्रेशन AddNameToStudent नाम: स्ट्रिंग
रेल कंसोल पर अपनी रूबी खोलें और दर्ज करें:
ActiveRecord::Migration.rename_column :tablename, :old_column, :new_column
रेल पर रूबी के लिए 4:
def change
rename_column :table_name, :column_name_old, :column_name_new
end
वैकल्पिक विकल्प के रूप में, यदि आप माइग्रेशन के विचार से विवाहित नहीं हैं, तो ActiveRecord के लिए एक आकर्षक मणि है जो स्वचालित रूप से आपके लिए नाम बदलता है, डेटामैपर शैली। आप जो भी करते हैं वह आपके मॉडल में कॉलम नाम बदलता है (और सुनिश्चित करें कि आपने मॉडल. auto_upgrade को अपने मॉडल.आरबी के नीचे रखा है) और व्हायोला! फ्लाई पर डाटाबेस अपडेट किया गया है।
https://github.com/DAddYE/mini_record
नोट: विवादों को रोकने के लिए आपको डीबी / schema.rb को नूक करने की आवश्यकता होगी
अभी भी बीटा चरणों में और स्पष्ट रूप से सभी के लिए नहीं, लेकिन अभी भी एक आकर्षक विकल्प है (मैं वर्तमान में इसे बिना किसी समस्या के दो गैर-तुच्छ उत्पादन अनुप्रयोगों में उपयोग कर रहा हूं)
अद्यतन - create_table का एक करीबी चचेरा भाई change_table है, जो मौजूदा तालिकाओं को बदलने के लिए उपयोग किया जाता है। इसका उपयोग create_table के समान रूप में किया जाता है लेकिन ब्लॉक पर उत्पन्न वस्तु अधिक चाल जानता है। उदाहरण के लिए:
class ChangeBadColumnNames < ActiveRecord::Migration
def change
change_table :your_table_name do |t|
t.rename :old_column_name, :new_column_name
end
end
end
यदि हम अन्य परिवर्तन विधियों के साथ करते हैं तो इस तरह से अधिक कुशल है: इंडेक्स को हटाएं / इंडेक्स / इंडेक्स / कॉलम जोड़ें, उदाहरण के लिए हम आगे की तरह कर सकते हैं:
# Rename
t.rename :old_column_name, :new_column_name
# Add column
t.string :new_column
# Remove column
t.remove :removing_column
# Index column
t.index :indexing_column
#...
माइग्रेशन फ़ाइल जेनरेट करें:
rails g migration FixName
# डीबी / माइग्रेट / xxxxxxxxxx.rb बनाता है
अपनी इच्छानुसार माइग्रेशन संपादित करें।
class FixName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
def change
rename_column :table_name, :old_column_name, :new_column_name
end
$: rails g migration RenameHashedPasswordColumn
invoke active_record
create db/migrate/20160323054656_rename_hashed_password_column.rb
उस माइग्रेशन फ़ाइल को खोलें और नीचे दी गई फ़ाइल को संशोधित करें (अपना मूल table_name
दर्ज करें)
class RenameHashedPasswordColumn < ActiveRecord::Migration
def change
rename_column :table_name, :hased_password, :hashed_password
end
end