Class: Upgrade030HasDynamicColumns

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/generators/has_dynamic_columns/templates/migration_0.3.0.rb

Class Method Summary collapse

Class Method Details

.downObject



87
88
# File 'lib/generators/has_dynamic_columns/templates/migration_0.3.0.rb', line 87

def self.down
end

.upObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/generators/has_dynamic_columns/templates/migration_0.3.0.rb', line 2

def self.up
	add_column :dynamic_column_data, :datum_type, :string
	add_column :dynamic_column_data, :datum_id, :integer

	create_table :dynamic_column_boolean_data do |t|
		t.boolean :value
		t.timestamps null: false
	end
	add_index(:dynamic_column_boolean_data, [:value])

	create_table :dynamic_column_date_data do |t|
		t.date :value
		t.timestamps null: false
	end
	add_index(:dynamic_column_date_data, [:value])

	create_table :dynamic_column_datetime_data do |t|
		t.datetime :value
		t.timestamps null: false
	end
	add_index(:dynamic_column_datetime_data, [:value])

	create_table :dynamic_column_enum_data do |t|
		t.string :value
		t.timestamps null: false
	end
	add_index(:dynamic_column_enum_data, [:value])

	create_table :dynamic_column_float_data do |t|
		t.float :value
		t.timestamps null: false
	end
	add_index(:dynamic_column_float_data, [:value])

	create_table :dynamic_column_integer_data do |t|
		t.integer :value
		t.timestamps null: false
	end
	add_index(:dynamic_column_integer_data, [:value])

	create_table :dynamic_column_string_data do |t|
		t.string :value
		t.timestamps null: false
	end
	add_index(:dynamic_column_string_data, [:value])

	create_table :dynamic_column_text_data do |t|
		t.text :value
		t.timestamps null: false
	end
	add_index(:dynamic_column_text_data, [:value], :length => 255)

	create_table :dynamic_column_time_data do |t|
		t.time :value
		t.timestamps null: false
	end
	add_index(:dynamic_column_time_data, [:value])

	create_table :dynamic_column_timestamp_data do |t|
		t.timestamp :value
		t.timestamps null: false
	end
	add_index(:dynamic_column_timestamp_data, [:value])

	# Migrate data
	["string","integer","boolean","text"].each { |data_type|
		ActiveRecord::Base.connection.select_all("
		SELECT
			`dynamic_column_data`.*
		FROM `dynamic_columns`
			INNER JOIN `dynamic_column_data`
				ON `dynamic_column_data`.`dynamic_column_id` = `dynamic_columns`.`id`
		WHERE `dynamic_columns`.`data_type` = '#{data_type}'
		").each { |i|
			obj = "::HasDynamicColumns::DynamicColumn#{data_type.camelize}Datum".constantize.create(:value => i["value"])
			if datum = ::HasDynamicColumns::DynamicColumnDatum.where(:id => i["id"]).first
				datum.datum = obj
				datum.save
			end
		}
	}

	remove_column :dynamic_column_data, :value
end