active_record_table_info 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '0199acfc3e5118a88df5a365c2080f87100944701e151a93121d2b1a6f568fee'
4
+ data.tar.gz: dc669dd15f8ffd8ecedafd60395f0990d73de7ea0ad72100bf1cfcf669a307f8
5
+ SHA512:
6
+ metadata.gz: 8b6f238b3733a5476268913f72633c74f72acae37bae85ada1f8d6f0391ccfa0459b29dd458d20d71e8eca1ca8d15b3081e7e181b93a4d8f8f06d69e23aaa6f2
7
+ data.tar.gz: ea3d436d23f3ae2c87abb4573de0ba0def1bf51ef866c41c4734ac03f7d3e882f20e56ef41e1d9d4dcf0e057ae3d69d4f22af3b9e823e57311eca4c275bf3e9c
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ bundle exec rake
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
@@ -0,0 +1,13 @@
1
+ require:
2
+ - rubocop-rails
3
+ - rubocop-minitest
4
+ - rubocop-rake
5
+ - rubocop-performance
6
+
7
+ AllCops:
8
+ TargetRubyVersion: 2.4
9
+ TargetRailsVersion: 6.0
10
+ NewCops: enable
11
+
12
+ Rails:
13
+ Enabled: true
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in active_record_table_info.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'minitest', '~> 5.0'
11
+
12
+ gem 'rubocop', '~> 1.7'
13
+ gem 'rubocop-minitest'
14
+ gem 'rubocop-performance'
15
+ gem 'rubocop-rails'
16
+ gem 'rubocop-rake'
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2021 Narazaka
2
+
3
+ This software is provided 'as-is', without any express or implied
4
+ warranty. In no event will the authors be held liable for any damages
5
+ arising from the use of this software.
6
+
7
+ Permission is granted to anyone to use this software for any purpose,
8
+ including commercial applications, and to alter it and redistribute it
9
+ freely, subject to the following restrictions:
10
+
11
+ 1. The origin of this software must not be misrepresented; you must not
12
+ claim that you wrote the original software. If you use this software
13
+ in a product, an acknowledgment in the product documentation would be
14
+ appreciated but is not required.
15
+
16
+ 2. Altered source versions must be plainly marked as such, and must not be
17
+ misrepresented as being the original software.
18
+
19
+ 3. This notice may not be removed or altered from any source
20
+ distribution.
@@ -0,0 +1,47 @@
1
+ # ActiveRecordTableInfo
2
+
3
+ `User.table_info` shows columns and indexes like `SHOW CREATE TABLE`
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_record_table_info'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_record_table_info
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ class ApplicationRecord < ActiveRecord::Base
25
+ extend ActiveRecordTableInfo
26
+ end
27
+ ```
28
+
29
+ then
30
+
31
+ ```
32
+ User.table_info
33
+ ```
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record_table_info.
44
+
45
+ ## License
46
+
47
+ [Zlib](LICENSE)
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ require 'rubocop/rake_task'
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/active_record_table_info/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'active_record_table_info'
7
+ spec.version = ActiveRecordTableInfo::VERSION
8
+ spec.authors = ['Narazaka']
9
+ spec.email = ['info@narazaka.net']
10
+ spec.licenses = ['Zlib']
11
+
12
+ spec.summary = '`User.table_info` shows columns and indexes like `SHOW CREATE TABLE`'
13
+ spec.homepage = 'https://github.com/Narazaka/active_record_table_info'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/Narazaka/active_record_table_info.git'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ # Uncomment to register a new dependency of your gem
29
+ # spec.add_dependency "example-gem", "~> 1.0"
30
+
31
+ # For more information and examples about making a new gem, checkout our
32
+ # guide at: https://bundler.io/guides/creating_gem.html
33
+ end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "active_record_table_info"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'active_record_table_info/version'
4
+
5
+ module ActiveRecordTableInfo # rubocop:disable Style/Documentation
6
+ def show_create_table
7
+ puts connection.select_one("show create table #{table_name}")['Create Table'] # rubocop:disable Rails/Output
8
+ end
9
+
10
+ def table_info
11
+ puts (['-- COLUMNS --'] + table_columns + ['-- INDEXES --'] + table_indexes).map { |line| "#{line}\n" }.join # rubocop:disable Rails/Output
12
+ end
13
+
14
+ def table_columns # rubocop:disable Metrics/AbcSize
15
+ connection.columns(table_name).map do |column|
16
+ [
17
+ "`#{column.name}`",
18
+ column.sql_type,
19
+ column.null ? 'NULL' : 'NOT NULL',
20
+ column.try(:collation) ? "COLLATE #{column.collation}" : nil,
21
+ column.try(:extra) == 'auto_increment' ? 'AUTO_INCREMENT' : nil,
22
+ column.null || !column.default.nil? ? "DEFAULT #{column.default.inspect}" : nil
23
+ ].compact.join(' ')
24
+ end
25
+ end
26
+
27
+ def table_indexes
28
+ [table_primary_index] + table_non_primary_indexes
29
+ end
30
+
31
+ def table_primary_index
32
+ "PRIMARY KEY (#{primary_key})"
33
+ end
34
+
35
+ def table_non_primary_indexes
36
+ connection.indexes(table_name).map do |index|
37
+ "#{index.unique ? 'UNIQUE KEY' : 'KEY'} `#{index.name}` (#{index.columns.join(', ')})"
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordTableInfo
4
+ VERSION = '1.0.0'
5
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_table_info
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Narazaka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - info@narazaka.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - ".rubocop.yml"
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - active_record_table_info.gemspec
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/active_record_table_info.rb
31
+ - lib/active_record_table_info/version.rb
32
+ homepage: https://github.com/Narazaka/active_record_table_info
33
+ licenses:
34
+ - Zlib
35
+ metadata:
36
+ homepage_uri: https://github.com/Narazaka/active_record_table_info
37
+ source_code_uri: https://github.com/Narazaka/active_record_table_info.git
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.4.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.2.3
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: "`User.table_info` shows columns and indexes like `SHOW CREATE TABLE`"
57
+ test_files: []