phil_columns 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fe00b297726a9a6754dafc9c15d20fabfb36627
4
- data.tar.gz: 8b0c79e034604555729459c063ecbb5fddca5918
3
+ metadata.gz: 8f2e5f91da147995e312530fb811dc3ff34871c3
4
+ data.tar.gz: a8ed23b4ce9daecc5365212c0abb315c3183a7ae
5
5
  SHA512:
6
- metadata.gz: bd496ad0f7a245035a35dba388fde3bec8e94c123000d9b469e94f8cba218dabce4b4da78d3f75b26ca68f5c049c74ba41b8a16a7a2dbe51db10a8b0054559fd
7
- data.tar.gz: e7ff8a3f14196ff16416c0e2257acca04122ba7f4e066a5c4112408226cc0d8e1ddd962cbd975d58684e364b57c2f221d145fff8d4751cdc7157eba6a7ce8a8e
6
+ metadata.gz: ee4bf29e129c603dd4b7e72c4d32832f785a682a2ad98bf4aa76654fefdb57aef7015acc70a350c0287961c826f74d6e158343005d02cc8f707a16b69e61a352
7
+ data.tar.gz: 03f90d08be6e33b587e49494ebf1959f5668cba01e2a17c3d72b10b5ac1a705db86a3f9a7e8919fd6e0fa21c2c47f32f7557d8fdeff98d3e5115ba55f9f6b728
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ ![PhilColumns: No Fixtures Required](https://raw.githubusercontent.com/midas/phil_columns/master/readme/PhilColumns.png)
2
+
1
3
  # PhilColumns
2
4
 
3
5
  A utility for seeding databases for development and production (works in Rails and non-Rails Ruby projects).
@@ -32,11 +34,11 @@ Or
32
34
 
33
35
  Installing phil_columns into a Ruby project.
34
36
 
35
- $ phil_columns install some/path
37
+ $ phil-columns install some/path
36
38
 
37
39
  Installing phil_columns into a Rails project.
38
40
 
39
- $ phil_columns install --rails
41
+ $ phil-columns install --rails
40
42
 
41
43
  The installation process puts in place the seeds directory and a configuration file: .phil_columns. The --rails switch
42
44
  simply overrides some configurations to allow phil_columns to work in a Rails project.
@@ -46,7 +48,7 @@ simply overrides some configurations to allow phil_columns to work in a Rails pr
46
48
 
47
49
  Use the generator to create a seed.
48
50
 
49
- $ phil_columns generate seed add_things
51
+ $ phil-columns generate seed add_things
50
52
 
51
53
  The generator puts a file in place. Add your seeding logic to the #up and #down methods using any valid Ruby code.
52
54
 
@@ -66,53 +68,53 @@ The generator puts a file in place. Add your seeding logic to the #up and #down
66
68
 
67
69
  Execute the seed(s).
68
70
 
69
- $ phil_columns seed
71
+ $ phil-columns seed
70
72
 
71
73
 
72
74
  ### The Command Line Interface
73
75
 
74
76
  Get a summary of commands.
75
77
 
76
- $ phil_columns help
78
+ $ phil-columns help
77
79
 
78
80
  Get help with a command.
79
81
 
80
- $ phil_columns help COMMAND
81
- $ phil_columns help seed
82
+ $ phil-columns help COMMAND
83
+ $ phil-columns help seed
82
84
 
83
85
  Some commands have sub-commands. Get help witha sub-command.
84
86
 
85
- $ phil_columns COMMAND help SUBCOMMAND
86
- $ phil_columns generate help seed
87
+ $ phil-columns COMMAND help SUBCOMMAND
88
+ $ phil-columns generate help seed
87
89
 
88
90
 
89
91
  ### The Seed Command
90
92
 
91
93
  The simplest usage of the seed command defaults the environment to 'development' and the version to 'all.'
92
94
 
93
- $ phil_columns seed
95
+ $ phil-columns seed
94
96
 
95
97
  The environment can be overridden using the command line. The environment is used to select only seeds that have been specified for the
96
98
  specified environment.
97
99
 
98
- $ phil_columns seed --env production
99
- $ phil_columns seed -e production
100
+ $ phil-columns seed --env production
101
+ $ phil-columns seed -e production
100
102
 
101
103
  The version can be overridden using the command line. When a version is specified the seeding occurs up to the specified version. When
102
104
  the seeding direction is up the version specified is included in the seeding set. When seeding direction is down the specified version is
103
105
  not included in the seed set.
104
106
 
105
- $ phil_columns seed --version 20140513111454
106
- $ phil_columns seed -v 20140513111454
107
+ $ phil-columns seed --version 20140513111454
108
+ $ phil-columns seed -v 20140513111454
107
109
 
108
110
  The direction of the seeding can be altered to down using the down switch.
109
111
 
110
- $ phil_columns seed --down
111
- $ phil_columns seed -d
112
+ $ phil-columns seed --down
113
+ $ phil-columns seed -d
112
114
 
113
115
  Finally, a dry run can be invoked using the dry-run switch.
114
116
 
115
- $ phil_columns seed --dry-run
117
+ $ phil-columns seed --dry-run
116
118
 
117
119
 
118
120
  ### The Mulligan Command
@@ -123,39 +125,40 @@ term is borrowed from golf where a mulligan is a do-over.
123
125
 
124
126
  The simplest usage of the mulligan command defaults the environment to 'development' and the version to 'all.'
125
127
 
126
- $ phil_columns mulligan
128
+ $ phil-columns mulligan
127
129
 
128
130
  The environment can be overridden using the command line. The environment is used to select only seeds that have been specified for the
129
131
  specified environment.
130
132
 
131
- $ phil_columns mulligan --env production
132
- $ phil_columns mulligan -e production
133
+ $ phil-columns mulligan --env production
134
+ $ phil-columns mulligan -e production
133
135
 
134
136
  The version can be overridden using the command line. When a version is specified the seeding occurs up to the specified version. When
135
137
  the seeding direction is up the version specified is included in the seeding set. When seeding direction is down the specified version is
136
138
  not included in the seed set.
137
139
 
138
- $ phil_columns mulligan --version 20140513111454
139
- $ phil_columns mulligan -v 20140513111454
140
+ $ phil-columns mulligan --version 20140513111454
141
+ $ phil-columns mulligan -v 20140513111454
140
142
 
141
143
 
142
- ### The Empty Command
144
+ ### The Purge Command
143
145
 
144
- The empty command empties all tables (excluding the schema_migrations table in a Rials project).
146
+ The purge command purges the data from all tables (excluding the schema\_migrations table in a Rails project). If skip_tables_on_purge configuration
147
+ is specified, those tables are also skipped unless the --skip option is applied to the command.
145
148
 
146
- The simplest usage of the empty command.
149
+ The simplest usage of the purge command.
147
150
 
148
- $ phil_columns empty
151
+ $ phil-columns purge
149
152
 
150
153
 
151
154
  ### Additional Commands
152
155
 
153
156
  For documentation on additional commands/sub-commands use the command line interface's built in help features.
154
157
 
155
- $ phil_columns help
156
- $ phil_columns help COMMAND
157
- $ phil_columns COMMAND help
158
- $ phil_columns COMMAND help SUBCOMMAND
158
+ $ phil-columns help
159
+ $ phil-columns help COMMAND
160
+ $ phil-columns COMMAND help
161
+ $ phil-columns COMMAND help SUBCOMMAND
159
162
 
160
163
 
161
164
  ### Tags and Environments
@@ -173,7 +176,7 @@ Specifying environment(s) for a seed is accomplished with the ::envs class metho
173
176
 
174
177
  To change the environment use the env switch. When not specified the env defaults to development
175
178
 
176
- $ phil_columns seed -e production
179
+ $ phil-columns seed -e production
177
180
 
178
181
  Similary, applying tag(s) is accomplished using the ::tags class method.
179
182
 
@@ -186,14 +189,40 @@ Similary, applying tag(s) is accomplished using the ::tags class method.
186
189
  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
190
  configuration attribute.
188
191
 
189
- $ phil_columns seed defaults settings etc
192
+ $ phil-columns seed defaults settings etc
193
+
194
+ ### Dependencies
195
+
196
+ A seeds dependence on one more more other seeds to execute before it can be specified. To do so you must pass the version string(s) to the ::depends_on
197
+ method.
198
+
199
+ class AddThings < PhilColumns::Seed
200
+ envs :development
201
+
202
+ depends_on 20140523170555
203
+ ...
204
+ end
205
+
206
+ The version string(s) can be cpecified as Fixnum, String or Symbol. For instance.
207
+
208
+ class AddThings < PhilColumns::Seed
209
+ envs :development
210
+
211
+ depends_on 20140523170555
212
+ depends_on '20120528182320'
213
+ depends_on %w(20120705124536 20120717203851)
214
+ ...
215
+ end
216
+
217
+ When the currently executing seed's dependencies are not satisfied an error is raised and seeding is abandoned, outputting a helpful message to the console.
218
+
190
219
 
191
220
  #### Advanced Filtering
192
221
 
193
222
  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
223
  of tags. For example:
195
224
 
196
- $ phil_columns seed defaults ~settings --all
225
+ $ phil-columns seed defaults ~settings --all
197
226
 
198
227
 
199
228
 
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
data/lib/phil_columns.rb CHANGED
@@ -14,6 +14,7 @@ module PhilColumns
14
14
  autoload :Filter, 'phil_columns/filter'
15
15
  autoload :Migrator, 'phil_columns/migrator'
16
16
  autoload :Output, 'phil_columns/output'
17
+ autoload :Purger, 'phil_columns/purger'
17
18
  autoload :Seed, 'phil_columns/seed'
18
19
  autoload :SeedUtils, 'phil_columns/seed_utils'
19
20
  autoload :Seeder, 'phil_columns/seeder'
@@ -5,9 +5,15 @@ module PhilColumns
5
5
 
6
6
  autoload :Generate, 'phil_columns/cli/generate'
7
7
  autoload :List, 'phil_columns/cli/list'
8
+ autoload :Reset, 'phil_columns/cli/reset'
8
9
 
9
10
  include Thor::Actions
10
11
 
12
+ def self.default_tags_explanation
13
+ %(If default_tags are specified in the config file and no tags are provided as parameters to the command, the default tags are applied
14
+ as the tags. However, if tags are provided as parameters they override the defult tags.)
15
+ end
16
+
11
17
  def self.dry_run_option
12
18
  option :dry_run, type: :boolean, desc: "When true, output steps but does not execute protected blocks"
13
19
  end
@@ -20,6 +26,15 @@ module PhilColumns
20
26
  %(When --env[-e] option, override the environment. Default: development.)
21
27
  end
22
28
 
29
+ def self.skip_option
30
+ option :skip, type: :boolean, desc: "When true, skip tables listed in config", default: true
31
+ end
32
+
33
+ def self.skip_on_purge_description
34
+ %(When --no-skip, override the skip_tables_on_purge configuration. Otherwise, the tables specified in the skip_tables_on_purge configuration
35
+ will be skipped.)
36
+ end
37
+
23
38
  def self.operation_option
24
39
  option :operation, type: :string, aliases: '-o', desc: "The operation: all or any", default: 'any'
25
40
  end
@@ -38,12 +53,15 @@ module PhilColumns
38
53
  version is not included in the set.)
39
54
  end
40
55
 
41
- desc "empty", "Empty tables"
56
+ desc "purge", "Purge all data from tables"
42
57
  long_desc <<-LONGDESC
43
- Empties all tables excluding any project metadata tables, ie. schema_migrations when using ActiveRecord.
58
+ Purges all tables excluding any project metadata tables, ie. schema_migrations when using ActiveRecord.
59
+
60
+ #{skip_on_purge_description}
44
61
  LONGDESC
45
- def empty( *tags )
46
- execute PhilColumns::Command::Empty, tags: tags
62
+ skip_option
63
+ def purge( *tags )
64
+ execute PhilColumns::Command::Purge, tags: tags
47
65
  end
48
66
 
49
67
  desc 'generate SUBCOMMAND', "Generates different phil_columns assets"
@@ -76,6 +94,8 @@ module PhilColumns
76
94
  unload and load the schema is controlled by the configuration file attributes schema_unload\_strategy and schema\_load_strategy. The mulligan
77
95
  term is borrowed from golf where a mulligan is a do-over.
78
96
 
97
+ #{default_tags_explanation}
98
+
79
99
  #{env_option_description}
80
100
 
81
101
  #{operation_option_description}
@@ -99,10 +119,15 @@ module PhilColumns
99
119
  execute PhilColumns::Command::Mulligan, tags: tags
100
120
  end
101
121
 
122
+ desc 'reset SUBCOMMAND', "Reset database data, schema, etc"
123
+ subcommand "reset", PhilColumns::Cli::Reset
124
+
102
125
  desc "seed [TAGS]", "Execute the seeds"
103
126
  long_desc <<-LONGDESC
104
127
  Execute the seeds.
105
128
 
129
+ #{default_tags_explanation}
130
+
106
131
  When --down[-d] option, execute the down seeds.
107
132
 
108
133
  When --dry-run option, execute the seeds as a dry run.
@@ -0,0 +1,108 @@
1
+ require 'thor'
2
+
3
+ module PhilColumns
4
+ class Cli
5
+ class Reset < Thor
6
+
7
+ def self.banner( command, namespace=nil, subcommand=false )
8
+ return "#{basename} reset help [SUBCOMMAND]" if command.name == 'help'
9
+ "#{basename} #{command.usage}"
10
+ end
11
+
12
+ def self.default_tags_explanation
13
+ %(If default_tags are specified in the config file and no tags are provided as parameters to the command, the default tags are applied
14
+ as the tags. However, if tags are provided as parameters they override the defult tags.)
15
+ end
16
+
17
+ def self.env_option
18
+ option :env, type: :string, aliases: '-e', desc: "The environment to execute in", default: 'development'
19
+ end
20
+
21
+ def self.env_option_description
22
+ %(When --env[-e] option, override the environment. Default: development.)
23
+ end
24
+
25
+ def self.operation_option
26
+ option :operation, type: :string, aliases: '-o', desc: "The operation: all or any", default: 'any'
27
+ end
28
+
29
+ def self.operation_option_description
30
+ %(When --operation[-o] option, override the operation to one of any or all. Default: any.)
31
+ end
32
+
33
+ def self.skip_option
34
+ option :skip, type: :boolean, desc: "When true, skip tables listed in config", default: true
35
+ end
36
+
37
+ def self.skip_on_purge_description
38
+ %(When --no-skip, override the skip_tables_on_purge configuration. Otherwise, the tables specified in the skip_tables_on_purge configuration
39
+ will be skipped.)
40
+ end
41
+
42
+ def self.version_option
43
+ option :version, type: :string, aliases: '-v', desc: "The version to execute to", default: 'all'
44
+ end
45
+
46
+ def self.version_option_description
47
+ %(When --version[-v] option, override the version. Default: all. Provide the timestamp from the beginning of the seed file name
48
+ as the version parameter. When seeding up, the specified version is included in the seed set. When seeding down the specified
49
+ version is not included in the set.)
50
+ end
51
+
52
+ desc "reset data [TAGS]", "Reset the data in the database"
53
+ long_desc <<-LONGDESC
54
+ Reset the data in the database. Empties the tables and then executes seeds.
55
+
56
+ #{default_tags_explanation}
57
+
58
+ #{env_option_description}
59
+
60
+ #{operation_option_description}
61
+
62
+ #{skip_on_purge_description}
63
+
64
+ #{version_option_description}
65
+ LONGDESC
66
+ env_option
67
+ operation_option
68
+ skip_option
69
+ version_option
70
+ def data( *tags )
71
+ PhilColumns::Command::Reset::Data.new( options.merge( tags: tags )).execute
72
+ end
73
+
74
+ desc "reset schema", "Reset the database schema"
75
+ long_desc <<-LONGDESC
76
+ Reset the database schema. Drops the tables and loads the schema using migrations.
77
+
78
+ This operation is useful when the structure of the database has change and a load operation will not
79
+ pick up the changes.
80
+ LONGDESC
81
+ def schema( *tags )
82
+ PhilColumns::Command::Reset::Schema.new( options ).execute
83
+ end
84
+
85
+ def self.handle_argument_error( command, error, _, __ )
86
+ method = "handle_argument_error_for_#{command.name}"
87
+
88
+ if respond_to?( method )
89
+ send( method, command, error )
90
+ else
91
+ handle_argument_error_default( command, error )
92
+ end
93
+ end
94
+
95
+ def self.handle_argument_error_default( command, error )
96
+ $stdout.puts "Incorrect usage of generate subcommand: #{command.name}"
97
+ $stdout.puts " #{error.message}", ''
98
+ $stdout.puts "For correct usage:"
99
+ $stdout.puts " phil_columns generate help #{command.name}"
100
+ end
101
+
102
+ def self.handle_no_command_error( name )
103
+ $stdout.puts "Unrecognized command: #{name}"
104
+ end
105
+
106
+ end
107
+ end
108
+ end
@@ -2,12 +2,13 @@ module PhilColumns
2
2
  module Command
3
3
 
4
4
  autoload :Base, 'phil_columns/command/base'
5
- autoload :Empty, 'phil_columns/command/empty'
6
5
  autoload :Generate, 'phil_columns/command/generate'
7
6
  autoload :Generator, 'phil_columns/command/generator'
8
7
  autoload :Install, 'phil_columns/command/install'
9
- autoload :Mulligan, 'phil_columns/command/mulligan'
10
8
  autoload :List, 'phil_columns/command/list'
9
+ autoload :Mulligan, 'phil_columns/command/mulligan'
10
+ autoload :Purge, 'phil_columns/command/purge'
11
+ autoload :Reset, 'phil_columns/command/reset'
11
12
  autoload :Seed, 'phil_columns/command/seed'
12
13
 
13
14
  end
@@ -2,7 +2,6 @@ module PhilColumns
2
2
  module Command
3
3
  class Base
4
4
 
5
- include Thor::Actions
6
5
  include PhilColumns::Output
7
6
 
8
7
  def initialize( options )
@@ -48,9 +47,8 @@ module PhilColumns
48
47
  def load_environment
49
48
  return if env_files.nil? || env_files.empty?
50
49
 
51
- say 'Loading environment ...'
52
-
53
50
  env_files.each do |file|
51
+ say "Loading environment: #{file} ..."
54
52
  require file.expand_path
55
53
  end
56
54
  end
@@ -9,12 +9,26 @@ module PhilColumns
9
9
 
10
10
  def execute
11
11
  say "Installing phil columns", :cyan
12
- write "Creating seeds directory: #{seeds_path} ... "
13
- make_seeds_directory
14
- say_ok
15
- write "Writing config file: #{config_file_path} ... "
16
- write_config_file
17
- say_ok
12
+
13
+ confirm "Creating seeds directory: #{seeds_path} ... " do
14
+ make_seeds_directory
15
+ end
16
+
17
+ begin
18
+ write "Writing config file: #{config_file_path} ... "
19
+ write_config_file
20
+ say_ok
21
+ rescue PhilColumns::Error
22
+ say_skipping
23
+ end
24
+
25
+ begin
26
+ write "Writing env file: #{env_file_path} ... "
27
+ write_env_file
28
+ say_ok
29
+ rescue PhilColumns::Error
30
+ say_skipping
31
+ end
18
32
  end
19
33
 
20
34
  protected
@@ -24,7 +38,24 @@ module PhilColumns
24
38
  end
25
39
 
26
40
  def write_config_file
27
- config.save_to_file
41
+ if file_collision( config.config_filepath )
42
+ config.save_to_file
43
+ return
44
+ end
45
+
46
+ raise PhilColumns::Error, "Config file #{config.config_filepath} already exists"
47
+ end
48
+
49
+ def write_env_file
50
+ if file_collision( env_file_path.expand_path )
51
+ File.open env_file_path.expand_path, 'w' do |f|
52
+ f.puts( '# Add any Phil Columns only configuration in this file' )
53
+ end
54
+
55
+ return
56
+ end
57
+
58
+ raise PhilColumns::Error, "Config file #{env_file_path.expand_path} already exists"
28
59
  end
29
60
 
30
61
  def config
@@ -40,6 +71,7 @@ module PhilColumns
40
71
  schema_load_strategy: 'load',
41
72
  schema_unload_strategy: 'drop',
42
73
  seeds_path: seeds_path.to_s,
74
+ skip_tables_on_purge: []
43
75
  )
44
76
  end
45
77
 
@@ -48,14 +80,19 @@ module PhilColumns
48
80
  Pathname.new( rel )
49
81
  end
50
82
 
83
+ def env_file_path
84
+ Pathname.new( 'config/phil_columns.rb' )
85
+ end
86
+
51
87
  def rails?
52
88
  options[:rails]
53
89
  end
54
90
 
55
91
  def rails_default_settings
56
92
  {
57
- 'env_files' => [
58
- 'config/environment'
93
+ env_files: [
94
+ 'config/environment',
95
+ 'config/phil_columns'
59
96
  ]
60
97
  }
61
98
  end
@@ -25,8 +25,6 @@ module PhilColumns
25
25
 
26
26
  protected
27
27
 
28
- attr_reader :tags
29
-
30
28
  def tags
31
29
  @tags ||= []
32
30
  end
@@ -0,0 +1,18 @@
1
+ module PhilColumns
2
+ module Command
3
+ class Purge < Base
4
+
5
+ def execute
6
+ load_environment
7
+ purger.purge
8
+ end
9
+
10
+ protected
11
+
12
+ def purger
13
+ @purger ||= PhilColumns::Purger.new( config )
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module PhilColumns
2
+ module Command
3
+ module Reset
4
+
5
+ autoload :Data, 'phil_columns/command/reset/data'
6
+ autoload :Schema, 'phil_columns/command/reset/schema'
7
+
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ require 'pathname'
2
+
3
+ module PhilColumns
4
+ module Command
5
+ module Reset
6
+ class Data < Base
7
+
8
+ def execute
9
+ load_environment
10
+ purger.purge
11
+ seeder.execute
12
+ end
13
+
14
+ protected
15
+
16
+ def purger
17
+ @purger ||= PhilColumns::Purger.new( config )
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'pathname'
2
+
3
+ module PhilColumns
4
+ module Command
5
+ module Reset
6
+ class Schema < Base
7
+
8
+ def execute
9
+ load_environment
10
+ migrator.mulligan
11
+ archivist.clear_seeds
12
+ end
13
+
14
+ protected
15
+
16
+ def migrator
17
+ @migrator ||= PhilColumns::Migrator.new( config.merge( schema_load_strategy: 'migrate' ))
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -10,14 +10,7 @@ module PhilColumns
10
10
  say( "- DRY RUN -", :yellow ) if config.dry_run
11
11
  say "Seeding #{method_name} to version #{config.version}", :green
12
12
 
13
- seeds.each do |seed_meta|
14
- say ''
15
- say "* Executing seed: #{seed_meta.filepath}", :cyan
16
-
17
- instance = seed_meta.klass.new( config )
18
- instance.send( method_name )
19
- record_seed( seed_meta )
20
- end
13
+ seeder.execute
21
14
  end
22
15
 
23
16
  protected
@@ -81,6 +81,7 @@ module PhilColumns
81
81
  schema_load_strategy
82
82
  schema_unload_strategy
83
83
  seeds_path
84
+ skip_tables_on_purge
84
85
  )
85
86
  end
86
87
 
@@ -50,14 +50,6 @@ module PhilColumns
50
50
  end
51
51
  end
52
52
 
53
- def seed_already_executed?( version )
54
- archivist.seed_already_executed?( version )
55
- end
56
-
57
- def archivist
58
- @archivist ||= PhilColumns::Archivist.new
59
- end
60
-
61
53
  def any_exclusion_tags?
62
54
  config.exclusion_tags.size > 0
63
55
  end
@@ -1,6 +1,10 @@
1
1
  module PhilColumns
2
2
  module Output
3
3
 
4
+ def self.included( base )
5
+ base.send( :include, Thor::Shell )
6
+ end
7
+
4
8
  def write( msg, color=:white )
5
9
  $stdout.write( Rainbow( msg ).color( color ))
6
10
  end
@@ -17,6 +21,10 @@ module PhilColumns
17
21
  say 'ERROR', :red
18
22
  end
19
23
 
24
+ def say_skipping
25
+ say 'SKIPPING', :yellow
26
+ end
27
+
20
28
  def confirm( msg, color=:white, &block )
21
29
  write msg, color
22
30
  block.call
@@ -0,0 +1,44 @@
1
+ module PhilColumns
2
+ class Purger
3
+
4
+ include PhilColumns::Output
5
+
6
+ def initialize( config )
7
+ @config = config
8
+ end
9
+
10
+ def purge
11
+ tables.each do |table|
12
+ if config.skip &&
13
+ config.skip_tables_on_purge.include?( table )
14
+ say "Skipping #{table}", :yellow
15
+ next
16
+ end
17
+
18
+ confirm "Purging #{table} ... " do
19
+ klass = table.classify.constantize
20
+ klass.delete_all
21
+ end
22
+ end
23
+
24
+ archivist.clear_seeds
25
+ end
26
+
27
+ protected
28
+
29
+ attr_reader :config
30
+
31
+ def tables
32
+ migrator.tables.sort
33
+ end
34
+
35
+ def archivist
36
+ @archivist ||= PhilColumns::Archivist.new
37
+ end
38
+
39
+ def migrator
40
+ @migrator ||= PhilColumns::Migrator.new( config )
41
+ end
42
+
43
+ end
44
+ end
@@ -1,20 +1,24 @@
1
1
  module PhilColumns
2
2
  class Seed
3
3
 
4
- def self.envs( *envs )
5
- @_envs = envs.sort.map( &:to_s )
4
+ def self.depends_on( *depends_on )
5
+ self._depends_on += depends_on.sort.map( &:to_s )
6
6
  end
7
7
 
8
- def self.tags( *tags )
9
- @_tags = tags.sort.map( &:to_s )
8
+ def self.env( *envs )
9
+ self.envs( *envs )
10
10
  end
11
11
 
12
- def self._envs
13
- @_envs || []
12
+ def self.envs( *envs )
13
+ self._envs += envs.sort.map( &:to_s )
14
14
  end
15
15
 
16
- def self._tags
17
- @_tags || []
16
+ def self.tag( *tags )
17
+ self.tags( *tags )
18
+ end
19
+
20
+ def self.tags( *tags )
21
+ self._tags += tags.sort.map( &:to_s )
18
22
  end
19
23
 
20
24
  def initialize( config )
@@ -33,5 +37,29 @@ module PhilColumns
33
37
  block.call unless dry_run?
34
38
  end
35
39
 
40
+ def self._envs
41
+ @_envs || []
42
+ end
43
+
44
+ def self._tags
45
+ @_tags || []
46
+ end
47
+
48
+ def self._depends_on
49
+ @_depends_on || []
50
+ end
51
+
52
+ def self._envs=( val )
53
+ @_envs = val
54
+ end
55
+
56
+ def self._tags=( val )
57
+ @_tags = val
58
+ end
59
+
60
+ def self._depends_on=( val )
61
+ @_depends_on = val
62
+ end
63
+
36
64
  end
37
65
  end
@@ -16,13 +16,14 @@ module PhilColumns
16
16
  Hashie::Mash.new( envs: klass._envs,
17
17
  filepath: seed_filepath,
18
18
  klass: klass,
19
+ name: File.basename( seed_filepath, '.rb' ),
19
20
  tags: klass._tags,
20
21
  timestamp: discover_seed_timestamp( seed_filepath ))
21
22
  end
22
23
  end
23
24
 
24
25
  def seed_filepaths
25
- seeds = Dir.glob( "#{seeds_path}/*" )
26
+ seeds = Dir.glob( "#{seeds_path}/**/*.rb" )
26
27
  if config.down
27
28
  seeds.sort { |a,b| b <=> a }
28
29
  else
@@ -55,5 +56,13 @@ module PhilColumns
55
56
  snakecased.camelize.constantize
56
57
  end
57
58
 
59
+ def seed_already_executed?( version )
60
+ archivist.seed_already_executed?( version )
61
+ end
62
+
63
+ def archivist
64
+ @archivist ||= PhilColumns::Archivist.new
65
+ end
66
+
58
67
  end
59
68
  end
@@ -2,6 +2,7 @@ module PhilColumns
2
2
  class Seeder
3
3
 
4
4
  include PhilColumns::Output
5
+ include PhilColumns::SeedUtils
5
6
 
6
7
  def initialize( config )
7
8
  @config = config
@@ -9,7 +10,11 @@ module PhilColumns
9
10
 
10
11
  def execute
11
12
  seeds.each do |seed_meta|
12
- confirm "Executing seed: #{clean_filepath seed_meta.filepath} ... ", :cyan do
13
+ confirm "Executing seed: #{seed_meta.name} ... ", :cyan do
14
+ unless dependencies_satisfied?( seed_meta.klass.depends_on )
15
+ raise PhilColumns::Error, failed_dependencies_message( seed_meta )
16
+ end
17
+
13
18
  instance = seed_meta.klass.new( config )
14
19
  instance.send( method_name )
15
20
  record_seed( seed_meta )
@@ -19,7 +24,8 @@ module PhilColumns
19
24
 
20
25
  protected
21
26
 
22
- attr_reader :config
27
+ attr_reader :config,
28
+ :unsatisfieds
23
29
 
24
30
  def record_seed( seed_meta )
25
31
  if method_name == :up
@@ -29,12 +35,9 @@ module PhilColumns
29
35
  end
30
36
  end
31
37
 
32
- def clean_filepath( filepath )
33
- File.basename( filepath, '.rb' )
34
- end
35
-
36
- def archivist
37
- @archivist ||= PhilColumns::Archivist.new
38
+ def dependencies_satisfied?( dependencies )
39
+ @unsatisfieds = dependencies.select { |version| !seed_already_executed?( version ) }
40
+ unsatisfieds.blank?
38
41
  end
39
42
 
40
43
  def seeds
@@ -54,5 +57,10 @@ module PhilColumns
54
57
  config.down
55
58
  end
56
59
 
60
+ def failed_dependencies_message( seed_meta )
61
+ "Cannot execute seed #{seed_meta.name} due to unsatisfied dependencies: #{unsatisfieds.join ', '}. Please adjust " +
62
+ "the specified tags (#{config.tags.join ', '}) to resolve the failed dependencies."
63
+ end
64
+
57
65
  end
58
66
  end
@@ -1,3 +1,3 @@
1
1
  module PhilColumns
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phil_columns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JC. ason Harrelson (midas)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-28 00:00:00.000000000 Z
11
+ date: 2014-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -127,6 +127,7 @@ description: A utility for seeding databases for development and production. Se
127
127
  email:
128
128
  - jason@lookforwardenterprises.com
129
129
  executables:
130
+ - phil-columns
130
131
  - phil_columns
131
132
  extensions: []
132
133
  extra_rdoc_files: []
@@ -140,15 +141,16 @@ files:
140
141
  - LICENSE.txt
141
142
  - README.md
142
143
  - Rakefile
144
+ - bin/phil-columns
143
145
  - bin/phil_columns
144
146
  - lib/phil_columns.rb
145
147
  - lib/phil_columns/archivist.rb
146
148
  - lib/phil_columns/cli.rb
147
149
  - lib/phil_columns/cli/generate.rb
148
150
  - lib/phil_columns/cli/list.rb
151
+ - lib/phil_columns/cli/reset.rb
149
152
  - lib/phil_columns/command.rb
150
153
  - lib/phil_columns/command/base.rb
151
- - lib/phil_columns/command/empty.rb
152
154
  - lib/phil_columns/command/generate.rb
153
155
  - lib/phil_columns/command/generate/seed.rb
154
156
  - lib/phil_columns/command/generator.rb
@@ -157,12 +159,17 @@ files:
157
159
  - lib/phil_columns/command/list/tagged_with.rb
158
160
  - lib/phil_columns/command/list/tags.rb
159
161
  - lib/phil_columns/command/mulligan.rb
162
+ - lib/phil_columns/command/purge.rb
163
+ - lib/phil_columns/command/reset.rb
164
+ - lib/phil_columns/command/reset/data.rb
165
+ - lib/phil_columns/command/reset/schema.rb
160
166
  - lib/phil_columns/command/seed.rb
161
167
  - lib/phil_columns/configuration.rb
162
168
  - lib/phil_columns/error.rb
163
169
  - lib/phil_columns/filter.rb
164
170
  - lib/phil_columns/migrator.rb
165
171
  - lib/phil_columns/output.rb
172
+ - lib/phil_columns/purger.rb
166
173
  - lib/phil_columns/railtie.rb
167
174
  - lib/phil_columns/seed.rb
168
175
  - lib/phil_columns/seed_utils.rb
@@ -170,6 +177,7 @@ files:
170
177
  - lib/phil_columns/version.rb
171
178
  - lib/phil_columns/with_backend.rb
172
179
  - phil_columns.gemspec
180
+ - readme/PhilColumns.png
173
181
  - spec/spec_helper.rb
174
182
  - templates/seed_class.erb
175
183
  homepage: https://github.com/midas/phil_columns
@@ -1,30 +0,0 @@
1
- module PhilColumns
2
- module Command
3
- class Empty < Base
4
-
5
- def execute
6
- load_environment
7
-
8
- table_classes.each do |klass|
9
- confirm "Deleting from #{klass.name.tableize} ... " do
10
- klass.delete_all
11
- end
12
- end
13
-
14
- archivist.clear_seeds
15
- migrator.clear_migrations_table
16
- end
17
-
18
- protected
19
-
20
- def table_classes
21
- tables.map { |t| t.classify.constantize }
22
- end
23
-
24
- def tables
25
- migrator.tables.sort
26
- end
27
-
28
- end
29
- end
30
- end