rails_parallel 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -24,9 +24,15 @@ You'll want to add a lib/tasks/rails_parallel.rake with at least the following:
24
24
  RailsParallel::Rake.launch
25
25
  end
26
26
 
27
- # RailsParallel runs this if it needs to reload the DB.
28
27
  namespace :db do
28
+ # RailsParallel runs this if it needs to reload the DB.
29
29
  task :setup => ['db:drop', 'db:create', 'db:schema:load']
30
+
31
+ # RailsParallel normally doesn't mess with your current DB,
32
+ # only the 'test' env DB. Run this to load it if required.
33
+ task :load => :environment do
34
+ RailsParallel::Rake.load_current_db
35
+ end
30
36
  end
31
37
  end
32
38
 
@@ -2,6 +2,7 @@ require 'rake/testtask'
2
2
  require 'fcntl'
3
3
 
4
4
  require 'rails_parallel/object_socket'
5
+ require 'rails_parallel/schema'
5
6
 
6
7
  module RailsParallel
7
8
  class Rake
@@ -18,6 +19,10 @@ module RailsParallel
18
19
  instance.run(name, ruby_opts, files)
19
20
  end
20
21
 
22
+ def self.load_current_db
23
+ instance.load_schema
24
+ end
25
+
21
26
  def launch
22
27
  return if @pid
23
28
  at_exit { shutdown }
@@ -63,6 +68,11 @@ module RailsParallel
63
68
  end
64
69
  end
65
70
 
71
+ def load_schema
72
+ Schema.new(schema_file).load_main_db
73
+ puts "RP: Loaded #{Rails.env} schema."
74
+ end
75
+
66
76
  private
67
77
 
68
78
  def expect(want)
@@ -1,8 +1,8 @@
1
1
  require 'rails_parallel/forks'
2
2
  require 'rails_parallel/collector'
3
3
  require 'rails_parallel/timings'
4
+ require 'rails_parallel/schema'
4
5
  require 'rails_parallel/runner/child'
5
- require 'rails_parallel/runner/schema'
6
6
  require 'rails_parallel/runner/test_runner'
7
7
 
8
8
  class Test::Unit::TestResult
@@ -0,0 +1,90 @@
1
+ require 'rails_parallel/forks'
2
+
3
+ module RailsParallel
4
+ class Schema
5
+ include Forks
6
+
7
+ def initialize(file)
8
+ @file = file
9
+ end
10
+
11
+ def load_main_db
12
+ if load_db(1)
13
+ failed = 0
14
+ ObjectSpace.each_object(Class) do |klass|
15
+ next unless klass < ActiveRecord::Base
16
+
17
+ klass.reset_column_information
18
+ begin
19
+ klass.columns
20
+ rescue StandardError => e
21
+ failed += 1
22
+ raise e if failed > 3
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def load_db(number)
29
+ update_db_config(number)
30
+ if schema_loaded?
31
+ reconnect
32
+ false
33
+ else
34
+ schema_load
35
+ true
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def reconnect(override = {})
42
+ ActiveRecord::Base.establish_connection(@dbconfig.merge(override))
43
+ ActiveRecord::Base.connection
44
+ end
45
+
46
+ def update_db_config(number)
47
+ config = ActiveRecord::Base.configurations[Rails.env]
48
+ config['database'] += "_#{number}" unless number == 1
49
+ @dbconfig = config.with_indifferent_access
50
+ end
51
+
52
+ def schema_load
53
+ dbname = @dbconfig[:database]
54
+ mysql_args = ['-u', 'root']
55
+
56
+ connection = reconnect(:database => nil)
57
+ connection.execute("DROP DATABASE IF EXISTS #{dbname}")
58
+ connection.execute("CREATE DATABASE #{dbname}")
59
+
60
+ File.open(@file) do |fh|
61
+ pid = fork do
62
+ STDIN.reopen(fh)
63
+ exec(*['mysql', mysql_args, dbname].flatten)
64
+ end
65
+ wait_for(pid)
66
+ end
67
+
68
+ reconnect
69
+ sm_table = ActiveRecord::Migrator.schema_migrations_table_name
70
+ ActiveRecord::Base.connection.execute("INSERT INTO #{sm_table} (version) VALUES ('#{@file}')")
71
+ end
72
+
73
+ def schema_loaded?
74
+ begin
75
+ ActiveRecord::Base.establish_connection(@dbconfig)
76
+ ActiveRecord::Base.connection
77
+ rescue StandardError
78
+ return false
79
+ end
80
+
81
+ begin
82
+ sm_table = ActiveRecord::Migrator.schema_migrations_table_name
83
+ migrated = ActiveRecord::Base.connection.select_values("SELECT version FROM #{sm_table}")
84
+ migrated.include?(@file)
85
+ rescue ActiveRecord::StatementInvalid
86
+ false
87
+ end
88
+ end
89
+ end
90
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsParallel
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_parallel
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adrian Irving-Beer
@@ -88,8 +88,8 @@ files:
88
88
  - lib/rails_parallel/runner.rb
89
89
  - lib/rails_parallel/runner/child.rb
90
90
  - lib/rails_parallel/runner/parent.rb
91
- - lib/rails_parallel/runner/schema.rb
92
91
  - lib/rails_parallel/runner/test_runner.rb
92
+ - lib/rails_parallel/schema.rb
93
93
  - lib/rails_parallel/timings.rb
94
94
  - lib/rails_parallel/version.rb
95
95
  - rails_parallel.gemspec
@@ -1,93 +0,0 @@
1
- require 'rails_parallel/object_socket'
2
- require 'rails_parallel/runner/test_runner'
3
-
4
- module RailsParallel
5
- class Runner
6
- class Schema
7
- include Forks
8
-
9
- def initialize(file)
10
- @file = file
11
- end
12
-
13
- def load_main_db
14
- if load_db(1)
15
- failed = 0
16
- ObjectSpace.each_object(Class) do |klass|
17
- next unless klass < ActiveRecord::Base
18
-
19
- klass.reset_column_information
20
- begin
21
- klass.columns
22
- rescue StandardError => e
23
- failed += 1
24
- raise e if failed > 3
25
- end
26
- end
27
- end
28
- end
29
-
30
- def load_db(number)
31
- update_db_config(number)
32
- if schema_loaded?
33
- reconnect
34
- false
35
- else
36
- schema_load
37
- true
38
- end
39
- end
40
-
41
- private
42
-
43
- def reconnect(override = {})
44
- ActiveRecord::Base.establish_connection(@dbconfig.merge(override))
45
- ActiveRecord::Base.connection
46
- end
47
-
48
- def update_db_config(number)
49
- config = ActiveRecord::Base.configurations[Rails.env]
50
- config['database'] += "_#{number}" unless number == 1
51
- @dbconfig = config.with_indifferent_access
52
- end
53
-
54
- def schema_load
55
- dbname = @dbconfig[:database]
56
- mysql_args = ['-u', 'root']
57
-
58
- connection = reconnect(:database => nil)
59
- connection.execute("DROP DATABASE IF EXISTS #{dbname}")
60
- connection.execute("CREATE DATABASE #{dbname}")
61
-
62
- File.open(@file) do |fh|
63
- pid = fork do
64
- STDIN.reopen(fh)
65
- exec(*['mysql', mysql_args, dbname].flatten)
66
- end
67
- wait_for(pid)
68
- end
69
-
70
- reconnect
71
- sm_table = ActiveRecord::Migrator.schema_migrations_table_name
72
- ActiveRecord::Base.connection.execute("INSERT INTO #{sm_table} (version) VALUES ('#{@file}')")
73
- end
74
-
75
- def schema_loaded?
76
- begin
77
- ActiveRecord::Base.establish_connection(@dbconfig)
78
- ActiveRecord::Base.connection
79
- rescue StandardError
80
- return false
81
- end
82
-
83
- begin
84
- sm_table = ActiveRecord::Migrator.schema_migrations_table_name
85
- migrated = ActiveRecord::Base.connection.select_values("SELECT version FROM #{sm_table}")
86
- migrated.include?(@file)
87
- rescue ActiveRecord::StatementInvalid
88
- false
89
- end
90
- end
91
- end
92
- end
93
- end