Q:

Updating a live project

Situation:

I've just deployed the first version of my website (in PHP with
MySQL database). I now need to move to version 2 of my website.
I typically build my website on localhost before moving it to the production server
I now want to restructure my database. But I'm not clear how to do this without losing / corrupting existing user data.
I'm worried that WHILE I'm making the above changes, a user may try
to make edits to the database.

Questions for you

Let's say I restructure my database in localhost. How do I duplicate
this restructuring on the live server? Do I need to run every query
on both localhost and production servers? (Seems very inefficient -
is there a better way to do this?)
How do I make sure that while I'm restructuring the database, the
sanctity of the data is not harmed (i.e. how do I make sure the
"version" of the database does not change until I finish my job?)

A:

You create a "migration script" in sql that collects all queries required to migrate a database between two states. You try that how often required in your development environment and once on the production environment. 
During that migration of the production environment you enable a "maintenance mode" that prevents any changes to the data being made and clearly states so to the user. Be transparent here. Two variants are in use: 

you block all requests and temporarily replace the site by a "maintenance sign", this is typically done if the migration only takes a very short time. If that is not sufficient, then 
you have to implement a read-only or maintenance mode into your logic rendering all data as read-only. Again: be transparent to you users in this. 

If that is all too much hacking, then you might want to learn from the "big players" who never manipulate running systems at all, but only switch between instances, often a pool of instances. So you prepare a second, new system and only switch to that at a given point in time. Still you have to take care of a read-only period to allow data synchronization, but the switch itself is fast and reversible. 

