combustion 0.5.4 → 0.5.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: adee1390f356a9707415fc7b73ee772c98013820
4
- data.tar.gz: 82d9808d3817693cac00fad905c14358b9f3b30b
3
+ metadata.gz: 3267619b0496c3956bfa0bd994939298763e4aed
4
+ data.tar.gz: ef5c03a4638db69326a76bd2aa949a20f321ab84
5
5
  SHA512:
6
- metadata.gz: 326878f655959ee100820d57b3fd7ba9628b02b685f025875120ae9e209ceeeabdfbd5b19e0b9dd4f95508410b784c0a15648d63beab3bf973de969bac32f402
7
- data.tar.gz: 17984826025904c80d93ea41323a8b966917aaa7ce3c23ad2496714c730623c702c42b2ebb6ee990b9fafc47650fd9f32ec18c5856b3e25fed4a4a3910e3dbe6
6
+ metadata.gz: 14fa5db249addbd51fee818b3bc8954ba455313e93388e8e9b044a931d9d9c1276605dfb50b59fe4f603e3a180a8b6533fdbface66ff247d94a64d886ca63cb3
7
+ data.tar.gz: 43b4c7cc7b9e35c5a2e1d8d772c5924b54e732580ab6b7cba5cddc1771d0124ce6f668bf9cca68f744c7d6b30f8d7edcbf512239d6e3698d0079163b73e719dc
data/README.md CHANGED
@@ -10,25 +10,27 @@ Get the gem into either your gemspec or your Gemfile, depending on how you manag
10
10
 
11
11
  ```ruby
12
12
  # gemspec
13
- gem.add_development_dependency 'combustion', '~> 0.5.4'
13
+ gem.add_development_dependency 'combustion', '~> 0.5.5'
14
14
 
15
15
  # Gemfile
16
- gem 'combustion', '~> 0.5.4', :group => :test
16
+ gem 'combustion', '~> 0.5.5', :group => :test
17
17
  ```
18
18
 
19
19
  In your `spec_helper.rb`, get Combustion to set itself up - which has to happen before you introduce `rspec/rails` and - if being used - `capybara/rails`. Here's an example within context:
20
20
 
21
21
  ```ruby
22
- require 'rubygems'
23
- require 'bundler/setup'
22
+ require 'bundler'
24
23
 
25
- require 'combustion'
26
- require 'capybara/rspec'
24
+ Bundler.require :default, :development
27
25
 
26
+ # If you're using all parts of Rails:
28
27
  Combustion.initialize! :all
28
+ # Or, load just what you need:
29
+ # Combustion.initialize! :active_record, :action_controller
29
30
 
30
31
  require 'rspec/rails'
31
- require 'capybara/rails'
32
+ # If you're using Capybara:
33
+ # require 'capybara/rails'
32
34
 
33
35
  RSpec.configure do |config|
34
36
  config.use_transactional_fixtures = true
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'combustion'
4
- s.version = '0.5.4'
4
+ s.version = '0.5.5'
5
5
  s.authors = ['Pat Allan']
6
6
  s.email = ['pat@freelancing-gods.com']
7
7
  s.homepage = 'https://github.com/pat/combustion'
@@ -1,186 +1,25 @@
1
1
  module Combustion
2
- class Database
3
- def self.setup
4
- reset_database
5
- load_schema
6
- migrate
7
- end
8
-
9
- def self.reset_database
10
- ActiveRecord::Base.configurations = YAML.load(ERB.new(File.read("#{Rails.root}/config/database.yml")).result)
11
- abcs = ActiveRecord::Base.configurations
12
- case abcs['test']['adapter']
13
- when /mysql/
14
- drop_database(abcs['test']['database'])
15
- create_database(abcs['test'])
16
- ActiveRecord::Base.establish_connection(:test)
17
- when /postgresql/
18
- ActiveRecord::Base.clear_active_connections!
19
- drop_database(abcs['test'])
20
- create_database(abcs['test'])
21
- when /sqlite/
22
- drop_database(abcs['test'])
23
- create_database(abcs['test'])
24
- when 'sqlserver'
25
- test = abcs.deep_dup['test']
26
- test_database = test['database']
27
- test['database'] = 'master'
28
- ActiveRecord::Base.establish_connection(test)
29
- ActiveRecord::Base.connection.recreate_database!(test_database)
30
- when "oci", "oracle"
31
- ActiveRecord::Base.establish_connection(:test)
32
- ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
33
- ActiveRecord::Base.connection.execute(ddl)
34
- end
35
- when 'firebird'
36
- ActiveRecord::Base.establish_connection(:test)
37
- ActiveRecord::Base.connection.recreate_database!
38
- else
39
- raise "Cannot reset databases for '#{abcs['test']['adapter']}'"
40
- end
41
- end
42
-
43
- def self.load_schema
44
- case Combustion.schema_format
45
- when :ruby
46
- load Rails.root.join('db', 'schema.rb')
47
- when :sql
48
- ActiveRecord::Base.connection.execute(
49
- File.read(Rails.root.join('db', 'structure.sql'))
50
- )
51
- else
52
- raise "Unknown schema format: #{Combustion.schema_format}"
53
- end
54
- end
55
-
56
- def self.migrate
57
- migrator = ActiveRecord::Migrator
58
- engine_path = Rails.application.root.sub(::Combustion.path, '')
59
- engine_migration_paths = Rails.application.paths['db/migrate'].to_a
60
-
61
- if engine_migration_paths.include?(engine_path.join('db/migrate').to_s)
62
- paths = []
63
- else
64
- paths = base_migration_paths
65
- end
66
-
67
- paths += engine_migration_paths
68
- paths.uniq!
69
-
70
- # Append the migrations inside the internal app's db/migrate directory
71
- paths << File.join(Rails.root, 'db/migrate')
72
-
73
- if ActiveRecord::VERSION::STRING >= '3.1.0'
74
- migrator.migrate paths, nil
75
- else
76
- paths.each { |path| migrator.migrate path, nil }
77
- end
78
- end
79
-
80
- private
81
-
82
- def self.base_migration_paths
83
- if ActiveRecord::Migrator.respond_to?(:migrations_paths)
84
- ActiveRecord::Migrator.migrations_paths
85
- else
86
- Array('db/migrate/')
87
- end
88
- end
89
-
90
- def self.create_database(config)
91
- begin
92
- if config['adapter'] =~ /sqlite/
93
- if File.exist?(config['database'])
94
- $stderr.puts "#{config['database']} already exists"
95
- else
96
- begin
97
- # Create the SQLite database
98
- ActiveRecord::Base.establish_connection(config)
99
- ActiveRecord::Base.connection
100
- rescue Exception => e
101
- $stderr.puts e, *(e.backtrace)
102
- $stderr.puts "Couldn't create database for #{config.inspect}"
103
- end
104
- end
105
- return # Skip the else clause of begin/rescue
106
- else
107
- ActiveRecord::Base.establish_connection(config)
108
- ActiveRecord::Base.connection
109
- end
110
- rescue
111
- case config['adapter']
112
- when /^(jdbc)?mysql/
113
- if config['adapter'] =~ /jdbc/
114
- #FIXME After Jdbcmysql gives this class
115
- require 'active_record/railties/jdbcmysql_error'
116
- error_class = ArJdbcMySQL::Error
117
- else
118
- error_class = config['adapter'] =~ /mysql2/ && defined?(Mysql2) ? Mysql2::Error : Mysql::Error
119
- end
120
- access_denied_error = 1045
121
- begin
122
- ActiveRecord::Base.establish_connection(config.merge('database' => nil))
123
- ActiveRecord::Base.connection.create_database(config['database'], mysql_creation_options(config))
124
- ActiveRecord::Base.establish_connection(config)
125
- rescue error_class => sqlerr
126
- if sqlerr.errno == access_denied_error
127
- print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
128
- root_password = $stdin.gets.strip
129
- grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \
130
- "TO '#{config['username']}'@'localhost' " \
131
- "IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;"
132
- ActiveRecord::Base.establish_connection(config.merge(
133
- 'database' => nil, 'username' => 'root', 'password' => root_password))
134
- ActiveRecord::Base.connection.create_database(config['database'], mysql_creation_options(config))
135
- ActiveRecord::Base.connection.execute grant_statement
136
- ActiveRecord::Base.establish_connection(config)
137
- else
138
- $stderr.puts sqlerr.error
139
- $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}"
140
- $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
141
- end
142
- end
143
- when /^(jdbc)?postgresql$/
144
- @encoding = config['encoding'] || ENV['CHARSET'] || 'utf8'
145
- begin
146
- ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
147
- ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
148
- ActiveRecord::Base.establish_connection(config)
149
- rescue Exception => e
150
- $stderr.puts e, *(e.backtrace)
151
- $stderr.puts "Couldn't create database for #{config.inspect}"
152
- end
153
- end
154
- else
155
- $stderr.puts "#{config['database']} already exists"
156
- end
157
- end
158
-
159
- def self.drop_database(config)
160
- case config['adapter']
161
- when /^(jdbc)?mysql/
162
- ActiveRecord::Base.establish_connection(config)
163
- ActiveRecord::Base.connection.drop_database config['database']
164
- when /^(jdbc)?sqlite/
165
- require 'pathname'
166
- path = Pathname.new(config['database'])
167
- file = path.absolute? ? path.to_s : File.join(Rails.root, path)
168
-
169
- FileUtils.rm_f(file) if File.exist?(file)
170
- when /^(jdbc)?postgresql$/
171
- ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
172
- ActiveRecord::Base.connection.drop_database config['database']
173
- end
174
- end
175
-
176
- def self.mysql_creation_options(config)
177
- @charset = ENV['CHARSET'] || 'utf8'
178
- @collation = ENV['COLLATION'] || 'utf8_unicode_ci'
2
+ module Databases
3
+ #
4
+ end
179
5
 
180
- {
181
- :charset => (config['charset'] || @charset),
182
- :collation => (config['collation'] || @collation)
183
- }
6
+ class Database
7
+ def self.setup(adapter = nil)
8
+ Combustion::Database::Reset.call adapter
9
+ Combustion::Database::LoadSchema.call
10
+ Combustion::Database::Migrate.call
184
11
  end
185
12
  end
186
13
  end
14
+
15
+ require 'combustion/database/load_schema'
16
+ require 'combustion/database/migrate'
17
+ require 'combustion/database/reset'
18
+
19
+ require 'combustion/databases/base'
20
+ require 'combustion/databases/firebird'
21
+ require 'combustion/databases/mysql'
22
+ require 'combustion/databases/oracle'
23
+ require 'combustion/databases/postgresql'
24
+ require 'combustion/databases/sql_server'
25
+ require 'combustion/databases/sqlite'
@@ -0,0 +1,26 @@
1
+ class Combustion::Database::LoadSchema
2
+ UnknownSchemaFormat = Class.new StandardError
3
+
4
+ def self.call
5
+ new.call
6
+ end
7
+
8
+ def call
9
+ case schema_format
10
+ when :ruby
11
+ load Rails.root.join('db', 'schema.rb')
12
+ when :sql
13
+ ActiveRecord::Base.connection.execute(
14
+ File.read(Rails.root.join('db', 'structure.sql'))
15
+ )
16
+ else
17
+ raise UnknownSchemaFormat, "Unknown schema format: #{schema_format}"
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def schema_format
24
+ Combustion.schema_format
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ class Combustion::Database::Migrate
2
+ def self.call
3
+ new.call
4
+ end
5
+
6
+ def call
7
+ if ActiveRecord::VERSION::STRING >= '3.1.0'
8
+ migrator.migrate paths, nil
9
+ else
10
+ paths.each { |path| migrator.migrate path, nil }
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def base_migration_paths
17
+ if migrator.respond_to?(:migrations_paths)
18
+ migrator.migrations_paths
19
+ else
20
+ Array('db/migrate/')
21
+ end
22
+ end
23
+
24
+ def migrator
25
+ @migrator ||= ActiveRecord::Migrator
26
+ end
27
+
28
+ def paths
29
+ engine_path = Rails.application.root.sub(::Combustion.path, '')
30
+ migration_paths = Rails.application.paths['db/migrate'].to_a
31
+
32
+ if migration_paths.include?(engine_path.join('db/migrate').to_s)
33
+ paths = []
34
+ else
35
+ paths = base_migration_paths
36
+ end
37
+
38
+ (paths + migration_paths + [File.join(Rails.root, 'db/migrate')]).uniq
39
+ end
40
+ end
@@ -0,0 +1,53 @@
1
+ class Combustion::Database::Reset
2
+ UnsupportedDatabase = Class.new StandardError
3
+
4
+ def self.call(adapter = nil)
5
+ new(adapter).call
6
+ end
7
+
8
+ def initialize(adapter = nil)
9
+ @adapter = adapter
10
+
11
+ set_configurations
12
+ end
13
+
14
+ def call
15
+ operator.reset
16
+ end
17
+
18
+ private
19
+
20
+ def configuration
21
+ @configuration ||= ActiveRecord::Base.configurations['test']
22
+ end
23
+
24
+ def operator
25
+ operator_class.new configuration
26
+ end
27
+
28
+ def operator_class
29
+ @operator ||= case configuration['adapter']
30
+ when /mysql/
31
+ Combustion::Databases::MySQL
32
+ when /postgresql/, /postgis/
33
+ Combustion::Databases::PostgreSQL
34
+ when /sqlite/
35
+ Combustion::Databases::SQLite
36
+ when /sqlserver/
37
+ Combustion::Databases::SQLServer
38
+ when 'oci', 'oracle'
39
+ Combustion::Databases::Oracle
40
+ when 'firebird'
41
+ Combustion::Databases::Firebird
42
+ else
43
+ raise UnsupportedDatabase,
44
+ "Unsupported database type: #{configuration['adapter']}"
45
+ end
46
+ end
47
+
48
+ def set_configurations
49
+ ActiveRecord::Base.configurations = YAML.load(
50
+ ERB.new(File.read("#{Rails.root}/config/database.yml")).result
51
+ )
52
+ end
53
+ end
@@ -0,0 +1,22 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+
3
+ class Combustion::Databases::Base
4
+ def initialize(configuration)
5
+ @configuration = configuration
6
+ end
7
+
8
+ def reset
9
+ drop
10
+ create
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :configuration
16
+
17
+ delegate :establish_connection, :connection, :to => :base
18
+
19
+ def base
20
+ ActiveRecord::Base
21
+ end
22
+ end
@@ -0,0 +1,6 @@
1
+ class Combustion::Databases::Firebird < Combustion::Databases::Base
2
+ def reset
3
+ establish_connection :test
4
+ connection.recreate_database!
5
+ end
6
+ end
@@ -0,0 +1,71 @@
1
+ class Combustion::Databases::MySQL < Combustion::Databases::Base
2
+ ACCESS_DENIED_ERROR = 10145
3
+
4
+ def reset
5
+ super
6
+
7
+ establish_connection :test
8
+ end
9
+
10
+ private
11
+
12
+ def charset
13
+ configuration['charset'] || ENV['CHARSET'] || 'utf8'
14
+ end
15
+
16
+ def collation
17
+ configuration['collation'] || ENV['COLLATION'] || 'utf8_unicode_ci'
18
+ end
19
+
20
+ def create
21
+ establish_connection configuration.merge('database' => nil)
22
+ connection.create_database configuration['database'], creation_options
23
+ establish_connection configuration
24
+ rescue error_class => error
25
+ if error.errno == ACCESS_DENIED_ERROR
26
+ create_as_root
27
+ else
28
+ $stderr.puts error.error
29
+ $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{charset}, collation: #{collation}"
30
+ $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
31
+ end
32
+ end
33
+
34
+ def create_as_root
35
+ print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
36
+ root_password = $stdin.gets.strip
37
+ establish_connection configuration.merge(
38
+ 'database' => nil, 'username' => 'root', 'password' => root_password
39
+ )
40
+ connection.create_database config['database'], creation_options
41
+ connection.execute grant_statement
42
+ establish_connection configuration
43
+ end
44
+
45
+ def creation_options
46
+ {:charset => charset, :collation => collation}
47
+ end
48
+
49
+ def drop
50
+ establish_connection configuration
51
+ connection.drop_database configuration['database']
52
+ end
53
+
54
+ def error_class
55
+ if configuration['adapter'] =~ /jdbc/
56
+ #FIXME After Jdbcmysql gives this class
57
+ require 'active_record/railties/jdbcmysql_error'
58
+ error_class = ArJdbcMySQL::Error
59
+ else
60
+ error_class = config['adapter'] =~ /mysql2/ && defined?(Mysql2) ? Mysql2::Error : Mysql::Error
61
+ end
62
+ end
63
+
64
+ def grant_statement
65
+ <<-SQL
66
+ GRANT ALL PRIVILEGES ON #{configuration['database']}.*
67
+ TO '#{configuration['username']}'@'localhost'
68
+ IDENTIFIED BY '#{configuration['password']}' WITH GRANT OPTION;
69
+ SQL
70
+ end
71
+ end
@@ -0,0 +1,8 @@
1
+ class Combustion::Databases::Oracle < Combustion::Databases::Base
2
+ def reset
3
+ establish_connection :test
4
+ connection.structure_drop.split(";\n\n").each do |ddl|
5
+ connection.execute(ddl)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,39 @@
1
+ class Combustion::Databases::PostgreSQL < Combustion::Databases::Base
2
+ def reset
3
+ base.clear_active_connections!
4
+
5
+ super
6
+ end
7
+
8
+ private
9
+
10
+ def create
11
+ establish_connection postgres_configuration
12
+ connection.create_database configuration['database'],
13
+ configuration.merge('encoding' => encoding)
14
+ establish_connection configuration
15
+ rescue Exception => error
16
+ $stderr.puts error, *(error.backtrace)
17
+ $stderr.puts "Couldn't create database for #{configuration.inspect}"
18
+ end
19
+
20
+ def drop
21
+ establish_connection postgres_configuration
22
+ connection.drop_database configuration['database']
23
+ end
24
+
25
+ def encoding
26
+ configuration['encoding'] || ENV['CHARSET'] || 'utf8'
27
+ end
28
+
29
+ def postgres_configuration
30
+ configuration.merge(
31
+ 'database' => 'postgres',
32
+ 'schema_search_path' => schema_search_path
33
+ )
34
+ end
35
+
36
+ def schema_search_path
37
+ configuration['adapter'][/postgis/] ? 'public, postgis' : 'public'
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ class Combustion::Databases::SQLServer < Combustion::Databases::Base
2
+ def reset
3
+ establish_connection configuration.merge('database' => 'master')
4
+ connection.recreate_database! configuration['database']
5
+ end
6
+ end
@@ -0,0 +1,35 @@
1
+ require 'fileutils'
2
+ require 'pathname'
3
+
4
+ class Combustion::Databases::SQLite < Combustion::Databases::Base
5
+ private
6
+
7
+ def create
8
+ if exists?
9
+ $stderr.puts "#{config['database']} already exists"
10
+ return
11
+ end
12
+
13
+ establish_connection configuration
14
+ connection
15
+ rescue Exception => error
16
+ $stderr.puts error, *(error.backtrace)
17
+ $stderr.puts "Couldn't create database for #{configuration.inspect}"
18
+ end
19
+
20
+ def drop
21
+ FileUtils.rm_f file if exists?
22
+ end
23
+
24
+ def exists?
25
+ File.exist? file
26
+ end
27
+
28
+ def file
29
+ @file ||= path.absolute? ? path.to_s : File.join(Rails.root, path)
30
+ end
31
+
32
+ def path
33
+ @path ||= Pathname.new configuration['database']
34
+ end
35
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: combustion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Allan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-12 00:00:00.000000000 Z
11
+ date: 2016-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -113,6 +113,16 @@ files:
113
113
  - lib/combustion.rb
114
114
  - lib/combustion/application.rb
115
115
  - lib/combustion/database.rb
116
+ - lib/combustion/database/load_schema.rb
117
+ - lib/combustion/database/migrate.rb
118
+ - lib/combustion/database/reset.rb
119
+ - lib/combustion/databases/base.rb
120
+ - lib/combustion/databases/firebird.rb
121
+ - lib/combustion/databases/mysql.rb
122
+ - lib/combustion/databases/oracle.rb
123
+ - lib/combustion/databases/postgresql.rb
124
+ - lib/combustion/databases/sql_server.rb
125
+ - lib/combustion/databases/sqlite.rb
116
126
  - lib/combustion/generator.rb
117
127
  - spec/database_spec.rb
118
128
  - spec/dummy/db/migrate/20150717075542_create_dummy_test_table.rb