how to update whole database without losing data - php

I am using mysql database for my site.
I have create one site using codeigniter php framework and mysql.
Now after few months I have updated that site and also the database.
I have added some new columns to my database table not deleted or alert any previous one.
Now I want to update my site but don't want to lose data that I already have right now.
Is there anyway I can update database without losing the data present in it.

If your database is created/updated automatically by some modeling tool, I think the best you way you should do is to understand those changes and write the "alter table" statements yourself and run it in your deployment.
As user1281385 pointed in answer https://stackoverflow.com/a/22012478/1033539 , there may be some tool that can help you generate those statements.

SQLyog has a great feature called "Schema Synchronization tool" which will do it
they also have a blog comparing other methods of doing it
http://blog.webyog.com/2012/10/16/so-how-do-you-sync-your-database-schema/
Im sure other similar tools can do it also
Edit:
Mysql Workbench also has this feature
http://www.mysql.com/products/workbench/features.html

Related

Recreate a database using existing php code

So I have an old website which was coded over an extended period of time but has been inactive for 3 or so years. I have the full PHP source to the site, but the problem is I do not have a backup of the database any longer. I'm wondering what the best solution to recreating the database would be? It is a large site so manually going through each PHP file and trying to keep track of which tables are referenced is no small task. I've tried googling for the answer but have had no luck. Does anyone know of any tools that are available to help extract this information from the PHP and at least give me the basis of a database skeleton? Otherwise, has anyone ever had to do this? Any tips to help me along and possibly speed up the process? It is a mySQL database I'm trying to use.
The way I would do it:
Write a subset of SQLi or whatever interface was used to access the DB to intercept all DB accesses.
Replace all DB accesses with the dummy version of yours.
The basic idea is to emulate the DB so that the PHP code runs long enough to activate the various DB accesses, which in turn will allow you to analyze the way the DB is built and used.
From within these dummy functions:
print the SQL code used
regenerate just enough dummy results to let the rest of the code run, based on the tables and fields mentioned in the query parameters and the PHP code that retrieves them (you won't learn much from a SELECT *, but you can see what fields the PHP code expects to get from it)
once you have understood enough of the DB structure, recreate the tables and let the original code work on them little by little
have the previous designer flogged to death for not having provided a way to recreate the DB programatically
There are currently two answers based on the information you provided.
1) you can't do this
PHP is a typeless language. you could check you sql statements for finding field and table names. but it will not complete. if there is a select * from table, you can't see the fields. so you need to check there php accesses the fields. maybe by name or by index. you could be happy if this is done by name, because you can extract the name of the fields. finally the data types will missing. also missing: where are is an index on, what are primary keys, constrains etc.
2) easy, yes you can!
because your php is using a modern framework with contains a orm. this created the database for you. a meta information are included in the php classes/design.
just check the manual how to recreate the database.

Upgrading the existing database in a PHP system

I'm working on a software solution which was written using PHP Symfony with mysql database. When we do upgrades to the existing product what we use now is copping the existing database to a new database and do the upgrade standing on the new database. But the current method of asking the user to copy the existing database does not seem to be the professional way to do an upgrade.
Is there any standard way of doing that automatically and preserve the consistency of the old database? Please help me on this issue. Thanks in advance.
You could create a copy of the tables with a different table prefix (like updateAttempt_) and then if everything goes well delete the old ones and rename the new ones to the old ones.
Although, If you're doing this to make sure the data isn't corrupted in the event something goes wrong.. isn't that what TRANSACTIONS are for?

What happens to unused fields

Since RedBean creates all columns by itself what would happen if I don't need a field any more. Is there an easy way to remove it without deleting the table and lose all data?
Can this be solved automaticaly or how would RedBean react if I delete the column manually?
Delete the table column in the usual way from your MySQL client (say, phpMyAdmin or SQLYog) or from the MySQL console.
RedBean can't get confused by this "external meddling" that you're worried about, because it runs on each PHP script execution and, to the best of my knowledge, carries no state across invocations. It's really just an abstraction over data storage.
Interestingly, the RedBean Wiki doesn't appear to talk about this sort of thing at all.

Should I use phpmyadmin or php to add content a to a database?

My knowledge of MySQL is very basic here... so I'm just wondering if I want to create a table in a database, and add rows to that table, is it best to do so through phpmyadmin or should I do so in a PHP file?
Of course you can do it programmatically, maybe for your project you'll have to do it later anyway. But to get used to this whole SQL stuff, maybe it would be better to use some administration tool, like the mentioned phpMyAdmin or MySQL Workbench. I wouldn't recommend the commandline tool for starting, except you like a puristic commandline environment.
I just found this phpMyAdminDemo. Maybe it's good to start with, if you really want to use PHPMyAdmin. But if you don't have to, I would recommend to use Mysql Workbench, because it has a nice user interface and I hope it's relatively easy to deal with. A really nice feature is, that you can create diagrams of your database in the GUI, and forward it to the database. Even if you modify the diagram (e.g. adding columns), you can synchronize it with the database with only a few clicks. Additionally to that you can enter and edit data with Workbench as well.
So you might have a basic database structure then - after you struggled through some select statements in PHP like in PHP MySQL Select you will maybe finally get to the point where you want to: Like creating tables with PHP and MySQL or inserting data with PHP and MySQL
[Edit] I re-read your question as your title and question don't match. To answer your question; Creating the database schema is what phpmyadmin was made for. For managing data see what I wrote below.
Depends on your situation. If there's only you and just you managing the content then it can be an easy way to insert and edit data quickly. If you want to do anything advanced, for example:
WYSIWYG (HTML editing) or
Validation
then you'll need to make something yourself. I wouldn't recommend you have a client using a CMS to edit through phpmyadmin as they're given too much power and could screw things up.
I'd suggest using phpmyadmin as its pretty easy for novice users. Here is a very detailed article to add a table in phpmyadmin - http://php.about.com/od/learnmysql/ss/create_tables.htm

MYSQL Event to update another database table

I have just taken over a project for a client and the database schema is in a total mess. I would like to rename a load of fields make it a relationship database.
But doing this will be a painstaking process as they have an API running of it also. So the idea would be to create a new database and start re-writing the code to use this instead. But I need a way to keep these tables in sync during this process.
Would you agree that I should use MYSQL EVENT's to keep updating the new table on Inserts / updates & deletes??
Or can you suggest a better way??
Hope you can advise !!
thanks for any input I get
I had the same problem in my project. I did the same thing like you - writing the whole database new. We developed the new database and the fitting code and after finishing that work we made a migration script (small application) which did the migration from old to new database.
Hope this gives you some ideas...
I had the same problem as well, and went by the way of duplicating data at the point of user input (basically saved to both databases at once, since the original schema lacked all the required information). After several months of development the customer realized that he is going to move to a completely new database and considered it too risky (and I agree with him).
Thus, I would advice to utter clearly to your customer that you are going to implement a new database instead of iterative refactoring of the current one.

Categories