All Projects → winebarrel → Ridgepole

winebarrel / Ridgepole

Licence: mit
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)

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Ridgepole

Database rewinder
minimalist's tiny and ultra-fast database cleaner
Stars: ✭ 685 (-18.45%)
Mutual labels:  mysql, postgresql, rails
Graphjin
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
Stars: ✭ 1,264 (+50.48%)
Mutual labels:  mysql, postgresql, rails
Bookshelf
A simple Node.js ORM for PostgreSQL, MySQL and SQLite3 built on top of Knex.js
Stars: ✭ 6,252 (+644.29%)
Mutual labels:  mysql, postgresql
Usql
Universal command-line interface for SQL databases
Stars: ✭ 6,869 (+717.74%)
Mutual labels:  mysql, postgresql
Smartsql
SmartSql = MyBatis in C# + .NET Core+ Cache(Memory | Redis) + R/W Splitting + PropertyChangedTrack +Dynamic Repository + InvokeSync + Diagnostics
Stars: ✭ 775 (-7.74%)
Mutual labels:  mysql, postgresql
Activerecord Postgis Adapter
ActiveRecord connection adapter for PostGIS, based on postgresql and rgeo
Stars: ✭ 746 (-11.19%)
Mutual labels:  postgresql, rails
Weapsy
ASP.NET Core CMS
Stars: ✭ 748 (-10.95%)
Mutual labels:  mysql, postgresql
Eralchemy
Entity Relation Diagrams generation tool
Stars: ✭ 767 (-8.69%)
Mutual labels:  mysql, postgresql
Sequelize
An easy-to-use and promise-based multi SQL dialects ORM tool for Node.js
Stars: ✭ 25,422 (+2926.43%)
Mutual labels:  mysql, postgresql
Schemats
Generate typescript interface definitions from SQL database schema
Stars: ✭ 799 (-4.88%)
Mutual labels:  mysql, postgresql
Fluentpdo
A PHP SQL query builder using PDO
Stars: ✭ 783 (-6.79%)
Mutual labels:  mysql, postgresql
Sqltabs
Rich SQL client for Postgresql, MySQL, MS SQL, Amazon Redshift, Google Firebase (Firestore)
Stars: ✭ 809 (-3.69%)
Mutual labels:  mysql, postgresql
Db Dumper
Dump the contents of a database
Stars: ✭ 744 (-11.43%)
Mutual labels:  mysql, postgresql
Vscode Sqltools
Database management for VSCode
Stars: ✭ 741 (-11.79%)
Mutual labels:  mysql, postgresql
Mybb
MyBB is a free and open source forum software.
Stars: ✭ 750 (-10.71%)
Mutual labels:  mysql, postgresql
Metabase
The simplest, fastest way to get business intelligence and analytics to everyone in your company 😋
Stars: ✭ 26,803 (+3090.83%)
Mutual labels:  mysql, postgresql
Sqldef
Idempotent MySQL/PostgreSQL schema management by SQL
Stars: ✭ 762 (-9.29%)
Mutual labels:  mysql, postgresql
Auto App
Crie um aplicativo com todas as tabelas de um dos seus bancos sem uma linha de código.
Stars: ✭ 18 (-97.86%)
Mutual labels:  mysql, postgresql
Zxw.framework.netcore
基于EF Core的Code First模式的DotNetCore快速开发框架,其中包括DBContext、IOC组件autofac和AspectCore.Injector、代码生成器(也支持DB First)、基于AspectCore的memcache和Redis缓存组件,以及基于ICanPay的支付库和一些日常用的方法和扩展,比如批量插入、更新、删除以及触发器支持,当然还有demo。欢迎提交各种建议、意见和pr~
Stars: ✭ 691 (-17.74%)
Mutual labels:  mysql, postgresql
Xorm
Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
Stars: ✭ 6,464 (+669.52%)
Mutual labels:  mysql, postgresql

Ridgepole

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)

Gem Version Build Status Coverage Status

ChangeLog
  • >= 0.4.8
    • activerecord-mysql-unsigned is now optional. Please pass --enable-mysql-unsigned after you install activerecord-mysql-unsigned if you want to use.
    • Please pass --enable-foreigner after you install foreigner if you want to use the foreign key.
  • >= 0.4.11
    • Add --enable-mysql-pkdump option.
  • >= 0.4.12
    • Fix activerecord-mysql-unsigned version: ~> 0.2.0
  • >= 0.5.0
    • Fix activerecord-mysql-unsigned version: ~> 0.3.1
  • >= 0.5.1
    • Add --enable-migration-comments option (migration_comments is required)
    • Fix rails version < 4.2.0
  • >= 0.5.2
    • Add --enable-mysql-awesome option (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
  • >= 0.6.0
    • Fix rails version ~> 4.2.1
    • Disable following libraries support:
      • activerecord-mysql-unsigned
      • migration_comments
      • foreigner
    • Disable sqlite support
    • Add PostgreSQL test
    • Remove --mysql-awesome-unsigned-pk option
  • >= 0.6.1
  • >= 0.6.3
    • Fix default option (pull#48)
    • Add --enable-migration-comments option (pull#50)
    • Disable rename_table_indexes
  • >= 0.6.4
    • Execute sql using external script (pull#56)
    • Add --mysql-use-alter option
    • Add --alter-extra option
    • Add --dump-with-default-fk-name option
    • Support t.index (pull#64)
    • Remove migration_comments
    • Fix foreign key apply order
  • >= 0.6.5
    • Fix rails version '>= 4.2', '< 6'
    • Support new types (pull#84)
    • Support default: -> { ... } (pull#85)
    • Support DDL Comment (Rails5 only)
    • Output schema diff when pass --verbose
    • Support composite primary key (Rails5 only / pull#97)
  • >= 0.6.6
  • >= 0.7.0
    • Remove Rails 4.x support
    • Add Rails 5.1 support
    • Remove --enable-mysql-awesome option
    • Add --skip-drop-table option
    • Support foreign key without name
    • Support MySQL JSON Type and Generated Columns
    • Add --mysql-change-table-options option
    • Pass config from env
    • Fix change fk order
    • Add --check-relation-type option
    • Add --skip-column-comment-change option
    • Add --default-bigint-limit option
    • Add --ignore-table-comment option
  • >= 0.7.1
    • Remove --reverse option
    • Add --allow-pk-change option
    • Add --create-table-with-index option
    • Add --mysql-dump-auto-increment option (rails >= 5.1)
  • >= 0.7.2
    • Support Rails 5.2
  • >= 0.7.3
    • Add --mysql-change-table-comment option (pull#166)
    • Refactoring with RuboCop
    • Support primary key adding/dropping (issue#246)
  • >= 0.7.4
  • >= 0.7.5
  • >= 0.7.6
  • >= 0.7.7
  • >= 0.7.8
    • Fix for add_foreign_key(..., column: ,,,) (pull#278)
  • >= 0.8.0
    • Support Rails 6.0
  • >= 0.8.1
    • Drop tables in an order considering foreign key constraints (pull#284)
  • >= 0.8.2
  • >= 0.8.3
    • Fix "topological sort failed" error (pull#287)
  • >= 0.8.4
    • Display a warning if an InnoDB table doesn't have any indexes on a column where it has a foreign key (pull#290)
  • >= 0.8.5
    • Improve warning message on table options (pull#291)
  • >= 0.8.6
    • Support multiple databases feature (pull#297)
  • >= 0.8.7
  • >= 0.8.8
    • Fix keyword arguments warnings in Ruby 2.7 (pull#303)
  • >= 0.8.9
    • Fix unexpected differences on text types and blob types on Rails 6 (pull#306)
    • Fix unexpected warning when a foreign key is added on the primary key (pull#307)
  • >= 0.8.10
    • Raise an error if an InnoDB column has a foreign key but no index (pull#310)
  • >= 0.8.11
    • Fix FK index check support multiple PK (pull#315)
    • Support t.reference() foreign_key option (pull#316)
  • >= 0.8.12
    • Pluralize column specified by references (pull#317)
  • >= 0.8.13
    • Support serial and bigserial column types (pull#321)
  • >= 0.9.0
  • >= 0.9.1
  • >= 0.9.2
    • Support t.column index option (pull#353)

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:

gem 'ridgepole'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ridgepole

Help

Usage: ridgepole [options]
    -c, --config CONF_OR_FILE
    -E, --env ENVIRONMENT
    -s, --spec-name SPEC_NAME
    -a, --apply
    -m, --merge
    -f, --file SCHEMAFILE
        --dry-run
        --table-options OPTIONS
        --table-hash-options OPTIONS
        --alter-extra ALTER_SPEC
        --external-script SCRIPT
        --bulk-change
        --default-bool-limit LIMIT
        --default-int-limit LIMIT
        --default-bigint-limit LIMIT
        --default-float-limit LIMIT
        --default-string-limit LIMIT
        --default-text-limit LIMIT
        --default-binary-limit LIMIT
        --pre-query QUERY
        --post-query QUERY
    -e, --export
        --split
        --split-with-dir
    -d, --diff DSL1 DSL2
        --with-apply
    -o, --output SCHEMAFILE
    -t, --tables TABLES
        --ignore-tables REGEX_LIST
        --dump-without-table-options
        --dump-with-default-fk-name
        --index-removed-drop-column
        --skip-drop-table
        --mysql-change-table-options
        --mysql-change-table-comment
        --check-relation-type DEF_PK
        --ignore-table-comment
        --skip-column-comment-change
        --create-table-with-index
        --allow-pk-change
        --mysql-dump-auto-increment
    -r, --require LIBS
        --log-file LOG_FILE
        --verbose
        --debug
        --[no-]color
    -v, --version

Usage

$ git init
Initialized empty Git repository in ...

$ cat config.yml
adapter: mysql2
encoding: utf8
database: blog
username: root

$ ridgepole -c config.yml --export -o Schemafile
# or `ridgepole -c '{adapter: mysql2, database: blog}' ...`
# or `ridgepole -c 'mysql2://root:[email protected]:3306/blog' ...`
# or `export DB_URL='mysql2://...'; ridgepole -c env:DB_URL ...`
Export Schema to `Schemafile`

$ cat Schemafile
create_table "articles", force: :cascade do |t|
  t.string   "title"
  t.text     "text"
  t.datetime "created_at"
  t.datetime "updated_at"
end

$ git add .
$ git commit -m 'first commit'  -a
[master (root-commit) a6c2d31] first commit
 2 files changed, 10 insertions(+)
 create mode 100644 Schemafile
 create mode 100644 config.yml

$ vi Schemafile
$ git diff
diff --git a/Schemafile b/Schemafile
index f5848b9..c266fed 100644
--- a/Schemafile
+++ b/Schemafile
@@ -1,6 +1,7 @@
 create_table "articles", force: :cascade do |t|
   t.string   "title"
   t.text     "text"
+  t.text     "author"
   t.datetime "created_at"
   t.datetime "updated_at"
 end

$ ridgepole -c config.yml --apply --dry-run
Apply `Schemafile` (dry-run)
add_column("articles", "author", :text, {:after=>"text"})

# ALTER TABLE `articles` ADD `author` text AFTER `text`

$ ridgepole -c config.yml --apply
Apply `Schemafile`
-- add_column("articles", "author", :text, {:after=>"text"})
   -> 0.0202s

Rename

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

Foreign Key

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"

Ignore Column/Index/FK

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

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.

Execute

create_table "authors", force: :cascade do |t|
  t.string "name", null: false
end

create_table "books", force: :cascade do |t|
  t.string  "title",     null: false
  t.integer "author_id", null: false
end

add_index "books", ["author_id"], name: "idx_author_id", using: :btree

execute("ALTER TABLE books ADD CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors (id)") do |c|
  # Execute SQL only if there is no foreign key
  c.raw_connection.query(<<-SQL).each.size.zero?
    SELECT 1 FROM information_schema.key_column_usage
     WHERE TABLE_SCHEMA = 'bookshelf'
       AND CONSTRAINT_NAME = 'fk_author' LIMIT 1
  SQL
end

Diff

$ ridgepole --diff file1.schema file2.schema
add_column("articles", "author", :text, {:after=>"title"})
rename_column("articles", "text", "desc")

# You can apply to the database the difference:
# $ ridgepole -c config.yml --diff file1.schema file2.schema --with-apply

You can also compare databases and files.

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

Execute SQL using external script

$ 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

$ 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

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
$ ridgepole -a -c database.yml --check-relation-type bigint # default primary key type (e.g. `<5.1`: integer, `>=5.1`: bigint for MySQL)
Apply `Schemafile`
...
[WARNING] Relation column type is different.
              employees.id: bigint
  dept_manager.employee_id: integer
...

Run tests

docker-compose up -d
bundle install
bundle exec appraisal install
bundle exec appraisal activerecord-5.1 rake
# POSTGRESQL=1 bundle exec appraisal activerecord-5.1 rake
# MYSQL57=1 bundle exec appraisal activerecord-5.1 rake

Notice: mysql-client/postgresql-client is required.

Demo

Example project

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].