active_record_schema 0.2.5 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -47,6 +47,7 @@ and the following migration will be generated
47
47
  ``` rb
48
48
  class InitPostsSchema < ActiveRecord::Migration
49
49
  def change
50
+ create_table :posts
50
51
  add_column :posts, :title, :string
51
52
  add_column :posts, :body, :text
52
53
 
@@ -56,6 +57,8 @@ class InitPostsSchema < ActiveRecord::Migration
56
57
  end
57
58
  ```
58
59
 
60
+ **NOTE:** `create_table :posts` is added only if `posts` table does not exist yet
61
+
59
62
  Generating a migration for new columns is the same, lets add a new field to `Post` (eg. `pubdate`):
60
63
 
61
64
  ``` rb
@@ -82,6 +85,8 @@ class AddPubdateToPosts < ActiveRecord::Migration
82
85
  end
83
86
  ```
84
87
 
88
+ **NOTE:** No migration will be generated in case all changes are up-to-date
89
+
85
90
  ## Has and Belongs To Many (HBTM) associations
86
91
 
87
92
  Lets try to add a HBTM association to our `Post` model
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.3.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "active_record_schema"
8
- s.version = "0.2.5"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["mcasimir"]
@@ -7,6 +7,10 @@ module ActiveRecordSchema
7
7
  self.respond_to?(diff_method) ? self.send(diff_method) : []
8
8
  end
9
9
 
10
+ def nothing_to_do?
11
+ table_exists? && diff(:fields, :add).empty? && diff(:indexes, :add).empty? && diff(:joins, :add).empty?
12
+ end
13
+
10
14
  def table_exists?
11
15
  _table_exists?
12
16
  end
@@ -16,14 +20,13 @@ module ActiveRecordSchema
16
20
  end
17
21
 
18
22
  def _diff_indexes_add
19
- model.schema.indexes.values.delete_if {|index| _index_names.include?(index.name.to_s) }
23
+ model.schema.indexes.values.delete_if {|index| _index_exists?(index.name) }
20
24
  end
21
25
 
22
26
  def _diff_joins_add
23
27
  model.schema.joins.values.delete_if {|join| _table_names.include?(join.table.to_s) }
24
28
  end
25
29
 
26
-
27
30
  def _connection
28
31
  ActiveRecord::Base.connection
29
32
  end
@@ -36,10 +39,6 @@ module ActiveRecordSchema
36
39
  _table_exists? ? _connection.columns(_table).map(&:name) : []
37
40
  end
38
41
 
39
- def _index_names
40
- _table_exists? ? _connection.indexes(_table).map(&:name) : []
41
- end
42
-
43
42
  def _table_names
44
43
  _connection.tables
45
44
  end
@@ -47,6 +46,10 @@ module ActiveRecordSchema
47
46
  def _table_exists?
48
47
  _connection.table_exists?(_table)
49
48
  end
49
+
50
+ def _index_exists?(columns)
51
+ _connection.index_exists?(_table, columns)
52
+ end
50
53
 
51
54
  end
52
55
  end
@@ -15,8 +15,11 @@ module ActiveRecordSchema
15
15
  ActiveRecordSchema.autoload_paths.each do |p|
16
16
  load(p)
17
17
  end
18
-
19
- migration_template "migration_from_model.rb.erb", "db/migrate/#{file_name}.rb"
18
+ if model.schema.nothing_to_do?
19
+ say "Nothing to do for '#{options[:from]}'"
20
+ else
21
+ migration_template "migration_from_model.rb.erb", "db/migrate/#{file_name}.rb"
22
+ end
20
23
  else
21
24
  migration_template "migration.rb", "db/migrate/#{file_name}.rb"
22
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -89,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  segments:
91
91
  - 0
92
- hash: -3601192281659392675
92
+ hash: -1716665284013531094
93
93
  required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements: