From d20694e1de7dd77e9405f9b0be31fd7df9c6ced5 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Fri, 23 Apr 2021 19:52:36 +0200 Subject: [PATCH] update all tables to add timestamps --- ...23172422_add_created_at_and_updated_at.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 db/migrations/20210423172422_add_created_at_and_updated_at.php diff --git a/db/migrations/20210423172422_add_created_at_and_updated_at.php b/db/migrations/20210423172422_add_created_at_and_updated_at.php new file mode 100644 index 0000000..31a19a5 --- /dev/null +++ b/db/migrations/20210423172422_add_created_at_and_updated_at.php @@ -0,0 +1,53 @@ +getAdapter()->getOption('name'); + $query = 'SELECT table_name FROM information_schema.tables WHERE table_schema = \'' . $database_name . '\''; + $tables = $this->query($query)->fetchAll(); + foreach ($tables as $table) + { + //Do not modify phinxlog + if ($table['table_name'] == 'phinxlog') + { + continue; + } + + //Foreach table add timestamps, created_at and updated_at whith default values + $table = $this->table($table['table_name']); + $table->addColumn('created_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP']); + $table->addColumn('updated_at', 'timestamp', ['null' => true, 'update' => 'CURRENT_TIMESTAMP']); + $table->update(); + } + + } +}