Class: HerokuSchemas::SchemaReference
- Inherits:
-
Object
- Object
- HerokuSchemas::SchemaReference
- Defined in:
- lib/heroku-schemas/schema_reference.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#base_database_url ⇒ Object
readonly
Returns the value of attribute base_database_url.
-
#database_app ⇒ Object
readonly
Returns the value of attribute database_app.
-
#database_url ⇒ Object
readonly
Returns the value of attribute database_url.
-
#database_variable ⇒ Object
readonly
Returns the value of attribute database_variable.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #==(schema_reference) ⇒ Object
- #app_config_variables(app) ⇒ Object
- #configure ⇒ Object
- #find_database_variable(config_variables, database_url) ⇒ Object
- #get_base_database_url(url) ⇒ Object
-
#initialize(options) ⇒ SchemaReference
constructor
String references to schemas can take the following forms: my_app my_app:my_schema my_app:MY_DATABASE_URL:my_schema.
Constructor Details
#initialize(options) ⇒ SchemaReference
String references to schemas can take the following forms: my_app my_app:my_schema my_app:MY_DATABASE_URL:my_schema
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/heroku-schemas/schema_reference.rb', line 10 def initialize() defaults = { heroku: nil, string_reference: nil, related_apps: [] } .reverse_merge!(defaults) @heroku = [:heroku] @string_reference = [:string_reference] @related_apps = [:related_apps] @database_variable = nil @schema = nil raise 'String reference not provided' if @string_reference.blank? configure end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
3 4 5 |
# File 'lib/heroku-schemas/schema_reference.rb', line 3 def app @app end |
#base_database_url ⇒ Object (readonly)
Returns the value of attribute base_database_url.
3 4 5 |
# File 'lib/heroku-schemas/schema_reference.rb', line 3 def base_database_url @base_database_url end |
#database_app ⇒ Object (readonly)
Returns the value of attribute database_app.
3 4 5 |
# File 'lib/heroku-schemas/schema_reference.rb', line 3 def database_app @database_app end |
#database_url ⇒ Object (readonly)
Returns the value of attribute database_url.
3 4 5 |
# File 'lib/heroku-schemas/schema_reference.rb', line 3 def database_url @database_url end |
#database_variable ⇒ Object (readonly)
Returns the value of attribute database_variable.
3 4 5 |
# File 'lib/heroku-schemas/schema_reference.rb', line 3 def database_variable @database_variable end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
3 4 5 |
# File 'lib/heroku-schemas/schema_reference.rb', line 3 def schema @schema end |
Instance Method Details
#==(schema_reference) ⇒ Object
82 83 84 85 86 |
# File 'lib/heroku-schemas/schema_reference.rb', line 82 def ==(schema_reference) return true if self.base_database_url == schema_reference.base_database_url && self.schema == schema_reference.schema false end |
#app_config_variables(app) ⇒ Object
73 74 75 |
# File 'lib/heroku-schemas/schema_reference.rb', line 73 def app_config_variables(app) @heroku.get_config_vars(app).body end |
#configure ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/heroku-schemas/schema_reference.rb', line 27 def configure refs = @string_reference.split(':') raise "Invalid schema reference: #{@string_reference}" unless [1, 2, 3].include?(refs.length) if refs.length == 1 @app = refs.first elsif refs.length == 2 @app, @schema = refs else @app, @database_variable, @schema = refs end app_variables = app_config_variables(@app) @database_variable ||= 'DATABASE_URL' @database_url = app_variables[@database_variable] @schema ||= SchemaUtilities.url_to_schema(@database_url) raise "Database URL not found for database variable #{@database_variable}" if @database_url.blank? = SchemaUtilities.validate_schema(@schema) raise if != true @database_url = @base_database_url = get_base_database_url(@database_url) @database_url = "#{@database_url}?schema_search_path=#{@schema}" unless @schema == 'public' if @database_variable == 'DATABASE_URL' @related_apps.each do |app| config_variables = app_config_variables(app) database_variable = find_database_variable(config_variables, @base_database_url) if database_variable @database_variable = database_variable @database_app = app break end end end raise "Database variable not found" if @database_variable.blank? end |
#find_database_variable(config_variables, database_url) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/heroku-schemas/schema_reference.rb', line 64 def find_database_variable(config_variables, database_url) standardized_database_url = get_base_database_url(database_url) config_variables.each do |name, value| next if name == 'DATABASE_URL' return name if get_base_database_url(value) == standardized_database_url end nil end |
#get_base_database_url(url) ⇒ Object
77 78 79 80 |
# File 'lib/heroku-schemas/schema_reference.rb', line 77 def get_base_database_url(url) return nil if url.nil? url.split('?').first end |