storey 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ class Storey::Dumper
2
+ def self.dump(options={})
3
+ case Rails.configuration.active_record.schema_format
4
+ when :sql ; self.dump_structure_sql(options)
5
+ when :ruby ; self.dump_schema_rb(options)
6
+ end
7
+ end
8
+
9
+ def self.dump_schema_rb(options={})
10
+ require 'active_record/schema_dumper'
11
+ filename = options[:file] || "#{Rails.root}/db/schema.rb"
12
+ File.open(filename, "w:utf-8") do |file|
13
+ ActiveRecord::Base.establish_connection(Rails.env)
14
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
15
+ end
16
+ end
17
+
18
+ def self.dump_structure_sql(options={})
19
+ abcs = ActiveRecord::Base.configurations
20
+ filename = options[:file] || File.join(Rails.root, "db", "structure.sql")
21
+ set_psql_env(abcs[Rails.env])
22
+ search_path = abcs[Rails.env]['schema_search_path']
23
+ unless search_path.blank?
24
+ search_path = search_path.split(",").map{|search_path_part| "--schema=#{Shellwords.escape(search_path_part.strip)}" }.join(" ")
25
+ end
26
+ `pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(abcs[Rails.env]['database'])}`
27
+ raise 'Error dumping database' if $?.exitstatus == 1
28
+ File.open(filename, "a") { |f| f << "SET search_path TO #{ActiveRecord::Base.connection.schema_search_path};\n\n" }
29
+ end
30
+
31
+ private
32
+
33
+ def self.set_psql_env(config)
34
+ ENV['PGHOST'] = config['host'] if config['host']
35
+ ENV['PGPORT'] = config['port'].to_s if config['port']
36
+ ENV['PGPASSWORD'] = config['password'].to_s if config['password']
37
+ ENV['PGUSER'] = config['username'].to_s if config['username']
38
+ end
39
+ end
@@ -1,6 +1,14 @@
1
1
  module Storey::Migrator
2
2
  extend self
3
3
 
4
+ def migrate_all
5
+ self.migrate 'public'
6
+ Storey::Dumper.dump
7
+ Storey.schemas(public: false).each do |schema|
8
+ self.migrate schema
9
+ end
10
+ end
11
+
4
12
  def migrate(schema)
5
13
  Storey.switch schema do
6
14
  ActiveRecord::Migrator.migrate ActiveRecord::Migrator.migrations_path
@@ -1,3 +1,3 @@
1
1
  module Storey
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
data/lib/storey.rb CHANGED
@@ -6,6 +6,7 @@ require 'storey/exceptions'
6
6
  require 'storey/migrator'
7
7
  require 'storey/duplicator'
8
8
  require 'storey/hstore'
9
+ require 'storey/dumper'
9
10
 
10
11
  module Storey
11
12
  extend self
@@ -8,10 +8,8 @@ namespace :storey do
8
8
  end
9
9
 
10
10
  desc "Migrate all schemas including public"
11
- task :migrate => "db:migrate" do
12
- Storey.schemas.each do |schema|
13
- Storey::Migrator.migrate schema
14
- end
11
+ task migrate: :environment do
12
+ Storey::Migrator.migrate_all
15
13
  end
16
14
 
17
15
  desc "Rolls the schema back to the previous version (specify steps w/ STEP=n) across all schemas."
@@ -7,16 +7,31 @@ describe Storey::Migrator do
7
7
  Storey.create @schema_1
8
8
  end
9
9
 
10
- describe "#migrate" do
11
- it "should connect to new db, then reset when done" do
12
- ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(@schema_1).once
13
- ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(@original_schema).once
14
- Storey::Migrator.migrate(@schema_1)
10
+ describe '.migrate_all' do
11
+ it 'should migrate the default search path first, then all available schemas' do
12
+ described_class.should_receive(:migrate).with('public').ordered
13
+ described_class.should_receive(:migrate).with(@schema_1).ordered
14
+ described_class.migrate_all
15
15
  end
16
16
 
17
- it "should migrate db" do
18
- ActiveRecord::Migrator.should_receive(:migrate)
19
- Storey::Migrator.migrate(@schema_1)
17
+ it 'should dump the database' do
18
+ Storey::Dumper.should_receive(:dump)
19
+ described_class.migrate_all
20
+ end
21
+ end
22
+
23
+ describe '.migrate' do
24
+ context 'given a schema' do
25
+ it 'should connect to new db, then reset when done' do
26
+ ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(@schema_1).once
27
+ ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(@original_schema).once
28
+ Storey::Migrator.migrate(@schema_1)
29
+ end
30
+
31
+ it "should migrate db" do
32
+ ActiveRecord::Migrator.should_receive(:migrate)
33
+ Storey::Migrator.migrate(@schema_1)
34
+ end
20
35
  end
21
36
  end
22
37
 
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Storey::Dumper do
4
+ describe '.dump' do
5
+ context 'when the schema_format is :ruby' do
6
+ before do
7
+ Rails.configuration.active_record.schema_format = :ruby
8
+ end
9
+
10
+ it 'should create a db/schema.rb file' do
11
+ schema_rb_path = File.join Rails.root, 'db', 'schema.rb'
12
+ FileUtils.rm schema_rb_path if File.exists?(schema_rb_path)
13
+ described_class.dump
14
+ File.should exist(schema_rb_path)
15
+ FileUtils.rm schema_rb_path if File.exists?(schema_rb_path)
16
+ end
17
+
18
+ context 'given a file is specified' do
19
+ it 'should create the schema dump file in the specified location' do
20
+ schema_rb_path = File.join Rails.root, 'db', 'schema2.rb'
21
+ FileUtils.rm schema_rb_path if File.exists?(schema_rb_path)
22
+ described_class.dump file: schema_rb_path
23
+ File.should exist(schema_rb_path)
24
+ FileUtils.rm schema_rb_path if File.exists?(schema_rb_path)
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'when the schema_format is :sql' do
30
+ before do
31
+ Rails.configuration.active_record.schema_format = :sql
32
+ end
33
+
34
+ it 'should create a db/structure.sql file' do
35
+ structure_path = File.join Rails.root, 'db', 'structure.sql'
36
+ FileUtils.rm structure_path if File.exists?(structure_path)
37
+ described_class.dump
38
+ File.should exist(structure_path)
39
+ FileUtils.rm structure_path
40
+ end
41
+
42
+ context 'when the file is specified' do
43
+ it 'should create dump into the given file' do
44
+ structure_path = File.join Rails.root, 'db', 'structure2.sql'
45
+ FileUtils.rm structure_path if File.exists?(structure_path)
46
+ described_class.dump file: structure_path
47
+ File.should exist(structure_path)
48
+ FileUtils.rm structure_path
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Storey, '.schema_search_path_for' do
4
+ context 'given a blank argument' do
5
+ it 'should return the default path' do
6
+ Storey.switch
7
+ Storey.schema.should == Storey.default_search_path
8
+ end
9
+ end
10
+ end
@@ -47,5 +47,6 @@ describe Storey, "#schemas" do
47
47
  Storey.schemas(:public => false).should_not include("public")
48
48
  end
49
49
  end
50
+
50
51
  end
51
52
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-17 00:00:00.000000000Z
12
+ date: 2012-06-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec-rails
16
- requirement: &76025600 !ruby/object:Gem::Requirement
16
+ requirement: &82507520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *76025600
24
+ version_requirements: *82507520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: pg
27
- requirement: &76025050 !ruby/object:Gem::Requirement
27
+ requirement: &82506870 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.12.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *76025050
35
+ version_requirements: *82506870
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: database_cleaner
38
- requirement: &76024290 !ruby/object:Gem::Requirement
38
+ requirement: &82506250 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *76024290
46
+ version_requirements: *82506250
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: pry
49
- requirement: &76023620 !ruby/object:Gem::Requirement
49
+ requirement: &82505580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *76023620
57
+ version_requirements: *82505580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rails
60
- requirement: &76023040 !ruby/object:Gem::Requirement
60
+ requirement: &82504910 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 3.2.2
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *76023040
68
+ version_requirements: *82504910
69
69
  description: Storey aims to simplify the implementation of managing a multi-tenant
70
70
  application.
71
71
  email:
@@ -81,6 +81,7 @@ files:
81
81
  - README.md
82
82
  - Rakefile
83
83
  - lib/storey.rb
84
+ - lib/storey/dumper.rb
84
85
  - lib/storey/duplicator.rb
85
86
  - lib/storey/exceptions.rb
86
87
  - lib/storey/hstore.rb
@@ -134,10 +135,12 @@ files:
134
135
  - spec/storey/create_plain_schema_spec.rb
135
136
  - spec/storey/create_spec.rb
136
137
  - spec/storey/drop_spec.rb
138
+ - spec/storey/dumper_spec.rb
137
139
  - spec/storey/duplicate_spec.rb
138
140
  - spec/storey/excluded_models_spec.rb
139
141
  - spec/storey/hstore_spec.rb
140
142
  - spec/storey/persistent_schemas_spec.rb
143
+ - spec/storey/schema_search_path_for_spec.rb
141
144
  - spec/storey/schema_spec.rb
142
145
  - spec/storey/schemas_spec.rb
143
146
  - spec/storey/suffixify_spec.rb