pry-helper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.solargraph.yml +11 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +75 -0
- data/LICENSE.txt +21 -0
- data/README.md +8 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/pry-helper/commands/models.rb +44 -0
- data/lib/pry-helper/commands/table.rb +55 -0
- data/lib/pry-helper/commands/vd.rb +46 -0
- data/lib/pry-helper/commands.rb +6 -0
- data/lib/pry-helper/concerns/global_data_definition.rb +245 -0
- data/lib/pry-helper/concerns/table_data_definition.rb +550 -0
- data/lib/pry-helper/concerns.rb +2 -0
- data/lib/pry-helper/definition.rb +246 -0
- data/lib/pry-helper/ext/active_record/connection_adapters/abstract_mysql_adapter.rb +53 -0
- data/lib/pry-helper/ext/active_record/connection_adapters/postgresql/schema_statements.rb +8 -0
- data/lib/pry-helper/ext/array.rb +168 -0
- data/lib/pry-helper/ext/hash.rb +41 -0
- data/lib/pry-helper/ext/kernel.rb +111 -0
- data/lib/pry-helper/ext/object.rb +22 -0
- data/lib/pry-helper/ext/string.rb +13 -0
- data/lib/pry-helper/ext/time.rb +15 -0
- data/lib/pry-helper/ext.rb +5 -0
- data/lib/pry-helper/id.rb +59 -0
- data/lib/pry-helper/mysqldump.rb +44 -0
- data/lib/pry-helper/vd.rb +42 -0
- data/lib/pry-helper/version.rb +3 -0
- data/lib/pry-helper.rb +20 -0
- data/pry-helper.gemspec +32 -0
- metadata +188 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3a183f019f5bf5b3c3fe36d26687b5cd8d08f45d34ec628c734f08e35a8de8ed
|
4
|
+
data.tar.gz: 4f9f94dc18258f514af2263e7b33fc2ab6d99dc13d3f6fa4903f75aa2b2dd94a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e62ec3a4851f8351f3d2d7bc0a235eb7fe5e8de501c8f48b76d36f2306629394edba6480dea3dc4a9051edb4577b390c3b508fc4601a726031c70cd861f3d68f
|
7
|
+
data.tar.gz: bb27b6fe1389c76c6bfdb76b8dff54071366e10ba40eb829bc26e5202d94d96b28b42d8fc6d459cb9b5b19c10845e6063b17830fe15b25beec34d02ef9bfae1c
|
data/.gitignore
ADDED
data/.solargraph.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at liuxiang@ktjr.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pry-helper (0.1.0)
|
5
|
+
activerecord
|
6
|
+
activesupport
|
7
|
+
caxlsx
|
8
|
+
pry-doc
|
9
|
+
rainbow
|
10
|
+
roo
|
11
|
+
table_print
|
12
|
+
terminal-table
|
13
|
+
|
14
|
+
GEM
|
15
|
+
remote: https://rubygems.org/
|
16
|
+
specs:
|
17
|
+
activemodel (7.0.3.1)
|
18
|
+
activesupport (= 7.0.3.1)
|
19
|
+
activerecord (7.0.3.1)
|
20
|
+
activemodel (= 7.0.3.1)
|
21
|
+
activesupport (= 7.0.3.1)
|
22
|
+
activesupport (7.0.3.1)
|
23
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
24
|
+
i18n (>= 1.6, < 2)
|
25
|
+
minitest (>= 5.1)
|
26
|
+
tzinfo (~> 2.0)
|
27
|
+
caxlsx (3.2.0)
|
28
|
+
htmlentities (~> 4.3, >= 4.3.4)
|
29
|
+
marcel (~> 1.0)
|
30
|
+
nokogiri (~> 1.10, >= 1.10.4)
|
31
|
+
rubyzip (>= 1.3.0, < 3)
|
32
|
+
coderay (1.1.3)
|
33
|
+
concurrent-ruby (1.1.10)
|
34
|
+
htmlentities (4.3.4)
|
35
|
+
i18n (1.12.0)
|
36
|
+
concurrent-ruby (~> 1.0)
|
37
|
+
marcel (1.0.2)
|
38
|
+
method_source (1.0.0)
|
39
|
+
mini_portile2 (2.8.0)
|
40
|
+
minitest (5.16.3)
|
41
|
+
nokogiri (1.13.8)
|
42
|
+
mini_portile2 (~> 2.8.0)
|
43
|
+
racc (~> 1.4)
|
44
|
+
pry (0.14.1)
|
45
|
+
coderay (~> 1.1)
|
46
|
+
method_source (~> 1.0)
|
47
|
+
pry-doc (1.3.0)
|
48
|
+
pry (~> 0.11)
|
49
|
+
yard (~> 0.9.11)
|
50
|
+
racc (1.6.0)
|
51
|
+
rainbow (3.1.1)
|
52
|
+
rake (12.3.3)
|
53
|
+
roo (2.9.0)
|
54
|
+
nokogiri (~> 1)
|
55
|
+
rubyzip (>= 1.3.0, < 3.0.0)
|
56
|
+
rubyzip (2.3.2)
|
57
|
+
table_print (1.5.7)
|
58
|
+
terminal-table (3.0.2)
|
59
|
+
unicode-display_width (>= 1.1.1, < 3)
|
60
|
+
tzinfo (2.0.5)
|
61
|
+
concurrent-ruby (~> 1.0)
|
62
|
+
unicode-display_width (2.2.0)
|
63
|
+
webrick (1.7.0)
|
64
|
+
yard (0.9.28)
|
65
|
+
webrick (~> 1.7.0)
|
66
|
+
|
67
|
+
PLATFORMS
|
68
|
+
ruby
|
69
|
+
|
70
|
+
DEPENDENCIES
|
71
|
+
pry-helper!
|
72
|
+
rake (~> 12.0)
|
73
|
+
|
74
|
+
BUNDLED WITH
|
75
|
+
2.2.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Liu Xiang
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "pry-helper"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'terminal-table'
|
2
|
+
|
3
|
+
module PryHelper::Commands
|
4
|
+
module Models
|
5
|
+
class << self
|
6
|
+
def models
|
7
|
+
t = []
|
8
|
+
t << ['Table Name', 'Model Class', 'Abbr', 'Comment']
|
9
|
+
t << nil
|
10
|
+
PryHelper::Definition.models.each do |definition|
|
11
|
+
t << [definition[:table], definition[:model].name, definition[:abbr] || '', definition[:comment] || '']
|
12
|
+
end
|
13
|
+
t
|
14
|
+
end
|
15
|
+
|
16
|
+
def models_table(regexp)
|
17
|
+
Terminal::Table.new do |t|
|
18
|
+
models.each_with_index { |row, idx| t << (row || :separator) if row.nil? ||
|
19
|
+
regexp.nil? ||
|
20
|
+
idx.zero? ||
|
21
|
+
row.any? { |e| e =~ regexp }
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Pry.commands.block_command 'm' do |regexp|
|
29
|
+
puts
|
30
|
+
puts Models::models_table(regexp.try { |e| e.start_with?('/') ? eval(e) : Regexp.new(e) })
|
31
|
+
end
|
32
|
+
|
33
|
+
Pry.commands.alias_command 'l', 'm'
|
34
|
+
end
|
35
|
+
|
36
|
+
module Kernel
|
37
|
+
def models
|
38
|
+
PryHelper::Commands::Models::models
|
39
|
+
end
|
40
|
+
|
41
|
+
def tables
|
42
|
+
models
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'terminal-table'
|
2
|
+
|
3
|
+
module PryHelper::Commands
|
4
|
+
module Table
|
5
|
+
class << self
|
6
|
+
def get_table_name(name)
|
7
|
+
name = name.to_s
|
8
|
+
return name if name =~ /^[a-z]/
|
9
|
+
if Object.const_defined?(name)
|
10
|
+
klass = Object.const_get(name)
|
11
|
+
return klass.table_name if klass < ActiveRecord::Base
|
12
|
+
end
|
13
|
+
name
|
14
|
+
end
|
15
|
+
|
16
|
+
def table_info_table(table_name)
|
17
|
+
Terminal::Table.new do |t|
|
18
|
+
table_info(table_name).each { |row| t << (row || :separator) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def table_info(table_name)
|
23
|
+
t = []
|
24
|
+
t << ['PK', 'Name', 'SQL Type', 'Ruby Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
|
25
|
+
t << nil
|
26
|
+
connection = ::ActiveRecord::Base.connection
|
27
|
+
connection.columns(table_name).each do |column|
|
28
|
+
pk = if [connection.primary_key(table_name)].flatten.include?(column.name)
|
29
|
+
'Y'
|
30
|
+
else
|
31
|
+
''
|
32
|
+
end
|
33
|
+
t << [pk, column.name, column.sql_type,
|
34
|
+
column.sql_type_metadata.type, column.sql_type_metadata.limit || '',
|
35
|
+
column.sql_type_metadata.precision || '', column.sql_type_metadata.scale || '', column.default || '',
|
36
|
+
column.null, column.comment || '']
|
37
|
+
end
|
38
|
+
t
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
Pry.commands.block_command 't' do |name|
|
43
|
+
table_name = Table::get_table_name(name)
|
44
|
+
puts
|
45
|
+
puts "Table: #{table_name}"
|
46
|
+
puts Table::table_info_table(table_name)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module Kernel
|
52
|
+
def table(name)
|
53
|
+
PryHelper::Commands::Table::table_info(PryHelper::Commands::Table::get_table_name(name))
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'pry-helper/vd'
|
2
|
+
|
3
|
+
module PryHelper::Commands
|
4
|
+
module VD
|
5
|
+
class << self
|
6
|
+
def get_table_name(name)
|
7
|
+
name = name.to_s
|
8
|
+
return name if name =~ /^[a-z]/
|
9
|
+
if Object.const_defined?(name)
|
10
|
+
klass = Object.const_get(name)
|
11
|
+
return klass.table_name if klass < ActiveRecord::Base
|
12
|
+
end
|
13
|
+
name
|
14
|
+
end
|
15
|
+
|
16
|
+
def table_info_vd(table_name)
|
17
|
+
PryHelper::VD.new do |vd|
|
18
|
+
table_info(table_name).each { |row| vd << row }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def table_info(table_name)
|
23
|
+
t = []
|
24
|
+
t << ['PK', 'Name', 'SQL Type', 'Ruby Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
|
25
|
+
connection = ::ActiveRecord::Base.connection
|
26
|
+
connection.columns(table_name).each do |column|
|
27
|
+
pk = if [connection.primary_key(table_name)].flatten.include?(column.name)
|
28
|
+
'Y'
|
29
|
+
else
|
30
|
+
''
|
31
|
+
end
|
32
|
+
t << [pk, column.name, column.sql_type,
|
33
|
+
column.sql_type_metadata.type, column.sql_type_metadata.limit || '',
|
34
|
+
column.sql_type_metadata.precision || '', column.sql_type_metadata.scale || '', column.default || '',
|
35
|
+
column.null, column.comment || '']
|
36
|
+
end
|
37
|
+
t
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Pry.commands.block_command 'vd' do |name|
|
42
|
+
table_name = VD::get_table_name(name)
|
43
|
+
VD::table_info_vd(table_name)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,245 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module PryHelper
|
4
|
+
module Concerns
|
5
|
+
module GlobalDataDefinition
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
|
10
|
+
# Example:
|
11
|
+
#
|
12
|
+
# create_table :post, id: false, primary_key: :id do |t|
|
13
|
+
# t.column :id, :bigint, precison: 19, comment: 'ID'
|
14
|
+
# t.column :name, :string, comment: '名称'
|
15
|
+
# t.column :gmt_created, :datetime, comment: '创建时间'
|
16
|
+
# t.column :gmt_modified, :datetime, comment: '最后修改时间'
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# Creates a new table with the name +table_name+. +table_name+ may either
|
20
|
+
# be a String or a Symbol.
|
21
|
+
#
|
22
|
+
# There are two ways to work with #create_table. You can use the block
|
23
|
+
# form or the regular form, like this:
|
24
|
+
#
|
25
|
+
# === Block form
|
26
|
+
#
|
27
|
+
# # create_table() passes a TableDefinition object to the block.
|
28
|
+
# # This form will not only create the table, but also columns for the
|
29
|
+
# # table.
|
30
|
+
#
|
31
|
+
# create_table(:suppliers) do |t|
|
32
|
+
# t.column :name, :string, limit: 60
|
33
|
+
# # Other fields here
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# === Block form, with shorthand
|
37
|
+
#
|
38
|
+
# # You can also use the column types as method calls, rather than calling the column method.
|
39
|
+
# create_table(:suppliers) do |t|
|
40
|
+
# t.string :name, limit: 60
|
41
|
+
# # Other fields here
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# === Regular form
|
45
|
+
#
|
46
|
+
# # Creates a table called 'suppliers' with no columns.
|
47
|
+
# create_table(:suppliers)
|
48
|
+
# # Add a column to 'suppliers'.
|
49
|
+
# add_column(:suppliers, :name, :string, {limit: 60})
|
50
|
+
#
|
51
|
+
# The +options+ hash can include the following keys:
|
52
|
+
# [<tt>:id</tt>]
|
53
|
+
# Whether to automatically add a primary key column. Defaults to true.
|
54
|
+
# Join tables for {ActiveRecord::Base.has_and_belongs_to_many}[rdoc-ref:Associations::ClassMethods#has_and_belongs_to_many] should set it to false.
|
55
|
+
#
|
56
|
+
# A Symbol can be used to specify the type of the generated primary key column.
|
57
|
+
# [<tt>:primary_key</tt>]
|
58
|
+
# The name of the primary key, if one is to be added automatically.
|
59
|
+
# Defaults to +id+. If <tt>:id</tt> is false, then this option is ignored.
|
60
|
+
#
|
61
|
+
# If an array is passed, a composite primary key will be created.
|
62
|
+
#
|
63
|
+
# Note that Active Record models will automatically detect their
|
64
|
+
# primary key. This can be avoided by using
|
65
|
+
# {self.primary_key=}[rdoc-ref:AttributeMethods::PrimaryKey::ClassMethods#primary_key=] on the model
|
66
|
+
# to define the key explicitly.
|
67
|
+
#
|
68
|
+
# [<tt>:options</tt>]
|
69
|
+
# Any extra options you want appended to the table definition.
|
70
|
+
# [<tt>:temporary</tt>]
|
71
|
+
# Make a temporary table.
|
72
|
+
# [<tt>:force</tt>]
|
73
|
+
# Set to true to drop the table before creating it.
|
74
|
+
# Set to +:cascade+ to drop dependent objects as well.
|
75
|
+
# Defaults to false.
|
76
|
+
# [<tt>:if_not_exists</tt>]
|
77
|
+
# Set to true to avoid raising an error when the table already exists.
|
78
|
+
# Defaults to false.
|
79
|
+
# [<tt>:as</tt>]
|
80
|
+
# SQL to use to generate the table. When this option is used, the block is
|
81
|
+
# ignored, as are the <tt>:id</tt> and <tt>:primary_key</tt> options.
|
82
|
+
#
|
83
|
+
# ====== Add a backend specific option to the generated SQL (MySQL)
|
84
|
+
#
|
85
|
+
# create_table(:suppliers, options: 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4')
|
86
|
+
#
|
87
|
+
# generates:
|
88
|
+
#
|
89
|
+
# CREATE TABLE suppliers (
|
90
|
+
# id bigint auto_increment PRIMARY KEY
|
91
|
+
# ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
92
|
+
#
|
93
|
+
# ====== Rename the primary key column
|
94
|
+
#
|
95
|
+
# create_table(:objects, primary_key: 'guid') do |t|
|
96
|
+
# t.column :name, :string, limit: 80
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# generates:
|
100
|
+
#
|
101
|
+
# CREATE TABLE objects (
|
102
|
+
# guid bigint auto_increment PRIMARY KEY,
|
103
|
+
# name varchar(80)
|
104
|
+
# )
|
105
|
+
#
|
106
|
+
# ====== Change the primary key column type
|
107
|
+
#
|
108
|
+
# create_table(:tags, id: :string) do |t|
|
109
|
+
# t.column :label, :string
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
# generates:
|
113
|
+
#
|
114
|
+
# CREATE TABLE tags (
|
115
|
+
# id varchar PRIMARY KEY,
|
116
|
+
# label varchar
|
117
|
+
# )
|
118
|
+
#
|
119
|
+
# ====== Create a composite primary key
|
120
|
+
#
|
121
|
+
# create_table(:orders, primary_key: [:product_id, :client_id]) do |t|
|
122
|
+
# t.belongs_to :product
|
123
|
+
# t.belongs_to :client
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# generates:
|
127
|
+
#
|
128
|
+
# CREATE TABLE order (
|
129
|
+
# product_id bigint NOT NULL,
|
130
|
+
# client_id bigint NOT NULL
|
131
|
+
# );
|
132
|
+
#
|
133
|
+
# ALTER TABLE ONLY "orders"
|
134
|
+
# ADD CONSTRAINT orders_pkey PRIMARY KEY (product_id, client_id);
|
135
|
+
#
|
136
|
+
# ====== Do not add a primary key column
|
137
|
+
#
|
138
|
+
# create_table(:categories_suppliers, id: false) do |t|
|
139
|
+
# t.column :category_id, :bigint
|
140
|
+
# t.column :supplier_id, :bigint
|
141
|
+
# end
|
142
|
+
#
|
143
|
+
# generates:
|
144
|
+
#
|
145
|
+
# CREATE TABLE categories_suppliers (
|
146
|
+
# category_id bigint,
|
147
|
+
# supplier_id bigint
|
148
|
+
# )
|
149
|
+
#
|
150
|
+
# ====== Create a temporary table based on a query
|
151
|
+
#
|
152
|
+
# create_table(:long_query, temporary: true,
|
153
|
+
# as: "SELECT * FROM orders INNER JOIN line_items ON order_id=orders.id")
|
154
|
+
#
|
155
|
+
# generates:
|
156
|
+
#
|
157
|
+
# CREATE TEMPORARY TABLE long_query AS
|
158
|
+
# SELECT * FROM orders INNER JOIN line_items ON order_id=orders.id
|
159
|
+
#
|
160
|
+
# See also TableDefinition#column for details on how to create columns.
|
161
|
+
def create_table(table_name, **options, &blk)
|
162
|
+
ActiveRecord::Base.connection.create_table(table_name, **options, &blk)
|
163
|
+
end
|
164
|
+
|
165
|
+
# Creates a new join table with the name created using the lexical order of the first two
|
166
|
+
# arguments. These arguments can be a String or a Symbol.
|
167
|
+
#
|
168
|
+
# # Creates a table called 'assemblies_parts' with no id.
|
169
|
+
# create_join_table(:assemblies, :parts)
|
170
|
+
#
|
171
|
+
# You can pass an +options+ hash which can include the following keys:
|
172
|
+
# [<tt>:table_name</tt>]
|
173
|
+
# Sets the table name, overriding the default.
|
174
|
+
# [<tt>:column_options</tt>]
|
175
|
+
# Any extra options you want appended to the columns definition.
|
176
|
+
# [<tt>:options</tt>]
|
177
|
+
# Any extra options you want appended to the table definition.
|
178
|
+
# [<tt>:temporary</tt>]
|
179
|
+
# Make a temporary table.
|
180
|
+
# [<tt>:force</tt>]
|
181
|
+
# Set to true to drop the table before creating it.
|
182
|
+
# Defaults to false.
|
183
|
+
|
184
|
+
#
|
185
|
+
# Note that #create_join_table does not create any indices by default; you can use
|
186
|
+
# its block form to do so yourself:
|
187
|
+
#
|
188
|
+
# create_join_table :products, :categories do |t|
|
189
|
+
# t.index :product_id
|
190
|
+
# t.index :category_id
|
191
|
+
# end
|
192
|
+
#
|
193
|
+
# ====== Add a backend specific option to the generated SQL (MySQL)
|
194
|
+
#
|
195
|
+
# create_join_table(:assemblies, :parts, options: 'ENGINE=InnoDB DEFAULT CHARSET=utf8')
|
196
|
+
#
|
197
|
+
# generates:
|
198
|
+
#
|
199
|
+
# CREATE TABLE assemblies_parts (
|
200
|
+
# assembly_id bigint NOT NULL,
|
201
|
+
# part_id bigint NOT NULL,
|
202
|
+
# ) ENGINE=InnoDB DEFAULT CHARSET=utf8
|
203
|
+
#
|
204
|
+
def create_join_table(table_1, table_2, column_options: {}, **options)
|
205
|
+
ActiveRecord::Base.connection.create_join_table(table_1, table_2, column_options, **options)
|
206
|
+
end
|
207
|
+
|
208
|
+
# Drops a table from the database.
|
209
|
+
#
|
210
|
+
# [<tt>:force</tt>]
|
211
|
+
# Set to +:cascade+ to drop dependent objects as well.
|
212
|
+
# Defaults to false.
|
213
|
+
# [<tt>:if_exists</tt>]
|
214
|
+
# Set to +true+ to only drop the table if it exists.
|
215
|
+
# Defaults to false.
|
216
|
+
#
|
217
|
+
# Although this command ignores most +options+ and the block if one is given,
|
218
|
+
# it can be helpful to provide these in a migration's +change+ method so it can be reverted.
|
219
|
+
# In that case, +options+ and the block will be used by #create_table.
|
220
|
+
def drop_table(table_name, **options)
|
221
|
+
ActiveRecord::Base.connection.drop_table(table_name, **options)
|
222
|
+
end
|
223
|
+
|
224
|
+
# Drops the join table specified by the given arguments.
|
225
|
+
# See #create_join_table for details.
|
226
|
+
#
|
227
|
+
# Although this command ignores the block if one is given, it can be helpful
|
228
|
+
# to provide one in a migration's +change+ method so it can be reverted.
|
229
|
+
# In that case, the block will be used by #create_join_table.
|
230
|
+
def drop_join_table(table_1, table_2, **options)
|
231
|
+
ActiveRecord::Base.connection.drop_join_table(table_1, table_2, **options)
|
232
|
+
end
|
233
|
+
|
234
|
+
# Renames a table.
|
235
|
+
#
|
236
|
+
# rename_table('octopuses', 'octopi')
|
237
|
+
#
|
238
|
+
def rename_table(table_name, new_name)
|
239
|
+
ActiveRecord::Base.connection.rename_table(table_name, new_name)
|
240
|
+
end
|
241
|
+
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|