CakePHP: How to deploy DB schema changes to produc

2019-06-06 21:07发布

We're running CakePHP v2.3

We just had a weird incident during a push to production where we threw the following errors immediately after pulling the changes and we were not able to load any pages. The column (User.group_id) had just been dropped by us. We then deleted the caches (persistent and models) thinking that would prevent errors resulting from the changed schema. Instead, the caches were not recreated and it generated the following error and we were not able to load any pages...

2013-09-27 07:53:05 Error: [PDOException] SQLSTATE[42S22]: Column not found: 1054 Unknown column 'User.group_id' in 'field list'
Request URL: /admin
Stack Trace:
#0 /home/username/public_html/lib/Cake/Model/Datasource/DboSource.php(459): PDOStatement->execute(Array)
#1 /home/username/public_html/lib/Cake/Model/Datasource/DboSource.php(425): DboSource->_execute('SELECT `User`.`...', Array)
#2 /home/username/public_html/lib/Cake/Model/Datasource/DboSource.php(669): DboSource->execute('SELECT `User`.`...', Array, Array)
#3 /home/username/public_html/lib/Cake/Model/Datasource/DboSource.php(1080): DboSource->fetchAll('SELECT `User`.`...', false)
#4 /home/username/public_html/lib/Cake/Model/Model.php(2674): DboSource->read(Object(User), Array)
#5 /home/username/public_html/lib/Cake/Model/Datasource/DboSource.php(598): Model->find('first', Array)
#6 /home/username/public_html/lib/Cake/Model/Model.php(799): DboSource->query('findById', Array, Object(User))
#7 /home/username/public_html/app/Controller/AppController.php(95): Model->__call('findById', Array)
#8 /home/username/public_html/app/Controller/AppController.php(95): User->findById('12')
#9 /home/username/public_html/lib/Cake/Controller/Component/Auth/ControllerAuthorize.php(64): AppController->isAuthorized('12')
#10 /home/username/public_html/lib/Cake/Controller/Component/AuthComponent.php(409): ControllerAuthorize->authorize('12', Object(CakeRequest))
#11 /home/username/public_html/lib/Cake/Controller/Component/AuthComponent.php(335): AuthComponent->isAuthorized('12')
#12 [internal function]: AuthComponent->startup(Object(PagesController))
#13 /home/username/public_html/lib/Cake/Utility/ObjectCollection.php(131): call_user_func_array(Array, Array)
#14 [internal function]: ObjectCollection->trigger(Object(CakeEvent))
#15 /home/username/public_html/lib/Cake/Event/CakeEventManager.php(247): call_user_func(Array, Object(CakeEvent))
#16 /home/username/public_html/lib/Cake/Controller/Controller.php(670): CakeEventManager->dispatch(Object(CakeEvent))
#17 /home/username/public_html/lib/Cake/Routing/Dispatcher.php(183): Controller->startupProcess()
#18 /home/username/public_html/lib/Cake/Routing/Dispatcher.php(161): Dispatcher->_invoke(Object(PagesController), Object(CakeRequest), Object(CakeResponse))
#19 /home/username/public_html/app/webroot/index.php(97): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#20 {main}

After some scrambling, we switched the debug mode from 0 (production) to 2 (development) and this caused the caches to be recreated and the pages to load properly. We then switched it back to 0 and everything remained fine.

So in the aftermath of this incident, we have a two-part question:

  1. Why did this happen? We tested this procedure on our local dev server, although we were probably in debug mode 2 the whole time.

  2. What is the correct way to push CakePHP code updates with corresponding schema changes to production? Up until recently, most of our schema changes have been additions, so it was safe to manually add the tables / fields to the production DB prior to pulling the new code. But with "breaking" changes that remove tables / fields used by the current code, this is not an option.

1条回答
Juvenile、少年°
2楼-- · 2019-06-06 21:48

{From here: https://stackoverflow.com/questions/3402246/cache-file-model-cakephp/}

  1. Inside the app/tmp/cache/ folder is models. This is the cached schema of your tables. This had the reference to the field that was removed from your table.
  2. My correct way: I clear out the models folder whenever I have schema changes. You could just remove the one file representing the table if you are worried about slowing down your server when it needs to rebuild your caches.
查看更多
登录 后发表回答