Skip to content

Commit

Permalink
Use diesel trigger for updated_at
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Sep 1, 2024
1 parent ee0a928 commit 9e389b7
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.

DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();
36 changes: 36 additions & 0 deletions v-model/migrations/00000000000000_diesel_initial_setup/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.




-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
4 changes: 4 additions & 0 deletions v-model/migrations/2024-01-23-181727_api_user/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CREATE TABLE api_user (
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
SELECT diesel_manage_updated_at('api_user');

CREATE TABLE api_key (
id UUID PRIMARY KEY,
Expand All @@ -16,6 +17,7 @@ CREATE TABLE api_key (
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
SELECT diesel_manage_updated_at('api_key');

CREATE TABLE api_user_provider (
id UUID PRIMARY KEY,
Expand All @@ -27,6 +29,7 @@ CREATE TABLE api_user_provider (
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
SELECT diesel_manage_updated_at('api_user_provider');

CREATE UNIQUE INDEX api_user_provider_idx ON api_user_provider (provider, provider_id);

Expand All @@ -37,3 +40,4 @@ CREATE TABLE api_user_access_token (
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
SELECT diesel_manage_updated_at('api_user_access_token');
3 changes: 2 additions & 1 deletion v-model/migrations/2024-01-23-181824_login_attempt/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ CREATE TABLE login_attempt(

created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)
);
SELECT diesel_manage_updated_at('login_attempt');
1 change: 1 addition & 0 deletions v-model/migrations/2024-01-23-181913_access_group/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ CREATE TABLE access_groups (
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
SELECT diesel_manage_updated_at('access_groups');

ALTER TABLE api_user ADD COLUMN groups UUID[] NOT NULL DEFAULT ARRAY[]::UUID[];
4 changes: 3 additions & 1 deletion v-model/migrations/2024-01-23-182113_mappers/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CREATE TABLE mapper (
max_activations INTEGER,
depleted_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ,
CHECK (activations <= max_activations)
);
);
SELECT diesel_manage_updated_at('mapper');
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ CREATE TABLE magic_link_attempt(

created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)
);
SELECT diesel_manage_updated_at('magic_link_attempt');
7 changes: 0 additions & 7 deletions v-model/src/storage/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ where
.set((
api_user::permissions.eq(excluded(api_user::permissions)),
api_user::groups.eq(excluded(api_user::groups)),
api_user::updated_at.eq(Utc::now()),
))
.execute_async(&*self.pool.get().await?)
.await?;
Expand Down Expand Up @@ -422,7 +421,6 @@ impl ApiUserProviderStore for PostgresStore {
.set((
api_user_provider::emails.eq(excluded(api_user_provider::emails)),
api_user_provider::display_names.eq(excluded(api_user_provider::display_names)),
api_user_provider::updated_at.eq(Utc::now()),
))
.get_result_async(&*self.pool.get().await?)
.await?;
Expand All @@ -440,7 +438,6 @@ impl ApiUserProviderStore for PostgresStore {
let provider_m: ApiUserProviderModel = update(api_user_provider::dsl::api_user_provider)
.set((
api_user_provider::api_user_id.eq(provider.user_id.into_untyped_uuid()),
api_user_provider::updated_at.eq(Utc::now()),
))
.filter(api_user_provider::id.eq(provider.id.into_untyped_uuid()))
.filter(api_user_provider::api_user_id.eq(current_api_user_id.into_untyped_uuid()))
Expand Down Expand Up @@ -641,7 +638,6 @@ impl LoginAttemptStore for PostgresStore {
login_attempt::error.eq(excluded(login_attempt::error)),
login_attempt::provider_authz_code.eq(excluded(login_attempt::provider_authz_code)),
login_attempt::provider_error.eq(excluded(login_attempt::provider_error)),
login_attempt::updated_at.eq(Utc::now()),
))
.get_result_async(&*self.pool.get().await?)
.await?;
Expand Down Expand Up @@ -1127,7 +1123,6 @@ impl MagicLinkAttemptStore for PostgresStore {
.do_update()
.set((
magic_link_attempt::attempt_state.eq(excluded(magic_link_attempt::attempt_state)),
magic_link_attempt::updated_at.eq(Utc::now()),
))
.get_result_async(&*self.pool.get().await?)
.await?;
Expand All @@ -1152,7 +1147,6 @@ impl MagicLinkAttemptStore for PostgresStore {
let attempt_m: Option<MagicLinkAttemptModel> = query
.set((
magic_link_attempt::attempt_state.eq(to),
magic_link_attempt::updated_at.eq(Utc::now()),
))
.get_result_async(&*self.pool.get().await?)
.await
Expand Down Expand Up @@ -1231,7 +1225,6 @@ where
.set((
access_groups::name.eq(excluded(access_groups::name)),
access_groups::permissions.eq(excluded(access_groups::permissions)),
access_groups::updated_at.eq(Utc::now()),
))
.get_result_async(&*self.pool.get().await?)
.await?;
Expand Down

0 comments on commit 9e389b7

Please sign in to comment.