Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

ridgepole

> 后端框架
Open source

Ridgepole is a tool to manage DB schema. It defines DB schema using Rails DSL, and updates DB schema according to DSL. (like Chef/Puppet)

1.1K stars0 likes3 views
WebsiteGitHub

About

Ridgepole is a tool to manage DB schema. It defines DB schema using Rails DSL, and updates DB schema according to DSL. (like Chef/Puppet)

Ridgepole is a tool to manage DB schema.

It defines DB schema using Rails DSL, and updates DB schema according to DSL. (like Chef/Puppet)

[!TIP] Also developing similar declarative schema tools — single Go binary, plain SQL as the schema definition. Give it a try.

  • pistachio: for PostgreSQL

[!warning] The order of columns when exporting has changed in Rails 8.1. https://github.com/rails/rails/pull/53281

If you do not want to sort the columns, use --disable-sort-columns option.

Note

  • ridgepole v3.2.0
    • Use udiff instead of diffy (cf. https://github.com/ridgepole/ridgepole/pull/657)
  • ridgepole v3.1.0
    • Support Rails 8.1 (cf. https://github.com/ridgepole/ridgepole/pull/589)
  • ridgepole v3.0.0
    • Support Rails 8.0 (cf. https://github.com/ridgepole/ridgepole/pull/504)
  • ridgepole v2.0.0
    • Support Trilogy (cf. https://github.com/ridgepole/ridgepole/pull/447)
    • Support Rails 7.1 (cf. https://github.com/ridgepole/ridgepole/pull/441)
    • Drop support AcriveRecord 6.0 (cf. https://github.com/ridgepole/ridgepole/pull/440)
  • Drop support ActiveRecord 5.x in ridgepole v1.2.0.
  • Partitioning is no longer supported in ridgepole v1.1.0.
  • ActiveRecord 7.x has some incompatible changes. If you get unintended differences in datetime columns consider changing precision:
    • Add precision: nil for columns that previously had no precision specified (cf. https://github.com/ridgepole/ridgepole/issues/381)
    • Remove precision: 6 from columns that previously had precision: 6 specified explicitly (cf. https://github.com/ridgepole/ridgepole/issues/406)
  • For ActiveRecord 7.x series, please use AcriveRecord 7.0.2 or higher / Ridgepole 1.0.3 or higher.
    • cf. https://github.com/ridgepole/ridgepole/pull/380
  • 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/ridgepole/ridgepole/pull/323
  • DROP TABLE is skipped by default in v1.0 and later versions.
    • If you want to DROP TABLE, please pass --drop-table.
    • cf. https://github.com/ridgepole/ridgepole/pull/363
  • In Rails 7.0, the output of dumper is different from Rails 6.
    • cf. https://github.com/rails/rails/issues/43909
    • cf. https://github.com/rails/rails/commit/c2a6f618d22cca4d9b7be7fa7652e7aac509350c#diff-55f41513f027a3d219629f475f03c2d1105ca55c5093d691e1b3dc4710c6cc0b
  • SQLite does not support.

Installation

Add this line to your application's Gemfile:

gem 'ridgepole'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install ridgepole

Help

…

Usage

…

Rename

ruby
create_table "articles", force: :cascade do |t|
  t.string   "title"
  t.text     "desc", renamed_from: "text"
  t.text     "author"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "user_comments", force: :cascade, renamed_from: "comments" do |t|
  t.string   "commenter"
  t.text     "body"
  t.integer  "article_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

[!note] When using renamed_from on a table, Ridgepole will only perform the rename operation. All other changes to that table (columns, indexes, foreign keys, etc.) will not be detected during the same migration.

If you need to rename AND modify a table, do it in two separate steps:

  1. First migration: Add renamed_from to rename the table
  2. Second migration: Remove renamed_from and apply your desired changes

Foreign Key

ruby
create_table "parent", force: :cascade do |t|
end

create_table "child", id: false, force: :cascade do |t|
  t.bigint "id"
  t.bigint "parent_id"
end

add_index "child", ["parent_id"], name: "par_ind", using: :btree

add_foreign_key "child", "parent", name: "child_ibfk_1"

CHECK Constraint

ruby
create_table "products", force: :cascade do |t|
  t.string  "name", null: false
  t.decimal "price", precision: 10, scale: 2
  t.integer "quantity"

  t.check_constraint "price > 0", name: "price_check"
  t.check_constraint "quantity >= 0", name: "quantity_check"
end

[!important] Matching Schemafile to Database Output

CHECK constraint expressions may be formatted differently by your database when exported. For example:

  • DSL: "(price > 0)"
  • Database export: "`price` > 0" (MySQL) or "((price > 0))" (PostgreSQL)

To avoid unnecessary migrations where constraints are dropped and recreated: Please modify your Schemafile to match exactly what the SchemaDumper outputs. This approach maintains simplicity and prevents migration issues instead of maintaining complex normalization logic in Ridgepole.

Run ridgepole --export to see the exact format your database uses, then update your Schemafile accordingly.

Ignore Column/Index/FK

ruby
create_table "articles", force: :cascade do |t|
  t.string   "title", ignore: true # All changes are ignored
  t.text     "desc", renamed_from: "text"
  t.text     "author"
  t.datetime "created_at"
  t.datetime "updated_at"
end

Collation/Charset

ruby
create_table "articles", force: :cascade, 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

Charset:

activerecord 5.0.0 and activerecord-mysql-awesome dumps a collation rather than charset because it does not determine the default collation for charset. Specifying a collation for each column would work if it is possible.

See mysql> show character set; to find charset / collation pair for your system.

Generated Column (MySQL)

There should be NO extra white spaces in the expression (such as after comma). Quotes in expression may cause the operations failure with MySQL 8.0.

ruby
create_table "users", force: :cascade do |t|
  t.string   "last_name"
  t.string   "first_name"
  t.virtual  "full_name", type: :string, as: "concat(`last_name`,' ',`first_name`)", stored: true
end

Functional Index (MySQL)

MySQL 8.0.13 or later supports functional key parts, also known as expression indexes. MySQL normalizes these expressions when storing them, and Ridgepole compares the normalized expression with the one in your Schemafile. To avoid repeated diffs, use the expression produced by ridgepole --export in your Schemafile.

ruby
create_table "users", force: :cascade do |t|
  t.datetime "deleted_at"
  t.string   "email", null: false
  t.string   "name", null: false

  t.index "(lower(`name`))", name: "index_users_on_lower_name"
  t.index "(case when (`deleted_at` is null) then `email` else NULL end)",
          name: "index_users_on_active_email", unique: true
end

Execute

…

You can also compare databases and files.

bash
$ ridgepole --diff config.yml file1.schema
remove_column("articles", "author")

Execute SQL using external script

bash
$ cat test.sh
#!/bin/sh
SQL="$1"
CONFIG_JSON="$2"
echo "$SQL" | mysql -u root my_db

$ ridgepole -c config.yml --apply --external-script ./test.sh

Add extra statement to ALTER

bash
$ ridgepole -a -c database.yml --alter-extra="LOCK=NONE" --debug
Apply `Schemafile`
...
-- add_column("dept_manager", "to_date2", :date, {:null=>false, :after=>"from_date"})
   (42.2ms)  ALTER TABLE `dept_manager` ADD `to_date2` date NOT NULL AFTER `from_date`,LOCK=NONE
   -> 0.0428s
-- remove_column("dept_manager", "to_date")
   (46.9ms)  ALTER TABLE `dept_manager` DROP `to_date`,LOCK=NONE
   -> 0.0471s

Relation column type check

ruby
create_table "employees", force: :cascade do |t|
  t.integer "emp_no", null: false
  t.string  "first_name", limit: 14, null: false
  t.string  "last_name", limit: 16, null: false
end

create_table "dept_manager", force: :cascade do |t|
  t.integer "employee_id"
  t.string  "dept_no", limit: 4, null: false
end
bash
$ ridgepole -a -c database.yml --check-relation-type bigint # default primary key type (e.g. bigint for MySQL)
Apply `Schemafile`
...
[WARNING] Relation column type is different.
              employees.id: bigint
  dept_manager.employee_id: integer
...

Define a partial index in PostgreSQL

Partial indexes in PostgreSQL are normalized so differences are always detected.

ruby
create_table "users", id: :serial, force: :cascade do |t|
  t.text "email"
  t.text "name"
  t.index ["email"], name: "idx_users_email", unique: true, where: "email is not null"
end
bash
% ridgepole -a -c database.yml --dry-run --verbose
Apply `Schemafile` (dry-run)
# Parse DSL
# ...
# Compare definitions
#   users
      :options=>
       {:name=>"idx_users_email",
        :unique=>true,
-       :where=>"(email IS NOT NULL)"}}},
+       :where=>"email is not null"}}},
  :options=>{:id=>:serial}}

Use a normalized WHERE clause to avoid detecting differences.

ruby
#t.index ["email"], name: "idx_users_email", unique: true, where: "email is not null"
t.index ["email"], name: "idx_users_email", unique: true, where: "(email IS NOT NULL)"

see https://github.com/ridgepole/ridgepole/issues/568

Run tests

bash
docker compose up -d
bundle install
bundle exec appraisal install
bundle exec appraisal activerecord-8.1 rake
# POSTGRESQL=1 bundle exec appraisal activerecord-8.1 rake
# MYSQL80=1 bundle exec appraisal activerecord-8.1 rake

[!note] mysql-client/postgresql-client is required for testing.

Demo

  • asciinema.org/a/9349
  • asciinema.org/a/11788

Example project

  • https://github.com/winebarrel/ridgepole-example
    • https://github.com/winebarrel/ridgepole-example/pull/1
    • https://github.com/winebarrel/ridgepole-example/pull/2

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyactiverecordmysqlpostgresqlrails

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架