zdm 1.0.6 → 1.0.7

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
  SHA1:
3
- metadata.gz: 30ce840e815a4a41f68dbf90830f69f253b5c75f
4
- data.tar.gz: 453c0299bbfe3bfe72850c1f644bcd7460ed1d79
3
+ metadata.gz: 325f45e849c39b9770c22c7ce892a41f153b6a30
4
+ data.tar.gz: acb9889d53d320c1d9b74d570f2cfdbc18d2828f
5
5
  SHA512:
6
- metadata.gz: a1fb2a19947baf93d0be201af690a51e22bd5ef05d9766b310ffb194e6ba7f4c6bfed360733b7d3851a77c94cf9ac9632a6cf8a5f5846cec52b00b4b6c4ca43f
7
- data.tar.gz: 583007414de8ff16f071760707ec701276aa6fab9c8efcd17e2efe7d6de15c7358b90ee6195f2ed7bf113f4eee369c45fd9de96a81e0760703a2e5c5c2a3929e
6
+ metadata.gz: 3a6efd42171ebfe5bc3f152cbc238cf06cfe53322e66d6198bd45dcd702ed5ce6b67446aaee5251f21c63c7de32ae3c7493357b5039457ce513fbd8c7040c813
7
+ data.tar.gz: 6773233f8e943de709541d87ea7517b0a0d2d662564d27d5f235976086410a22ed845c2b723a5a5ab0e758fb1dc9183e95016cf0a7007d9d0aa501f6dda156f2
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- zdm (1.0.6)
4
+ zdm (1.0.7)
5
5
  activerecord (>= 4.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- zdm (1.0.6)
4
+ zdm (1.0.7)
5
5
  activerecord (>= 4.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- zdm (1.0.6)
4
+ zdm (1.0.7)
5
5
  activerecord (>= 4.0)
6
6
 
7
7
  GEM
@@ -1,3 +1,3 @@
1
1
  module Zdm
2
- VERSION = '1.0.6'
2
+ VERSION = '1.0.7'
3
3
  end
data/lib/zdm.rb CHANGED
@@ -136,6 +136,34 @@ module Zdm
136
136
  def rename_column(old_name, new_name)
137
137
  raise "Unsupported: you must first run a migration adding the column `#{new_name}`, deploy the code live, then run another migration at a later time to remove the column `#{old_name}`"
138
138
  end
139
+
140
+ def add_index(column_names, opts = {})
141
+ column_names = Array(column_names)
142
+ index_name = opts[:name] || "index_#{@origin}_on_#{column_names.join('_and_')}"
143
+ raise ArgumentError, "Index name '#{index_name}' on table #{@origin} is too long" if index_name.length > 64
144
+ index_type = opts[:type] || (opts[:unique] ? 'UNIQUE' : '')
145
+ index_using = "USING #{opts[:using] || 'btree'}"
146
+ index_columns = quoted_columns_for_index(column_names, opts).join(',')
147
+ ddl("CREATE #{index_type} INDEX `#{index_name}` #{index_using} ON `#{@copy}` (#{index_columns})")
148
+ end
149
+
150
+ def remove_index(index_name)
151
+ ddl("ALTER TABLE `#{@copy}` DROP INDEX `#{index_name}`")
152
+ end
153
+
154
+ private
155
+
156
+ def quoted_columns_for_index(column_names, opts)
157
+ option_strings = Hash[column_names.map {|name| [name, '']}]
158
+ length = opts[:length]
159
+ case length
160
+ when Hash
161
+ column_names.each {|name| option_strings[name] += "(#{length[name]})" if length.has_key?(name) && length[name].present?}
162
+ when Integer
163
+ column_names.each {|name| option_strings[name] += "(#{length})"}
164
+ end
165
+ column_names.map {|name| "`#{name}`#{option_strings[name]}"}
166
+ end
139
167
  end
140
168
 
141
169
  class Migrator
@@ -36,6 +36,9 @@ describe Zdm do
36
36
  m.alter("DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci")
37
37
  m.add_column('test', "varchar(32) DEFAULT 'foo'")
38
38
  m.change_column('name', 'varchar(99) NOT NULL')
39
+ m.remove_index('index_people_on_created_at')
40
+ m.add_index(:code)
41
+ m.add_index([:created_at, :code], unique: true, name: 'idx_test', length: {code: 191})
39
42
  end
40
43
 
41
44
  conn = ActiveRecord::Base.connection
@@ -50,8 +53,9 @@ describe Zdm do
50
53
  `test` varchar(32) COLLATE utf8_unicode_ci DEFAULT 'foo',
51
54
  PRIMARY KEY (`id`),
52
55
  UNIQUE KEY `index_people_on_name` (`name`) USING BTREE,
56
+ UNIQUE KEY `idx_test` (`created_at`,`code`(191)) USING BTREE,
53
57
  KEY `index_people_on_account_id_and_code` (`account_id`,`code`(191)) USING BTREE,
54
- KEY `index_people_on_created_at` (`created_at`) USING BTREE
58
+ KEY `index_people_on_code` (`code`(191)) USING BTREE
55
59
  ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
56
60
  EOS
57
61
 
@@ -82,10 +86,12 @@ describe Zdm do
82
86
  `account_id` int(11) DEFAULT NULL,
83
87
  `name` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
84
88
  `code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
85
- `created_at` datetime DEFAULT NULL, PRIMARY KEY (`id`),
89
+ `created_at` datetime DEFAULT NULL,
90
+ PRIMARY KEY (`id`),
86
91
  UNIQUE KEY `index_people_on_name` (`name`) USING BTREE,
92
+ UNIQUE KEY `idx_test` (`created_at`,`code`(191)) USING BTREE,
87
93
  KEY `index_people_on_account_id_and_code` (`account_id`,`code`(191)) USING BTREE,
88
- KEY `index_people_on_created_at` (`created_at`) USING BTREE
94
+ KEY `index_people_on_code` (`code`(191)) USING BTREE
89
95
  ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
90
96
  EOS
91
97
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zdm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - ITRP Institute, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-17 00:00:00.000000000 Z
11
+ date: 2017-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord