-
Notifications
You must be signed in to change notification settings - Fork 67
Multi database support #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,28 @@ | ||
| database_name = "avram_dev" | ||
|
|
||
| Avram::Repo.configure do |settings| | ||
| class TestDatabase < Avram::Database | ||
| end | ||
|
|
||
| class DatabaseWithIncorrectSettings < Avram::Database | ||
| end | ||
|
|
||
| TestDatabase.configure do |settings| | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Configure databases |
||
| settings.url = ENV["DATABASE_URL"]? || Avram::PostgresURL.build( | ||
| hostname: "db", | ||
| database: database_name, | ||
| username: "lucky", | ||
| password: "developer" | ||
| ) | ||
| end | ||
|
|
||
| DatabaseWithIncorrectSettings.configure do |settings| | ||
| settings.url = Avram::PostgresURL.build( | ||
| hostname: "db", | ||
| database: database_name, | ||
| username: "incorrect" | ||
| ) | ||
| end | ||
|
|
||
| Avram.configure do |settings| | ||
| settings.database_to_migrate = TestDatabase.new | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| require "./spec_helper" | ||
|
|
||
| private class ModelWithBadDatabase < BaseModel | ||
| table do | ||
| end | ||
|
|
||
| # Use a different database for this fake model | ||
| def self.database | ||
| DatabaseWithIncorrectSettings | ||
| end | ||
| end | ||
|
|
||
| describe "Configuring and connecting to different databases" do | ||
| it "tries to connect to the configured database" do | ||
| # It will fail to connect which is what we expect since we configured | ||
| # a database with an incorrect URL | ||
| # | ||
| # If it does not raise an error then the connection is good, | ||
| # which is not what we configured | ||
| expect_raises Avram::ConnectionError do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this raise an exception error message letting us know which one it wasn't able to connect to? Our app could have up to 4 PG connections technically (a bit absurd, I know), so if for some reason one of them didn't connection, it would be nice to see
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes great point! |
||
| ModelWithBadDatabase::BaseQuery.new.select_count | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| require "spec" | ||
| require "../src/avram" | ||
| require "./support/base_model" | ||
| require "./support/**" | ||
| require "../config/database" | ||
|
|
||
| Db::Create.new.call | ||
| Db::Migrate.new.call | ||
|
|
||
| Spec.before_each do | ||
| Avram::Repo.truncate | ||
| TestDatabase.truncate | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class BaseModel < Avram::Model | ||
| def self.database | ||
| TestDatabase | ||
| end | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is how you set what database a model uses. Can be done on the abstract class or could be overridden per model |
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class Blob < Avram::Model | ||
| class Blob < BaseModel | ||
| table do | ||
| column doc : JSON::Any? | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class Company < Avram::Model | ||
| class Company < BaseModel | ||
| table do | ||
| column sales : Int64 | ||
| column earnings : Float64 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class LineItem < Avram::Model | ||
| class LineItem < BaseModel | ||
| skip_default_columns | ||
|
|
||
| table do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class MenuOption < Avram::Model | ||
| class MenuOption < BaseModel | ||
| skip_default_columns | ||
|
|
||
| table do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class Price < Avram::Model | ||
| class Price < BaseModel | ||
| skip_default_columns | ||
|
|
||
| table do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class Product < Avram::Model | ||
| class Product < BaseModel | ||
| skip_default_columns | ||
|
|
||
| table do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class Scan < Avram::Model | ||
| class Scan < BaseModel | ||
| skip_default_columns | ||
|
|
||
| table do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class SignInCredential < Avram::Model | ||
| class SignInCredential < BaseModel | ||
| table do | ||
| belongs_to user : User | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class Tag < Avram::Model | ||
| class Tag < BaseModel | ||
| skip_default_columns | ||
|
|
||
| table do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class Tagging < Avram::Model | ||
| class Tagging < BaseModel | ||
| skip_default_columns | ||
|
|
||
| table do | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is how to create a new Database