GithubHelp home page GithubHelp logo

Comments (7)

NullVoxPopuli avatar NullVoxPopuli commented on July 4, 2024

Looks like this works:

 Logidze.without_logging { 
  MyRecord.connection.execute("SELECT logidze_snapshot('#{my_record.attributes.to_json}'::jsonb)")
}

from logidze.

palkan avatar palkan commented on July 4, 2024

Looks like this works:

Yep, without_logging disables only triggers.

But we don't have a procedure to update existing log_data (i.e. to add a new snapshot).
logidze_snapshot can be used only to create the initial log_data.

I think, it's a good idea to have an ability to manually create versions. Will put it on the TODO list)
Thanks!

from logidze.

NullVoxPopuli avatar NullVoxPopuli commented on July 4, 2024

oh. how does the updating process work?
(This is something I need for work)

maybe logidze_version instead of logidze_snapshot?

from logidze.

palkan avatar palkan commented on July 4, 2024

Take a look at the trigger function.

If you just want to add a new snapshot, you can do smth like this:

 CREATE OR REPLACE FUNCTION logidze_add_snapshot(row jsonb, log_data jsonb) RETURNS jsonb AS $body$
   DECLARE
     new_v integer;
     size integer;
     new_log_data jsonb;
   BEGIN
     -- Calculate new version number
     new_v := (row#>>'{log_data,h,-1,v}')::int + 1;
     -- Calculate the size of log
     size := jsonb_array_length(row#>'{log_data,h}');
     
     -- Add snapshot to the end of the log
     new_log_data := jsonb_set(
       row->'log_data',
       ARRAY['h', size::text],
       logidze_version(new_v, logidze_snapshot(row)),
       true
     );
     
     -- Update version number
     new_log_data := jsonb_set(
       new_log_data,
       '{v}',
       to_jsonb(new_v)
     );

     RETURN new_log_data;
   END
 $body$
 LANGUAGE plpgsql;

And then:

UPDATE users SET log_data = logidze_add_snapshot(to_jsonb(users.*), log_data) WHERE id=123

Note that the script above doesn't consider columns filtering and the history size limit.

from logidze.

NullVoxPopuli avatar NullVoxPopuli commented on July 4, 2024

This is what I have so far:

      CREATE OR REPLACE FUNCTION add_to_snapshot(id bigint, tbl text) RETURNS jsonb AS $body$
        BEGIN
          EXECUTE format('
            UPDATE %1$s
            SET log_data = logidze_add_snapshot(to_jsonb(%1$s.*), log_data)
            WHERE %1$s.id = %2$s
          ', tbl, id);
        END;
      $body$
      LANGUAGE plpgsql;

      CREATE OR REPLACE FUNCTION logidze_add_snapshot(obj jsonb, log_data jsonb) RETURNS jsonb AS $body$
        DECLARE
          new_v integer;
          size integer;
          new_log_data jsonb;
        BEGIN
          -- Calculate new version number
          new_v := (obj#>>'{log_data,h,-1,v}')::int + 1;
          -- Calculate the size of log
          size := jsonb_array_length(obj#>'{log_data,h}');

          -- Add snapshot to the end of the log
          new_log_data := jsonb_set(
            obj->'log_data',
            ARRAY['h', size::text],
            logidze_version(new_v, logidze_snapshot(obj)),
            true
          );

          -- Update version number
          new_log_data := jsonb_set(
            new_log_data,
            '{v}',
            to_jsonb(new_v)
          );

          RETURN new_log_data;
        END;
      $body$
      LANGUAGE plpgsql;
   # ApplicaitonRecord
  def save_version
    return false unless has_attribute?(:log_data)

    klass = self.class
    klass.connection.execute(%(
      SELECT add_to_snapshot(#{id}, '#{klass.table_name}')
    ))
  end

Currently getting this error:

ActiveRecord::StatementInvalid: PG::InvalidParameterValue: ERROR:  cannot set path in scalar
CONTEXT:  PL/pgSQL function logidze_add_snapshot(jsonb,jsonb) line 13 at assignment
SQL statement "
            UPDATE step_definitions
            SET log_data = logidze_add_snapshot(to_jsonb(step_definitions.*), log_data)
            WHERE step_definitions.id = 1410
          "
PL/pgSQL function add_to_snapshot(bigint,text) line 3 at EXECUTE
: 
      SELECT add_to_snapshot(1410, 'step_definitions')
    

not sure what's going on yet (new to this level of sql) -- but I figured I'd post what I have in case others want to see

from logidze.

NullVoxPopuli avatar NullVoxPopuli commented on July 4, 2024

it looks like the problem was that I'm trying to use these functions when log_data is nil. if log_data is already populated, it appears to work. So, I guess there needs to be some conditions where it creates a new snapshot if there isn't one already.

from logidze.

palkan avatar palkan commented on July 4, 2024

Yep, you should add some checks. Also you may want to add columns filtering support.

from logidze.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.