Class: EnterpriseMti::Migration::SqlFactory::SqlFactory
- Inherits:
-
Object
- Object
- EnterpriseMti::Migration::SqlFactory::SqlFactory
- Defined in:
- lib/enterprise_mti/migration/sql_factory/sql_factory.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#subclass_tables ⇒ Object
Returns the value of attribute subclass_tables.
-
#superclass_table ⇒ Object
Returns the value of attribute superclass_table.
Instance Method Summary collapse
- #add_column(column, opts = {}) ⇒ Object
- #add_constraint(name, opts = {}) ⇒ Object
- #alter_column(column, opts = {}) ⇒ Object
- #alter_table(table) ⇒ Object
- #defer ⇒ Object
- #drop_column(column) ⇒ Object
- #drop_constraint(name) ⇒ Object
- #not_null ⇒ Object
-
#options_parser(opts = {}) ⇒ Object
DSL ##.
- #references(opts = {}) ⇒ Object
- #sql_for_down ⇒ Object
- #sql_for_up ⇒ Object
-
#subclass_tables_down ⇒ Object
Down methods (subclass) ##.
- #subclass_tables_id_alterations_down ⇒ Object
- #subclass_tables_id_alterations_up ⇒ Object
-
#subclass_tables_up ⇒ Object
Up methods (subclass) ##.
-
#superclass_table_down ⇒ Object
Down methods (superclass) ##.
- #superclass_table_foreign_keys_down ⇒ Object
- #superclass_table_foreign_keys_up ⇒ Object
-
#superclass_table_up ⇒ Object
Up methods (superclass) ##.
- #superclass_table_xor_constraint_down ⇒ Object
- #superclass_table_xor_constraint_up ⇒ Object
- #unique ⇒ Object
Instance Attribute Details
#subclass_tables ⇒ Object
Returns the value of attribute subclass_tables.
6 7 8 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 6 def subclass_tables @subclass_tables end |
#superclass_table ⇒ Object
Returns the value of attribute superclass_table.
6 7 8 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 6 def superclass_table @superclass_table end |
Instance Method Details
#add_column(column, opts = {}) ⇒ Object
107 108 109 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 107 def add_column(column, opts={}) "ADD COLUMN #{column} #{ opts}" end |
#add_constraint(name, opts = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 117 def add_constraint(name, opts={}) case opts[:type] when :check type = 'CHECK (' suffix = ')' when :foreign_key type = "FOREIGN KEY(#{opts[:column]})" end "ADD CONSTRAINT #{name} #{type} #{yield} #{suffix}" end |
#alter_column(column, opts = {}) ⇒ Object
110 111 112 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 110 def alter_column(column, opts={}) "ALTER COLUMN #{column} SET #{ opts}" end |
#alter_table(table) ⇒ Object
106 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 106 def alter_table(table); "ALTER TABLE #{table} #{yield};"; end |
#defer ⇒ Object
128 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 128 def defer; "DEFERRABLE INITIALLY DEFERRED"; end |
#drop_column(column) ⇒ Object
113 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 113 def drop_column(column); "DROP COLUMN #{column}"; end |
#drop_constraint(name) ⇒ Object
127 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 127 def drop_constraint(name); "DROP CONSTRAINT #{name}"; end |
#not_null ⇒ Object
114 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 114 def not_null; "NOT NULL"; end |
#options_parser(opts = {}) ⇒ Object
DSL ##
97 98 99 100 101 102 103 104 105 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 97 def (opts={}) sql = [] sql.push opts[:type] sql.push not_null if opts[:nullable] == false sql.push unique if opts[:unique] sql.push references table: opts[:references][:table], column: opts[:references][:column] if opts[:references] sql.push defer if opts[:defer] sql.join(' ') end |
#references(opts = {}) ⇒ Object
116 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 116 def references(opts={}); "REFERENCES #{opts[:table]}(#{opts[:column]})"; end |
#sql_for_down ⇒ Object
12 13 14 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 12 def sql_for_down [subclass_tables_down, superclass_table_down].join end |
#sql_for_up ⇒ Object
8 9 10 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 8 def sql_for_up [superclass_table_up, subclass_tables_up].join end |
#subclass_tables_down ⇒ Object
Down methods (subclass) ##
83 84 85 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 83 def subclass_tables_down subclass_tables_id_alterations_down end |
#subclass_tables_id_alterations_down ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 87 def subclass_tables_id_alterations_down subclass_tables.map do |subclass_table| alter_table subclass_table do drop_constraint 'id_fkey' end end end |
#subclass_tables_id_alterations_up ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 51 def subclass_tables_id_alterations_up subclass_tables.map { |subclass_table| alter_table subclass_table do add_constraint "id_fkey", type: :foreign_key, column: 'id' do references: { table: superclass_table, column: "#{subclass_table}_id" }, defer: true end end } end |
#subclass_tables_up ⇒ Object
Up methods (subclass) ##
47 48 49 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 47 def subclass_tables_up subclass_tables_id_alterations_up end |
#superclass_table_down ⇒ Object
Down methods (superclass) ##
63 64 65 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 63 def superclass_table_down [superclass_table_xor_constraint_down, superclass_table_foreign_keys_down] end |
#superclass_table_foreign_keys_down ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 73 def superclass_table_foreign_keys_down subclass_tables.map do |subclass_table| alter_table superclass_table do drop_column("#{subclass_table}_id") end end end |
#superclass_table_foreign_keys_up ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 22 def superclass_table_foreign_keys_up subclass_tables.map { |subclass_table| alter_table superclass_table do add_column "#{subclass_table}_id", type: id_type, nullable: false, unique: true, references: { table: subclass_table, column: "id" }, defer: true end } end |
#superclass_table_up ⇒ Object
Up methods (superclass) ##
18 19 20 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 18 def superclass_table_up [superclass_table_foreign_keys_up, superclass_table_xor_constraint_up] end |
#superclass_table_xor_constraint_down ⇒ Object
67 68 69 70 71 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 67 def superclass_table_xor_constraint_down alter_table superclass_table do drop_constraint "#{superclass_table}_xor" end end |
#superclass_table_xor_constraint_up ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 35 def superclass_table_xor_constraint_up alter_table superclass_table do add_constraint "#{superclass_table}_xor", type: :check do subclass_tables.map { |subclass_table| "(#{subclass_table}_id IS #{not_null})::#{integer}" }.join(' + ') << ' = 1' end end end |
#unique ⇒ Object
115 |
# File 'lib/enterprise_mti/migration/sql_factory/sql_factory.rb', line 115 def unique; "UNIQUE"; end |