|
1 | 1 | module Hyperloop
|
2 | 2 | module AutoCreate
|
| 3 | + def table_exists? |
| 4 | + # works with both rails 4 and 5 without deprecation warnings |
| 5 | + if connection.respond_to?(:data_sources) |
| 6 | + connection.data_sources.include?(table_name) |
| 7 | + else |
| 8 | + connection.tables.include?(table_name) |
| 9 | + end |
| 10 | + end |
| 11 | + |
3 | 12 | def needs_init?
|
4 |
| - return false if Hyperloop.transport == :none |
5 |
| - return true if connection.respond_to?(:data_sources) && !connection.data_sources.include?(table_name) |
6 |
| - return true if !connection.respond_to?(:data_sources) && !connection.tables.include?(table_name) |
7 |
| - Hyperloop.on_server? |
8 |
| - # the above line appears equivilent to the following two original lines |
9 |
| - # the only |
10 |
| - # return false unless Hyperloop.on_server? |
11 |
| - # return true if defined?(Rails::Server) |
12 |
| - # the following lines appeared to be unreachable but perhaps because on_server uses Rails.const_defined? it has some |
13 |
| - # subtle difference. |
14 |
| - # return true unless Connection.root_path |
15 |
| - # uri = URI("#{Connection.root_path}server_up") |
16 |
| - # http = Net::HTTP.new(uri.host, uri.port) |
17 |
| - # request = Net::HTTP::Get.new(uri.path) |
18 |
| - # if uri.scheme == 'https' |
19 |
| - # http.use_ssl = true |
20 |
| - # http.verify_mode = OpenSSL::SSL::VERIFY_NONE |
21 |
| - # end |
22 |
| - # http.request(request) && return rescue true |
| 13 | + Hyperloop.transport != :none && Hyperloop.on_server? && !table_exists? |
23 | 14 | end
|
24 | 15 |
|
25 | 16 | def create_table(*args, &block)
|
@@ -111,6 +102,10 @@ class << self
|
111 | 102 | attr_accessor :transport
|
112 | 103 |
|
113 | 104 | def active
|
| 105 | + # if table doesn't exist then we are either calling from within |
| 106 | + # a migration or from a console before the server has ever started |
| 107 | + # in these cases there are no channels so we return nothing |
| 108 | + return [] unless table_exists? |
114 | 109 | if Hyperloop.on_server?
|
115 | 110 | expired.delete_all
|
116 | 111 | refresh_connections if needs_refresh?
|
@@ -158,9 +153,10 @@ def root_path=(path)
|
158 | 153 | end
|
159 | 154 |
|
160 | 155 | def root_path
|
161 |
| - QueuedMessage.root_path |
162 |
| - rescue |
163 |
| - nil |
| 156 | + # if the QueuedMessage table doesn't exist then we are either calling from within |
| 157 | + # a migration or from a console before the server has ever started |
| 158 | + # in these cases there is no root path to the server |
| 159 | + QueuedMessage.root_path if QueuedMessage.table_exists? |
164 | 160 | end
|
165 | 161 |
|
166 | 162 | def refresh_connections
|
|
0 commit comments