update migration with trigger on updated_at

Signed-off-by: Lykke Axlin <lykkeaxlin@hotmail.com>

Co-authored-by: klaraab <klarabroman@live.se>
This commit is contained in:
Lykke Axlin
2021-09-23 16:07:31 +02:00
parent ac9a05b33d
commit 184ab1381f
2 changed files with 44 additions and 5 deletions
@@ -0,0 +1,35 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
exports.up = function (knex) {
return knex.raw(`
CREATE OR REPLACE FUNCTION update_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
`);
};
exports.down = function (knex) {
return knex.raw(`
DROP FUNCTION IF EXISTS update_timestamp() CASCADE;
`);
};
@@ -31,13 +31,17 @@ exports.up = async function setUpTables(knex) {
.defaultTo('proposed')
.notNullable()
.comment('The status of the Bazaar project');
table
.dateTime('updated_at')
.defaultTo(knex.fn.now())
.notNullable()
.comment('The timestamp when this entry was updated');
table.timestamps(true, true);
});
await knex.raw(`
CREATE TRIGGER update_timestamp
BEFORE UPDATE
ON metadata
FOR EACH ROW
EXECUTE PROCEDURE update_timestamp();
`);
await knex.schema.createTable('members', table => {
table.comment('The table of Bazaar members');
table.text('entity_ref').notNullable().comment('The ref of the entity');