phenix 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/phenix.rb +78 -0
  3. data/lib/phenix/version.rb +3 -0
  4. metadata +150 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d06f9c67aabcc12b5bcc69f946707721c300bdd1
4
+ data.tar.gz: cea000f8802461f4e74c7e570bac1dc019276d2c
5
+ SHA512:
6
+ metadata.gz: 7d703ecd56aec74b5614e8ad9b6f30f08adb91d2f507ffad3513bc2a506074a4397214c41612747816c1df91886d95156a230e278d38cdcfe70aacb7e93b8bc6
7
+ data.tar.gz: b24beb8e739f32dec960a06ec2f2b3b5ab1decec035d1baf4786e4ab4c3a8cc9ddeedfc7eb21fb5cec2e1c02282d16ae949502d74af1c78c53d2c05a702c58a4
data/lib/phenix.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'phenix/version'
2
+ require 'erb'
3
+
4
+ module Phenix
5
+ class << self
6
+ attr_accessor :database_config_path, :schema_path, :skip_database
7
+ attr_accessor :current_configuration
8
+
9
+ def configure
10
+ self.database_config_path = File.join(Dir.pwd, 'test', 'database.yml')
11
+ self.schema_path = File.join(Dir.pwd, 'test', 'schema.rb')
12
+ self.skip_database = ->(_, _) { false }
13
+
14
+ yield(self) if block_given?
15
+ end
16
+ end
17
+
18
+ def rise!(with_schema: true, config_path: Phenix.database_config_path)
19
+ load_database_config(config_path)
20
+ drop_databases
21
+ create_databases(with_schema)
22
+ end
23
+
24
+ def burn!
25
+ drop_databases
26
+ end
27
+
28
+ def load_database_config(config_path = Phenix.database_config_path)
29
+ erb_config = IO.read(config_path)
30
+ yaml_config = ERB.new(erb_config).result
31
+ ActiveRecord::Base.configurations = Phenix.current_configuration = YAML.load(yaml_config)
32
+ end
33
+
34
+ private
35
+
36
+ def create_databases(with_schema)
37
+ for_each_database do |name, conf|
38
+ run_mysql_command(conf, "CREATE DATABASE #{conf['database']}")
39
+ ActiveRecord::Base.establish_connection(name.to_sym)
40
+ populate_database if with_schema
41
+ end
42
+ end
43
+
44
+ def populate_database
45
+ ActiveRecord::Migration.verbose = false
46
+ load(Phenix.schema_path)
47
+ end
48
+
49
+ def drop_databases
50
+ for_each_database do |name, conf|
51
+ run_mysql_command(conf, "DROP DATABASE IF EXISTS #{conf['database']}")
52
+ end
53
+ end
54
+
55
+ def for_each_database
56
+ Phenix.current_configuration.each do |name, conf|
57
+ next if conf['database'].nil?
58
+ next if Phenix.skip_database.call(name, conf)
59
+ yield(name, conf)
60
+ end
61
+ end
62
+
63
+ def run_mysql_command(conf, command)
64
+ @mysql_command ||= begin
65
+ commands = [
66
+ 'mysql',
67
+ "--user=#{conf['username']}"
68
+ ]
69
+ commands << "--host=#{conf['host']}" if conf['host'].present?
70
+ commands << "--port=#{conf['port']}" if conf['port'].present?
71
+ commands << " --password=#{conf['password']} 2> /dev/null" if conf['password'].present?
72
+ commands.join(' ')
73
+ end
74
+ `echo "#{command}" | #{@mysql_command}`
75
+ end
76
+
77
+ extend self
78
+ end
@@ -0,0 +1,3 @@
1
+ module Phenix
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phenix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pierre Schambacher
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5.1'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '3.2'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '5.1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.5'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.5'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.4'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.4'
75
+ - !ruby/object:Gem::Dependency
76
+ name: wwtd
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: bump
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: mysql2
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description:
118
+ email:
119
+ - pschambacher@zendesk.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - lib/phenix.rb
125
+ - lib/phenix/version.rb
126
+ homepage: https://github.com/zendesk/phenix
127
+ licenses: []
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.4.5.1
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Read a dynamic database.yml file and allow you to drop/create the database
149
+ on demand.
150
+ test_files: []