Module: ViewsInMigrations::SchemaStatements
- Defined in:
- lib/views_in_migrations.rb
Instance Method Summary collapse
- #assert_absence_of_column(klass_name, column) ⇒ Object
- #assert_presence_of_column(klass_name, column) ⇒ Object
- #create_view(view_name, *args) ⇒ Object
- #current_column_names_for(klass_name) ⇒ Object
- #define_view(view_name, for_dump = false) ⇒ Object
- #drop_view(view_name, options = {}) ⇒ Object
- #refresh_view(view_name) ⇒ Object
- #refresh_views ⇒ Object
- #sanitize_view_sql(view_sql, for_dump = false) ⇒ Object
- #table_is_view?(table_name) ⇒ Boolean
- #view_definition(view_name) ⇒ Object
- #views ⇒ Object
Instance Method Details
#assert_absence_of_column(klass_name, column) ⇒ Object
96 97 98 99 100 |
# File 'lib/views_in_migrations.rb', line 96 def assert_absence_of_column(klass_name, column) if current_column_names_for(klass_name).include?(column.to_s) raise Mysql::Error.new("Invalid view on #{klass_name}, has invalid column: #{column}") end end |
#assert_presence_of_column(klass_name, column) ⇒ Object
90 91 92 93 94 |
# File 'lib/views_in_migrations.rb', line 90 def assert_presence_of_column(klass_name, column) unless current_column_names_for(klass_name).include?(column.to_s) raise Mysql::Error.new("Invalid view on #{klass_name}, missing required column: #{column}") end end |
#create_view(view_name, *args) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/views_in_migrations.rb', line 17 def create_view(view_name, *args) = {} if args.first.kind_of?(String) select_statement = args.first = args. end if [:force] && table_exists?(view_name) drop_view(view_name, ) end if select_statement [:options] ||= " WITH CHECK OPTION" view_sql = "CREATE VIEW #{quote_table_name(view_name)} AS " view_sql << select_statement view_sql << "#{[:options]}" execute view_sql end end |
#current_column_names_for(klass_name) ⇒ Object
102 103 104 105 106 |
# File 'lib/views_in_migrations.rb', line 102 def current_column_names_for(klass_name) klass = klass_name.to_s.classify.constantize klass.reset_column_information klass.column_names end |
#define_view(view_name, for_dump = false) ⇒ Object
46 47 48 |
# File 'lib/views_in_migrations.rb', line 46 def define_view(view_name, for_dump = false) sanitize_view_sql(view_definition(view_name), for_dump) end |
#drop_view(view_name, options = {}) ⇒ Object
13 14 15 |
# File 'lib/views_in_migrations.rb', line 13 def drop_view(view_name, = {}) execute "DROP VIEW IF EXISTS #{quote_table_name(view_name)}" end |
#refresh_view(view_name) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/views_in_migrations.rb', line 79 def refresh_view(view_name) raise "#{view_name.inspect} is not a view" unless table_is_view?(view_name) sql = define_view(view_name) drop_view(view_name) create_view(view_name,sql) end |
#refresh_views ⇒ Object
86 87 88 |
# File 'lib/views_in_migrations.rb', line 86 def refresh_views views.each{|view| refresh_view!(view)} end |
#sanitize_view_sql(view_sql, for_dump = false) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/views_in_migrations.rb', line 50 def sanitize_view_sql(view_sql, for_dump = false) sql = view_sql.dup # remove newlines sql.gsub!(/\n|^\s*|\s*$/,'') # we can just use SELECT *... sql.gsub!(/SELECT .* FROM/i, "SELECT * FROM" ) sql.gsub!('where', 'WHERE') if for_dump #insert newlines for readability sql.gsub!(/(from|where|in|\() /i, '\n\1 ') end sql end |
#table_is_view?(table_name) ⇒ Boolean
75 76 77 |
# File 'lib/views_in_migrations.rb', line 75 def table_is_view?(table_name) views.include?(table_name) end |
#view_definition(view_name) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/views_in_migrations.rb', line 38 def view_definition(view_name) select_value(%{ SELECT view_definition FROM information_schema.views WHERE table_name = #{view_name.inspect} AND table_schema = #{current_database.inspect} }) end |
#views ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/views_in_migrations.rb', line 67 def views select_values( %{ select table_name FROM information_schema.views WHERE table_schema = #{current_database.inspect} }) end |