multi_ar 1.0.0.pre.rc1 → 1.0.0.pre.rc2

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: 8bb5b123951c55685e13a738e7cfbf9f12a60c01
4
- data.tar.gz: 9e91ce2afc46d52e33ea4f2c7e735307d978f609
3
+ metadata.gz: d54d942aefe519b7959ea7d8207c03b304c437f2
4
+ data.tar.gz: abf83b48064b3e378000ff19b05642c7986919ee
5
5
  SHA512:
6
- metadata.gz: 6c685d03c1cbe001b957c946b97513f940957c8399a293d82fc1a9fcdcff8bbb2084c1ee59cf872fe324764bfba4af1871541692022224805c105892e6bed37d
7
- data.tar.gz: 3e70852de5be0497a6de35a8cfddb9708b1e633a63409f3af42d537e2e2e34b4588ff4a8a1c88873b33af6ecfe8af7b34f08592db61a83bdf655d21456a3752a
6
+ metadata.gz: 7d4b9241137a3c3bc78f0b9c0c8b04fa13701ef08250596c5fb1b54a95063049fa950c17298ecc64f92afc81bdef7fcbe9a69108b1dac8b0f7986404b500b756
7
+ data.tar.gz: ea38c3fb2c44b79041ff14e94988e1c7494a57091a7f5bf3fbedf74ebd9ae62dffbe975b8e5ccee222d11a301ce05df973fbfbdfe679d937dc6cd7423e0617c5
checksums.yaml.gz.sig CHANGED
Binary file
data/bin/multi_ar CHANGED
@@ -1,65 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "trollop"
3
+ require_relative "../lib/multi_ar/interface"
4
4
 
5
- require_relative "../lib/multi_ar"
6
5
  require_relative "../lib/multi_ar/version"
7
- require_relative "../lib/multi_ar/helper/rake_ext"
8
6
 
9
- # configuration
10
-
11
- p = Trollop::Parser.new do
12
- version "multi_ar-#{MultiAR::VERSION}"
13
- banner <<-EOS
14
- Helper utility to executing Rake tasks for ActiveRecord migrations.
15
- EOS
16
- opt "all_rake_tasks", "List all Rake tasks, not only commented ones", short: "A", type: :flag
17
- opt "common_migrations", "Run the migrations bundled with the gem, for common databases.", type: :flag, default: true
18
- opt "config", "Location to configuration file of MultiAR.", type: :string, default: "config/multi_ar.yaml"
19
- opt "databases", "List of databases to perform operations", type: :strings
20
- opt "db_config", "Path to database config file", type: :string, default: "config/database.yaml"
21
- opt "environment", "The environment to use. Corresponds to database config name " +
22
- "(environment for foo_development is “development”).", type: :string, default: "development"
23
- # TODO: not implemented currently, do we really need this?
24
- #opt "list_databases", "Lists databases that contains migrations in the gem", type: :flag
25
- opt "migration_dir", "The directory where migrations for databases are read from", type: :string, default: "db/migrate"
26
- opt "task", "Rake task to execute", type: :string
27
- opt "tasks", "List available Rake tasks", short: "T", type: :flag
28
- opt "verbose", "Be more verbose", type: :flag
29
- end
30
-
31
- opts = Trollop::with_standard_exception_handling p do
32
-
33
- args = ARGV.clone
34
-
35
- # and then reparse with arguments from the config file (here so that Trollop can erase ARGV)
36
- result = p.parse ARGV
37
-
38
- # show help screen
39
- raise Trollop::HelpNeeded if args.empty? # need to be different what we parse, as parser will remove used arguments
40
-
41
- result
42
- end
43
-
44
- # running the task
45
-
46
- multi_ar = MultiAR::MultiAR.new common_migrations: opts["common_migrations"],
47
- config: opts["config"],
48
- databases: opts["databases"],
49
- db_config: opts["db_config"],
50
- environment: opts["environment"],
51
- migration_dir: opts["migration_dir"]
52
-
53
- if opts["tasks"] || opts["all_rake_tasks"]
54
- multi_ar.list_tasks all_rake_tasks: opts["all_rake_tasks"]
55
- exit 1
56
- end
57
-
58
- if opts["task"].nil?
59
- puts "Task must be specified. Check if you passed --task option."
60
- exit 1
61
- end
62
- puts "Running task #{opts["task"]}" if opts["verbose"]
63
- multi_ar.rake_task opts["task"]
64
-
65
- #rake.top_level
7
+ interface = MultiAR::Interface.new
8
+ interface.version = "multi_ar-#{MultiAR::VERSION}"
9
+ interface.description = "Multi database migration tools utilizing ActiveRecord"
10
+ interface.migration_framework = true
11
+ opts = interface.cli do |parser|
12
+ # Nya.
13
+ end
@@ -38,6 +38,9 @@ module MultiAR
38
38
  # Array of databases that will be used insted if none have not been passed through CLI.
39
39
  attr_accessor :databases
40
40
 
41
+ # If set to true, migration framework and other Rake related functionality will be enabled.
42
+ attr_accessor :migration_framework
43
+
41
44
  # Boolean of whether no arguments are needed
42
45
  attr_accessor :run_by_default
43
46
 
@@ -64,13 +67,25 @@ module MultiAR
64
67
  p.version @version if @version
65
68
  p.banner @description if @description
66
69
  p.opt "init", "Create stub environment with configuration and database.yaml", type: :string
67
- p.opt "databases", "Databases that will be enabled", type: :strings if @options["databases"]
70
+ p.opt "databases", "List of databases to perform operations", type: :strings if @options["databases"]
68
71
  p.opt "db_config", "Path to database config file", type: :string, default: "config/database.yaml" if @options["db_config"]
69
- p.opt "config", "Path to config file", type: :string, default: "config/settings.yaml" if @options["config"]
72
+ p.opt "config", "Path to MultiAR framework config file", type: :string, default: "config/settings.yaml" if @options["config"]
70
73
  p.opt "dry", "Run the program without doing anything. Useful for debugging with -v", type: :flag if @options["dry"]
71
- p.opt "environment", "Environment to run the alarms for", type: :string, default: "development"
74
+ p.opt "environment", "The environment to use. Corresponds to database config name " +
75
+ "(environment for foo_development is “development”).", type: :string, default: "development"
72
76
  p.opt "verbose", "Be verbose", type: :flag if @options["verbose"]
73
77
 
78
+ if @migration_framework == true
79
+ p.opt "all_rake_tasks", "List all Rake tasks, not only commented ones", short: "A", type: :flag
80
+ # TODO: what for this is...?
81
+ #p.opt "common_migrations", "Run the migrations bundled with the gem, for common databases.", type: :flag, default: true
82
+ # TODO: not implemented currently, do we really need this?
83
+ #p.opt "list_databases", "Lists databases that contains migrations in the gem", type: :flag
84
+ p.opt "migration_dir", "The directory where migrations for databases are read from", type: :string, default: "db/migrate"
85
+ p.opt "task", "Rake task to execute", short: "t", type: :string
86
+ p.opt "tasks", "List available Rake tasks", short: "T", type: :flag
87
+ end
88
+
74
89
  yield p if block_given?
75
90
 
76
91
  opts = Trollop::with_standard_exception_handling p do
@@ -85,16 +100,44 @@ module MultiAR
85
100
  result
86
101
  end
87
102
 
88
- bootstrap opts if opts["init"]
103
+ bootstrap opts if opts["init"] # Bootstrap will exit after execution; in that case nothing after this will be run.
89
104
 
90
105
  raise "--config must be path to valid file" if @options["config"] and not File.exist? opts["config"]
91
106
  raise "config/database.yaml seems to be missing" if @options["db_config"] and not File.exist? opts["db_config"]
92
107
 
93
108
  @opts = opts
109
+
110
+ init_multi_ar
111
+
112
+ # Then run Rake tasks as requested
113
+
114
+ return if not @migration_framework # TODO: I think there should be much more fine grained control for this
115
+
116
+ if opts["tasks"] || opts["all_rake_tasks"]
117
+ @multi_ar.list_tasks all_rake_tasks: opts["all_rake_tasks"]
118
+ exit 1
119
+ end
120
+
121
+ if opts["task"].nil?
122
+ puts "Task must be specified. Check if you passed --task option."
123
+ exit 1
124
+ end
125
+
126
+ puts "Running task #{opts["task"]}" if opts["verbose"]
127
+ @multi_ar.rake_task opts["task"]
94
128
  end
95
129
 
96
130
  private
97
131
 
132
+ def init_multi_ar
133
+ @multi_ar = MultiAR::MultiAR.new common_migrations: @opts["common_migrations"],
134
+ config: @opts["config"],
135
+ databases: @opts["databases"],
136
+ db_config: @opts["db_config"],
137
+ environment: @opts["environment"],
138
+ migration_dir: @opts["migration_dir"]
139
+ end
140
+
98
141
  # @note This method will always quit the application or raise another exception for errors. Catch SystemExit if that’s not good for you.
99
142
  def bootstrap opts
100
143
  opts["databases"] ||= @databases
@@ -1,6 +1,6 @@
1
1
  require "active_record"
2
2
 
3
- require_relative "multi_ar"
3
+ require_relative "../multi_ar"
4
4
 
5
5
  module MultiAR
6
6
 
@@ -9,9 +9,9 @@ require "active_record/tasks/database_tasks"
9
9
 
10
10
  require_relative "migration_generator"
11
11
 
12
- module MultiAR::Rake
12
+ module Rake
13
13
 
14
- # Utility that defines Rake tasks of Slam
14
+ # Utility that defines Rake tasks of MultiAR
15
15
  class Tasks
16
16
  #include Rake::DSL
17
17
 
@@ -24,17 +24,17 @@ module MultiAR::Rake
24
24
  unless name.kind_of?(String) || name.nil?
25
25
  raise ArgumentError, "Expected a String or Symbol for a namespace name"
26
26
  end
27
- Rake.application.in_namespace(name, &block)
27
+ ::Rake.application.in_namespace(name, &block)
28
28
  end
29
29
 
30
30
  # Defines a Rake task’s description (see Rake’s desc() in dsl_definition.rb)
31
31
  def self.desc(description)
32
- Rake.application.last_description = description
32
+ ::Rake.application.last_description = description
33
33
  end
34
34
 
35
35
  # Defines a Rake task (see Rake’s task() in dsl_definition.rb)
36
36
  def self.task(*args, &block)
37
- Rake::Task.define_task(*args, &block)
37
+ ::Rake::Task.define_task(*args, &block)
38
38
  end
39
39
  end
40
40
 
@@ -42,10 +42,9 @@ module MultiAR::Rake
42
42
  attr_accessor :databases
43
43
  attr_accessor :environment
44
44
  attr_accessor :common_migrations
45
- attr_accessor :migration_dir
46
45
  end
47
46
 
48
- # When called, this declares Rake tasks of Slam project,
47
+ # When called, this declares Rake tasks of MultiAR,
49
48
  # most notably custom active record migration tasks.
50
49
  def self.define
51
50
  load "active_record/railties/databases.rake"
@@ -84,14 +83,11 @@ module MultiAR::Rake
84
83
  multiple_databases_task "migrate", "db" do |database_name|
85
84
  establish_connection database_name
86
85
 
87
- if common_migrations
88
- # TODO: Slam.root
89
- path = "#{__dir__}/../../../db/migrate/#{database_name}"
86
+ MultiAR::MultiAR.migration_dirs.each do |dir|
87
+ path = "#{dir}/#{database_name}/"
88
+ # The database should be present only on one migration dir, so this will fail if there is more than one migration dir
90
89
  ActiveRecord::Migrator.migrate path if Dir.exist? path
91
90
  end
92
-
93
- path = "#{migration_dir}/#{database_name}/"
94
- ActiveRecord::Migrator.migrate path if Dir.exist? path
95
91
  end
96
92
 
97
93
  multiple_databases_task "create", "db" do |database_name|
@@ -102,8 +98,11 @@ module MultiAR::Rake
102
98
  multiple_databases_task "rollback", "db" do |database_name|
103
99
  establish_connection database_name
104
100
  step = ENV['STEP'] ? ENV['STEP'].to_i : 1
105
- path = "#{migration_dir}/#{database_name}/"
106
- ActiveRecord::Migrator.rollback(path, step) if Dir.exist? path
101
+ MultiAR::MultiAR.migration_dirs.each do |dir|
102
+ path = "#{dir}/#{database_name}/"
103
+ # The database should be present only on one migration dir, so this will fail if there is more than one migration dir
104
+ ActiveRecord::Migrator.rollback(path, step) if Dir.exist? path
105
+ end
107
106
  end
108
107
 
109
108
  multiple_databases_task "drop", "db" do |database_name|
@@ -117,9 +116,9 @@ module MultiAR::Rake
117
116
  def self.rename_task name, namespace = nil
118
117
  new_name = "old_#{name}"
119
118
  #new_name = "#{namespace}:#{new_name}" unless namespace.nil?
120
- Rake::Task[name].rename(new_name)
121
- old_comment = Rake::Task[new_name].comment
122
- Rake::Task[new_name].clear_comments
119
+ ::Rake::Task[name].rename(new_name)
120
+ old_comment = ::Rake::Task[new_name].comment
121
+ ::Rake::Task[new_name].clear_comments
123
122
  old_comment
124
123
  end
125
124
 
@@ -130,7 +129,7 @@ module MultiAR::Rake
130
129
  DSL.desc "Runs task #{name} for all selected databases"
131
130
  DSL.task name.to_sym do
132
131
  databases.each do |database_name|
133
- Rake::Task["#{name}:#{database_name}"].invoke
132
+ ::Rake::Task["#{name}:#{database_name}"].invoke
134
133
  end
135
134
  end
136
135
 
@@ -1,4 +1,4 @@
1
1
 
2
2
  module MultiAR
3
- VERSION = "1.0.0-rc1"
3
+ VERSION = "1.0.0-rc2"
4
4
  end
data/lib/multi_ar.rb CHANGED
@@ -7,15 +7,17 @@ require_relative "multi_ar/database"
7
7
 
8
8
  module MultiAR
9
9
 
10
- # Base of Slam gem.
10
+ # Base of MultiAR gem.
11
11
  #
12
- # Must be initialized before most actions works, that relies on Slam#app for getting configuration.
12
+ # Must be initialized before most actions works, that relies on MultiAR#app for getting configuration.
13
13
  class MultiAR
14
14
 
15
15
  attr_reader :databases
16
16
  attr_reader :db_config
17
17
  attr_reader :environment
18
18
 
19
+ @@migration_dirs = []
20
+
19
21
  class << self
20
22
  # Instance of MultiAR::MultiAR, automatically assigned by MultiAR::MultiAR#new.
21
23
  # Used internally in the gem, to access configuration and other internal parts.
@@ -24,7 +26,7 @@ module MultiAR
24
26
 
25
27
  # @param databases array of available databases
26
28
  # @todo config file is overriding parameters passed here... I think it should be other way around, but need more custom logic for that :/
27
- def initialize databases:, environment: "development", config: "config/multi_ar.yaml", db_config: "config/database.yaml", common_migrations: true, migration_dir: "db/migrate"
29
+ def initialize databases:, environment: "development", config: "config/multi_ar.yaml", db_config: "config/database.yaml", common_migrations: true
28
30
 
29
31
  # first load config
30
32
  if File.exist? config
@@ -44,25 +46,44 @@ module MultiAR
44
46
  @db_config = db_config
45
47
  @environment = environment
46
48
 
47
- Database.initialize db_config: db_config, migration_dir: migration_dir
49
+ Database.initialize db_config: db_config
48
50
 
49
51
  ActiveRecord::Tasks::DatabaseTasks.class_eval { attr_accessor :sub_db_dir }
50
52
  ActiveRecord::Tasks::DatabaseTasks.sub_db_dir = databases.first # TODO: I don’t think this is how it should work
51
53
 
52
- @rake = Rake::Application.new
53
- Rake.application = @rake
54
+ @rake = ::Rake::Application.new
55
+ ::Rake.application = @rake
54
56
  @rake.init
55
- Rake::TaskManager.record_task_metadata = true
57
+ ::Rake::TaskManager.record_task_metadata = true
56
58
 
57
- RakeTasks.databases = databases
58
- RakeTasks.environment = environment
59
- RakeTasks.common_migrations = common_migrations
60
- RakeTasks.migration_dir = migration_dir
61
- RakeTasks.define
59
+ Rake::Tasks.databases = databases
60
+ Rake::Tasks.environment = environment
61
+ Rake::Tasks.common_migrations = common_migrations
62
+ Rake::Tasks.define
62
63
 
63
64
  MultiAR.app = self
64
65
  end
65
66
 
67
+ # Array of paths to directories where migrations resides.
68
+ # @see add_migration_dir
69
+ def self.migration_dirs
70
+ return @@migration_dirs
71
+ end
72
+
73
+ # Add a path to a directory where migrations resides. For standard Rails setup, this would be “db/migrate”.
74
+ #
75
+ # The directory structure of how MultiAR uses the path is a bit different from traditional way: for each
76
+ # database, there is directory inside the migration dir.
77
+ #
78
+ # For example, if project uses database named “messy_database” and migration dir is “my/migration/dir”,
79
+ # migrations would be looked from path “my/migration/dir/messy_database”.
80
+ #
81
+ # @note often you want to add full path to this dir, `__dir__` is useful for this.
82
+ def self.add_migration_dir path
83
+ raise "Migration dir #{path} does not exist." unless Dir.exist? path
84
+ @@migration_dirs << path
85
+ end
86
+
66
87
  # @todo this shows rake in start of the command, we want to show multi_ar instead.
67
88
  def list_tasks all_rake_tasks: false
68
89
  @rake.options.show_all_tasks = true if all_rake_tasks
@@ -71,6 +92,7 @@ module MultiAR
71
92
  @rake.display_tasks_and_comments
72
93
  end
73
94
 
95
+ # Invokes Rake task from `task_name`
74
96
  def rake_task task_name
75
97
  @rake.invoke_task task_name
76
98
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_ar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.rc1
4
+ version: 1.0.0.pre.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samu Voutilainen
@@ -29,7 +29,7 @@ cert_chain:
29
29
  Ws73WZ+F+DOnfopIMUaeYUzKyUG+59p17ls//xzTouiLjcC9ee5ZaC9e+TzYsp4t
30
30
  ofnwRrqEHVW+zRWs/VIwm70F2Jnl5eSc3MjyhpWIC0eGFXtBY1XYCrHeCdQ=
31
31
  -----END CERTIFICATE-----
32
- date: 2015-10-01 00:00:00.000000000 Z
32
+ date: 2015-10-06 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: trollop
@@ -141,16 +141,14 @@ description: |-
141
141
  email: smar@smar.fi
142
142
  executables:
143
143
  - multi_ar
144
- - mar
145
144
  extensions: []
146
145
  extra_rdoc_files: []
147
146
  files:
148
- - bin/mar
149
147
  - bin/multi_ar
150
148
  - lib/multi_ar.rb
151
149
  - lib/multi_ar/database.rb
152
150
  - lib/multi_ar/interface.rb
153
- - lib/multi_ar/multi_ar_model.rb
151
+ - lib/multi_ar/model.rb
154
152
  - lib/multi_ar/rake/ext.rb
155
153
  - lib/multi_ar/rake/migration_generator.rb
156
154
  - lib/multi_ar/rake/tasks.rb
@@ -175,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
173
  version: 1.3.1
176
174
  requirements: []
177
175
  rubyforge_project:
178
- rubygems_version: 2.4.5.1
176
+ rubygems_version: 2.4.8
179
177
  signing_key:
180
178
  specification_version: 4
181
179
  summary: Multi database migrations and utilities for ActiveRecord
metadata.gz.sig CHANGED
Binary file
data/bin/mar DELETED
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # A hack to get this file work as wrapper for the real exe
4
- require "#{__dir__}/multi_ar"