activerecord-database_schema 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +27 -0
- data/lib/active_record/database_schema/dumper.rb +110 -0
- data/lib/active_record/database_schema/railtie.rb +20 -0
- data/lib/activerecord-database_schema.rb +2 -0
- metadata +83 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'ActiveRecord DatabaseSchema (Extended Support)'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module ActiveRecord
|
3
|
+
module DatabaseSchema
|
4
|
+
|
5
|
+
module Dumper
|
6
|
+
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
alias_method_chain :table, :extended_support
|
11
|
+
end
|
12
|
+
|
13
|
+
# Override de #table
|
14
|
+
def table_with_extended_support(table, stream)
|
15
|
+
columns = @connection.columns(table)
|
16
|
+
|
17
|
+
begin
|
18
|
+
tbl = StringIO.new
|
19
|
+
|
20
|
+
# first dump primary key column
|
21
|
+
if @connection.respond_to?(:pk_and_sequence_for)
|
22
|
+
pk, _ = @connection.pk_and_sequence_for(table)
|
23
|
+
elsif @connection.respond_to?(:primary_key)
|
24
|
+
pk = @connection.primary_key(table)
|
25
|
+
end
|
26
|
+
|
27
|
+
tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}"
|
28
|
+
if columns.detect { |c| c.name == pk }
|
29
|
+
if pk != 'id'
|
30
|
+
tbl.print %Q(, :primary_key => "#{pk}")
|
31
|
+
end
|
32
|
+
else
|
33
|
+
tbl.print ", :id => false"
|
34
|
+
end
|
35
|
+
tbl.print ", :force => true"
|
36
|
+
#TODO: Cambiar que en vez de ponerlo en :options, ponerlo en :mysql_options:
|
37
|
+
#tbl.print ", :options =>\"#{@connection.table_options(table)}\""
|
38
|
+
tbl.puts " do |t|"
|
39
|
+
|
40
|
+
# then dump all non-primary key columns
|
41
|
+
column_specs = columns.map do |column|
|
42
|
+
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil?
|
43
|
+
next if column.name == pk
|
44
|
+
spec = {}
|
45
|
+
spec[:name] = column.name.inspect
|
46
|
+
|
47
|
+
# AR has an optimization which handles zero-scale decimals as integers. This
|
48
|
+
# code ensures that the dumper still dumps the column as a decimal.
|
49
|
+
spec[:type] = if column.type == :integer && [/^numeric/, /^decimal/].any? { |e| e.match(column.sql_type) }
|
50
|
+
'decimal'
|
51
|
+
else
|
52
|
+
column.type.to_s
|
53
|
+
end
|
54
|
+
# Unsigned Column Support:
|
55
|
+
spec[:unsigned] = 'true' if /unsigned/i.match(column.sql_type)
|
56
|
+
spec[:limit] = column.limit.inspect if column.limit != @types[column.type][:limit] && spec[:type] != 'decimal'
|
57
|
+
spec[:precision] = column.precision.inspect if column.precision
|
58
|
+
spec[:scale] = column.scale.inspect if column.scale
|
59
|
+
spec[:null] = 'false' unless column.null
|
60
|
+
spec[:default] = default_string(column.default) if column.has_default?
|
61
|
+
# Column Comment Support:
|
62
|
+
spec[:comment] = "\"#{column.comment}\"" if column.respond_to?(:comment) and column.comment.present?
|
63
|
+
(spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.inspect} => ")}
|
64
|
+
spec
|
65
|
+
end.compact
|
66
|
+
|
67
|
+
# find all migration keys used in this table
|
68
|
+
keys = [:name, :limit, :unsigned, :precision, :scale, :default, :null, :comment] & column_specs.map{ |k| k.keys }.flatten
|
69
|
+
|
70
|
+
# figure out the lengths for each column based on above keys
|
71
|
+
lengths = keys.map{ |key| column_specs.map{ |spec| spec[key] ? spec[key].length + 2 : 0 }.max }
|
72
|
+
|
73
|
+
# the string we're going to sprintf our values against, with standardized column widths
|
74
|
+
format_string = lengths.map{ |len| "%-#{len}s" }
|
75
|
+
|
76
|
+
# find the max length for the 'type' column, which is special
|
77
|
+
type_length = column_specs.map{ |column| column[:type].length }.max
|
78
|
+
|
79
|
+
# add column type definition to our format string
|
80
|
+
format_string.unshift " t.%-#{type_length}s "
|
81
|
+
|
82
|
+
format_string *= ''
|
83
|
+
|
84
|
+
column_specs.each do |colspec|
|
85
|
+
values = keys.zip(lengths).map{ |key, len| colspec.key?(key) ? colspec[key] + ", " : " " * len }
|
86
|
+
values.unshift colspec[:type]
|
87
|
+
tbl.print((format_string % values).gsub(/,\s*$/, ''))
|
88
|
+
tbl.puts
|
89
|
+
end
|
90
|
+
|
91
|
+
tbl.puts " end"
|
92
|
+
tbl.puts
|
93
|
+
|
94
|
+
indexes(table, tbl)
|
95
|
+
|
96
|
+
tbl.rewind
|
97
|
+
stream.print tbl.read
|
98
|
+
rescue => e
|
99
|
+
stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
|
100
|
+
stream.puts "# #{e.message}"
|
101
|
+
stream.puts
|
102
|
+
end
|
103
|
+
|
104
|
+
stream
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module ActiveRecord
|
3
|
+
module DatabaseSchema
|
4
|
+
|
5
|
+
class Railtie < ::Rails::Railtie #:nodoc:
|
6
|
+
|
7
|
+
initializer 'active_record.extended_support.database_schema' do |app|
|
8
|
+
|
9
|
+
ActiveSupport.on_load(:active_record) do
|
10
|
+
|
11
|
+
# Agregar el modulo de parches al SchemaDumper de ActiveRecord:
|
12
|
+
require 'active_record/database_schema/dumper'
|
13
|
+
ActiveRecord::SchemaDumper.send :include, ActiveRecord::DatabaseSchema::Dumper
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-database_schema
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Roberto Quintanilla Gonzalez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.12
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.12
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: mysql2
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Support for unsigned columns and comments in ActiveRecord Schema Dumps.
|
47
|
+
email:
|
48
|
+
- roberto.quintanilla@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/active_record/database_schema/dumper.rb
|
54
|
+
- lib/active_record/database_schema/railtie.rb
|
55
|
+
- lib/activerecord-database_schema.rb
|
56
|
+
- MIT-LICENSE
|
57
|
+
- Rakefile
|
58
|
+
- README.rdoc
|
59
|
+
homepage: https://github.com/vovimayhem
|
60
|
+
licenses: []
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.8.25
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Support for unsigned columns and comments in ActiveRecord Schema Dumps.
|
83
|
+
test_files: []
|