Files

Class Index [+]

Quicksearch

Ubiquo::Tasks::Database

Public Instance Methods

fix_sequence_consistency(tables) click to toggle source

If any of these “tables” has a sequence field, make sure that the next value that will be returned does not conflict with the imported fixtures

     # File vendor/plugins/ubiquo_core/lib/ubiquo/tasks/database.rb, line 155
155:       def fix_sequence_consistency(tables)
156:         (tables || []).each do |table_name|
157:           ActiveRecord::Base.connection.list_sequences(table_name.to_s + "_$").each do |sequence|
158:             ActiveRecord::Base.connection.reset_sequence_value(sequence)
159:           end
160:         end
161:       end
fixture_path(fixture_name=nil) click to toggle source

(Not documented)

    # File vendor/plugins/ubiquo_core/lib/ubiquo/tasks/database.rb, line 5
 5:       def fixture_path(fixture_name=nil)
 6:         if ENV["FIXTURES_DIR"]
 7:           pathdir = File.dirname(ENV["FIXTURES_DIR"])
 8:         else
 9:           pathdir= case ENV["RAILS_ENV"]
10:                    when "test"
11:                      Rails.root.join('test','fixtures')
12:                    when "production"
13:                      Rails.root.join('db','bootstrap')
14:                    when "preproduction"
15:                      Rails.root.join('db','pre_bootstrap')
16:                    else
17:                      Rails.root.join('db','dev_bootstrap')
18:                    end
19:         end
20:         path = pathdir.to_s
21:         path += "/#{fixture_name}.yml" if fixture_name
22:         if File.exists?(pathdir)
23:           raise StandardError.new("Path %s exists and is a file." % pathdir) if File.file?(pathdir)
24:         else
25:           p "Creating dir %s" % pathdir
26:           FileUtils.mkdir_p(pathdir)
27:         end
28:         path
29:       end
import_model_fixture(model) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_core/lib/ubiquo/tasks/database.rb, line 87
 87:       def import_model_fixture(model)
 88:         filename = fixture_path(model.tableize)
 89:         success = Hash.new
 90:         success[model.to_sym] ||= 0
 91:         records = YAML::load( File.open(filename))
 92:         @model = Class.const_get(model)
 93:         @model.transaction do
 94:           records.sort.each do |r|
 95:             row = r[1]
 96:             @new_model = @model.new
 97: 
 98:             row.each_pair do |column, value|
 99:               if column.to_sym
100:                 @new_model.send(column + '=', value)
101:               else
102:                 p "Column not found" + column.to_s
103:               end
104:             end
105: 
106:             begin
107:               if @new_model.save
108:                 success[model.to_sym] = (success[model.to_sym] ? success[model.to_sym] + 1 : 1)
109:               end
110:             rescue
111:               p "#{@new_model.class.to_s} failed to import: " + r.inspect
112:               p @new_model.errors.inspect
113:             end
114:           end
115: 
116:           p "Total of #{success[model.to_sym]} #{@new_model.class.to_s} records imported successfully"
117:         end
118:         fix_sequence_consistency [model.tableize]
119:       end
import_table_fixture(table) click to toggle source

(Not documented)

    # File vendor/plugins/ubiquo_core/lib/ubiquo/tasks/database.rb, line 53
53:       def import_table_fixture(table)
54:         filename = fixture_path(table)
55:         success = Hash.new
56:         success[table.to_sym] ||= 0
57:         records = YAML::load( File.open(filename))
58: 
59:         records.sort.each do |r|
60:           row = r[1]
61:           columns = []
62:           values = []
63: 
64:           row.each_pair do |column, value|
65:             if column.to_sym
66:               columns << ActiveRecord::Base.connection.quote_column_name(column)
67:               values << ActiveRecord::Base.connection.quote(value)
68:             else
69:               p "Column not found" + column.to_s
70:             end
71:           end
72: 
73:           insert_sql = "INSERT INTO #{table} (" + columns.join(', ') + ") VALUES (" + values.join(', ') + ")"
74: 
75:           begin
76:             if ActiveRecord::Base.connection.execute(insert_sql)
77:               success[table.to_sym] = (success[table.to_sym] ? success[table.to_sym] + 1 : 1)
78:             end
79:           rescue
80:             p "#{table} failed to import: " + insert_sql
81:           end
82:         end
83: 
84:         p "Total of #{success[table.to_sym]} #{table} records imported successfully"
85:       end
join_table_names(table_names, model_names, group_names) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_core/lib/ubiquo/tasks/database.rb, line 121
121:       def join_table_names(table_names, model_names, group_names)
122:         tables = table_names.to_s.split(',')
123:         model_names.to_s.split(',').each do |model|
124:           tables << model.tableize
125:         end
126:         process_groups(group_names) do |table|
127:           tables << table
128:         end
129:         tables.uniq
130:       end
ordered_yaml(data) click to toggle source

This is like Hash.to_yaml except that it sorts by key before converting

     # File vendor/plugins/ubiquo_core/lib/ubiquo/tasks/database.rb, line 133
133:       def ordered_yaml(data)
134:         YAML::quick_emit( data.object_id, {} ) do |out|
135:           out.map( data.taguri, data.to_yaml_style ) do |map|
136:             data.sort.each do |k, v|
137:               map.add( k, v )
138:             end
139:           end
140:         end
141:       end
process_groups(group_list) {|table| ...} click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_core/lib/ubiquo/tasks/database.rb, line 143
143:       def process_groups(group_list)
144:         group_list.to_s.split(',').each do |group|
145:           tables = Ubiquo::Config.get(:model_groups)[group.to_sym]
146:           tables.each do |table|
147:             yield table
148:           end if tables
149:           fix_sequence_consistency tables
150:         end
151:       end
write_all_yaml_fixtures_to_file() click to toggle source

(Not documented)

    # File vendor/plugins/ubiquo_core/lib/ubiquo/tasks/database.rb, line 46
46:       def write_all_yaml_fixtures_to_file
47:         skip_tables = ["schema_info", "schema_migrations"]
48:         (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
49:           write_yaml_fixtures_to_file(table_name)
50:         end
51:       end
write_yaml_fixtures_to_file(table_name) click to toggle source

(Not documented)

    # File vendor/plugins/ubiquo_core/lib/ubiquo/tasks/database.rb, line 31
31:       def write_yaml_fixtures_to_file(table_name)
32:         # fixture id generator
33:         i = '0'
34:         fixture_id = lambda { |fixture| fixture['id'] || i.succ! }
35: 
36:         # generate and write the data
37:         File.open(fixture_path(table_name), 'w' ) do |file|
38:           data = ActiveRecord::Base.connection.select_all("SELECT * FROM #{table_name}")
39:           file.write ordered_yaml(data.inject({}) { |hash, record|
40:             hash["#{table_name}_#{"%0.3i" % fixture_id.call(record)}"] = record
41:             hash
42:           })
43:         end
44:       end

Disabled; run with $DEBUG to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.