ardm-migrations 1.2.1 → 1.3.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
  SHA1:
3
- metadata.gz: 34885d81397d6d6c250c93a2a7c7fc3d2476bdb2
4
- data.tar.gz: a4331b8c984cac8a7cca893bd7c8152f52b47cb6
3
+ metadata.gz: 97d660536f5688e8e542243fff240640fc79e7da
4
+ data.tar.gz: 97555cdcf0f6bbd6c47564d89f3dc38ab033ac83
5
5
  SHA512:
6
- metadata.gz: dcc5f0fad6fcd309dfbf4784b9a234b314fe7cea46ebb3d68dedd1dc1eaeecaf7abaa5b99bde7cd51d14e9d64a027c15d5f60b0657e9958c3417497214a00ec0
7
- data.tar.gz: 3c06cbde474c3e5a445f04174f6fa10cc465e9c8635879f491e53ddf88ab9e61292013a0bb286b6d377c49a72b12bc98b85f0aad4a3caa770b495fe2211f6772
6
+ metadata.gz: f234a045be1464d4c866687526dcf1cf92d543d6e4eda98d45d4819699325c36a76d7eda56e1d250f9afc98c4baff267db186edd6c7f04a9ff32159e232aac7b
7
+ data.tar.gz: eda55ad74db16e83868f0305fe4651db9ca70db62fc45c731c624ac5da3c51943e9957b7ce395362c92055f17bb6de53aa8d83662872ed62261398fe29b8b2c4
data/README.md CHANGED
@@ -1,3 +1,11 @@
1
+ [![Gem Version](https://badge.fury.io/rb/ardm-migrations.png)][gem]
2
+ [![Build Status](https://travis-ci.org/ar-dm/ardm-migrations.png?branch=master)][travis]
3
+ [![Dependency Status](https://gemnasium.com/ar-dm/ardm-migrations.png?travis)][gemnasium]
4
+
5
+ [gem]: https://rubygems.org/gems/ardm-migrations
6
+ [travis]: https://travis-ci.org/ar-dm/ardm-migrations
7
+ [gemnasium]: https://gemnasium.com/ar-dm/ardm-migrations
8
+
1
9
  # ardm-migrations
2
10
 
3
11
  A fork of [`dm-migrations`](https://github.com/datamapper/dm-migrations).
@@ -24,7 +24,7 @@ namespace :db do
24
24
  end
25
25
 
26
26
  desc "Perform non destructive auto-migration"
27
- task :upgrade => :environment do
27
+ task :upgrade => :environment do |t, _|
28
28
  puts "=> Auto-upgrading"
29
29
  ::DataMapper.auto_upgrade!
30
30
  puts "<= #{t.name} done"
@@ -202,7 +202,7 @@ module DataMapper
202
202
 
203
203
  schema_primitive = schema[:primitive]
204
204
 
205
- if primitive == String && schema_primitive != 'TEXT' && schema_primitive != 'CLOB' && schema_primitive != 'NVARCHAR'
205
+ if primitive == String && schema_primitive != 'TEXT' && schema_primitive != 'CLOB' && schema_primitive != 'NVARCHAR' && schema_primitive != 'BYTEA'
206
206
  schema[:length] = property.length
207
207
  elsif primitive == BigDecimal || primitive == Float
208
208
  schema[:precision] = property.precision
@@ -238,8 +238,13 @@ module DataMapper
238
238
  statement << "(#{connection.quote_value(length)})"
239
239
  end
240
240
 
241
- statement << " DEFAULT #{connection.quote_value(schema[:default])}" if schema.key?(:default)
241
+ default = schema[:default]
242
+ if default
243
+ statement << " DEFAULT #{connection.quote_value(default)}"
244
+ end
245
+
242
246
  statement << ' NOT NULL' unless schema[:allow_nil]
247
+
243
248
  statement
244
249
  end
245
250
 
@@ -22,8 +22,8 @@ module DataMapper
22
22
 
23
23
  # @api semipublic
24
24
  def field_exists?(storage_name, field_name)
25
- result = select("SELECT c.name FROM sysobjects as o JOIN syscolumns AS c ON o.id = c.id WHERE o.name = #{quote_name(storage_name)} AND c.name LIKE ?", field_name).first
26
- result ? result.field == field_name : false
25
+ result = select("SELECT c.name FROM sysobjects as o JOIN syscolumns AS c ON o.id = c.id WHERE o.name = ? AND c.name LIKE ?", storage_name, field_name).first
26
+ result ? result.to_s == field_name.to_s : false
27
27
  end
28
28
 
29
29
  module SQL #:nodoc:
@@ -7,9 +7,8 @@ module DataMapper
7
7
 
8
8
  def self.included(base)
9
9
  DataMapper.extend(Migrations::SingletonMethods)
10
- [ :Repository, :Model ].each do |name|
11
- DataMapper.const_get(name).send(:include, Migrations.const_get(name))
12
- end
10
+ DataMapper::Repository.send(:include, Migrations::Repository)
11
+ DataMapper::Model.send(:include, Migrations::Model)
13
12
  end
14
13
 
15
14
  # @api semipublic
@@ -184,9 +184,8 @@ module DataMapper
184
184
 
185
185
  def self.include_migration_api
186
186
  DataMapper.extend(SingletonMethods)
187
- [ :Repository, :Model ].each do |name|
188
- DataMapper.const_get(name).send(:include, const_get(name))
189
- end
187
+ DataMapper::Repository.send(:include, Repository)
188
+ DataMapper::Model.send(:include, Model)
190
189
  DataMapper::Model.append_extensions(Model)
191
190
  Adapters::AbstractAdapter.descendants.each do |adapter_class|
192
191
  Adapters.include_migration_api(DataMapper::Inflector.demodulize(adapter_class.name))
@@ -138,6 +138,26 @@ module DataMapper
138
138
  end
139
139
  end
140
140
 
141
+ #
142
+ # Execute raw SQL and return a result set.
143
+ #
144
+ # @param [String] sql
145
+ # The raw SQL statement.
146
+ #
147
+ # @param [Array] bind_values
148
+ # Additional values to bind to the statement.
149
+ #
150
+ # @return [Array<Struct>]
151
+ # The result set.
152
+ #
153
+ # @since 1.3.0
154
+ #
155
+ def select(sql, *bind_values)
156
+ say_with_time(sql) do
157
+ adapter.select(sql, *bind_values)
158
+ end
159
+ end
160
+
141
161
  def create_table(table_name, opts = {}, &block)
142
162
  execute TableCreator.new(adapter, table_name, opts, &block).to_sql
143
163
  end
@@ -41,6 +41,27 @@ module SQL
41
41
  "ALTER TABLE #{quote_name(name)} MODIFY COLUMN #{column.to_sql}"
42
42
  end
43
43
 
44
+ def rename_column_type_statement(table_name, old_col, new_col)
45
+ table = quote_name(table_name)
46
+ column_info = select("SHOW COLUMNS FROM #{table} LIKE ?", old_col).first
47
+
48
+ column_options = {
49
+ :name => column_info.field,
50
+ :primitive => column_info.type,
51
+ :serial => column_info.extra == 'auto_increment',
52
+ :allow_nil => column_info.null == 'YES',
53
+ :default => column_info.default,
54
+ }
55
+
56
+ column = with_connection do |connection|
57
+ property_schema_statement(connection, column_options)
58
+ end
59
+
60
+ column_definition = column.split(' ', 2).last
61
+
62
+ "ALTER TABLE #{table} CHANGE #{quote_name(old_col)} #{quote_name(new_col)} #{column_definition}"
63
+ end
64
+
44
65
  class Table
45
66
  def initialize(adapter, table_name)
46
67
  @columns = []
@@ -40,6 +40,10 @@ module SQL
40
40
  "ALTER TABLE #{quote_name(name)} ALTER COLUMN #{column.to_sql}"
41
41
  end
42
42
 
43
+ def rename_column_type_statement(table_name, old_col, new_col)
44
+ "ALTER TABLE #{quote_name(table_name)} RENAME COLUMN #{quote_name(old_col)} TO #{quote_name(new_col)}"
45
+ end
46
+
43
47
  class Table < SQL::Table
44
48
  def initialize(adapter, table_name)
45
49
  @adapter, @name = adapter, table_name
@@ -27,7 +27,11 @@ module SQL
27
27
  true
28
28
  end
29
29
 
30
- def change_column_type_statement(*args)
30
+ def change_column_type_statement(*)
31
+ raise NotImplementedError
32
+ end
33
+
34
+ def rename_column_type_statement(*)
31
35
  raise NotImplementedError
32
36
  end
33
37
 
@@ -34,7 +34,7 @@ module SQL
34
34
 
35
35
  def rename_column(name, new_name, opts = {})
36
36
  # raise NotImplemented for SQLite3
37
- @statements << "ALTER TABLE #{quoted_table_name} RENAME COLUMN #{quote_column_name(name)} TO #{quote_column_name(new_name)}"
37
+ @statements << @adapter.rename_column_type_statement(table_name, name, new_name)
38
38
  end
39
39
 
40
40
  def change_column(name, type, opts = {})
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
2
  module Migrations
3
- VERSION = '1.2.1'
3
+ VERSION = '1.3.0'
4
4
  end
5
5
  end
@@ -123,6 +123,10 @@ describe "A Migration" do
123
123
  @migration.should respond_to(:execute)
124
124
  end
125
125
 
126
+ it "should make available an #select method" do
127
+ @migration.should respond_to(:select)
128
+ end
129
+
126
130
  it "should run the sql passed into the #execute method"
127
131
  # TODO: Find out how to stub the DataMapper::database.execute method
128
132
  end
@@ -131,23 +131,60 @@ describe "SQL generation" do
131
131
  describe DataMapper::Migration, "#modify_table helper" do
132
132
  before do
133
133
  @migration = DataMapper::Migration.new(1, :create_people_table, :verbose => false) { }
134
-
135
134
  end
136
135
 
137
136
  it "should have a #modify_table helper" do
138
137
  @migration.should respond_to(:modify_table)
139
138
  end
140
139
 
141
- case DataMapper::Spec.adapter_name.to_sym
142
- when :postgres
140
+ describe '#change_column' do
143
141
  before do
144
142
  @modifier = DataMapper::Migration::TableModifier.new(@adapter, :people) do
145
143
  change_column :name, 'VARCHAR(200)'
146
144
  end
147
145
  end
148
146
 
149
- it "should alter the column" do
150
- @modifier.to_sql.should == %q{ALTER TABLE "people" ALTER COLUMN "name" VARCHAR(200)}
147
+ case DataMapper::Spec.adapter_name.to_sym
148
+ when :mysql
149
+ it 'alters the column' do
150
+ @modifier.to_sql.should == %q{ALTER TABLE `people` MODIFY COLUMN `name` VARCHAR(200)}
151
+ end
152
+ when :postgres
153
+ it 'alters the column' do
154
+ @modifier.to_sql.should == %q{ALTER TABLE "people" ALTER COLUMN "name" VARCHAR(200)}
155
+ end
156
+ end
157
+ end
158
+
159
+ describe "#rename_column" do
160
+ case DataMapper::Spec.adapter_name.to_sym
161
+ when :postgres
162
+ before do
163
+ @modifier = DataMapper::Migration::TableModifier.new(@adapter, :people) do
164
+ rename_column :name, :first_name
165
+ end
166
+ end
167
+
168
+ it "should rename the column" do
169
+ @modifier.to_sql.should == %q{ALTER TABLE "people" RENAME COLUMN "name" TO "first_name"}
170
+ end
171
+ when :mysql
172
+ before do
173
+ # create the table so the existing column schema can be instrospected
174
+ @adapter.execute("CREATE TABLE `people` (name VARCHAR(50) DEFAULT 'John' NOT NULL)")
175
+
176
+ @modifier = DataMapper::Migration::TableModifier.new(@adapter, :people) do
177
+ rename_column :name, :first_name
178
+ end
179
+ end
180
+
181
+ after do
182
+ @adapter.execute('DROP TABLE `people`')
183
+ end
184
+
185
+ it "should change the column" do
186
+ @modifier.to_sql.should == %q{ALTER TABLE `people` CHANGE `name` `first_name` varchar(50) DEFAULT 'John' NOT NULL}
187
+ end
151
188
  end
152
189
  end
153
190
  end
@@ -190,6 +190,22 @@ describe 'Migration' do
190
190
  end
191
191
  end
192
192
 
193
+ describe '#execute' do
194
+ before do
195
+ @adapter.stub!(:select)
196
+ end
197
+
198
+ it 'should send the SQL it its executing to the adapter execute method' do
199
+ @adapter.should_receive(:select).with('SELECT SOME SQL')
200
+ @m.select('SELECT SOME SQL')
201
+ end
202
+
203
+ it 'should output the SQL it is executing' do
204
+ @m.should_receive(:write).with(/SELECT SOME SQL/)
205
+ @m.select('SELECT SOME SQL')
206
+ end
207
+ end
208
+
193
209
  describe 'helpers' do
194
210
  before do
195
211
  @m.stub!(:execute) # don't actually run anything
@@ -0,0 +1,11 @@
1
+ namespace :db do
2
+ task :create do
3
+ sh 'mysql -uroot -e "CREATE DATABASE IF NOT EXISTS datamapper_default_tests;"'
4
+ sh 'mysql -uroot -e "CREATE DATABASE IF NOT EXISTS datamapper_alternate_tests;"'
5
+ end
6
+
7
+ task :drop do
8
+ sh 'mysql -uroot -e "DROP DATABASE IF EXISTS datamapper_default_tests;"'
9
+ sh 'mysql -uroot -e "DROP DATABASE IF EXISTS datamapper_alternate_tests;"'
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ardm-migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Emde
@@ -119,6 +119,7 @@ files:
119
119
  - spec/unit/sql/table_modifier_spec.rb
120
120
  - spec/unit/sql/table_spec.rb
121
121
  - spec/unit/sql_spec.rb
122
+ - tasks/db.rake
122
123
  - tasks/spec.rake
123
124
  - tasks/yard.rake
124
125
  - tasks/yardstick.rake