diff --git a/plugins/bazaar-backend/migrations/20210923133459_trigger.js b/plugins/bazaar-backend/migrations/20210923133459_trigger.js new file mode 100644 index 0000000000..0b40117239 --- /dev/null +++ b/plugins/bazaar-backend/migrations/20210923133459_trigger.js @@ -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; + `); +}; diff --git a/plugins/bazaar-backend/migrations/20210902073122_init.js b/plugins/bazaar-backend/migrations/20210923133506_init.js similarity index 90% rename from plugins/bazaar-backend/migrations/20210902073122_init.js rename to plugins/bazaar-backend/migrations/20210923133506_init.js index 0649061c10..62d646c6df 100644 --- a/plugins/bazaar-backend/migrations/20210902073122_init.js +++ b/plugins/bazaar-backend/migrations/20210923133506_init.js @@ -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');