db_auto_migrations 1.0.1 → 2.0.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
  SHA256:
3
- metadata.gz: df6c792de2c4c0f0b66a8b3820e90c3009a50cdbce199fd1131cb0de91e6d0cc
4
- data.tar.gz: 67a006293c936f44c594180e32510fa4dba8b97fef80a8aac436316c4ab3c870
3
+ metadata.gz: a5272025e67595659eda471e791a719c5c0fb183a5ccdad06ba369bd6994d850
4
+ data.tar.gz: 881d42074270490280868e3d8b75a5d63de6c7676681b99c191af716a1277611
5
5
  SHA512:
6
- metadata.gz: 93296e2066d8659e27f97bea67ddbbf3d8629f8203e8302d8fc9d608c4e216992c36a336fc07a50cf25272044faff5c64c804003cb0f49a94948b5b90c37c2ee
7
- data.tar.gz: ff3cb880fb309b4c50628aa9c4a3b1f5c80584bf09f4ba76ec96378bd51428ff70e2356be0f6f63ccb841190a50733cce38c38e31794f61a46fe2b23d9383f3e
6
+ metadata.gz: 8e6dcc9cb708510d5805ca3b635559ac0a5433d78bd312bd3bde04cb916fda5c2fa352bd3f956c302face70425398479ee018c47f19e9d271059f02c1bafb263
7
+ data.tar.gz: 12a4f1a0ee28e691ca427a86814e0c1aea7f47fa257bc840f55479cf41bb93f8e9a647fd2564a6d243bdc29baa5bf5575e5c96f32714e7fe2ce8d6640889688c
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.0"
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 = "2021-12-08"
15
15
  s.description = "Auto database migration.".freeze
16
16
  s.email = "rubyer1993@gmail.com".freeze
17
17
  s.executables = ["setup".freeze]
@@ -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.0
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: 2021-12-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Auto database migration.
14
14
  email: rubyer1993@gmail.com