Skip to content

change from present? (Rails) to native Ruby check #57

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

Merged
merged 4 commits into from
Sep 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/weaviate/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create(
req.body["properties"] = properties unless properties.nil?
if multi_tenant.is_a?(Hash)
req.body["multiTenancyConfig"] = multi_tenant
elsif multi_tenant.present?
elsif multi_tenant && !(multi_tenant.respond_to?(:empty?) && multi_tenant.empty?)
Copy link
Collaborator

@andreibondarev andreibondarev Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mculp If you look at the method signature: multi_tenant: nil, so it's either nil or "whatever the user passes in" (which should be true.

Copy link
Collaborator

@andreibondarev andreibondarev Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should just change the method signature to:

def create(
  multi_tenant: false

and then in the method itself, always set the multiTenancyConfig to:

req.body["multiTenancyConfig"] = {enabled: multi_tenant}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this code is allowing a Hash to be passed in and set instead of { enabled: true }

if multi_tenant.is_a?(Hash)
  req.body["multiTenancyConfig"] = multi_tenant

I think because this is valid, according to the docs:

    multi_tenancy_config=Configure.multi_tenancy(
        enabled=True,
        auto_tenant_creation=True
    )

so looks like there are two options here we need to handle

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt about:

def create(
  multi_tenant: false,
  auto_tenant_creation: false,

# ...

req.body["multiTenancyConfig"] = { enabled: multi_tenant }
req.body["multiTenancyConfig"].merge!(auto_tenant_creation: auto_tenant_creation) if auto_tenant_creation

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mculp I don't see the auto_tenant_creation option. I'm not sure what the Python client implement but this gem just interfaces with the REST API.

https://weaviate.io/developers/weaviate/config-refs/schema

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if you'd like to change it -- let's do it!

Copy link
Contributor Author

@mculp mculp Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a couple real HTTP calls from the client and checked that this did automatically create tenants when set up properly. I think it'd be nice to use something like VCR for recording requests and responses for use in tests. I started to set it up in this PR, but decided to limit the changeset here as much as I could.

I left the is_a?(Hash) check in there for backwards compatibility. It seems like whoever added this is using the code like:

schema.create({
  class_name: "Question",
  multi_tenant: { enabled: true, autoTenantCreation: true, autoTenantActivation: true }
})

I added a shortcut:

schema.create({
  class_name: "Question",
  multi_tenant: true,
  auto_tenant_creation: true,
  auto_tenant_activation: true
})

These result in the same multiTenancyConfig, and I added a spec for the new case.

It seems a little less than optimal, but I didn't want scope to creep too far. I'm mainly here about the .present? issue.

That said, while I was testing, I found one more instance of .blank? (another Rails method, the inverse of .present?) and just changed it to .nil? (Ruby method).

While I was writing this code, I had a lot of ideas for refactoring/simplification, but wanted to go ahead and ship this before moving on to anything else.

req.body["multiTenancyConfig"] = {enabled: true}
end
req.body["invertedIndexConfig"] = inverted_index_config unless inverted_index_config.nil?
Expand Down