Skip to content

Commit

Permalink
Merge branch '0.9' into 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
winebarrel committed Jan 28, 2021
2 parents dff9676 + f9ad439 commit f249ee9
Show file tree
Hide file tree
Showing 135 changed files with 1,410 additions and 1,743 deletions.
24 changes: 22 additions & 2 deletions .github/workflows/test.yml
Expand Up @@ -20,13 +20,15 @@ jobs:
- MYSQL57=1
- POSTGRESQL=1
gemfile:
- gemfiles/activerecord_5.0.gemfile
- gemfiles/activerecord_5.1.gemfile
- gemfiles/activerecord_5.2.gemfile
- gemfiles/activerecord_6.0.gemfile
- gemfiles/activerecord_6.1.gemfile
exclude:
- ruby: 2.4
gemfile: gemfiles/activerecord_6.0.gemfile
- ruby: 2.4
gemfile: gemfiles/activerecord_6.1.gemfile

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -58,6 +60,24 @@ jobs:
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}

- run: ${{ matrix.env }} bundle exec rake
- name: Run tests
run: ${{ matrix.env }} bundle exec rake
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}

- name: Coveralls Parallel
uses: coverallsapp/github-action@v1.1.2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: run-${{ matrix.ruby }}-${{ matrix.env }}-${{ matrix.gemfile }}
parallel: true

finish:
needs: build
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@v1.1.2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
6 changes: 6 additions & 0 deletions .simplecov
@@ -0,0 +1,6 @@
# frozen_string_literal: true

SimpleCov.start do
# exclude directories and files
add_filter '/spec/'
end
9 changes: 4 additions & 5 deletions Appraisals
@@ -1,10 +1,5 @@
# frozen_string_literal: true

appraise 'activerecord-5.0' do
gem 'activerecord', '~> 5.0.1'
gem 'pg', '< 1.0.0'
end

appraise 'activerecord-5.1' do
gem 'activerecord', '~> 5.1.0'
end
Expand All @@ -17,3 +12,7 @@ end
appraise 'activerecord-6.0' do
gem 'activerecord', '~> 6.0.0'
end

appraise 'activerecord-6.1' do
gem 'activerecord', '~> 6.1.0'
end
58 changes: 18 additions & 40 deletions README.md
Expand Up @@ -6,8 +6,9 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
(like Chef/Puppet)

[![Gem Version](https://badge.fury.io/rb/ridgepole.svg)](http://badge.fury.io/rb/ridgepole)
[![Build Status](https://github.com/winebarrel/ridgepole/workflows/test/badge.svg?branch=0.8)](https://github.com/winebarrel/ridgepole/actions)
[![Coverage Status](https://coveralls.io/repos/github/winebarrel/ridgepole/badge.svg?branch=0.8)](https://coveralls.io/github/winebarrel/ridgepole?branch=0.8)
[![Unstable Version](https://img.shields.io/badge/unstable-0.9.0.rc1-brightgreen.svg?longCache=true&style=flat)](https://rubygems.org/gems/ridgepole/versions/0.9.0.rc1)
[![Build Status](https://github.com/winebarrel/ridgepole/workflows/test/badge.svg?branch=0.9)](https://github.com/winebarrel/ridgepole/actions)
[![Coverage Status](https://coveralls.io/repos/github/winebarrel/ridgepole/badge.svg?branch=0.9)](https://coveralls.io/github/winebarrel/ridgepole?branch=0.9)

<details><summary>ChangeLog</summary>

Expand Down Expand Up @@ -129,8 +130,22 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
* Pluralize column specified by `references` ([pull#317](https://github.com/winebarrel/ridgepole/pull/317))
* `>= 0.8.13`
* Support `serial` and `bigserial` column types ([pull#321](https://github.com/winebarrel/ridgepole/pull/321))
* `>= 0.9.0`
* Remove `--mysql-alter-index` option ([pull#330](https://github.com/winebarrel/ridgepole/pull/330))
* Add `--table-hash-options` option ([pull#331](https://github.com/winebarrel/ridgepole/pull/331))
* Support Rails 6.1 ([pull#323](https://github.com/winebarrel/ridgepole/pull/323))
* Disable Rails 5.0 support ([pull#335](https://github.com/winebarrel/ridgepole/pull/335))
* Fix PK AUTO_INCREMENT change bug ([pull#334](https://github.com/winebarrel/ridgepole/pull/334))
</details>

**Notice**

ActiveRecord 6.1 is supported in ridgepole v0.9, but the ActiveRecord dump has been changed, so there is a difference between ActiveRecord 5.x/6.0 format.

**If you use ActiveRecord 6.1, please modify Schemafile format**.

cf. https://github.com/winebarrel/ridgepole/pull/323

## Installation

Add this line to your application's Gemfile:
Expand All @@ -145,26 +160,6 @@ Or install it yourself as:

$ gem install ridgepole

## Omnibus Package (deb/rpm)

see https://github.com/winebarrel/ridgepole/releases.

### Install from deb

```sh
sudo dpkg -i ridgepole_x.x.x+xxx-x_amd64.deb
sudo apt install build-essential libmysqlclient-dev
sudo /opt/ridgepole/embedded/bin/gem install mysql2
```

### Install from rpm

```sh
sudo yum install ridgepole-x.x.x+xxx-x.el7.x86_64.rpm
sudo yum install make gcc mariadb-devel
sudo /opt/ridgepole/embedded/bin/gem install mysql2
```

## Help
```
Usage: ridgepole [options]
Expand All @@ -176,6 +171,7 @@ Usage: ridgepole [options]
-f, --file SCHEMAFILE
--dry-run
--table-options OPTIONS
--table-hash-options OPTIONS
--alter-extra ALTER_SPEC
--external-script SCRIPT
--bulk-change
Expand All @@ -196,7 +192,6 @@ Usage: ridgepole [options]
-o, --output SCHEMAFILE
-t, --tables TABLES
--ignore-tables REGEX_LIST
--mysql-use-alter
--dump-without-table-options
--dump-with-default-fk-name
--index-removed-drop-column
Expand Down Expand Up @@ -406,20 +401,6 @@ Apply `Schemafile`
-> 0.0471s
```

## Use ALTER instead of CREATE/DROP INDEX

```sh
$ ridgepole -a -c database.yml --mysql-use-alter --debug
Apply `Schemafile`
...
-- remove_index("dept_manager", {:name=>"emp_no"})
(19.2ms) ALTER TABLE `dept_manager` DROP INDEX `emp_no`
-> 0.0200s
-- add_index("dept_manager", ["emp_no"], {:name=>"emp_no2", :using=>:btree})
(23.4ms) ALTER TABLE `dept_manager` ADD INDEX `emp_no2` USING btree (`emp_no`)
-> 0.0243s
```

## Relation column type check

```ruby
Expand Down Expand Up @@ -468,6 +449,3 @@ bundle exec appraisal activerecord-5.1 rake
* 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: 13 additions & 3 deletions bin/ridgepole
Expand Up @@ -86,6 +86,19 @@ ARGV.options do |opt|
opt.on('-f', '--file SCHEMAFILE') { |v| file = v }
opt.on('', '--dry-run') { options[:dry_run] = true }
opt.on('', '--table-options OPTIONS') { |v| options[:table_options] = v }
opt.on('', '--table-hash-options OPTIONS') do |v|
# NOTE: Ruby2.4 doesn't support `symbolize_names: true`
hash = YAML.safe_load(v).deep_symbolize_keys

case hash[:id]
when String
hash[:id] = hash[:id].to_sym
when Hash
hash[:id][:type] = hash[:id][:type].to_sym if hash[:id][:type]
end

options[:table_hash_options] = hash
end
opt.on('', '--alter-extra ALTER_SPEC') { |v| options[:alter_extra] = v }
opt.on('', '--external-script SCRIPT') { |v| options[:external_script] = v }
opt.on('', '--bulk-change') do
Expand Down Expand Up @@ -121,7 +134,6 @@ ARGV.options do |opt|
opt.on('-o', '--output SCHEMAFILE') { |v| output_file = v }
opt.on('-t', '--tables TABLES', Array) { |v| options[:tables] = v }
opt.on('', '--ignore-tables REGEX_LIST', Array) { |v| options[:ignore_tables] = v.map { |i| Regexp.new(i) } }
opt.on('', '--mysql-use-alter') { options[:mysql_use_alter] = true }
opt.on('', '--dump-without-table-options') { options[:dump_without_table_options] = true }
opt.on('', '--dump-with-default-fk-name') { options[:dump_with_default_fk_name] = true }
opt.on('', '--index-removed-drop-column') { options[:index_removed_drop_column] = true }
Expand All @@ -135,8 +147,6 @@ ARGV.options do |opt|
opt.on('', '--create-table-with-index') { options[:create_table_with_index] = true }

opt.on('', '--mysql-dump-auto-increment') do
raise OptionParser::InvalidOption, '`mysql-dump-auto-increment` is not available in `activerecord < 5.1`' if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('5.1')

options[:mysql_dump_auto_increment] = true
end

Expand Down
Expand Up @@ -2,7 +2,6 @@

source "https://rubygems.org"

gem "activerecord", "~> 5.0.1"
gem "pg", "< 1.0.0"
gem "activerecord", "~> 6.1.0"

gemspec path: "../"
11 changes: 8 additions & 3 deletions lib/ridgepole/client.rb
Expand Up @@ -15,8 +15,6 @@ def initialize(conn_spec, options = {})
@parser = Ridgepole::DSLParser.new(@options)
@diff = Ridgepole::Diff.new(@options)

require 'ridgepole/ext/abstract_mysql_adapter/use_alter_index' if @options[:mysql_use_alter]

require 'ridgepole/ext/abstract_mysql_adapter/dump_auto_increment' if @options[:mysql_dump_auto_increment]
end

Expand All @@ -32,14 +30,21 @@ def diff(dsl, opts = {})
logger.verbose_info('# Parse DSL')
expected_definition, expected_execute = @parser.parse(dsl, opts)
expected_definition.each do |_table, definition|
definition[:options][:options] ||= @options[:table_options] if @options[:table_options]
merge_table_options(definition)
end
logger.verbose_info('# Load tables')
current_definition, _current_execute = @parser.parse(@dumper.dump, opts)
logger.verbose_info('# Compare definitions')
@diff.diff(current_definition, expected_definition, execute: expected_execute)
end

private

def merge_table_options(definition)
definition[:options].reverse_merge!(@options[:table_hash_options]) if @options[:table_hash_options]
definition[:options][:options] ||= @options[:table_options] if @options[:table_options]
end

class << self
def diff(dsl_or_config1, dsl_or_config2, options = {})
logger = Ridgepole::Logger.instance
Expand Down
17 changes: 16 additions & 1 deletion lib/ridgepole/delta.rb
Expand Up @@ -292,6 +292,19 @@ def append_change_table_options(table_name, table_options, buf)
buf.puts
end

def append_change_table_raw_options(table_name, raw_table_options, table_charset, table_collation, buf)
if raw_table_options.blank? && ActiveRecord.gem_version >= Gem::Version.new('6.1.0')
# Implicit engine is InnoDB in 6.1.0
# related: https://github.com/rails/rails/pull/39365/files#diff-868f1dccfcbed26a288bf9f3fd8a39c863a4413ab0075e12b6805d9798f556d1R441
raw_table_options = +'ENGINE=InnoDB'
end

raw_table_options << " DEFAULT CHARSET=#{table_charset}" if table_charset
raw_table_options << " COLLATE=#{table_collation}" if table_collation

append_change_table_options(table_name, raw_table_options, buf)
end

def append_change_table_comment(table_name, table_comment, buf)
comment_literal = "COMMENT=#{ActiveRecord::Base.connection.quote(table_comment)}"
append_change_table_options(table_name, comment_literal, buf)
Expand All @@ -303,6 +316,8 @@ def append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
indices = attrs[:indices] || {}
foreign_keys = attrs[:foreign_keys] || {}
table_options = attrs[:table_options]
table_charset = attrs[:table_charset]
table_collation = attrs[:table_collation]
table_comment = attrs[:table_comment]

if !definition.empty? || !indices.empty? || !primary_key_definition.empty?
Expand All @@ -316,7 +331,7 @@ def append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)

append_change_foreign_keys(table_name, foreign_keys, pre_buf_for_fk, post_buf_for_fk, @options) unless foreign_keys.empty?

append_change_table_options(table_name, table_options, buf) if table_options
append_change_table_raw_options(table_name, table_options, table_charset, table_collation, buf) if table_options || table_charset || table_collation

append_change_table_comment(table_name, table_comment, buf) if table_comment

Expand Down

0 comments on commit f249ee9

Please sign in to comment.