Skip to content

Commit

Permalink
Support activerecord-mysql-awesome
Browse files Browse the repository at this point in the history
  • Loading branch information
Genki Sugawara committed Jan 9, 2015
1 parent 3876d4c commit 63ed921
Show file tree
Hide file tree
Showing 17 changed files with 409 additions and 186 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ rvm:
script:
- bundle install
- bundle exec rake
env:
matrix:
- ENABLE_MYSQL_AWESOME=0
- ENABLE_MYSQL_AWESOME=1
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
* `>= 0.5.1`
* Add `--enable-migration-comments` option ([migration_comments](https://github.com/pinnymz/migration_comments) is required)
* Fix rails version `< 4.2.0`
* `>= 0.5.2`
* Add `--enable-mysql-awesome` option ([activerecord-mysql-awesome](https://github.com/kamipo/activerecord-mysql-awesome) is required)
* It is not possible to enable both `--enable-migration-comments` and `--enable-mysql-awesome`

## Installation

Expand Down Expand Up @@ -55,6 +58,7 @@ Usage: ridgepole [options]
-e, --export
--split
--split-with-dir
--without-table-options
-d, --diff DSL1 DSL2
--reverse
--with-apply
Expand All @@ -65,7 +69,9 @@ Usage: ridgepole [options]
--enable-mysql-pkdump
--enable-foreigner
--enable-migration-comments
--enable-mysql-awesome
--normalize-mysql-float
-r, --require LIBS
--log-file LOG_FILE
--verbose
--debug
Expand Down
9 changes: 8 additions & 1 deletion bin/ridgepole
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ ARGV.options do |opt|
opt.on('-e', '--export') { set_mode[:export] }
opt.on('', '--split') {|v| split = true }
opt.on('', '--split-with-dir') {|v| split = :with_dir }
opt.on('', '--without-table-options') { options[:without_table_options] = true }
opt.on('-d', '--diff DSL1 DSL2') {|diff_arg1|
set_mode[:diff]
diff_arg2 = ARGV.first
Expand All @@ -94,8 +95,10 @@ ARGV.options do |opt|
opt.on('', '--enable-mysql-unsigned') { options[:enable_mysql_unsigned] = true }
opt.on('', '--enable-mysql-pkdump') { options[:enable_mysql_pkdump] = true }
opt.on('', '--enable-foreigner') { options[:enable_foreigner] = true }
opt.on('', '--enable-migration-comments') { options[:migration_comments] = true }
opt.on('', '--enable-migration-comments') { options[:enable_migration_comments] = true }
opt.on('', '--enable-mysql-awesome') { options[:enable_mysql_awesome] = true }
opt.on('', '--normalize-mysql-float') { options[:normalize_mysql_float] = true }
opt.on('-r' , '--require LIBS', Array) {|v| v.each {|i| require i } }
opt.on('' , '--log-file LOG_FILE') {|v| options[:log_file] = v }
opt.on('' , '--verbose') { Ridgepole::Logger.verbose = true }
opt.on('' , '--debug') { options[:debug] = true }
Expand All @@ -118,6 +121,10 @@ ARGV.options do |opt|
end

begin
if options[:enable_migration_comments] and options[:enable_mysql_awesome]
raise "It is not possible to enable both `--enable-migration-comments` and `--enable-mysql-awesome`"
end

logger = Ridgepole::Logger.instance
logger.set_debug(options[:debug])

Expand Down
7 changes: 6 additions & 1 deletion lib/ridgepole/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ def initialize(conn_spec, options = {})
require 'activerecord-mysql-pkdump'
end

if @options[:migration_comments]
if @options[:enable_migration_comments]
require 'migration_comments'
end

if @options[:enable_mysql_awesome]
require 'activerecord/mysql/awesome/base'
require 'ridgepole/ext/mysql_awesome.rb'
end

if @options[:enable_foreigner]
Ridgepole::ForeignKey.init
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ridgepole/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def normalize_column_options!(attrs)
end

# XXX: MySQL only?
if @options[:enable_mysql_unsigned]
if @options[:enable_mysql_unsigned] or @options[:enable_mysql_awesome]
opts[:unsigned] = false unless opts.has_key?(:unsigned)
end

Expand Down
6 changes: 6 additions & 0 deletions lib/ridgepole/dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def dump
line !~ /\A#/ &&
line !~ /\AActiveRecord::Schema\.define/ &&
line !~ /\Aend/
}.map {|line|
if @options[:without_table_options] and line =~ /\A create_table /
line.gsub(/, options: "[^"]*"/, '')
else
line
end
}.join.strip_heredoc

definitions = []
Expand Down
8 changes: 8 additions & 0 deletions lib/ridgepole/ext/mysql_awesome.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'active_record/connection_adapters/abstract/schema_definitions'

# XXX: https://github.com/waka/activerecord-mysql-unsigned/blob/v0.3.1/lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract/schema_definitions.rb#L14
class ActiveRecord::ConnectionAdapters::TableDefinition
def primary_key(name, type = :primary_key, options = {})
column(name, type, options.merge(primary_key: true).reverse_merge(unsigned: true))
end
end
2 changes: 1 addition & 1 deletion lib/ridgepole/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ridgepole
VERSION = '0.5.1'
VERSION = '0.5.2.beta'
end
1 change: 1 addition & 0 deletions ridgepole.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'foreigner'
spec.add_development_dependency 'activerecord-mysql-pkdump', '>= 0.1.0'
spec.add_development_dependency 'migration_comments'
spec.add_development_dependency 'activerecord-mysql-awesome'
end
2 changes: 1 addition & 1 deletion spec/0_diff/dump_disable_unsigned_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe 'Ridgepole::Client#dump' do
context 'when there is a tables (disable unsigned)' do
before { restore_tables }
subject { client(enable_mysql_unsigned: false) }
subject { client(enable_mysql_unsigned: false, enable_mysql_awesome: false) }

it {
expect(subject.dump).to eq <<-RUBY.strip_heredoc.strip
Expand Down
24 changes: 24 additions & 0 deletions spec/bigint_pk/bigint_pkspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
if mysql_awesome_enabled?
describe 'Ridgepole::Client (with bigint pk)' do
let(:dsl) {
<<-RUBY
create_table "books", id: :primary_key, limit: 8, force: true do |t|
t.string "title", null: false
t.integer "author_id", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
RUBY
}

context 'when dump with activerecord-mysql-pkdump' do
subject { client }

before { subject.diff(dsl).migrate }

it {
expect(show_create_table(:books)).to include '`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT'
}
end
end
end
3 changes: 3 additions & 0 deletions spec/cli/ridgepole_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
-e, --export
--split
--split-with-dir
--without-table-options
-d, --diff DSL1 DSL2
--reverse
--with-apply
Expand All @@ -33,7 +34,9 @@
--enable-mysql-pkdump
--enable-foreigner
--enable-migration-comments
--enable-mysql-awesome
--normalize-mysql-float
-r, --require LIBS
--log-file LOG_FILE
--verbose
--debug
Expand Down
132 changes: 132 additions & 0 deletions spec/collation/collation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
if mysql_awesome_enabled?
describe 'Ridgepole::Client#diff -> migrate' do
context 'when change column (add collation)' do
let(:actual_dsl) {
<<-RUBY
create_table "employee_clubs", force: true do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false
t.text "text", null: false
end
RUBY
}

let(:expected_dsl) {
<<-RUBY
create_table "employee_clubs", force: true do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "ascii_bin"
t.text "text", null: false, collation: "utf8mb4_bin"
end
RUBY
}

before { subject.diff(actual_dsl).migrate }
subject { client }

it {
delta = subject.diff(expected_dsl)
expect(delta.differ?).to be_truthy
expect(subject.dump).to eq actual_dsl.strip_heredoc.strip
delta.migrate
expect(subject.dump).to eq expected_dsl.strip_heredoc.strip
}
end

context 'when change column (delete collation)' do
let(:actual_dsl) {
<<-RUBY
create_table "employee_clubs", force: true do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "ascii_bin"
t.text "text", null: false, collation: "utf8mb4_bin"
end
RUBY
}

let(:expected_dsl) {
<<-RUBY
create_table "employee_clubs", force: true do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false
t.text "text", null: false
end
RUBY
}

before { subject.diff(actual_dsl).migrate }
subject { client }

it {
delta = subject.diff(expected_dsl)
expect(delta.differ?).to be_truthy
expect(subject.dump).to eq actual_dsl.strip_heredoc.strip
delta.migrate
expect(subject.dump).to eq expected_dsl.strip_heredoc.strip
}
end

context 'when change column (change collation)' do
let(:actual_dsl) {
<<-RUBY
create_table "employee_clubs", force: true do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "ascii_bin"
t.text "text", null: false, collation: "utf8mb4_bin"
end
RUBY
}

let(:expected_dsl) {
<<-RUBY
create_table "employee_clubs", force: true do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "utf8mb4_bin"
t.text "text", null: false, collation: "ascii_bin"
end
RUBY
}

before { subject.diff(actual_dsl).migrate }
subject { client }

it {
delta = subject.diff(expected_dsl)
expect(delta.differ?).to be_truthy
expect(subject.dump).to eq actual_dsl.strip_heredoc.strip
delta.migrate
expect(subject.dump).to eq expected_dsl.strip_heredoc.strip
}
end

context 'when change column (no change collation)' do
let(:actual_dsl) {
<<-RUBY
create_table "employee_clubs", force: true do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "ascii_bin"
t.text "text", null: false, collation: "utf8mb4_bin"
end
RUBY
}

before { subject.diff(actual_dsl).migrate }
subject { client }

it {
delta = subject.diff(actual_dsl)
expect(delta.differ?).to be_falsey
expect(subject.dump).to eq actual_dsl.strip_heredoc.strip
delta.migrate
expect(subject.dump).to eq actual_dsl.strip_heredoc.strip
}
end
end
end

0 comments on commit 63ed921

Please sign in to comment.