CakeDC Blog

TIPS, INSIGHTS AND THE LATEST FROM THE EXPERTS BEHIND CAKEPHP

CakePHP Migrations plugin: easily version and deploy whole applications

This article is a quick introduction to the Migrations plugin, open sourced a few weeks ago by our company. You will see how simple it is to use the plugin and what you could do with it. I hope this article will show you the benefits of using migrations in your CakePHP applications and make you give it a try right after the reading!

Here is a one-sentence description of the plugin: the Migrations plugin allows developers to easily version and automate the creation / update process of any database schema and application data from the command line.

For information, CakeDC uses this plugin on its project since several years to make team collaboration and deployment easier. The plugin has been entirely rewritten a few months ago and fully tested (code coverage >95% as always at CakeDC) before being open sourced under the MIT license. It is now available to the community along with its documentation... and it is free!

Why is it useful?

It has been a while since companies integrated Source Code Management in their development process and CVS, SVN, Mercurial or Git are now common tools. Inspired from the open source movement it is also a good practice for single developers to version application source code.

As you might know, an application almost always depends of the database schema it is aimed to use... however it is not easy to version both the source code and database schema with a SCM. Let's take the example of a CakePHP application: until now the only way to do was to version a single file, either a sql dump or a CakePHP schema.php file generated with the cake schema shell. These two approaches are not very convenient to use on a daily basis, the first one forcing the developer to drop and recreate the whole database every time!

Moreover, a web application development is never really finished (there are always new features to add, software updates or bug fixing to do...) and deploying these change on a test or production server is always a delicate task.

Here comes the Migrations plugin! It provides a simple and easy way to version a database... and to perform many other different tasks thanks to its callback system. Here are some features:

  • keep a local database schema up-to-date: you just have to run all non applied migrations to update the local database schema to the latest version
  • make team work easier: when several developers work on the same application it is important that all of them work with the same database schema during all the development cycle. With migrations every commit is tied to the database schema at this precise instant, which makes easy switching branches and resetting a branch to a specific commit.
  • make installation and updates easier: ready to push the new version of your application live? You will only have to push the sources on the server and run all non applied migrations!
  • migrate more than database schema: the callback system allows you to do everything you want before (or after) applying (or reverting) each migration. Here are some examples: creating an initial admin account, add initial or test data to the application (lorem ipsums, categories, content...), update values from the database, send an email if debug > 0... The only limit may be your imagination ;)

Where can I find the code?

Announced a few weeks ago, a packaged version of the plugin can be downloaded from the new "Downloads" section of CakeDC.com. This page contains a link to download the 1.0 version, the plugin documentation and the Codaset project url for tickets and direct Git access to the repository.

To make people aware of the need to show their support to the Cake Software Fundation by donating a few bucks (this is unfortunately not done enough), the plugin was first available to donors only. The "Download without donation" button was added later, when the repository was made public! However, if you find this plugin useful please consider making a donation to the CSF... that is the best thing you could do for thanking us.

Click here to lend your support to: cakephp1x and make a donation at www.pledgie.com !

Even better! A sample application was also released for those who want to see how migrations could be used and integrated in an application. To play with it, Download the code or git clone the project using:

git clone git://codaset.com/cakedc/sample-migrations-application.git sample_migrations

You will only need to create a database.php configuration file and update CakePHP's core location to make the application work. Git users, run

git submodule init
git submodule update

to automatically add the migrations plugin as a submodule!

What do I need to use it in my application?

Note: the packaged plugin is for the CakePHP 1.3 version only. You can either download the 1.3-beta package of the framework, or use the 1.2 branch available in the Git repository.

Adding the plugin to an existing application is very simple. If you downloaded the archive containing the plugin code, unzip it in the "/plugins/migrations" folder of your application. Git users can add it as a submodule with the following command:

git submodule add git://codaset.com/cakedc/migrations.git plugins/migrations

To check that it is installed correctly, execute the following command from your application root (it will display the available command to use the plugin):

cake migration help

If you encounter any problem here, please read the official documentation about CakePHP's console usage.

How does it work?

This post is not aimed at providing a comprehensive tutorial on how to use the plugin, thus I will just introduce the most useful commands along with some use cases.

For a complete documentation, please read the official documentation provided on the plugin page. For a simple (but useful for understanding purpose) use case you can take a look at the sample application introduced above. Going through the commit history will allow you to understand how migrations could be used in a development process.

Create a migration

To generate a new migration, type the following command

cake migration generate

The tool will ask you to give a name to the migration and suggest to do a dump of the current database schema. If a "schema.php" file is found in the application, it will ask you if you want to generate a diff between this schema and your current database one.

Generated migration files will be added to the "/config/migrations" application directory.

Apply / Revert migrations

When you pull an application containing migrations, several commands are available to apply or revert migrations. The simplest one is:

cake migration

It will display all the found migrations along with their status (applied or not applied) and id number. Just enter a migration number to update your database to the correct version. Some convenience commands are also available. You can use:

cake migration up, down, all or reset

These commands will respectively:

  • apply the next migration
  • revert the latest applied migration
  • apply all non applied migrations (and thus update the schema to the most recent version)
  • revert all applied migrations (and empty the database)

Migrations for plugins

Adding plugins to an existing application often implies adding new tables to the database or altering existing ones. The Migrations plugin brings a quick and efficient way to automate this installation. On the one hand developers can easily add necessary migrations to their plugin (making upgrades easier), on the other hand users can apply them as easily.

The only difference compared with commands introduced above is the parameter "-plugin pluginname" that needs to be added. Here is how the user will install the database for the newly added / updated plugin "test":

cake migration run all -plugin test

I would like to highlight the fact that callbacks allow the developer to do everything they want before / after each migration. It is convenient for adding initial data, and one can even implement a callback method opening the bootstrap.php file to append plugin's configuration entries there (it is just an example ;)).

... going further

Of course, feel free to add any remark or example of migrations use in the comments.

As this post is not aimed at providing support for the plugin, I recommend you to use the official tools available:

  • If you found a bug or want to suggest enhancements: open a ticket!
  • An installation problem or a question about the plugin usage? Ask your question to the community on CakeQs!
  • You would like a custom version of this plugin, or professional related services... contact us, it is our job ;)

I hope you enjoyed this post, it is now time for you to start playing with the Migrations plugin...

Latest articles

Navigation options for improved usability

Ease of navigation is closely linked to user experience - it can make or break how the user interacts with your website. From being able to find and use your navigation menu, through to the user being able to find the information they are looking for, it is important to ensure your navigation options are cleverly designed and stick to best practices.   If visitors are having difficulty with your navigation options, you are missing an opportunity to either create a conversion, delight your customer or engage a potential client.   When it comes to your website usability, here are some things you can focus on to ensure your site’s navigation is a user-friendly one:   1.     Keep it simple While this one may sound obvious, it is important to ensure that you avoid making your navigation difficult to comprehend. Examples of this include cluttered navigation menus, disorganized sub menus   2.     Keep it predictable While creativity makes your website stand out and is great to catch a user's attention and provoke emotion, it is important not to practise creativity in areas where predictability is preferred by the user or visitor. Such as when creating your navigation menu or the placement thereof.   3.     Keep it consistent It is key to keep the theme and structure of the different pages of your site consistent. Check our CakeDC.com menu and the different pages, each page keeps the overall theme and structure consistent. This is to ensure that your user is able to make sense of the content as quickly as possible when switching between pages.   4.     Have a clear hierarchical structure Every category and clickable sub category should have a clear hierarchical structure and should be visible in your menu. Doing this gives your user a clear view and pathway for them to go to the exact page or content that they are wanting. This point is particularly important for website that have a wide range of products or services.   5.    Make it distinct Navigation options should be clearly visible and easy to find. They should stand out from other graphics, images or backgrounds. This can be done through size, color and font.   6.    Link the logo to the homepage A good practice is to link the homepage to the logo of your company on your website. This logo should be in the same place on every page. Users have a high tendency to click on your logo, with the expectation that it will lead back to the home page as this is a generally predictable behavior across websites and design practices.   7.     Always include a search bar Search bars are necessary for making your website more usable to your visitors. Some visitors only want to find information by using a search bar within your site. Offer your users a way to navigate through your website without having to go through every page or menu option.

Difference between UX and UI

UX and UI are often misused in the tech industry. Understanding the key differences between UX and UI is beneficial, we take a quick look at both. UX, User Experience Design, and UI, User Interface Design, are both crucial to a product, such as a website, and work closely together while remaining vastly different disciplines. UX design tends to be more analytical and technical, while UI is not. A basic example for UX design would refer to how users interact with CakeDC.com, where they find the navigation menus (is this as per industry norm or do they have difficulty navigating around the website to find the information they are looking for, or how to contact CakeDC via our contact form or telephone number). Whereas UI design looks at ensuring brand relevance through the look and feel of the site, keeping color standards as per best practice. UX, User Experience Design UXD or User Experience Design refers to the process of enhancing the experience that a user has with a company, its products or its services. This is done by focusing on increasing the ease of use as well as improving the overall interaction between the user and the product or service. Good user experience design translates to customer satisfaction and loyalty so it's vitally important to ensure good design is put to practice! As a UX designer, you will  need to understand your site’s users and potential users, from creating persona’s to determining user stories and carrying out user testing. A persona could be an example of a customer who is seeking more information by contacting you versus a visitor who would like to learn more by reading your blog. UI, User Interface Design User Interface Design is the look, feel and interactivity of the product, basically referring to the means by which the user and a product (such as a website) interact with each other. The end goal of UI Design is to “achieve structure, analysis and optimization of a customer’s experience with the company and product.” UI Design includes activities that range from user guides and story lines through to UI prototyping and implementation with the development team.
  While there are differences between UX and UI, there are some similarities, let’s look at these:

  • Have a primary objective of improving customer satisfaction such as improving the use of a “contact us” form
  • Focus on the user and his/her interaction with a product/service such as having an easy to navigate menu
  • Can be applied to any product
Here is an example of the planning behind CakeDC.com

Website redesign? Here's a checklist of things you’ll need to consider

Redesigning your website can be a daunting and scary task, however, with the proper preparation and the right development team it can be a breeze! It can be a potentially long and tedious process, with a lot that can go wrong. From just a visual overhaul through to improving branding, user experience and sales, a website redesign can encompass a wide variety of changes that you can benefit from. Whatever your reasoning is for choosing a redesign, it offers you an opportunity to re-evaluate the bigger picture and see where improvements can be made. Here’s a quick checklist of things to look out for when embarking on a website redesign

  1. Strategy - why are you doing a redesign. Pencil down your main points behind the redesign project. What are your goals, ideals, visions. Where do you want the redesign to get you. What are the measurable results that you are hoping to see - importantly, you should also benchmark your current traffic and metrics.
  2. Saving your current assets - Have you made the proper back ups of the important files, media etc so that these remain accessible after the redesign of your website is complete. Go a step further, and take your metrics to work out what the most important assets and pages of your website - such product pages with the most sales or blog posts with the most views or social shares.
  3. Define your target audience - who is your idea visitor? Look at your customer journey and describe your customer.
  4. Have you checked out your competitors? Conduct competitive research - their overall look, problem areas, good ideas that appeal to you and your product/redesign.
  5. Outline your key features - identify what is most important to your website redesign. From shopping carts to news posts, landing pages, social sharing, security updates.
  6. Set your budget - outline what you want and how much you’d like to spend on it. Who - an agency, a freelancer etc - as well as the size or scope of your project, backend applications or additional features that you are looking for.
  7. Create a timeline and schedule your milestones - When do you expect to see things happen.
  8. Have you considered optimization? Don’t forget to make sure that your site is optimized social media and search engines.
  9. Test and revise - before launching!

We Bake with CakePHP