All posts by Chui

Using Goose with MySQL

goose is a Database Migration manager written in Golang. (There’s another migration tool in Python called goose as well).

The documentation is a bit thin on the ground at present, and required a bit of code-reading. Conceptually, it is simple and easy to grok. Goose generates a series of timestamped migration files, which contains either raw SQL or go code, which can then execute Raw SQL statements. For better or worse, there is no ORM involved.

Here’s the basic configuration for connecting to mysql databases. (Note the driver is mymysql not mysql)

#file: db/dbconf.yml
development:
  driver: mymysql
  open: DBNAME/USERNAME/PASSWORD

For completeness, the ziutek mysql driver supports all the following connection strings.

DBNAME/USER/PASSWD
unix:SOCKPATH*DBNAME/USER/PASSWD
unix:SOCKPATH,OPTIONS*DBNAME/USER/PASSWD
tcp:ADDR*DBNAME/USER/PASSWD
tcp:ADDR,OPTIONS*DBNAME/USER/PASSWD
cloudsql:INSTANCE*DBNAME/USER/PASSWD

The simplest way to write the migration is using SQL (since I didn’t know much Go at all)

goose create SampleTable sql

in the db/migrations/20140601001151_SampleTable.sql we can modify it to the following

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied

CREATE TABLE sample
(id int primary key auto_increment,
 description varchar(50));

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

DROP TABLE sample;

Then, it’s a matter of running

goose up

to migrate to the latest version

Working with 4D database via ODBC

There are some peculiarities when working with 4D databases and .NET

Date time 0/00/0 12:00

0/00/0 12:00 is valid in ODBC, but not valid in .NET.
Recommendation – wrap all date calls with a CAST
e.g.
SELECT CAST(BIRTHDAY AS VARCHAR) AS BIRTHDAY

alternatively, use a CASE statement to convert this call to something palatable to 4D e.g.

CASE CAST(LASTSEENDATE AS VARCHAR) WHEN '0/00/0 12:00:00' THEN CAST('1/1/1970' AS TIMESTAMP) ELSE LASTSEENDATE END AS LASTSEENDATE

Other alternatives simply don’t work. e.g. ... THEN NULL ELSE ... and NULLIF()

Saw this in my email today wizpert – billed as “is a fast growing community of experts, where users seek advice and coaching on a range of programming languages, including Python, HTML, PHP, JavaScript and more.

Bookmarked!

Powershell equivalent of xargs

I wanted to be able to build extmap.xml for all the files that were in my packages\ directory. I had a Powershell script called ToExtMap which automated most of my tasks, and a little experimentation with Powershell gave me this one liner:


PS C:\Users\Chui\Projects\SAM-GET\packages>
Get-ChildItem -Filter *.dll -Recurse -name |
%{ ToExtMap $_ | Out-File ($_ -replace "dll", "extmap.xml") -encoding ASCII}

A bit of explanation might be in order:

  1. Get-ChildItem -Recurse finds all the files I require.
  2. The %{} is the shortcut for the foreach operator.
  3. Powershell is object oriented. $_ -replace ... executes a string substitution

Over at Forbes

It started with making one presentation slide for each idea. Each slide would have a heading and several bullet points defining the scope of the feature and metrics describing what success would look like and how it would impact the business. That’s all people got to make their case. And given the speed of the whole process, there was little time for precision.

Business Forms in Silverlight Part 2

(this is a continuation of Part 1)

From the previous post:

Basic DataForm experience
Basic DataForm experience

The first problem is the OrderDate is unintialized and renders as 0/00/0000.

The first thought is to change the OrderDate from DateTime to Nullable<DateTime>. This will require either the database schema be changed, or a second viewmodel created. Changing the database schema just to support a user-interface tweak is obviously out of the question. However, writing a viewmodel and copying all the properties over breaks a rule of “DRY” (Don’t Repeat Yourself).

Luckily, XAML binding supports the notion of binding converters. This lets us transforms inputs from one type to another.

public class NullValueConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is DateTime && targetType == typeof(Nullable<DateTime>))
        {
            if ((DateTime)value == DateTime.MinValue)
            {
                return null;
            }
        }
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

Applying the converter to the date picker, we get reasonably good results. For example, after entering a date and then clearing it, we get a validation error showing (see picture below). Furthermore, hitting cancel restores the control back to default value.

dataform2

This done, we are ready to tackle our next problem.

Aside

Here’s a sample of how converters are applied.

<sdk:DataForm>
  <sdk:DataForm.Resources>
    <local:NullValueConverter x:Key="NullValueConverter" />
  </sdk:DataForm.Resources>
  <sdk:DataField Label="Order Date">
    <sdk:DatePicker SelectedDate="{Binding OrderDate, Converter={StaticResource NullValueConverter}}" />
  </sdk:DataField>
</sdk:DataForm>