igorkasyanchuk / rails_db

Rails Database Viewer and SQL Query Runner
https://www.railsjazz.com/
MIT License
1.46k stars 111 forks source link

Rails DB

Please share this gem with your friends to get more feedback. I would really appreciate for the gem review if you have a blog. If you have ideas - please create an issue or contact me by email: igorkasyanchuk[@]gmail.com.

Gem Version RailsJazz https://www.patreon.com/igorkasyanchuk Listed on OpenSource-Heroes.com

<img src="https://raw.githubusercontent.com/igorkasyanchuk/rails_db/master/docs/main_view.png?token=AAArXeu9-vtuW8nIvc9RE0nOIhGbwxkbks5WKlTLwA%3D%3D" /> <img src="https://raw.githubusercontent.com/igorkasyanchuk/rails_db/master/docs/railsdb.png?token=AAArXeu9-vtuW8nIvc9RE0nOIhGbwxkbks5WKlTLwA%3D%3D" /> <img src="https://raw.githubusercontent.com/igorkasyanchuk/rails_db/master/docs/runsql.png?token=AAArXeu9-vtuW8nIvc9RE0nOIhGbwxkbks5WKlTLwA%3D%3D" />

Quick way to inspect your Rails database http://localhost:12345/rails/db, see content of tables, export them to CSV, EXPLAIN SQL and run SQL queries.

Demo available on: https://rails-db.herokuapp.com/rails/db/

Video Demos: https://youtu.be/TYsRxXRFp1g, https://youtu.be/dfViQPZw9zE

Requirements

Supports: PostgreSQL, MySQL, SQLite. Not sure about other adapters. Since gem is using ActiveRecord it will work with others DBs.

For older versions of Rails (Rails 3.2, 4.X) please user gem "rails_db", "1.6".

Note (!)

Gem works with ruby 1.9.3 but you need to use kaminari < 1.0.0 gem and rails_db <= 1.6. Please add to gemfile gem 'kaminari', '< 1.0.0'.

Main Features

Install

Add this to the development group in your Gemfile

For all environments (be careful with production environment):

  # Rails >= 6
  gem 'rails_db'

  # Rails < 6
  gem 'rails_db', '2.2.1'

Or only for development:

  group :development do
    # Rails >= 6
    # and there are is a big change it will work with Rails 5.2 too
    gem 'rails_db'

    # Rails < 6
    gem 'rails_db', '2.2.1'
  end

Run bundle install

Visit http://localhost:3000/rails/db and you will see your database tables and SQL editors.

Use port 12345 if running in 'standalone' mode

Optionally, add gem 'caxlsx_rails' to your application's Gemfile to be able to export to excel .xlsx files.

Customization

If you want to customize gem (create initializer) run in console:

rails g rails_db initializer

If will create file config/initializers/rails_db.rb.

Options

If you want to add routes manually (or customize the mounted location) you need to first turn off automatic_routes_mount and then add to your routes.rb

  mount RailsDb::Engine => '/rails/db', :as => 'rails_db'

If you wanted to only allow access to admins and, for example, you are using Devise you could do following (in config/initializers/rails_db.rb)

  config.verify_access_proc = proc { |controller| controller.current_user.admin? }

Data Tables

You can easily embed data-tables into your App with content from DB.

You can build data tables for the whole Table, Table with specific columns or based on SQL query.

Below you can see samples on how you can embed data-tables directly in your app.

<h3>Table</h3>

<%= rails_db_data_table 'accounts',
                        footer: true,
                        columns: ['id', 'name', 'users_count'],
                        header: true,
                        style: :bootstrap %>

<h3>SQL</h3>

<%= rails_db_data_table_sql 'select id, name, age from users order by age desc limit 10',
                            footer: false,
                            header: true %>

<img src="https://raw.githubusercontent.com/igorkasyanchuk/rails_db/master/docs/data_table_code.png?token=AAArXU_ChN3xNN2MajLy2Tv98ij6XK4Pks5WKlT3wA%3D%3D" />

Samples & Screenshots

Schema

<img src="https://raw.githubusercontent.com/igorkasyanchuk/rails_db/master/docs/autocomplete.png?token=AAArXU_ChN3xNN2MajLy2Tv98ij6XK4Pks5WKlT3wA%3D%3D" />

<img src="https://raw.githubusercontent.com/igorkasyanchuk/rails_db/master/docs/accounts_schema.png?token=AAArXU_ChN3xNN2MajLy2Tv98ij6XK4Pks5WKlT3wA%3D%3D" />

SQL Editor

Rails App SQL editor with syntax highlight and code complete.

<img src="https://raw.githubusercontent.com/igorkasyanchuk/rails_db/master/docs/execute_sql.png?token=AAArXf26vwsPwvV2MhgxWUepGb0tT6g4ks5WKlUJwA%3D%3D" />

Install & visit http://localhost:3000/rails/db to see it in action.

RailsDB Standalone

Run RailsDB Standalone commands locally withing configuration of your config/database.yml.

RailsDB Standalone and runsql doesn't require to be added to Gemfile or you application.

<img src="https://raw.githubusercontent.com/igorkasyanchuk/rails_db/master/docs/railsdb.png?token=AAArXf26vwsPwvV2MhgxWUepGb0tT6g4ks5WKlUJwA%3D%3D" />

RunSQL Util

Run SQL commands locally withing configuration of your config/database.yml

<img src="https://raw.githubusercontent.com/igorkasyanchuk/rails_db/master/docs/runsql.png?token=AAArXf26vwsPwvV2MhgxWUepGb0tT6g4ks5WKlUJwA%3D%3D" />

Edit Content in Rails DB

<img src="https://raw.githubusercontent.com/igorkasyanchuk/rails_db/master/docs/edit_popup.png?token=AAArXf26vwsPwvV2MhgxWUepGb0tT6g4ks5WKlUJwA%3D%3D" />

===

++There is a dependency on MySQL. You may need to gem install mysql2 -v '0.3.20'

Contributing

Local Development

Local Build

Run Tests

Locally works with Ruby 3.1.2 (dev env, tests) and goal is to upgrade dummy app and Ruby to newer versions.

To run tests locally you need to execute rake test withing root folder of project.

You can also change locally DB to verify gem accross multiple Databases (mysql, postgres, sqlite). For more details see test/dummy/config/database.yml (you need to set DB ENV variable).

Sample:

DB=sqlite BUNDLE_GEMFILE=./gemfiles/rails_7_1.gemfile rails test
DB=postgresql BUNDLE_GEMFILE=./gemfiles/rails_7_1.gemfile rails test
DB=mysql BUNDLE_GEMFILE=./gemfiles/rails_7_1.gemfile rails test

Or

DATABASE_NAME=rails_db_test DB=mysql BUNDLE_GEMFILE=./gemfiles/rails_7_1.gemfile rails test

Common Issues and Solutions

Plans

Other stuff

As a good addition to this gem (or as separate utility) you can use this gem: https://github.com/igorkasyanchuk/execute_sql which allows to execute SQL in rails console or app.

Dev Environment

Might be helpful, if you have issues with mysql2 gem installation on macOS:

gem install mysql2 -- \
 --with-mysql-lib=/usr/local/Cellar/mysql/8.0.33/lib \
 --with-mysql-dir=/usr/local/Cellar/mysql/8.0.33 \
 --with-mysql-config=/usr/local/Cellar/mysql/8.0.33/bin/mysql_config \
 --with-mysql-include=/usr/local/Cellar/mysql/8.0.33/include

<img src="https://opensource-heroes.com/svg/embed/igorkasyanchuk/rails_db" />

License

This project uses MIT-LICENSE.

<img src="https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/more_gems.png?raw=true" />