Class: Tmpfk::CLI
- Inherits:
-
Thor
- Object
- Thor
- Tmpfk::CLI
- Defined in:
- lib/tmpfk/cli.rb
Instance Method Summary collapse
Instance Method Details
#add ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/tmpfk/cli.rb', line 8 def add conn.data_sources.each do |table| next if conn.view_exists?(table) ActiveRecord::Base.table_name = table ActiveRecord::Base.columns.each do |column| fk_name = "#{[:prefix]}#{table}_#{column.name}_fk" primary_key = 'id' next unless column.foreign_key? next if conn.foreign_key_exists?(table, name: fk_name) begin conn.add_foreign_key( table, column.foreign_table, column: column.name, primary_key: primary_key, name: fk_name ) puts "ALTER TABLE #{table} ADD CONSTRAINT #{fk_name} FOREIGN KEY (#{column.name}) REFERENCES #{column.foreign_table} (#{primary_key})" rescue ActiveRecord::InvalidForeignKey => e c = Term::ANSIColor puts c.red(e.to_s) end end end puts 'Done.' end |
#drop ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/tmpfk/cli.rb', line 36 def drop conn.data_sources.each do |table| next if conn.view_exists?(table) ActiveRecord::Base.table_name = table ActiveRecord::Base.columns.each do |column| fk_name = "#{[:prefix]}#{table}_#{column.name}_fk" next unless column.foreign_key? next unless conn.foreign_key_exists?(table, name: fk_name) begin conn.remove_foreign_key( table, name: fk_name ) puts "ALTER TABLE #{table} DROP FOREIGN KEY #{fk_name}" rescue ActiveRecord::InvalidForeignKey => e c = Term::ANSIColor puts c.red(e.to_s) end end end puts 'Done.' end |