db_auto_migrations 1.0.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df6c792de2c4c0f0b66a8b3820e90c3009a50cdbce199fd1131cb0de91e6d0cc
4
- data.tar.gz: 67a006293c936f44c594180e32510fa4dba8b97fef80a8aac436316c4ab3c870
3
+ metadata.gz: de54072126cb57eb5acbedf47cb7f8b3419a08a48f81a7b006dd2ded86b52b18
4
+ data.tar.gz: d4bfbd1b4bc1115f310602ed5fd4f12c39963b08df2451705b2ad075527074d7
5
5
  SHA512:
6
- metadata.gz: 93296e2066d8659e27f97bea67ddbbf3d8629f8203e8302d8fc9d608c4e216992c36a336fc07a50cf25272044faff5c64c804003cb0f49a94948b5b90c37c2ee
7
- data.tar.gz: ff3cb880fb309b4c50628aa9c4a3b1f5c80584bf09f4ba76ec96378bd51428ff70e2356be0f6f63ccb841190a50733cce38c38e31794f61a46fe2b23d9383f3e
6
+ metadata.gz: 179255a3d3aacf942b5fca347a325aa90cefe1a6c554eb31df92178575b24b6c8b24ec871e2a94634c2254eb7d9530e5a839f86984fdced509a339427f7757a3
7
+ data.tar.gz: cd32cbaafd68b0c3b928b0d31d5414fbe39d99c74729c2d535f82e8086b0d2417025e973add7247eea1f0bd3be66278d810aa8da0c26f2a2944073cbc0554320
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 2.0.0
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: db_auto_migrations 1.0.1 ruby lib
5
+ # stub: db_auto_migrations 2.0.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "db_auto_migrations".freeze
9
- s.version = "1.0.1"
9
+ s.version = "2.0.1"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["sai (originally by PJ Hyett)".freeze]
14
- s.date = "2021-12-07"
14
+ s.date = "2023-09-15"
15
15
  s.description = "Auto database migration.".freeze
16
16
  s.email = "rubyer1993@gmail.com".freeze
17
17
  s.executables = ["setup".freeze]
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
32
32
  "lib/tasks/db_auto_migrations_tasks.rake",
33
33
  "test/db_auto_migrations_test.rb"
34
34
  ]
35
- s.homepage = "https://github.com/sai1024/auto_migrations".freeze
35
+ s.homepage = "https://github.com/gith-u-b/auto_migrations".freeze
36
36
  s.rubygems_version = "3.0.9".freeze
37
37
  s.summary = "Auto database migration.".freeze
38
38
  end
@@ -2,7 +2,6 @@
2
2
  Dir[File.join(File.dirname(__FILE__),'tasks/**/*.rake')].each { |f| load f } if defined?(Rake)
3
3
 
4
4
  module AutoMigrations
5
-
6
5
  def self.run
7
6
  # Turn off schema_info code for auto-migration
8
7
  class << ActiveRecord::Schema
@@ -10,29 +9,29 @@ module AutoMigrations
10
9
  attr_accessor :version
11
10
  def define(info={}, &block) @version = Time.now.utc.strftime("%Y%m%d%H%M%S"); instance_eval(&block) end
12
11
  end
13
-
12
+
14
13
  load(File.join(Rails.root, 'db', 'schema.rb'))
15
14
  ActiveRecord::Migration.drop_unused_tables
16
15
  ActiveRecord::Migration.drop_unused_indexes
17
16
  ActiveRecord::Migration.update_schema_version(ActiveRecord::Schema.version) if ActiveRecord::Schema.version
18
-
17
+
19
18
  class << ActiveRecord::Schema
20
19
  alias :define :old_define
21
20
  end
22
21
  end
23
-
22
+
24
23
  def self.schema_to_migration(with_reset = false)
25
24
  schema_in = File.read(File.join(Rails.root, "db", "schema.rb"))
26
25
  schema_in.gsub!(/#(.)+\n/, '')
27
26
  schema_in.sub!(/ActiveRecord::Schema.define(.+)do[ ]?\n/, '')
28
27
  schema_in.gsub!(/^/, ' ')
29
- schema = "class InitialSchema < ActiveRecord::Migration\n def self.up\n"
28
+ schema = "class InitialSchema < ActiveRecord::Migration\n def self.up\n"
30
29
  schema += " # We're resetting the migrations database...\n" +
31
30
  " drop_table :schema_migrations\n" +
32
31
  " initialize_schema_migrations_table\n\n" if with_reset
33
32
  schema += schema_in
34
33
  schema << "\n def self.down\n"
35
- schema << (ActiveRecord::Base.connection.tables - %w(schema_info schema_migrations)).map do |table|
34
+ schema << ((ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Base.connection.data_sources : ActiveRecord::Base.connection.tables) - %w(schema_info schema_migrations)).map do |table|
36
35
  " drop_table :#{table}\n"
37
36
  end.join
38
37
  schema << " end\nend\n"
@@ -40,18 +39,19 @@ module AutoMigrations
40
39
  File.open(migration_file, "w") { |f| f << schema }
41
40
  puts "Migration created at db/migrate/001_initial_schema.rb"
42
41
  end
43
-
42
+
44
43
  def self.included(base)
45
44
  base.extend ClassMethods
46
45
  class << base
47
46
  cattr_accessor :tables_in_schema, :indexes_in_schema
48
47
  self.tables_in_schema, self.indexes_in_schema = [], []
49
- alias_method_chain :method_missing, :auto_migration
48
+ alias_method :method_missing_without_auto_migration, :method_missing
49
+ alias_method :method_missing, :method_missing_with_auto_migration
50
50
  end
51
51
  end
52
52
 
53
53
  module ClassMethods
54
-
54
+
55
55
  def method_missing_with_auto_migration(method, *args, &block)
56
56
  case method
57
57
  when :create_table
@@ -59,29 +59,29 @@ module AutoMigrations
59
59
  when :add_index
60
60
  auto_add_index(method, *args, &block)
61
61
  else
62
- method_missing_without_auto_migration(method, *args, &block)
62
+ method_missing_without_auto_migration(method, *args, &block)
63
63
  end
64
64
  end
65
-
65
+
66
66
  def auto_create_table(method, *args, &block)
67
- table_name = args.shift.to_s
67
+ table_name = args.shift.to_s
68
68
  options = args.pop || {}
69
-
69
+
70
70
  (self.tables_in_schema ||= []) << table_name
71
71
 
72
72
  # Table doesn't exist, create it
73
- unless ActiveRecord::Base.connection.tables.include?(table_name)
73
+ unless (ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Base.connection.data_sources : ActiveRecord::Base.connection.tables).include?(table_name)
74
74
  return method_missing_without_auto_migration(method, *[table_name, options], &block)
75
75
  end
76
-
76
+
77
77
  # Grab database columns
78
78
  fields_in_db = ActiveRecord::Base.connection.columns(table_name).inject({}) do |hash, column|
79
79
  hash[column.name] = column
80
80
  hash
81
81
  end
82
-
82
+
83
83
  # Grab schema columns (lifted from active_record/connection_adapters/abstract/schema_statements.rb)
84
- table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(ActiveRecord::Base.connection)
84
+ table_definition = create_table_definition table_name, options[:temporary], options[:options]
85
85
  primary_key = options[:primary_key] || "id"
86
86
  table_definition.primary_key(primary_key) unless options[:id] == false
87
87
  yield table_definition
@@ -89,7 +89,7 @@ module AutoMigrations
89
89
  hash[column.name.to_s] = column
90
90
  hash
91
91
  end
92
-
92
+
93
93
  # Add fields to db new to schema
94
94
  (fields_in_schema.keys - fields_in_db.keys).each do |field|
95
95
  column = fields_in_schema[field]
@@ -98,13 +98,13 @@ module AutoMigrations
98
98
  options[:null] = column.null if !column.null.nil?
99
99
  add_column table_name, column.name, column.type.to_sym, options
100
100
  end
101
-
101
+
102
102
  # Remove fields from db no longer in schema
103
103
  (fields_in_db.keys - fields_in_schema.keys & fields_in_db.keys).each do |field|
104
104
  column = fields_in_db[field]
105
105
  remove_column table_name, column.name
106
106
  end
107
-
107
+
108
108
  (fields_in_schema.keys & fields_in_db.keys).each do |field|
109
109
  if field != primary_key #ActiveRecord::Base.get_primary_key(table_name)
110
110
  changed = false # flag
@@ -118,16 +118,18 @@ module AutoMigrations
118
118
 
119
119
  # Special catch for precision/scale, since *both* must be specified together
120
120
  # Always include them in the attr struct, but they'll only get applied if changed = true
121
- new_attr[:precision] = fields_in_schema[field][:precision]
122
- new_attr[:scale] = fields_in_schema[field][:scale]
121
+ new_attr[:precision] = fields_in_schema[field].precision
122
+ new_attr[:scale] = fields_in_schema[field].scale
123
123
 
124
124
  # Next, iterate through our extended attributes, looking for any differences
125
125
  # This catches stuff like :null, :precision, etc
126
- fields_in_schema[field].each_pair do |att,value|
127
- next if att == :type or att == :base or att == :name # special cases
126
+ fields_in_schema[field][:options].each_pair do |att,value|
127
+ next unless [:limit, :precision, :scale, :default, :null, :collation, :comment].include?(att)
128
+
128
129
  if !value.nil?
129
130
  value_in_db = fields_in_db[field].send(att)
130
131
  value_in_db = value_in_db.to_i if att == :default && new_type == :integer && value_in_db.class == String
132
+ value_in_db = value_in_db.to_f if att == :default && new_type == :float && value_in_db.class == String
131
133
  if att == :default && new_type == :boolean && value_in_db.class == String
132
134
  value_in_db_to_i = value_in_db.to_i
133
135
  value_in_db = false if value_in_db_to_i == 0
@@ -138,7 +140,7 @@ module AutoMigrations
138
140
  new_attr[att] = value
139
141
  changed = true
140
142
  end
141
- end
143
+ end
142
144
  end
143
145
 
144
146
  # Change the column if applicable
@@ -146,13 +148,13 @@ module AutoMigrations
146
148
  end
147
149
  end
148
150
  end
149
-
150
- def auto_add_index(method, *args, &block)
151
+
152
+ def auto_add_index(method, *args, &block)
151
153
  table_name = args.shift.to_s
152
154
  fields = Array(args.shift).map(&:to_s)
153
155
  options = args.shift
154
156
 
155
- index_name = options[:name] if options
157
+ index_name = options[:name] if options
156
158
  index_name ||= ActiveRecord::Base.connection.index_name(table_name, :column => fields)
157
159
 
158
160
  (self.indexes_in_schema ||= []) << index_name
@@ -161,13 +163,13 @@ module AutoMigrations
161
163
  method_missing_without_auto_migration(method, *[table_name, fields, options], &block)
162
164
  end
163
165
  end
164
-
166
+
165
167
  def drop_unused_tables
166
- (ActiveRecord::Base.connection.tables - tables_in_schema - %w(schema_info schema_migrations)).each do |table|
168
+ ((ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Base.connection.data_sources : ActiveRecord::Base.connection.tables) - tables_in_schema - %w(schema_info schema_migrations)).each do |table|
167
169
  drop_table table
168
170
  end
169
171
  end
170
-
172
+
171
173
  def drop_unused_indexes
172
174
  tables_in_schema.each do |table_name|
173
175
  indexes_in_db = ActiveRecord::Base.connection.indexes(table_name).map(&:name)
@@ -176,9 +178,9 @@ module AutoMigrations
176
178
  end
177
179
  end
178
180
  end
179
-
181
+
180
182
  def update_schema_version(version)
181
- if ActiveRecord::Base.connection.tables.include?("schema_migrations")
183
+ if (ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Base.connection.data_sources : ActiveRecord::Base.connection.tables).include?("schema_migrations")
182
184
  ActiveRecord::Base.connection.update("INSERT INTO schema_migrations VALUES ('#{version}')")
183
185
  end
184
186
  schema_file = File.join(Rails.root, "db", "schema.rb")
@@ -186,9 +188,12 @@ module AutoMigrations
186
188
  schema.sub!(/:version => \d+/, ":version => #{version}")
187
189
  File.open(schema_file, "w") { |f| f << schema }
188
190
  end
189
-
190
- end
191
191
 
192
+ private
193
+ def create_table_definition(name, temporary, options)
194
+ ActiveRecord::ConnectionAdapters::TableDefinition.new({}, name, temporary, options)
195
+ end
196
+ end
192
197
  end
193
198
 
194
199
  ActiveRecord::Migration.send :include, AutoMigrations
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db_auto_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sai (originally by PJ Hyett)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-07 00:00:00.000000000 Z
11
+ date: 2023-09-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Auto database migration.
14
14
  email: rubyer1993@gmail.com
@@ -30,7 +30,7 @@ files:
30
30
  - lib/db_auto_migrations.rb
31
31
  - lib/tasks/db_auto_migrations_tasks.rake
32
32
  - test/db_auto_migrations_test.rb
33
- homepage: https://github.com/sai1024/auto_migrations
33
+ homepage: https://github.com/gith-u-b/auto_migrations
34
34
  licenses: []
35
35
  metadata: {}
36
36
  post_install_message:
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  requirements: []
51
- rubygems_version: 3.0.9
51
+ rubygems_version: 3.2.32
52
52
  signing_key:
53
53
  specification_version: 4
54
54
  summary: Auto database migration.