Living in the cloud

My hard-drive crashed last Saturday, which kind of pissed me off as I was planning on spending Sunday playing online with one of my mates. So I spent Sunday and Monday formatting and re-installing everything.

To my surprise: I didn’t actually loose anything. I hadn’t realised how much of my data actually lives on the cloud.

It turns out 100% of my data lives “up there”.

Over the years I guess I’ve been slowly migrating without noticing the full extent.

This is the list of services I currently use:

  • Google Chrome (I keep my bookmarks and history synced across devices using my Google account)
  • Steam (I use this to purchase games online, I can re-install every game I bought on the platform on a new PC in less than a day)
  • Github (I host all my projects here, including personal projects on private repos. Yeah, I could use bitbucket as well I guess)
  • Spotify (Online music streaming service. I haven’t downloaded iTunes nor have I accessed Grooveshark in a couple of years)
  • Dropbox (I did quite a few referrals and I got bumped up to 56GB of space. Now I store all my photos, videos and music on Dropbox)
  • Google Drive (Why not just use Dropbox? Well… I find Google Drive convenient for documents, so I use it for all my office-related documents)

And that is how I successfully moved everything to the cloud. Hard-drive crash? No problem. Format, re-install, wait for downloads to complete, and you’re good to go.

If you have used other cloud services in the past that you think I should add to my list of must-haves, please let me know in the comment section below.

5 Solutions for MySQL Error 1235

I got stuck for hours the other day with this problem. I’ve used triggers for years in SQL Server without a problem, however, in MySQL the implementation is a bit iffy.

One problem I ran into yesterday while trying to restore a database was error 1235:

ERROR 1235 (42000) at line 1408: This version of MySQL doesn’t yet support ‘multiple triggers with the same action time and event for one table’

I scoured Google for about an hour, and came to the conclusion that there is multiple reasons this error may appear. I explain these reasons and provide solutions for them below.

First of all, I suggest you get a list of all triggers by running the following command:

SELECT trigger_schema, trigger_name
FROM information_schema.triggers
WHERE trigger_schema = 'NAME_OF_YOUR_DATABASE';

Reason #1:  You can’t combine both BEFORE/AFTER with INSERT/UPDATE/DELETE

This PDF describes this issue. The problem is simple: You can’t have BEFORE_INSERT and AFTER_INSERT for the same table. You may have been updating a column BEFORE insert and updating some other table AFTER insert with the ID. I suggest you move some of this logic (maybe the BEFORE trigger) to your code.

Reason #2: AFTER_DELETE sometimes fails with error 1235

You should avoid using AFTER_DELETE triggers. Move the logic to a BEFORE_DELETE trigger if you are going to use the OLD variable. No idea why this happens, maybe it is my specific MySQL version.

Reason #3: You can’t have triggers with the same name (duplicate triggers)

Sometimes you get another error code when doing this, but other times you get the same error 1235 with no explanation. Run the “show triggers” query I stated above and look for any triggers with the same name. Always run DROP TRIGGER before creating/modifying a trigger.

Reason #4: You can’t reference the SAME TABLE you are updating/inserting to in a trigger

For example, if you wanted to set the default password for a user through a trigger upon creating a new user record, you might have tried to do this: UPDATE users SET password = ‘newPassword’;

The correct way to do this is to set the variable in the BEFORE_INSERT trigger. Example: SET NEW.password = ‘newPassword’;

Reason #5: When exporting a database (as an SQL script) you may get database definers in the script

In other words, if you export a MySQL database to a .sql file, the actual file may contain things like TRIGGER ‘mydbname’.'trigger_name’. So if you try to restore it to a database with a different name it would fail. I would have expected another error code because the database it is referencing is NOT the one I am restoring to. Anyway, you receive error 1235 for this as well.

Quick fix: Open the .sql file and replace all mentions of `EXPORTED_DB_NAME`. with a blank string (i.e. replace with nothing).

Summary / TLDR:

Do not use BEFORE/UPDATE triggers on the same table for a same function (e.g. INSERT). Do not use AFTER_DELETE triggers, use BEFORE_DELETE instead. Do not have duplicate trigger names. Do not reference the same table you are updating or inserting to in the trigger, use OLD and NEW instead with SET instead of a subquery. If you are restoring a database from a backup, check that the backup does not contain mentions to the explicit database name (when restoring to a different database name).

Please remember: When creating an INSERT trigger you can only use the NEW variable, when creating an UPDATE trigger you can use OLD and NEW, and in a DELETE trigger you can only reference OLD.

A bug has been found in the system past couple of years have been really really hectic. Snowden leaked sensitive information, the Eurozone has on-going economic problems, the UK and USA are virtually police states, the NSA and GCHQ are spying on most internet users, there has been widespread protests across the world (Brazil, UK, USA, Siria, etc… “we are the 99%” anyone?). Just last week Glenn Greenwald was detained for 9 hours at Heathrow airport as being a “terrorism suspect”. What on earth is happening?

A bug has been found, or more like hundreds of bugs, and they have been unfixed, even though the issues have been raised.

Typically, socio-political changes occur on a massive scale. Democratic republics become dictatorships, whole countries become communist states, just to turn back a few decades later. When a government breaks down, a massive change is sure to ensue.

So why don’t we implement a system of incremental changes to prevent such breakdowns in the first place? Is it really so hard? 

Most large companies with web products have bug reporting systems or community forums. In some cases the community is involved in developing the changes that are required, which then get pushed to the main code repository. Ubuntu, Google Chrome (Chromium), WordPress, Eclipse, OpenOffice, Android, and others are examples of this. Some other companies are very open to public opinion and offer bug trackers or forums to track issues, for example: Github, MySQL, etc.

Some companies out there lack an effective means of communication, or don’t really care about what the majority of their customers think. Example: Microsoft. They screw up one version of windows, then they listen to feedback and produce a good version, then they think they can handle it from there and produce an aweful version, recognise their fault and open their channels again, and so on.

Another example is Facebook. They keep pushing unnecessary UI updates to their users, who are very pissed off. They want the core part of the UI to remain as-is. If it is not broken, don’t fix it.

Quite a few governments seem to take this stance as a philosophy: We know what is right, let’s fake some listening on our behalf but keep pushing our changes. They are not listening, and it has been backfiring on them these past few years.

The world changes fast, our societies change fast, why can’t the government change fast as well? If I was in charge, the first thing I would do would be to open-source the tax laws, the constitution, and other important legal documents. Why? The people know what is best: The majority will vote, the best ideas will be upvoted, releases can be scheduled on a yearly or bi-yearly basis, ensuring everything is kept up-to-date.

Is it really so hard to say you are a democracy and really be a democracy?

How to develop good software

As a software developer, I’ve come across many horrors in coding over the years. It’s no secret that most developers code things fast and ugly to get stuff out the door. But something strange happened to me recently; I think I am finally becoming a better coder.

I’ve been developing an ERP/MRP/CRM by myself for the past 3+ years and I’m finally reaching the release date. I’ve been testing the product with two clients for the past year, and naturally, many bugs have popped up out of nowhere, many features have been requested, and so on. You know… the typical software development cycle plagued with inadequacies.

However, I’ve finally learned a valuable piece of information I’d like to share:

Features, not rules.

I went in with a bunch of rules for the last meeting I had with my clients. They had to follow these rules to use the new features in my system correctly. They needed to follow certain processes in an orderly manner, they needed to avoid placing certain characters at the start of some files, they needed to use the correct extensions, they needed to avoid using certain half-baked process, they needed to process files at the end of the month and not in-between, etc.

Naturally, they were a bit taken aback. They are not computer-savvy, and they have a hard time grasping new things. “Why?” kept comming up all the time. I tried to explain that this is natural in a system so big, there are certain rules you must learn to avoid entering bad data, or messing up parts of the system, etc.

Halfway through the training I realised I was doing things the wrong way. Whenever you have to write down more than 3 rules for a process, it is almost always better to make the system handle all possible exceptions instead, and inform the user appropriately when they have made a mistake. It is easy to do this with forms for example, you write down some regex rules (or use JQuery Validate), you add extra back-end validation, you use a “form/validation” class, etc. However, when the processing takes place in the back-end (e.g. a file parser that changes content in the database), it is easy to expect too much of the users.

I left the meeting with all the rules I had written down scratched out and a page full of tasks to be performed.

I think it is vital for us to embrace this. A system must be able to handle anything thrown at it, it must be able to validate incorrect data, file extensions, and any other exception you may think of. It seems natural, most of you are thinking “this is so obvious, why write a blog post about it?”. Why? Well, because I keep seeing these kinds of mistakes all over the place. I’m sure many developers out there still think “oh, I’ll just explain the user how to do it”. Then you get a ton of calls of users asking why this or that is not working, and you have to explain that there is a certain process to be followed.

The answer is simple: Think before you code, and make whatever you code “think”.

How to deploy a static page to Heroku, the easy way!

I wanted to upload a simple website based on HTML, CSS and JS files, with a few images, and a favicon. So I registered at Heroku, bought a domain name, followed the Heroku instructions on how to set up the “Toolbelt” and GIT.

So I thought I was all set up: I created a folder with an index.html page, with the text “Coming Soon…”. I set up the repository, and tried to commit to Heroku, only to receive a message similar to “This is not a code repository” or something similar. Heroku is primarily aimed at apps running on frameworks, like Ruby on Rails, Django (Python), node.js, or… ahem… PHP.

So it seemed incredibly dumb that it can handle all these frameworks, but it doesn’t let me upload a static site! So I searched about, and found out how to do this by setting a demo (barebones) ruby app, as well as a Sinatra variation. However, this didn’t convince me. First of all I didn’t want to place my code in a “public” folder. No. I was probably being a bit picky, but when I expect things to work a certain way, I get annoyed when barriers are put up in my way.

I finally figured out that Heroku allows you to publish PHP sites as well, with NO configuration needed! That’s right, just create your .php files, and it will figure out that the Heroku app should run PHP for this site, and it will auto-configure the app to run on the latest version of PHP/Apache.

So here is how you do it:

Rename your index.html to home.html or something similar.

Create an index.php file with the following code:

<?php include_once("home.html"); ?>

That’s it!

Now go to your app folder in CMD (or Bash), and commit your code to Heroku using the following:

git add .
git commit -m 'Change this to a meaningful description'
git push heroku master

 And finally, you should get the following:

-----> Heroku receiving push
-----> PHP app detected
-----> Bundling Apache version 2.2.22
-----> Bundling PHP version 5.3.10
-----> Discovering process types
Default types for PHP -> web -----> Compiled slug size: 9.5MB
-----> Launching... done, v4

Congratulations! You have published a static site to Heroku with one line of PHP code :)