diff --git a/v-model/migrations/00000000000000_diesel_initial_setup/down.sql b/v-model/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..a9f5260 --- /dev/null +++ b/v-model/migrations/00000000000000_diesel_initial_setup/down.sql @@ -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(); diff --git a/v-model/migrations/00000000000000_diesel_initial_setup/up.sql b/v-model/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..d68895b --- /dev/null +++ b/v-model/migrations/00000000000000_diesel_initial_setup/up.sql @@ -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; diff --git a/v-model/migrations/2024-01-23-181727_api_user/up.sql b/v-model/migrations/2024-01-23-181727_api_user/up.sql index cf4087c..ee591f0 100644 --- a/v-model/migrations/2024-01-23-181727_api_user/up.sql +++ b/v-model/migrations/2024-01-23-181727_api_user/up.sql @@ -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, @@ -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, @@ -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); @@ -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'); diff --git a/v-model/migrations/2024-01-23-181824_login_attempt/up.sql b/v-model/migrations/2024-01-23-181824_login_attempt/up.sql index 3911215..536226e 100644 --- a/v-model/migrations/2024-01-23-181824_login_attempt/up.sql +++ b/v-model/migrations/2024-01-23-181824_login_attempt/up.sql @@ -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'); diff --git a/v-model/migrations/2024-01-23-181913_access_group/up.sql b/v-model/migrations/2024-01-23-181913_access_group/up.sql index bc9940a..e8bcfe1 100644 --- a/v-model/migrations/2024-01-23-181913_access_group/up.sql +++ b/v-model/migrations/2024-01-23-181913_access_group/up.sql @@ -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[]; \ No newline at end of file diff --git a/v-model/migrations/2024-01-23-182113_mappers/up.sql b/v-model/migrations/2024-01-23-182113_mappers/up.sql index 3add490..f519bd9 100644 --- a/v-model/migrations/2024-01-23-182113_mappers/up.sql +++ b/v-model/migrations/2024-01-23-182113_mappers/up.sql @@ -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) -); \ No newline at end of file +); +SELECT diesel_manage_updated_at('mapper'); diff --git a/v-model/migrations/2024-08-02-144835_magic_link_attempt/up.sql b/v-model/migrations/2024-08-02-144835_magic_link_attempt/up.sql index c616340..0af13e9 100644 --- a/v-model/migrations/2024-08-02-144835_magic_link_attempt/up.sql +++ b/v-model/migrations/2024-08-02-144835_magic_link_attempt/up.sql @@ -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'); diff --git a/v-model/src/storage/postgres.rs b/v-model/src/storage/postgres.rs index cba8ca4..e15ddc6 100644 --- a/v-model/src/storage/postgres.rs +++ b/v-model/src/storage/postgres.rs @@ -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?; @@ -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?; @@ -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())) @@ -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?; @@ -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?; @@ -1152,7 +1147,6 @@ impl MagicLinkAttemptStore for PostgresStore { let attempt_m: Option = 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 @@ -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?;