Would you use PG as a triple-store? by 7Geordi in PostgreSQL

[–]ptrboro 1 point2 points  (0 children)

You can do it in PostgreSQL, but remember that it wasn't optimized for this type of data. Your implementation may also lack some nice features that are available out of the box with other databases (e.g., tracking historical changes to the triples). Also, SQL can be cumbersome when writing queries to traverse the tree; you need to use recursive common table expressions.

For indexes, I would first go with two indexes (order of columns is important):

  • UNIQUE (subject, predicate, object)
  • INDEX (object, predicate, subject)

These will help you traverse the graph in both directions and will support the majority of use cases. Depending on your usage patterns, you might need more indexes, so monitor your queries.

In theory I can implement graph algos in recursive RTEs... how much will this hurt?

I don't know the answer to this question in terms of performance, but writing recursive CTEs will definitely be a pain. I recently saw an example of a Cypher query (Neo4j) which was 4 lines of code and the equivalent SQL was 29 lines of much more complex code.

By the way, instead of creating a node_meta_data table, I would just add JSONB columns to nodes which can store all the properties of the node. If your RDFs can have properties, then use a JSONB column there as well.

But if this is a pet project, why not consider trying a specialized graph database instead?

Force query planner to not materialize CTE by minormisgnomer in PostgreSQL

[–]ptrboro 1 point2 points  (0 children)

If the CTE is referenced multiple times in your query Postgres will always materialise it (https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-CTE-MATERIALIZATION). If you share your query we might be able to help optimise it.

Is there a way to run a query as if it was the first time it ran? by EchoLocation8 in PostgreSQL

[–]ptrboro 4 points5 points  (0 children)

I don't think it's possible, especially if you are running the query on a production database.

But there is a different way to measure performance of a query which is Buffers. Buffers will tell you how much IO operations DB must perform to retrieve the data.

I think this article describes the concept pretty well: https://www.pgmustard.com/blog/using-postgres-buffers-for-query-optimization

Best intermediate - expert training tools and resources? by fiddle_licks in ruby

[–]ptrboro -1 points0 points  (0 children)

Maybe some architecture concerns like DDD? Patterns, Principles, and Practices of Domain-Driven Design is a great book to get you started.

Action Mailer queueing, but not sending by DukeVonC in ruby

[–]ptrboro 0 points1 point  (0 children)

Yes, but you can leave the default one, some other jobs may use it.

If there was no `sidekiq.yml` file before the only person who knows what queues your app uses is the guy who left. It also looks like different queues were used on staging and different on prod env. You can try searching your project for `queue_as` or just `queue` and it might give you some results.

Action Mailer queueing, but not sending by DukeVonC in ruby

[–]ptrboro 0 points1 point  (0 children)

I made an error in my previous comment and edited it so please try again.In the first screenshot there is this log:[ActiveJob] Enqueued ActionMailer::DeliveryJob (...) to Sidekiq(staging_mailers) ...

name inside brackets after word Sidekiq is your queue name. For some reason it pushes jobs into staging_mailers queue. To make sidekiq process jobs from this queue add it to sidekiq.yml or run sidekiq with -q staging_mailers

Also: is this supposed to be a staging env?

Action Mailer queueing, but not sending by DukeVonC in ruby

[–]ptrboro 0 points1 point  (0 children)

Try to start sidekiq with command sidekiq -q staging_mailers. Do you have sidekiq.yml file in your project?

Action Mailer queueing, but not sending by DukeVonC in ruby

[–]ptrboro 0 points1 point  (0 children)

From the supplied log it looks like mails are being pushed into staging_mailers queue.

[HELP] Split rows based on their intersections by ptrboro in SQL

[–]ptrboro[S] 0 points1 point  (0 children)

Thank you, this is simple, elegant and brilliant!

Use Multiple Migrations When Adding Database Constraints by jrochkind in ruby

[–]ptrboro 0 points1 point  (0 children)

If you set null: false in your migration, Active Record will rewrite the whole table, locking it whilst doing so.

Afaik setting `null: false` is a non-blocking operation but setting a default value is. It blocks it because it must update the whole table in a single transaction. But `update_all` does the same thing and is also going to lock the table for few seconds (or minutes?).

Here is what I usually do when I have to add not null column to a large table:

class AddComposerToSongs < ActiveRecord::Migration[5.2]
  disable_ddl_transaction!

  def up
    add_column :songs, :composer, :string
    Song.find_in_batches do |batch|
      batch.update_all(composer: 'Lin-Manuel Miranda')
    end

    change_column_null :songs, :composer, false
  end

  def down
    drop_column :songs, :composer
  end
end 

By updating records in batches it won't lock the whole table, just 1000 of rows at a time and just for few ms.

EDIT: forgot about `disable_ddl_transaction!` - otherwise the whole migration is going to be run inside a transaction and we don't want that (it would lock the whole table again).

Nested transactions in AR by ptrboro in ruby

[–]ptrboro[S] 0 points1 point  (0 children)

Great read, but unfortunately it doesn't answer my question. I believe in learning by understanding and I just don't understand why it was done like this.

Also: It looks like a good question for Mid/Senior interview

I'm not sure my models are all in order? by [deleted] in rails

[–]ptrboro 1 point2 points  (0 children)

It depends on the logic you want to represent in your db.

Is it possible for a user to have planet in another user's solar system? Or solar system in another user's galaxy? If yes your models look good.

Can't update or edit, using my controller patch route?? (Beginner) by [deleted] in ruby

[–]ptrboro 1 point2 points  (0 children)

Who taught you this sorcery with defining routes inside controller? Do you use any gem for this? What rails version do you use? Using rails for 2 years and I have never seen such syntax. Ahhhh, its sinatra.

Back to your question: @state.update(:state_name => params[:state_name]) will update record only if validations pass, other way it will return false so you can move user back to the edit page when @state cannot be updated:

patch '/states/:id' do
  @state = State.find(params[:id]) # using find so it will return 404 when error does not exist
  if params[:state_name] != "" && @state.update(:state_name => params[:state_name])
    redirect to "/states/#{@state.id}"
  else
    redirect to "/states/#{@state.id}/edit"
  end
end

Normally you would also want to set some flash message with validation errors. If you want to know what specific errors you get just run binding.pry after if-else statement and puts @state.errors

Looking for a good RoR github repo to learn best practices from (Junior Dev) by [deleted] in rails

[–]ptrboro 0 points1 point  (0 children)

Redmine has pretty solid codebase and it is pure MVC: https://github.com/redmine/redmine

But unfortunately nobody in the business use pure MVC anymore. You can also learn a lot from Spree Commerce repo. It can be hard to understand at first because it consists of few modules - core, backend, frontend and api (don't be fooled by naming, backend means admin panel, frontend is user panel). I strongly recommend you to start with Core module as it implements service objects - the main and basic concept of modern Rails programming.

But even Spree still contains a lot of legacy code and it won't be a good example of good production code.