phil_columns 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +3 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +257 -0
  10. data/Rakefile +6 -0
  11. data/bin/phil_columns +6 -0
  12. data/lib/phil_columns.rb +27 -0
  13. data/lib/phil_columns/archivist.rb +36 -0
  14. data/lib/phil_columns/cli.rb +162 -0
  15. data/lib/phil_columns/cli/generate.rb +43 -0
  16. data/lib/phil_columns/cli/list.rb +68 -0
  17. data/lib/phil_columns/command.rb +14 -0
  18. data/lib/phil_columns/command/base.rb +64 -0
  19. data/lib/phil_columns/command/empty.rb +30 -0
  20. data/lib/phil_columns/command/generate.rb +9 -0
  21. data/lib/phil_columns/command/generate/seed.rb +25 -0
  22. data/lib/phil_columns/command/generator.rb +45 -0
  23. data/lib/phil_columns/command/install.rb +65 -0
  24. data/lib/phil_columns/command/list.rb +10 -0
  25. data/lib/phil_columns/command/list/tagged_with.rb +27 -0
  26. data/lib/phil_columns/command/list/tags.rb +37 -0
  27. data/lib/phil_columns/command/mulligan.rb +22 -0
  28. data/lib/phil_columns/command/seed.rb +44 -0
  29. data/lib/phil_columns/configuration.rb +88 -0
  30. data/lib/phil_columns/error.rb +5 -0
  31. data/lib/phil_columns/filter.rb +66 -0
  32. data/lib/phil_columns/migrator.rb +80 -0
  33. data/lib/phil_columns/output.rb +29 -0
  34. data/lib/phil_columns/railtie.rb +18 -0
  35. data/lib/phil_columns/seed.rb +37 -0
  36. data/lib/phil_columns/seed_utils.rb +59 -0
  37. data/lib/phil_columns/seeder.rb +58 -0
  38. data/lib/phil_columns/version.rb +3 -0
  39. data/lib/phil_columns/with_backend.rb +21 -0
  40. data/phil_columns.gemspec +30 -0
  41. data/spec/spec_helper.rb +2 -0
  42. data/templates/seed_class.erb +11 -0
  43. metadata +200 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fae5c938512a010ef32810b8a9a7e95d8a111e03
4
+ data.tar.gz: 560dd0e61b42b8300678346b1d1feeeeadfea172
5
+ SHA512:
6
+ metadata.gz: 3b0d80f5f82ee4803f1188b47c24c48c7380abf132bceb30bd88bd86583a9669208cfe04fadebdbd9b25acd0e9ece1dcf227f03e3719d4bbb53c4c5b48de7996
7
+ data.tar.gz: 678cdb916e14ea29853b488a8ce4464ff9146b6cf8bd00958a2076c418376e0ca34adbb6bcf57e04f5a24d6bff36a58e1379dec4a69612ea8fadc051079103ce
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ phil_columns
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.0
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in phil_columns.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 C. Jason Harrelson (midas)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,257 @@
1
+ # PhilColumns
2
+
3
+ A utility for seeding databases for development and production (works in Rails and non-Rails Ruby projects).
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'phil_columns'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install phil_columns
19
+
20
+ To use all features of phil_columns you will need a database adapter.
21
+
22
+ gem 'phil_columns-activerecord'
23
+
24
+ Or
25
+
26
+ $ gem install phil_columns-activerecord
27
+
28
+
29
+ ## Usage
30
+
31
+ ### Installation into a project
32
+
33
+ Installing phil_columns into a Ruby project.
34
+
35
+ $ phil_columns install some/path
36
+
37
+ Installing phil_columns into a Rails project.
38
+
39
+ $ phil_columns install --rails
40
+
41
+ The installation process puts in place the seeds directory and a configuration file: .phil_columns. The --rails switch
42
+ simply overrides some configurations to allow phil_columns to work in a Rails project.
43
+
44
+
45
+ ### Seeding Quick Start
46
+
47
+ Use the generator to create a seed.
48
+
49
+ $ phil_columns generate seed add_things
50
+
51
+ The generator puts a file in place. Add your seeding logic to the #up and #down methods using any valid Ruby code.
52
+
53
+ # 20140513111454_add_things.rb.
54
+
55
+ class AddThings < PhilColumns::Seed
56
+ envs :development
57
+
58
+ def up
59
+ # seed logic ...
60
+ end
61
+
62
+ def down
63
+ # seed logic ...
64
+ end
65
+ end
66
+
67
+ Execute the seed(s).
68
+
69
+ $ phil_columns seed
70
+
71
+
72
+ ### The Command Line Interface
73
+
74
+ Get a summary of commands.
75
+
76
+ $ phil_columns help
77
+
78
+ Get help with a command.
79
+
80
+ $ phil_columns help COMMAND
81
+ $ phil_columns help seed
82
+
83
+ Some commands have sub-commands. Get help witha sub-command.
84
+
85
+ $ phil_columns COMMAND help SUBCOMMAND
86
+ $ phil_columns generate help seed
87
+
88
+
89
+ ### The Seed Command
90
+
91
+ The simplest usage of the seed command defaults the environment to 'development' and the version to 'all.'
92
+
93
+ $ phil_columns seed
94
+
95
+ The environment can be overridden using the command line. The environment is used to select only seeds that have been specified for the
96
+ specified environment.
97
+
98
+ $ phil_columns seed --env production
99
+ $ phil_columns seed -e production
100
+
101
+ The version can be overridden using the command line. When a version is specified the seeding occurs up to the specified version. When
102
+ the seeding direction is up the version specified is included in the seeding set. When seeding direction is down the specified version is
103
+ not included in the seed set.
104
+
105
+ $ phil_columns seed --version 20140513111454
106
+ $ phil_columns seed -v 20140513111454
107
+
108
+ The direction of the seeding can be altered to down using the down switch.
109
+
110
+ $ phil_columns seed --down
111
+ $ phil_columns seed -d
112
+
113
+ Finally, a dry run can be invoked using the dry-run switch.
114
+
115
+ $ phil_columns seed --dry-run
116
+
117
+
118
+ ### The Mulligan Command
119
+
120
+ The mulligan command is a complete reset of the database. The tables are removed and rebuilt and the data is seeded. The strategy used to
121
+ unload and load the schema is controlled by the configuration file attributes schema_unload\_strategy and schema\_load_strategy. The mulligan
122
+ term is borrowed from golf where a mulligan is a do-over.
123
+
124
+ The simplest usage of the mulligan command defaults the environment to 'development' and the version to 'all.'
125
+
126
+ $ phil_columns mulligan
127
+
128
+ The environment can be overridden using the command line. The environment is used to select only seeds that have been specified for the
129
+ specified environment.
130
+
131
+ $ phil_columns mulligan --env production
132
+ $ phil_columns mulligan -e production
133
+
134
+ The version can be overridden using the command line. When a version is specified the seeding occurs up to the specified version. When
135
+ the seeding direction is up the version specified is included in the seeding set. When seeding direction is down the specified version is
136
+ not included in the seed set.
137
+
138
+ $ phil_columns mulligan --version 20140513111454
139
+ $ phil_columns mulligan -v 20140513111454
140
+
141
+
142
+ ### The Empty Command
143
+
144
+ The empty command empties all tables (excluding the schema_migrations table in a Rials project).
145
+
146
+ The simplest usage of the empty command.
147
+
148
+ $ phil_columns empty
149
+
150
+
151
+ ### Additional Commands
152
+
153
+ For documentation on additional commands/sub-commands use the command line interface's built in help features.
154
+
155
+ $ phil_columns help
156
+ $ phil_columns help COMMAND
157
+ $ phil_columns COMMAND help
158
+ $ phil_columns COMMAND help SUBCOMMAND
159
+
160
+
161
+ ### Tags and Environments
162
+
163
+ Tags and environments can be applied to seeds and filtered in command usage. The seed generator adds the development environment by default and
164
+ no tags. This feature enables efficiency and adaptability in development seeding and the possibility to use phil_columns seeding in production
165
+ (see Production Seeding section below).
166
+
167
+ Specifying environment(s) for a seed is accomplished with the ::envs class method.
168
+
169
+ class AddThings < PhilColumns::Seed
170
+ envs :development
171
+ ...
172
+ end
173
+
174
+ To change the environment use the env switch. When not specified the env defaults to development
175
+
176
+ $ phil_columns seed -e production
177
+
178
+ Similary, applying tag(s) is accomplished using the ::tags class method.
179
+
180
+ class AddThings < PhilColumns::Seed
181
+ envs :development
182
+ tags :default
183
+ ...
184
+ end
185
+
186
+ To change the tag(s) provide them after the command command line. When not specified the tag(s) default to those provided in the default_tags
187
+ configuration attribute.
188
+
189
+ $ phil_columns seed defaults settings etc
190
+
191
+ #### Advanced Filtering
192
+
193
+ Currently, filtering is applied with the any operation, which is the default. In the future development the all operation will be added and negation
194
+ of tags. For example:
195
+
196
+ $ phil_columns seed defaults ~settings --all
197
+
198
+
199
+
200
+ ### Production Seeding
201
+
202
+ Systems often have system level data that must be seeded on initial startup and as new features or rolled out. Some examples are settings, configurations,
203
+ roles, licenses, etc.
204
+
205
+ Phil_columns provides the ability to apply these system data seedings and commit them with features, analgous to a Rails migration. The production seed file
206
+ can be specified to run only in production, or in production and development if the data makes sense for both.
207
+
208
+ To specify a seed for production, simply add production to the specified environments.
209
+
210
+ class AddThings < PhilColumns::Seed
211
+ envs :development, :production
212
+ ...
213
+ end
214
+
215
+
216
+ ### The Configuration File
217
+
218
+ The configuration file is in YAML format.
219
+
220
+ ---
221
+ default_tags:
222
+ - default
223
+ env_files:
224
+ - config/environment
225
+ schema_load_strategy: load
226
+ schema_unload_strategy: drop
227
+ seeds_path: db/seeds
228
+
229
+ #### Configuration Attributes
230
+
231
+ ##### default_tags
232
+
233
+ [Array] A list of tags to apply when no tags are supplied to the command. When empty, no tags are applied as default. When not empty,
234
+ one or more tags are applied as default.
235
+
236
+ ##### env_files
237
+
238
+ [Array] A list of environment files to require. Helps to set up environment before certain commands are executed. You can provide your own, or utilize
239
+ the environment file(s) from a framework, such as Rails.
240
+
241
+ ##### schema_load_strategy
242
+
243
+ [String] The load strategy to use in commands that load the schema (ie. mulligan). One of load or migrate.
244
+
245
+ ##### schema_unload_strategy
246
+
247
+ [String] The unload strategy to use in commands that unload the schema (ie. mulligan). One of drop or migrate.
248
+
249
+ ##### seeds_path
250
+
251
+ [String] The relative path to the seeds directory.
252
+
253
+
254
+ ## Adapters and Extensions
255
+
256
+ * [phil_columns-activerecord](https://github.com/midas/phil_columns-activerecord)
257
+ * [phil_columns-factory_girl](https://github.com/midas/phil_columns-factory_girl)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/phil_columns ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'phil_columns'
5
+
6
+ PhilColumns::Cli.start
@@ -0,0 +1,27 @@
1
+ require 'active_support/core_ext/string'
2
+ require 'phil_columns/version'
3
+ require 'phil_columns/railtie' if defined? Rails
4
+ require 'rainbow'
5
+ require 'pry-debugger'
6
+
7
+ module PhilColumns
8
+
9
+ autoload :Archivist, 'phil_columns/archivist'
10
+ autoload :Cli, 'phil_columns/cli'
11
+ autoload :Command, 'phil_columns/command'
12
+ autoload :Configuration, 'phil_columns/configuration'
13
+ autoload :Error, 'phil_columns/error'
14
+ autoload :Filter, 'phil_columns/filter'
15
+ autoload :Migrator, 'phil_columns/migrator'
16
+ autoload :Output, 'phil_columns/output'
17
+ autoload :Seed, 'phil_columns/seed'
18
+ autoload :SeedUtils, 'phil_columns/seed_utils'
19
+ autoload :Seeder, 'phil_columns/seeder'
20
+ autoload :WithBackend, 'phil_columns/with_backend'
21
+
22
+ class << self
23
+ attr_accessor :archivist_klass
24
+ attr_accessor :migrator_klass
25
+ end
26
+
27
+ end
@@ -0,0 +1,36 @@
1
+ module PhilColumns
2
+ class Archivist
3
+
4
+ include PhilColumns::WithBackend
5
+
6
+ def initialize
7
+ @backend = PhilColumns::archivist_klass.new
8
+ end
9
+
10
+ def clear_seeds
11
+ raise( *error ) unless backend_responds?( :clear_seeds )
12
+ backend.send :clear_seeds
13
+ end
14
+
15
+ def record_seed( version )
16
+ raise( *error ) unless backend_responds?( :record_seed )
17
+ backend.send :record_seed, version
18
+ end
19
+
20
+ def remove_seed( version )
21
+ raise( *error ) unless backend_responds?( :remove_seed )
22
+ backend.send :remove_seed, version
23
+ end
24
+
25
+ def seed_already_executed?( version )
26
+ raise( *error ) unless backend_responds?( :seed_already_executed? )
27
+ backend.send :seed_already_executed?, version
28
+ end
29
+
30
+ def ensure_schema_seeds_table!
31
+ raise( *error ) unless backend_responds?( :ensure_schema_seeds_table! )
32
+ backend.send :ensure_schema_seeds_table!
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,162 @@
1
+ require 'thor'
2
+
3
+ module PhilColumns
4
+ class Cli < Thor
5
+
6
+ autoload :Generate, 'phil_columns/cli/generate'
7
+ autoload :List, 'phil_columns/cli/list'
8
+
9
+ include Thor::Actions
10
+
11
+ def self.dry_run_option
12
+ option :dry_run, type: :boolean, desc: "When true, output steps but does not execute protected blocks"
13
+ end
14
+
15
+ def self.env_option
16
+ option :env, type: :string, aliases: '-e', desc: "The environment to execute in", default: 'development'
17
+ end
18
+
19
+ def self.env_option_description
20
+ %(When --env[-e] option, override the environment. Default: development.)
21
+ end
22
+
23
+ def self.operation_option
24
+ option :operation, type: :string, aliases: '-o', desc: "The operation: all or any", default: 'any'
25
+ end
26
+
27
+ def self.operation_option_description
28
+ %(When --operation[-o] option, override the operation to one of any or all. Default: any.)
29
+ end
30
+
31
+ def self.version_option
32
+ option :version, type: :string, aliases: '-v', desc: "The version to execute to", default: 'all'
33
+ end
34
+
35
+ def self.version_option_description
36
+ %(When --version[-v] option, override the version. Default: all. Provide the timestamp from the beginning of the seed file name
37
+ as the version parameter. When seeding up, the specified version is included in the seed set. When seeding down the specified
38
+ version is not included in the set.)
39
+ end
40
+
41
+ desc "empty", "Empty tables"
42
+ long_desc <<-LONGDESC
43
+ Empties all tables excluding any project metadata tables, ie. schema_migrations when using ActiveRecord.
44
+ LONGDESC
45
+ def empty( *tags )
46
+ execute PhilColumns::Command::Empty, tags: tags
47
+ end
48
+
49
+ desc 'generate SUBCOMMAND', "Generates different phil_columns assets"
50
+ subcommand "generate", PhilColumns::Cli::Generate
51
+
52
+ desc "install PATH", "Install phil_columns within a project"
53
+ long_desc <<-LONGDESC
54
+ Install phil_columns within a project at path PATH.
55
+
56
+ Installs a .phil_columns configuration file within PATH and a seeds directory at <PATH>/seeds by default.
57
+
58
+ When --rails[-r] option, install with defaults for a Rails project. This includes the configuring the seeds directory
59
+ to db/seeds and adding config/enironment to the list of env_files in the configuration file.
60
+
61
+ When --seeds-path[-p] option, configure the seeds directory specified path. This option is not necessary if --rails
62
+ option provided.
63
+ LONGDESC
64
+ option :rails, type: :boolean, aliases: '-r', desc: "When true, install with defaults for Rails"
65
+ option :seeds_path, type: :string, aliases: '-p', desc: "The path of the project to install within", default: './seeds'
66
+ def install( path='.' )
67
+ execute PhilColumns::Command::Install, path: path
68
+ end
69
+
70
+ desc 'list SUBCOMMAND', "List different seed info"
71
+ subcommand "list", PhilColumns::Cli::List
72
+
73
+ desc "mulligan [TAGS]", "Unload and load the schema then execute seeds"
74
+ long_desc <<-LONGDESC
75
+ A complete reset of the database. The tables are removed and rebuilt and the data is seeded. The strategy used to
76
+ unload and load the schema is controlled by the configuration file attributes schema_unload\_strategy and schema\_load_strategy. The mulligan
77
+ term is borrowed from golf where a mulligan is a do-over.
78
+
79
+ #{env_option_description}
80
+
81
+ #{operation_option_description}
82
+
83
+ When --schema-load-strategy[-l] option, override the schema load strategy to one of load or migrate. When load is specified loads
84
+ the schema. When migrate specified runs the migrations. Load is a more efficient operation. Defaults to the value specified in
85
+ the configuration file attribute schema_load_strategy.
86
+
87
+ When --schema-unload-strategy[-l] option, override the schema unload strategy to one of drop or migrate. When drop is specified drops
88
+ the tables. When migrate specified runs the migrations down. Drop is a more efficient operation. Defaults to the value specified in
89
+ the configuration file attribute schema_unload_strategy.
90
+
91
+ #{version_option_description}
92
+ LONGDESC
93
+ env_option
94
+ operation_option
95
+ option :schema_load_strategy, type: :string, aliases: '-l', desc: "The schema load strategy to use: load or migrate"
96
+ option :schema_unload_strategy, type: :string, aliases: '-u', desc: "The schema unload strategy to use: drop or migrate"
97
+ version_option
98
+ def mulligan( *tags )
99
+ execute PhilColumns::Command::Mulligan, tags: tags
100
+ end
101
+
102
+ desc "seed [TAGS]", "Execute the seeds"
103
+ long_desc <<-LONGDESC
104
+ Execute the seeds.
105
+
106
+ When --down[-d] option, execute the down seeds.
107
+
108
+ When --dry-run option, execute the seeds as a dry run.
109
+
110
+ #{env_option_description}
111
+
112
+ #{operation_option_description}
113
+
114
+ #{version_option_description}
115
+ LONGDESC
116
+ option :down, type: :boolean, aliases: '-d', desc: "When true, executes down seeding"
117
+ dry_run_option
118
+ env_option
119
+ operation_option
120
+ version_option
121
+ def seed( *tags )
122
+ execute PhilColumns::Command::Seed, tags: tags
123
+ end
124
+
125
+ no_commands do
126
+
127
+ def execute( klass, additional_options={} )
128
+ klass.new( options.merge( additional_options )).execute
129
+ rescue PhilColumns::Error => e
130
+ handle_error( e )
131
+ exit 1
132
+ end
133
+
134
+ def handle_error( e )
135
+ say "Error: #{e.message}", :red
136
+ end
137
+
138
+ end
139
+
140
+ def self.handle_argument_error( command, error, _, __ )
141
+ method = "handle_argument_error_for_#{command.name}"
142
+
143
+ if respond_to?( method )
144
+ send( method, command, error )
145
+ else
146
+ handle_argument_error_default( command, error )
147
+ end
148
+ end
149
+
150
+ def self.handle_argument_error_default( command, error )
151
+ $stdout.puts "Incorrect usage of command: #{command.name}"
152
+ $stdout.puts " #{error.message}", ''
153
+ $stdout.puts "For correct usage:"
154
+ $stdout.puts " phil_columns help #{command.name}"
155
+ end
156
+
157
+ def self.handle_no_command_error( name )
158
+ $stdout.puts "Unrecognized command: #{name}"
159
+ end
160
+
161
+ end
162
+ end