Using git version control for PHP development - php

Is it possible to version control a PHP + MySQL + Apache project? And could it keep track of the changes in the database, like for example if I added a new table, can I possibly commit that?
Thank you.

It is not normal to keep databases in version control. Some developers use a sqlite database for development so that it can be checked into version control, but this can lead to issues as sqlite syntax can be different from MySQL.
However, you can keep your database schema and migrations source control. Look at a projects such as mysql-php-migrations to get started.
There's a good tutorial on using PHP with Git at http://net.tutsplus.com/tutorials/other/easy-version-control-with-git/ - this should get you started.

Using Git for your PHP scripts is no problem, however tracking changes to the database is a little trickier. If you have SQL scripts that create the database structure then these can be version controlled with no problems. Otherwise you could use mysqldump to output the structure to an SQL script after any changes you make:
mysqldump -d -h localhost -u root -pmypassword mydatabase > dumpfile.sql

You could use git to track PHP scripts and SQL scripts that create the necessary database structure. Those SQL scripts could of course be version controlled and recreate the database schema at any given state.

Git is essentially SCM, which means Source Control. Tables in mysql are stored as binary files, so it isn't very good idea.
You can, however, store SQL queries which create these tables, allowing you to re-create them if you needed to.
As for php, it will be all good.

I recently released a really simple shell script that will help keep changes to a MySQL database under version control.
https://github.com/stevecomrie/mysql-version-control

Related

Manually updating a Wordpress database

I am currently working with a team of developers trying to port our website to WordPress. On our local dev. server, we have complete ability to edit MYSQL schema. However, our parent company runs the MYSQL server on our production server, meaning we can't make script-based schema changes. Instead if we want to change anything in the MYSQL schema, we have to log the change, and mail it to them.
This makes upgrading WordPress tricky, since WordPress automatically generates and executes the SQL when it updates. We simply can't do this, and need a way to get SQL commands WordPress uses on our local server.
I agree with Pekka웃's comment, but if you really must do this I think the easiest way would be to use MySQL logging. Turn it on on your local machine, run your upgrade, and check the content of the file.
Depending on what version of MySQL you have, something like this should create a file to use as a base:
SET GLOBAL log_output = "FILE";
SET GLOBAL general_log_file = "D:/tmp/mysql.log";
SET GLOBAL general_log = 'ON';
I can't help think getting the parent company to run the upgrade would be much, much better though. Minor differences between dev and production could cause unexpected issues.

mercurial automated deployment on staging and production server using bitbucket

Presently we are using mercurial with tortoise HG for GUI to manage Repositories and our repositories are created on bitbucket.
Now I want to deploy my source code to my (staging/test) and production server. I had also googled for the same but I didn't found any good answer.
I also like to manage my sql database with mercurial. is it possible to make our database versions ?
Please help me!!
Thanks
If you think about versioning of database evolution, not about data in database, you have to read about Liquibase and using database changelog file for publishing DB-changes.
Short intro:
Each change in schema (creating database is also mandatory zero-level change) stored by Liqubase as changelog
Changelog stored with changeset, which introduced this change (added to set of previous changelogs)
Updating project from state A to state B assumes applying all changesets in A::B range to code and all database changelogs in these changesets
You cannot store your database in a repository but you can export your Database.
You can export your database either with the command line (mysqldump) or with a MySQL database manager. But I would recommend writing a command line script which does it for you automatically (because it is annoying to write that command several times).
Here is an article about synchronising a mysql database with git (which contains automatic scripts) that should help you. It shouldn't be hard to rewrite these scripts for HG.
When you push code, it is only pushed to the bitbucket server. If you want that your servers (or developers, tests or whatever) get the code you will need to pull the code from server (doing it with the command line command hg pull or in your GUI with the "Pull" button - google should help you to find it). There is no way that your server does it automatically because it would offer an security issues. (What would happen if somebody gain access to a developer account and if he push some code, which does bad things like pushing your customer's datas to another server? And to the "database" part. You should only save the structure in your repo, not user datas, etc).
Well, you should apply your code on your (test) server only with the command line (or a FTP client or whatever) because: The script, which runs, needs property a restart, or if you use a PHP server, your database may need an update when a new tables has been added).

How to access an SQLite database file stored on cd using php?

I have a small mySQL database and a few simple php based webpages that query the database, generate html tables and present them in the browser. The database is no longer being updated. So, searching and viewing subsets of the data is all that is required.
Some users are interested in distributing the searchable database on a cd or usb memory stick. SQLite seems to offer the answer, but I don't understand what is needed to make this work. What will be required to make a web browser based "app" work from a memory stick in the absence of a server like xampp?
XAMPP Lite from http://portableapps.com/apps/development/xampp
What you want is called a "standalone server". This is very common in almost every other modern language except PHP, for some reason. Googling "standalone php server" reveals several options:
QuickPHP
Nanoweb
Server2Go

How would you implement an auto-update for a standalone PHP project using GitHub?

My project is a collection of PHP scripts using MySQL as a database and needs to be installed locally using WAMP/LAMP/MAMP.
Previously I've been sending the users a link to a zipped archive and having them overwrite it, but since I took the plunge to GitHub, I've realized that there are far better ways; namely Service Hooks in GitHub. However, this would work fine as long as I don't alter the database in any way, which is a good possibility.
I've been toying with the idea of how I would implement this, but I can't find a clear solution. So far I've concluded with that I need to have a directory (say update/) which contains .sql files for each update. The PHP script will then check said directory for a file corresponding with the new version number (not sure how I will define a version number; I was thinking of using the commit ID, but that won't be available until after the commit, so...).
I would love some input on this!
Here's how I would tackle this (not the most elegant or performant):
Add a flag in the DB with a version number
Add a min-version number in your DB layer PHP file
Check that the DB version is greater than the min-version
If it is: continue about your business
Else: Run the PHP file in update/ which would have a series of ALTER TABLE commands to be run on the DB server
Update the min-version number in the DB to the latest number
All done
Alternately instead of querying the DB you can have a file which is generated by your DB interface PHP file (and ignored with .gitignore) which you can just as above.
I would really recommend checking out Doctrine and its migration feature.
This does exactly what you are looking for, plus you get a very nice tool for working with all other aspects of your database handling.

How to start creating a php script, that will be installed on many servers?

I was wondering how to start coding a script using php, and that script will be used on many websites.
so should I start first by creating the database ? and then start creating php files that will process data from the database ?
and should I start thinking of an install wizard for this script at first, or later when I finish the project I'll create one ?
I'm really confused on how to start a project, can you please give me some advice ?
and thanks everyone :D
should I start first by creating the database?
If you are going to use a database in your PHP script, then yes, you should install a database first. MySQL is a good start.
and then start creating php files that will process data from the database?
I would start on one server first, and create one PHP file called index.php that will do a database query. Then work your way to multiple PHP files from there.
and should I start thinking of an install wizard for this script at first, or later when I finish the project I'll create one.
Installing PHP files is 90% of the times as simple as just copying them onto your new server. I wouldn't worry about an install wizard just yet.
Another general tip because you are a beginner: install WAMPServer, it is a webserver/PHP server/MySQL Server in one that runs on your local computer. This is great for developing because you can just put your PHP files in C:\WAMP, edit them and directly see the result in your browser through http://localhost/. Then when you are happy you can upload to the server, or multiple servers. (Just by copying).
Most php software does not have, or need for that matter, what you would call an install wizzard.
I would suggest you to develop whichever way feels most natural to you.
Some people find it easier to start with the database design, while others prefer to write some code first and then expand the db schema further. There really is no right way to do it.
Starting a PHP project can be as easy as creating a text file and pumping out lines of code, however if you plan on creating a sizeable project, I would suggest a fully featured IDE.
Decide what dependencies your script has.
Decide which minimum version of PHP the script will be compatible with.
Work out a script which queries the users setup to detect whether these conditions are met or not. (eg does it rely on the mysql extension to be installed).
Detail how to meet each of the dependencies in case they are missing.
Explain which is the minimum version number supported, if your script detects it is below that version number.
Test it on your target Operating Systems.
Run a script which creates a database, test whether that was created. Provide detailed instructions on how to do this manually, and how to provide the correct privileges.
If necessary give them a config file which permits them to enter key information such as doc_root etc.
Conform to common wisdom such as short_tags = off else override these settings. Imagine the user is on shared hosting and is running on safe_mode = on.
Try and follow your own instructions and re-install it on your localhost, then on a live server - ideally on a variety of OSs too.

Categories