schema_dev 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/schema_dev +6 -6
- data/lib/schema_dev/config.rb +2 -2
- data/lib/schema_dev/executor.rb +1 -2
- data/lib/schema_dev/gemfile_selector.rb +5 -0
- data/lib/schema_dev/rspec/db.rb +96 -0
- data/lib/schema_dev/rspec.rb +11 -49
- data/lib/schema_dev/tasks.rb +51 -0
- data/lib/schema_dev/version.rb +1 -1
- data/schema_dev.gemspec +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21d86e552e688fc882a7f7e3f165c2e6a140ae93
|
4
|
+
data.tar.gz: 854490fbea9efd8801f64cc86668f99fd93b9877
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1967c1cb6a9c6798fad74869236336fe17b2cb8204f330d9018f1a7e18549b39908dbe6f83eaff7154fa993010bb288f6848f73f43cae25a3f5fdc388ba60efd
|
7
|
+
data.tar.gz: fef0914899f5aa01a8c573805f558a188a0635068021935853a59df179a0b673a790e9b8ebe09444b84844ea50cff43a365413511db84ebefedaa0c4f2a5dd96
|
data/bin/schema_dev
CHANGED
@@ -20,26 +20,26 @@ class CLI < Thor
|
|
20
20
|
|
21
21
|
desc "run command", "run a command over the matrix"
|
22
22
|
matrix_options
|
23
|
-
def
|
23
|
+
def matrix(*args)
|
24
24
|
$runner.run(args, **options.symbolize_keys)
|
25
25
|
end
|
26
26
|
|
27
|
-
desc "bundle", "shorthand for '#{$0}
|
27
|
+
desc "bundle", "shorthand for '#{$0} matrix bundle ...'"
|
28
28
|
matrix_options
|
29
29
|
def bundle(*args)
|
30
30
|
$runner.run('bundle', args, **options.symbolize_keys)
|
31
31
|
end
|
32
32
|
|
33
|
-
desc "rake", "shorthand for '#{$0}
|
33
|
+
desc "rake", "shorthand for '#{$0} matrix bundle exec rake ...'"
|
34
34
|
matrix_options
|
35
35
|
def rake(*args)
|
36
|
-
$runner.run('rake', args, **options.symbolize_keys)
|
36
|
+
$runner.run('bundle', 'exec', 'rake', args, **options.symbolize_keys)
|
37
37
|
end
|
38
38
|
|
39
|
-
desc "rspec", "shorthand for '#{$0}
|
39
|
+
desc "rspec", "shorthand for '#{$0} bundle exec rspec ...'"
|
40
40
|
matrix_options
|
41
41
|
def rspec(*args)
|
42
|
-
$runner.run('rspec', args, **options.symbolize_keys)
|
42
|
+
$runner.run('bundle', 'exec', 'rspec', args, **options.symbolize_keys)
|
43
43
|
end
|
44
44
|
|
45
45
|
end
|
data/lib/schema_dev/config.rb
CHANGED
@@ -11,10 +11,10 @@ module SchemaDev
|
|
11
11
|
|
12
12
|
class Config
|
13
13
|
|
14
|
-
attr_accessor :quick
|
14
|
+
attr_accessor :quick, :db
|
15
15
|
|
16
16
|
def self.load
|
17
|
-
new (YAML.load Pathname.new(CONFIG_FILE).read).deep_symbolize_keys
|
17
|
+
@@config ||= new (YAML.load Pathname.new(CONFIG_FILE).read).deep_symbolize_keys
|
18
18
|
end
|
19
19
|
|
20
20
|
class Tuple < KeyStruct[:ruby, :rails, :db]
|
data/lib/schema_dev/executor.rb
CHANGED
@@ -10,11 +10,10 @@ module SchemaDev
|
|
10
10
|
def initialize(ruby:, rails:, db: nil)
|
11
11
|
@ruby_selector = RubySelector.command(ruby)
|
12
12
|
@gemfile_selector = GemfileSelector.command(rails: rails, db: db)
|
13
|
-
@dev_env = "SCHEMA_DEV_DB=#{db.inspect}"
|
14
13
|
end
|
15
14
|
|
16
15
|
def run(cmd, dry_run: false)
|
17
|
-
fullcommand = ["/usr/bin/env", @
|
16
|
+
fullcommand = ["/usr/bin/env", @gemfile_selector, @ruby_selector, cmd].compact.join(' ')
|
18
17
|
puts "* #{fullcommand}"
|
19
18
|
return true if dry_run
|
20
19
|
|
@@ -16,5 +16,10 @@ module SchemaDev
|
|
16
16
|
def self.command(rails:, db: nil)
|
17
17
|
"BUNDLE_GEMFILE=#{gemfile(rails: rails, db: db)}"
|
18
18
|
end
|
19
|
+
|
20
|
+
def self.infer_db
|
21
|
+
(env = ENV['BUNDLE_GEMFILE']) =~ %r{rails.*/Gemfile[.](.*)}
|
22
|
+
$1 or raise "Can't infer db: Env BUNDLE_GEMFILE=#{env.inspect}) isn't a schema_dev Gemfile path with db"
|
23
|
+
end
|
19
24
|
end
|
20
25
|
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'pathname'
|
3
|
+
require_relative '../gemfile_selector'
|
4
|
+
|
5
|
+
module SchemaDev
|
6
|
+
module Rspec
|
7
|
+
module Db
|
8
|
+
extend self
|
9
|
+
|
10
|
+
def setup(db = nil)
|
11
|
+
@db = db || GemfileSelector.infer_db
|
12
|
+
set_logger
|
13
|
+
connect
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.include Helpers
|
16
|
+
config.filter_run_excluding :postgresql => :only unless Helpers.postgresql?
|
17
|
+
config.filter_run_excluding :postgresql => :skip if Helpers.postgresql?
|
18
|
+
config.filter_run_excluding :mysql => :only unless Helpers.mysql?
|
19
|
+
config.filter_run_excluding :mysql => :skip if Helpers.mysql?
|
20
|
+
config.filter_run_excluding :sqlite3 => :only unless Helpers.sqlite3?
|
21
|
+
config.filter_run_excluding :sqlite3 => :skip if Helpers.sqlite3?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def tmproot
|
26
|
+
@tmproot ||= Pathname.new('tmp').tap { |path| path.mkpath }
|
27
|
+
end
|
28
|
+
|
29
|
+
def configuration
|
30
|
+
case @db
|
31
|
+
when 'mysql'
|
32
|
+
{
|
33
|
+
:adapter => 'mysql',
|
34
|
+
:database => 'schema_plus_test',
|
35
|
+
:username => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
|
36
|
+
:encoding => 'utf8',
|
37
|
+
:min_messages => 'warning'
|
38
|
+
}
|
39
|
+
when 'mysql2'
|
40
|
+
{
|
41
|
+
:adapter => 'mysql2',
|
42
|
+
:database => 'schema_plus_test',
|
43
|
+
:username => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
|
44
|
+
:encoding => 'utf8',
|
45
|
+
:min_messages => 'warning'
|
46
|
+
}
|
47
|
+
when 'postgresql'
|
48
|
+
{
|
49
|
+
:adapter => 'postgresql',
|
50
|
+
:username => ENV['POSTGRESQL_DB_USER'],
|
51
|
+
:database => 'schema_plus_test',
|
52
|
+
:min_messages => 'warning'
|
53
|
+
}
|
54
|
+
when 'sqlite3'
|
55
|
+
{
|
56
|
+
:adapter => 'sqlite3',
|
57
|
+
:database => tmproot.join('schema_plus.sqlite3').to_s
|
58
|
+
}
|
59
|
+
else
|
60
|
+
raise "Unknown db adapter #{@db.inspect}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def connect
|
65
|
+
ActiveRecord::Base.configurations = { 'schema_dev' => configuration }
|
66
|
+
ActiveRecord::Base.establish_connection :schema_dev
|
67
|
+
case @db
|
68
|
+
when 'sqlite3'
|
69
|
+
ActiveRecord::Base.connection.execute "PRAGMA synchronous = OFF"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def set_logger
|
74
|
+
ruby = "#{RUBY_ENGINE}-#{RUBY_VERSION}"
|
75
|
+
rails = "rails-#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
76
|
+
ActiveRecord::Base.logger = Logger.new(tmproot.join("#{ruby}.#{rails}.#{@db}.log").open("w"))
|
77
|
+
end
|
78
|
+
|
79
|
+
module Helpers
|
80
|
+
extend self
|
81
|
+
|
82
|
+
def mysql?
|
83
|
+
ActiveRecord::Base.connection.adapter_name =~ /^mysql/i
|
84
|
+
end
|
85
|
+
|
86
|
+
def postgresql?
|
87
|
+
ActiveRecord::Base.connection.adapter_name =~ /^postgresql/i
|
88
|
+
end
|
89
|
+
|
90
|
+
def sqlite3?
|
91
|
+
ActiveRecord::Base.connection.adapter_name =~ /^sqlite/i
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/schema_dev/rspec.rb
CHANGED
@@ -1,58 +1,20 @@
|
|
1
|
-
|
2
|
-
require 'pathname'
|
1
|
+
require_relative 'rspec/db'
|
3
2
|
|
4
3
|
module SchemaDev
|
5
4
|
module Rspec
|
6
5
|
|
7
|
-
def self.
|
8
|
-
db
|
9
|
-
|
10
|
-
rails = "rails-#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
11
|
-
root = Pathname.new('tmp')
|
12
|
-
root.mkpath
|
13
|
-
configuration = case db
|
14
|
-
when 'mysql'
|
15
|
-
{
|
16
|
-
:adapter => 'mysql',
|
17
|
-
:database => 'schema_plus_test',
|
18
|
-
:username => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
|
19
|
-
:encoding => 'utf8',
|
20
|
-
:min_messages => 'warning'
|
21
|
-
}
|
22
|
-
when 'mysql2'
|
23
|
-
{
|
24
|
-
:adapter => 'mysql2',
|
25
|
-
:database => 'schema_plus_test',
|
26
|
-
:username => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
|
27
|
-
:encoding => 'utf8',
|
28
|
-
:min_messages => 'warning'
|
29
|
-
}
|
30
|
-
when 'postgresql'
|
31
|
-
{
|
32
|
-
:adapter => 'postgresql',
|
33
|
-
:username => ENV['POSTGRESQL_DB_USER'],
|
34
|
-
:database => 'schema_plus_test',
|
35
|
-
:min_messages => 'warning'
|
36
|
-
}
|
37
|
-
when 'sqlite3'
|
38
|
-
{
|
39
|
-
:adapter => 'sqlite3',
|
40
|
-
:database => root.join('schema_plus.sqlite3').to_s
|
41
|
-
}
|
42
|
-
else
|
43
|
-
raise "Unknown db adapter #{db.inspect}"
|
44
|
-
end
|
45
|
-
|
46
|
-
ActiveRecord::Base.logger = Logger.new(root.join("#{ruby}.#{rails}.#{db}.log").open("w"))
|
47
|
-
ActiveRecord::Base.configurations = { 'schema_dev' => configuration }
|
48
|
-
ActiveRecord::Base.establish_connection :schema_dev
|
6
|
+
def self.setup_db(db=nil)
|
7
|
+
Db.setup(db)
|
8
|
+
end
|
49
9
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
10
|
+
def self.db_configuration
|
11
|
+
Db.configuration
|
12
|
+
end
|
54
13
|
|
14
|
+
end
|
55
15
|
|
56
|
-
|
16
|
+
module Helpers
|
17
|
+
extend Rspec::Db::Helpers
|
57
18
|
end
|
19
|
+
|
58
20
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require_relative 'config'
|
2
|
+
|
3
|
+
config = SchemaDev::Config.load
|
4
|
+
|
5
|
+
DATABASES = %w[schema_plus_test]
|
6
|
+
|
7
|
+
dbms = [:postgresql, :mysql].select{|dbm| config.db.grep(/^#{dbm}/).any?}
|
8
|
+
|
9
|
+
if dbms.any?
|
10
|
+
{
|
11
|
+
postgresql: { uservar: 'POSTGRESQL_DB_USER', defaultuser: 'schema_plus', create: "createdb -U '%{user}' %{dbname}", drop: "dropdb -U '%{user}' %{dbname}" },
|
12
|
+
mysql: { uservar: 'MYSQL_DB_USER', defaultuser: 'schema_plus', create: "mysqladmin -u '%{user}' create %{dbname}", drop: "mysqladmin -u '%{user}' -f drop %{dbname}" }
|
13
|
+
}.slice(*dbms).each do |dbm, info|
|
14
|
+
namespace dbm do
|
15
|
+
user = ENV.fetch info[:uservar], info[:defaultuser]
|
16
|
+
task :create_databases do
|
17
|
+
DATABASES.each do |dbname|
|
18
|
+
system(info[:create] % {user: user, dbname: dbname})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
task :drop_databases do
|
22
|
+
DATABASES.each do |dbname|
|
23
|
+
system(info[:drop] % {user: user, dbname: dbname})
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
desc 'Create test databases'
|
30
|
+
task :create_databases do
|
31
|
+
invoke_multiple(dbms, "create_databases")
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'Drop test databases'
|
35
|
+
task :drop_databases do
|
36
|
+
invoke_multiple(dbms, "drop_databases")
|
37
|
+
end
|
38
|
+
|
39
|
+
def invoke_multiple(namespaces, task)
|
40
|
+
failed = namespaces.reject { |adapter|
|
41
|
+
begin
|
42
|
+
Rake::Task["#{adapter}:#{task}"].invoke
|
43
|
+
true
|
44
|
+
rescue => e
|
45
|
+
warn "\n#{e}\n"
|
46
|
+
false
|
47
|
+
end
|
48
|
+
}
|
49
|
+
fail "Failure in: #{failed.join(', ')}" if failed.any?
|
50
|
+
end
|
51
|
+
end
|
data/lib/schema_dev/version.rb
CHANGED
data/schema_dev.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["ronen@barzel.org"]
|
11
11
|
spec.summary = %q{SchemaPlus development tools}
|
12
12
|
spec.description = %q{SchemaPlus development tools}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/SchemaPlus/schema_dev"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_dev
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ronen barzel
|
@@ -143,14 +143,16 @@ files:
|
|
143
143
|
- lib/schema_dev/gemfile_selector.rb
|
144
144
|
- lib/schema_dev/matrix_executor.rb
|
145
145
|
- lib/schema_dev/rspec.rb
|
146
|
+
- lib/schema_dev/rspec/db.rb
|
146
147
|
- lib/schema_dev/ruby_selector.rb
|
147
148
|
- lib/schema_dev/runner.rb
|
149
|
+
- lib/schema_dev/tasks.rb
|
148
150
|
- lib/schema_dev/version.rb
|
149
151
|
- schema_dev.gemspec
|
150
152
|
- schema_dev.yml
|
151
153
|
- spec/config_spec.rb
|
152
154
|
- spec/spec_helper.rb
|
153
|
-
homepage:
|
155
|
+
homepage: https://github.com/SchemaPlus/schema_dev
|
154
156
|
licenses:
|
155
157
|
- MIT
|
156
158
|
metadata: {}
|