active_table 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -56,22 +56,33 @@ module ActiveTable
56
56
 
57
57
  unless connection.table_exists?(temp_table_name)
58
58
  db_drop_and_create_table(temp_table_name, @table[:options], @table[:block])
59
+ insert_rows!(@table[:name], :temporary => true)
60
+
61
+ case [tables_match?(@table[:name], temp_table_name), rows_match?(@table[:name], temp_table_name)]
62
+ when [false, false], [false, true]
63
+ db_drop_table(@table[:name])
64
+ connection.rename_table(temp_table_name, @table[:name])
65
+ when [true, false]
66
+ db_truncate_and_copy_tables(@table[:name], temp_table_name)
67
+ when [true, true]
68
+ db_drop_table(temp_table_name)
69
+ end
70
+ end
71
+ end
59
72
 
60
- unless tables_match?(@table[:name], temp_table_name)
61
- db_drop_table(@table[:name])
62
- connection.rename_table(temp_table_name, @table[:name])
63
-
64
- @@insert_statements.reject!{|statement| statement[:table_name] == @table[:name]}
65
- @@table_list << @table[:name] unless @@table_list.include?(@table[:name])
73
+ def insert_rows!(table_name, options = {})
74
+ @@insert_statements.reject!{|statement| statement[:table_name] == @table[:name]}
75
+ @@table_list << @table[:name] unless @@table_list.include?(@table[:name])
66
76
 
67
- @table[:rows].each do |row|
68
- insert_sql = generate_insert_sql_for_hash(connection, @table[:name], row)
69
- connection.execute insert_sql
70
- @@insert_statements << {:table_name => @table[:name], :sql => insert_sql}
71
- end
77
+ @table[:rows].each do |row|
78
+ if options[:temporary]
79
+ insert_sql_now = generate_insert_sql_for_hash(connection, "#{table_name}__temp", row)
80
+ insert_sql_later = generate_insert_sql_for_hash(connection, table_name, row)
72
81
  else
73
- db_drop_table(temp_table_name)
82
+ insert_sql_now = insert_sql_later = generate_insert_sql_for_hash(connection, table_name_name, row)
74
83
  end
84
+ @@insert_statements << {:table_name => @table[:name], :sql => insert_sql_later}
85
+ connection.execute insert_sql_now
75
86
  end
76
87
  end
77
88
 
@@ -82,6 +93,23 @@ module ActiveTable
82
93
  columns_1.to_json == columns_2.to_json
83
94
  end
84
95
 
96
+ def rows_match?(table_1, table_2)
97
+ return false unless connection.table_exists?(table_1) and connection.table_exists?(table_2)
98
+
99
+ query = "SELECT COUNT(*) AS rows FROM (SELECT DISTINCT * FROM ( SELECT * FROM #{connection.quote_table_name(table_1)} UNION ALL SELECT * FROM #{connection.quote_table_name(table_2)} ) t) s"
100
+ distinct_count = connection.execute(query).first["rows"]
101
+
102
+ query = "SELECT COUNT(*) AS rows FROM #{connection.quote_table_name(table_1)}"
103
+ current_count = connection.execute(query).first["rows"]
104
+
105
+ distinct_count == current_count
106
+ end
107
+
108
+ def db_truncate_and_copy_tables(destination, source)
109
+ connection.execute("DELETE FROM #{connection.quote_table_name(destination)}")
110
+ connection.execute("INSERT INTO #{connection.quote_table_name(destination)} SELECT * FROM #{connection.quote_table_name(source)}")
111
+ end
112
+
85
113
  def db_drop_and_create_table(name, options, block)
86
114
  db_drop_table(name)
87
115
  connection.create_table name, options do |t|
@@ -1,3 +1,3 @@
1
1
  module ActiveTable
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -94,6 +94,33 @@ describe "a model created with active_table" do
94
94
  lambda { @connection.execute("SELECT name FROM awesome_models") }.should raise_error
95
95
  lambda { @connection.execute("SELECT different_name FROM awesome_models") }.should_not raise_error
96
96
  end
97
+
98
+ it "should truncate and reload data if the data has changed" do
99
+ class AwesomeModel < ActiveTable::Base
100
+ active_table do
101
+ self.table_name = "awesome_models"
102
+ create_table :awesome_models do |t|
103
+ t.string :name
104
+ end
105
+
106
+ insert :id => 1, :name => "foo"
107
+ insert :id => 2, :name => "bar"
108
+ end
109
+ end
110
+
111
+ class MoreAwesomeModel < ActiveTable::Base
112
+ active_table do
113
+ self.table_name = "awesome_models"
114
+ create_table :awesome_models do |t|
115
+ t.string :name
116
+ end
117
+
118
+ insert :id => 1, :name => "baz"
119
+ end
120
+ end
121
+
122
+ @connection.execute("SELECT * FROM awesome_models").size.should == 1
123
+ end
97
124
  end
98
125
 
99
126
  context "#table_list" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-08-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2165817320 !ruby/object:Gem::Requirement
16
+ requirement: &2161615920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -24,7 +24,7 @@ dependencies:
24
24
  version: 4.0.0
25
25
  type: :runtime
26
26
  prerelease: false
27
- version_requirements: *2165817320
27
+ version_requirements: *2161615920
28
28
  description: Dynamically-populated ActiveRecord models based on static data
29
29
  email:
30
30
  - casecommons-dev@googlegroups.com