Skip to content

Commit

Permalink
Merge pull request #36 from winebarrel/v0.5.2
Browse files Browse the repository at this point in the history
v0.5.2
  • Loading branch information
I left from github. probably... committed Jan 15, 2015
2 parents 3876d4c + c9cef01 commit 61b175c
Show file tree
Hide file tree
Showing 22 changed files with 569 additions and 249 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
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ 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 `>= 0.0.3`)
* It is not possible to enable both `--enable-mysql-awesome` and --enable-migration-comments`, `--enable-mysql-awesome` and --enable-mysql-unsigned`, `--enable-mysql-awesome` and --enable-mysql-pkdump`
* Fix foreigner version `<= 1.7.1`

## Installation

Expand Down Expand Up @@ -65,7 +69,11 @@ Usage: ridgepole [options]
--enable-mysql-pkdump
--enable-foreigner
--enable-migration-comments
--enable-mysql-awesome
--mysql-awesome-unsigned-pk
--normalize-mysql-float
--dump-without-table-options
-r, --require LIBS
--log-file LOG_FILE
--verbose
--debug
Expand Down Expand Up @@ -177,9 +185,20 @@ create_table "articles", force: true, comment: "table comment" do |t|
end
```

## Collation
You can use the column collation by passing `--enable-mysql-awesome` ([activerecord-mysql-awesome](https://github.com/kamipo/activerecord-mysql-awesome) is required)

```ruby
create_table "articles", force: true, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "title", collation: "ascii_bin"
t.text "text", null: false, collation: "utf8mb4_bin"
t.datetime "created_at"
t.datetime "updated_at"
end
```

## bigint support
Export of `bigint` PK is enabled by passing `--enable-mysql-pkdump` ([activerecord-mysql-pkdump](https://github.com/winebarrel/activerecord-mysql-pkdump) is required)

```ruby
create_table "books", id: "bigint(20) PRIMARY KEY auto_increment", force: true do |t|
t.string "title", null: false
Expand Down Expand Up @@ -263,3 +282,6 @@ remove_column("articles", "author")
* https://github.com/winebarrel/ridgepole-example
* https://github.com/winebarrel/ridgepole-example/pull/1
* https://github.com/winebarrel/ridgepole-example/pull/2

## Similar tools
* [Codenize.tools](http://codenize.tools/)
16 changes: 15 additions & 1 deletion bin/ridgepole
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ 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('', '--mysql-awesome-unsigned-pk') { options[:mysql_awesome_unsigned_pk] = true }
opt.on('', '--normalize-mysql-float') { options[:normalize_mysql_float] = true }
opt.on('', '--dump-without-table-options') { options[:dump_without_table_options] = 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 @@ -111,6 +115,16 @@ ARGV.options do |opt|
puts opt.help
exit 1
end

if options[:enable_mysql_awesome]
if options[:enable_migration_comments]
raise "It is not possible to enable both `--enable-mysql-awesome` and `--enable-migration-comments`"
elsif options[:enable_mysql_unsigned]
raise "It is not possible to enable both `--enable-mysql-awesome` and `--enable-mysql-unsigned`"
elsif options[:enable_mysql_pkdump]
raise "It is not possible to enable both `--enable-mysql-awesome` and `--enable-mysql-pkdump`"
end
end
rescue => e
$stderr.puts("[ERROR] #{e.message}")
exit 1
Expand Down
10 changes: 9 additions & 1 deletion lib/ridgepole/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ 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'

if @options[:mysql_awesome_unsigned_pk]
require 'ridgepole/ext/mysql_awesome.rb'
end
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
2 changes: 1 addition & 1 deletion lib/ridgepole/dsl_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def add_index(table_name, column_name, options = {})
end

def require(file)
schemafile = File.join(@__working_dir, file)
schemafile = (file =~ %r|\A/|) ? file : File.join(@__working_dir, file)

if File.exist?(schemafile)
instance_eval(File.read(schemafile), schemafile)
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[:dump_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 @@
# 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
alias primary_key_without_unsigned primary_key

def primary_key(name, type = :primary_key, options = {})
primary_key_without_unsigned(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.beta6'
end
5 changes: 3 additions & 2 deletions ridgepole.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
spec.email = ['sugawara@cookpad.com']
spec.summary = %q{Ridgepole is a tool to manage DB schema.}
spec.description = %q{Ridgepole is a tool to manage DB schema. It defines DB schema using Rails DSL, and updates DB schema according to DSL.}
spec.homepage = 'https://github.com/winebarrel/ridgepole'
spec.homepage = 'http://ridgepole.codenize.tools/'
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0")
Expand All @@ -25,7 +25,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'mysql2'
spec.add_development_dependency 'coveralls'
spec.add_development_dependency 'activerecord-mysql-unsigned', '~> 0.3.1'
spec.add_development_dependency 'foreigner'
spec.add_development_dependency 'foreigner', '<= 1.7.1'
spec.add_development_dependency 'activerecord-mysql-pkdump', '>= 0.1.0'
spec.add_development_dependency 'migration_comments'
spec.add_development_dependency 'activerecord-mysql-awesome', '>= 0.0.3'
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
47 changes: 47 additions & 0 deletions spec/bigint_pk/bigint_pkspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
if mysql_awesome_enabled?
describe 'Ridgepole::Client (with bigint pk)' do
let(:dsl1) {
<<-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
}

let(:dsl2) {
<<-RUBY
create_table "books", id: :bigint, 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 with limit:8' do
subject { client }

before { subject.diff(dsl1).migrate }

it {
expect(show_create_table(:books)).to include '`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT'
expect(subject.dump).to eq dsl2.strip_heredoc.strip
}
end

context 'when with id:bigint' do
subject { client }

before { subject.diff(dsl2).migrate }

it {
expect(show_create_table(:books)).to include '`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT'
expect(subject.dump).to eq dsl2.strip_heredoc.strip
}
end
end
end
4 changes: 4 additions & 0 deletions spec/cli/ridgepole_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
--enable-mysql-pkdump
--enable-foreigner
--enable-migration-comments
--enable-mysql-awesome
--mysql-awesome-unsigned-pk
--normalize-mysql-float
--dump-without-table-options
-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
Loading

0 comments on commit 61b175c

Please sign in to comment.