Comments (7)
Looks like this works:
Logidze.without_logging {
MyRecord.connection.execute("SELECT logidze_snapshot('#{my_record.attributes.to_json}'::jsonb)")
}
from logidze.
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.
oh. how does the updating process work?
(This is something I need for work)
maybe logidze_version
instead of logidze_snapshot
?
from logidze.
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.
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.
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.
Yep, you should add some checks. Also you may want to add columns filtering support.
from logidze.
Related Issues (20)
- How to list all versions of a record? HOT 4
- Association versioning with ignore_log_data and at(version: ) doesn't return expected results HOT 5
- Partition-friendly logging (triggers) HOT 3
- JSONB column and switch_to! with append: true HOT 2
- PG::UndefinedFunction: ERROR: function hstore(model_name) does not exist HOT 1
- Meta per request HOT 1
- Logidze.ignore_log_data_by_default causing db:migrate errors HOT 2
- Responsible from different sources HOT 3
- Associations versioning - accessing versions with `at(version: #)` HOT 2
- How to tell logidze about a new column HOT 8
- GlobalID support for metadata
- `responsible_id` is not updated on relation with `touch: true` HOT 6
- reload_log_data doesn't work when used with acts_as_paranoid and deleted records HOT 2
- docs: logdize cannot set session meta data when using a connection pool HOT 1
- How can I get the last 100 versions of a model? regardless of the record HOT 2
- Saving log with only metadata changes HOT 1
- Not possible to use latest logidze and anyway_config with Ruby 3.3.0 HOT 1
- Make Sequel support independent from ActiveRecord and ActiveSupport
- Updating history when column names change HOT 3
- Filter by changed attrs HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logidze.