Month: July 2015

Combining your own local git repository with Openshift for deploying

On Openshift you can deploy a multitude of applications. You do this by adding the code to a git repository and pushing the result to the Openshift servers. In some cases, when you are already developing the code and you don’t want to manually copy the changes to the openshift checkout it is cumbersome to keep this in sync.

Remote git url during creation

The simplest solution is to, during the creation of your application, specify a remote git url to store the app data in. If for some reason you do not want that (repository is not public for example), there is another way to fix this.

Local branch

In this solution, a branch on your local repository will be used to push the code to the master of the Openshift repository. This solution is based on a Stack Overflow post.

Application creation

First, create the Openshift application using the normal flow and checkout the git repository linked to this application. We do this, because we want the contents of the .openshift folder, this is used by Openshift for configuration of the Cartridge. We will later overwrite the data in this repo with our own local git repository.

Preparing the local branch

In your own git repository with the existing code, branch from the master (or any other branch you want to openshift code to be based on):

> git checkout -b openshift
> git remote add openshift <openshift-git-repo-url>

We have just created a local branch called openshift, and added a remote repository called openshift. We will be committing changes to the local branch and pushing them to the openshift repo.

Now copy the .openshift folder from the checkout done earlier and copy this to your local openshift branch. After this modify any configuration you may have to match the openshift application (database, log locations, etc.). Commit your changes.

Pushing to openshift repo

We will now push the local branch to the openshift repo. This is not as simple as executing a git push, because the local branch is called openshift, but on the remote repository we want to update the master branch.

> git push openshift HEAD:master -f

This command will overwrite the remote repo with all local changes, because of the the -f flag. The HEAD:master parameter will tell git to push the branch to the master of the remote repo.

For any subsequent commits there is no need to specify -f again, because the repositories are now from the same source.

Leave a Comment

Split SQL dump into smaller files

In larger projects database can grow to impressive sizes of several gigabytes. I recently found myself needing to restore a table from a backup. But my texteditor was unable to open the dump.sql files of several gigabytes. So I whipped up a small PHP script that splits the file into small chunks of 1 MB each.

The table I was looking for was not that big, so the the CREATE and INSERT statements related to the table would end up into one file. A small ‘Find in files’ search would then reveal the file to check.

This method will work for any type of file, not only .sql files.

<?php
$fp = fopen('dump.sql', 'r');

$iter = 1;
while (!feof($fp)) {
    $fpOut = fopen($iter . '.sql', 'w');
    fputs($fpOut, fread($fp, 1024*1024));
    fclose($fpOut);
    $iter++;
}

Leave a Comment