Class: DBD4::RailsMigrationFile
- Inherits:
-
Object
- Object
- DBD4::RailsMigrationFile
- Defined in:
- lib/dbd4/rails_migration_file.rb
Defined Under Namespace
Classes: RailsModelFileError
Instance Attribute Summary collapse
-
#tablename ⇒ Object
Returns the value of attribute tablename.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
- #columnsToString(table) ⇒ Object
-
#initialize(options = {}) ⇒ RailsMigrationFile
constructor
A new instance of RailsMigrationFile.
- #to_str ⇒ Object
- #update(table) ⇒ Object
- #update_acts_as_graph_table(relation) ⇒ Object
- #update_implicit_join_table(relation) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RailsMigrationFile
Returns a new instance of RailsMigrationFile.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/dbd4/rails_migration_file.rb', line 34 def initialize( = {}) @warnings = Array.new @infos = Array.new @tablename = [:tablename] @virtual = [:virtual] if [:tablename] if Inflector.singularize(@tablename) == @tablename raise RailsModelFileError, "MigrationModelFile : tablename #{@tablename} is not valid, it must be plural, not singular" end partial_name = "_create_" + Inflector.underscore(Inflector.camelize(@tablename)) + ".rb" fileList = Array.new(Dir.new(File.join('db', 'migrate')).entries).delete_if { |e| e !~ /#{partial_name}/ } if fileList.size == 0 if @virtual == true or $force_migrations == true cmdline = ['migration', "create_" + Inflector.underscore(Inflector.camelize(@tablename)) ] puts "Calling generator: #{cmdline.join(' ')}" Rails::Generator::Scripts::Generate.new.run(cmdline) fileList = Array.new(Dir.new(File.join('db', 'migrate')).entries).delete_if { |e| e !~ /#{partial_name}/ } if fileList.size == 0 raise "Could not find generated migration file starting with #{partial_name} in dir db/migrate" end if fileList.size > 1 raise "Found more than 1 migration file starting with #{partial_name} in dir db/migrate" end else raise "Could not find migration file starting with #{partial_name} in dir db/migrate" end end if fileList.size > 1 raise "Found more than 1 migration file starting with #{partial_name} in dir db/migrate" end @migrationfile = File.join('db', 'migrate', fileList[0]) end end |
Instance Attribute Details
#tablename ⇒ Object
Returns the value of attribute tablename.
29 30 31 |
# File 'lib/dbd4/rails_migration_file.rb', line 29 def tablename @tablename end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
29 30 31 |
# File 'lib/dbd4/rails_migration_file.rb', line 29 def warnings @warnings end |
Instance Method Details
#columnsToString(table) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/dbd4/rails_migration_file.rb', line 69 def columnsToString(table) lines = Array.new table.columns.each_value do |c| next if c.primary_key? d = c.datatype case d.type when 'BOOL' lines << " t.column :#{c.name}, :boolean" when 'TEXT' lines << " t.column :#{c.name}, :text" when 'VARCHAR' if d.params['length'] lines << " t.column :#{c.name}, :string, :length => #{d.params['length']}" else lines << " t.column :#{c.name}, :string" end when 'FLOAT' lines << " t.column :#{c.name}, :float" when 'DATETIME' lines << " t.column :#{c.name}, :datetime" when 'DATE' lines << " t.column :#{c.name}, :date" when 'INTEGER' if d.params['length'] lines << " t.column :#{c.name}, :integer, :length => #{d.params['length']}" else lines << " t.column :#{c.name}, :integer" end when 'DECIMAL' lines << " t.column :#{c.name}, :decimal, :precision => #{d.params['length']}, :scale => #{d.params['decimals']}" when 'BINARY' lines << " t.column :#{c.name}, :binary" when 'BLOB' lines << " t.column :#{c.name}, :binary" else raise "Warning : table #{column.table.name} column #{column.name}, #{d.type} is not a valid type" end end if lines.size == 0 " #t.column :name, :type" else lines.join("\n") end end |
#to_str ⇒ Object
215 216 217 |
# File 'lib/dbd4/rails_migration_file.rb', line 215 def to_str @lines.join("") end |
#update(table) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/dbd4/rails_migration_file.rb', line 114 def update(table) raise "MigrationFile : migration file name not given" if ! @migrationfile c = Inflector.camelize(@tablename) if $save_migration_data = "restore_table_from_fixture(\"#{@tablename}\")" = "save_table_to_fixture(\"#{@tablename}\")" end new_migration_file_content = <<"MIGRATIONFILE" class Create#{c} < ActiveRecord::Migration def self.up create_table :#{@tablename} do |t| #{columnsToString(table)} end #{} end def self.down #{} drop_table :#{@tablename} end end MIGRATIONFILE old_migration_file_content = IO.readlines(@migrationfile).join("") if (old_migration_file_content != new_migration_file_content) puts "Updating migration file for table #{@tablename} (file : #{@migrationfile})..." newFile = File.open(@migrationfile, "w") newFile.puts new_migration_file_content end end |
#update_acts_as_graph_table(relation) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/dbd4/rails_migration_file.rb', line 182 def update_acts_as_graph_table(relation) raise "MigrationFile : migration file name not given" if ! @migrationfile c = Inflector.camelize(@tablename) if $save_migration_data = "restore_table_from_fixture(\"#{@tablename}\")" = "save_table_to_fixture(\"#{@tablename}\")" end new_migration_file_content = <<"MIGRATIONFILE" class Create#{c} < ActiveRecord::Migration def self.up create_table :#{@tablename}, :id => false, :force => true do |t| t.column :parent_id, :integer, :default => 0, :null => false t.column :child_id, :integer, :default => 0, :null => false end #{} end def self.down #{} drop_table :#{@tablename} end end MIGRATIONFILE old_migration_file_content = IO.readlines(@migrationfile).join("") if (old_migration_file_content != new_migration_file_content) puts "Updating migration file for table #{@tablename} (file : #{@migrationfile})..." newFile = File.open(@migrationfile, "w") newFile.puts new_migration_file_content end end |
#update_implicit_join_table(relation) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/dbd4/rails_migration_file.rb', line 147 def update_implicit_join_table(relation) raise "MigrationFile : migration file name not given" if ! @migrationfile c = Inflector.camelize(@tablename) if $save_migration_data = "restore_table_from_fixture(\"#{@tablename}\")" = "save_table_to_fixture(\"#{@tablename}\")" end new_migration_file_content = <<"MIGRATIONFILE" class Create#{c} < ActiveRecord::Migration def self.up create_table :#{@tablename}, :id => false do |t| t.column :#{relation.source_table.modelname}_id, :integer t.column :#{relation.destination_table.modelname}_id, :integer t.column :position, :integer, :default => nil end #{} end def self.down #{} drop_table :#{@tablename} end end MIGRATIONFILE old_migration_file_content = IO.readlines(@migrationfile).join("") if (old_migration_file_content != new_migration_file_content) puts "Updating migration file for table #{@tablename} (file : #{@migrationfile})..." newFile = File.open(@migrationfile, "w") newFile.puts new_migration_file_content end end |