Microsoft' error messages cipher

I have a T-SQL trigger creation script, which runs OK:

CREATE TRIGGER data_modified ON Northwind.dbo.Customers FOR INSERT, UPDATE, DELETE
AS

declare @rows as int;
set @rows = @@ROWCOUNT;

IF @rows = 0
BEGIN
    print 'no rows were affected';
    return;
end

if exists(select * from inserted)
begin
    if exists(select * from deleted)
    begin
        print 'updated ' + @rows + ' rows';
    end
    else
    begin
        print 'inserted ' + @rows + ' rows';
    end
end
else
begin
    print 'deleted ' + @rows + ' rows';
end

Yet, when I run some INSERT query, I got an error saying:

Msg 245, Level 16, State 1, Procedure data_modified, Line 21
Conversion failed when converting the varchar value 'inserted ' to data type int.

Mysterious, isn’t it? Let’s dig in, shall we?

Read more

Setting up Rails webserver

Foreword

This tutorial I wrote when was quitting my previous job, almost one year ago. But it’s still handy!

Abstract Rails application setup

$ git clone .../project_name.git
$ cd project_name
$ [sudo] bundle install
$ cat config/database.yml
$ # create database and/or change config/database.yml settings
$ rake db:migrate RAILS_ENV=production
$ rake db:seed RAILS_ENV=production # don't worry if one fails
$ # start the server of your choice

Puma webserver

Application-wide settings

First you need to set up Puma for your specific project. For this purpose, add this line to the Gemfile:

gem 'puma'

Then, run [sudo] bundle install.

When you are done, you should be able to create a Puma config file at $PROJECT_DIR/config/puma.rb:

def home_dir
    '/home/user/$PROJECT_DIR/'
end

def path(p)
    File.join(home_dir, p)
end

directory home_dir
environment 'development'
daemonize
pidfile path('tmp/pids/puma.pid')
state_path path('tmp/pids/puma.state')
stdout_redirect path('log/puma.log'), path('log/error.puma.log'), true
threads 0, 1
bind 'tcp://0.0.0.0:5100'
activate_control_app

More details here: https://github.com/puma/puma/blob/master/examples/config.rb

Now, add project root path to the /etc/puma.conf file, e. g.:

/home/user/project_name

Start Puma at boot

There is a specific utility, called Jungle. It manages your applications’ instances at startup.

Ububtu-based systems

First of all, create /etc/init/puma.conf file and fill it with this:

# /etc/init/puma.conf - Puma config

# This example config should work with Ubuntu 12.04+.  It
# allows you to manage multiple Puma instances with
# Upstart, Ubuntu's native service management tool.
#
# See workers.conf for how to manage all Puma instances at once.
#
# Save this config as /etc/init/puma.conf then manage puma with:
#   sudo start puma app=PATH_TO_APP
#   sudo stop puma app=PATH_TO_APP
#   sudo status puma app=PATH_TO_APP
#
# or use the service command:
#   sudo service puma {start,stop,restart,status}
#

description "Puma Background Worker"

# no "start on", we don't want to automatically start
stop on (stopping puma-manager or runlevel [06])

# change apps to match your deployment user if you want to use this as a less privileged user (recommended!)
setuid apps
setgid apps

respawn
respawn limit 3 30

instance ${app}

script
# this script runs in /bin/sh by default
# respawn as bash so we can source in rbenv/rvm
# quoted heredoc to tell /bin/sh not to interpret
# variables
exec /bin/bash <<'EOT'
  # set HOME to the setuid user's home, there doesn't seem to be a better, portable way
  export HOME="$(eval echo ~$(id -un))"

  cd $app

  if [ -d "$HOME/.rbenv/bin" ]; then
    export PATH="$HOME/.rbenv/bin:$PATH"
  elif [ -f  /etc/profile.d/rvm.sh ]; then
    source /etc/profile.d/rvm.sh
  elif [ -f /usr/local/rvm/scripts/rvm ]; then
    source /etc/profile.d/rvm.sh
  elif [ -f "$HOME/.rvm/scripts/rvm" ]; then
    source "$HOME/.rvm/scripts/rvm"
  elif [ -f /usr/local/share/chruby/chruby.sh ]; then
    source /usr/local/share/chruby/chruby.sh
    if [ -f /usr/local/share/chruby/auto.sh ]; then
      source /usr/local/share/chruby/auto.sh
    fi
    # if you aren't using auto, set your version here
    # chruby 2.0.0
  fi

  logger -t puma "Starting server: $app"

  exec bundle exec puma -C config/puma.rb
EOT
end script

Now, create /etc/init/puma-manager.conf and fill it with this:

# /etc/init/puma-manager.conf - manage a set of Pumas

# This example config should work with Ubuntu 12.04+.  It
# allows you to manage multiple Puma instances with
# Upstart, Ubuntu's native service management tool.
#
# See puma.conf for how to manage a single Puma instance.
#
# Use "stop puma-manager" to stop all Puma instances.
# Use "start puma-manager" to start all instances.
# Use "restart puma-manager" to restart all instances.
# Crazy, right?
#

description "Manages the set of puma processes"

# This starts upon bootup and stops on shutdown
start on runlevel [2345]
stop on runlevel [06]

# Set this to the number of Puma processes you want
# to run on this machine
env PUMA_CONF="/etc/puma.conf"

pre-start script
  for i in `cat $PUMA_CONF`; do
    app=`echo $i | cut -d , -f 1`
    logger -t "puma-manager" "Starting $app"
    start puma app=$app
  done
end script

And create a blank /etc/puma.conf file. This will be filled for each application separately.

Caveat:

You need to customise /etc/init/puma.conf to:

  • Set the right user your app should be running on unless you want root to execute it!
    • Look for setuid apps and setgid apps, uncomment those lines and replace apps to whatever your deployment user is.
    • Replace apps on the paths (or set the right paths to your user’s home) everywhere else.
  • Uncomment the source lines for rbenv or rvm support unless you use a system wide installation of Ruby.

Now, start Jungle like this: sudo start puma-manager. And all your applications should be available when you reboot the machine.

More details at https://github.com/puma/puma/tree/master/tools/jungle/

Debian-based systems

PENDING

Starting up and shutting down

To start up the application is easy enough. Just navigate yourself to project directory and run the following: puma -C config/puma.rb.

If you want to shut down one, run this command in the project directory: [sudo] pumactl -S tmp/pids/puma.state halt.

Speeding up with Ruby native extensions

Foreword

At my job, our current project has many bottle-necks, where Ruby really sucks on its performance. We were thinking on how to optimize them, and finally come to usage of Ruby Native API.

Our project uses Redis and MySQL hardly, so much of statistic data is stored in Redis. For speeding up. But one fine day made us use a reduce on a set of statistic data from Redis. And that’s where we got stuck on Ruby’ performance. Our server timed out in a minute of waiting for that reduce to complete.

Read more

Чоткій блог. Часть третя

Прєдупрєждєніє

Будь готов! В цій главі ми полностью переробим блог! Він не буде працювати (як раньше) аж до кінця домашнього заданія; код буде магічним; придеться пару раз удалять весь код з деяких файлів! Але в сухом остаткє, потом має стати ну дуже прикольно розробляти його дальше.

Read more

Чоткій блог. Часть друга

Моделі

Єсть така штука як MVC, Model-View-Controller. Це такий прінцип, по якому програма розділяється на три тіпа дєталєй:

  1. Model - отвічає за роботу чісто з базой даних
  2. View - отвічає чісто за отображеніє даних перед пользоватєльом (формочка, HTML, і так далєє)
  3. Controller - отвічає за обработку дєйствій пользоватєля, іспользує моделі і передає їх в’юхам

Так от, модель - це такій клас, який работає з рядочками одної таблички і другими табличками, шо прив’язані до нашої. Але тільки якшо та таблічка, з якою работає модель - главна в цій связі. Тоість, якшо у нас є модель Пост і модель Камєнтік, то модель Пост може вліять на Камєнтікі, а от модель Камєнтік вже нічо не може зробити з Постом. Тут таблічка Пост - главна, а таблічка Камєнтік - просто прив’язана до неї.

В общєм, модель - це такий удобний клас, в якому заникані всі запроси до бази даних. Ти визиваєш метод моделі, а получаєш - масив (або не масив, а тільки один його елємєнт) з запісями з бази даних. Або визиваєш другий мєтод і удаляєш/обновляєш/создаєш рядочки в базі.

Read more

Чоткій блог на PHP. Часть перша

Прєдісловіє

Дарагой друг! В цих статтях я розкажу тобі як зробить чоткій блог на чотком язику PHP. Розказувать буду просто, шоб всьо було понятно. Ну шо, паєхалі?

Сістємні трєбованія

Нехай у нас є який-небудь самий стандартний, простий, перший блог на PHP. Така собі кучка файлів для созданія постов, списка постов, перегляду оддєльного поста, реєстрації, логіна і логаута, камєнтіруванія… Просто купа файлів. В цих файлах у нас і HTML, і PHP код.

Read more

From ActiveModel to ActiveRecord

This short article covers some steps you need to implement to get your own ActiveRecord implementation.

Sample ActiveModel looks like this:

class Posting
  include ActiveModel::Validations

  attr_accessor :id, :title, :body, :tags

  validates :title, :presence => true
  validates :body, :presence => true

  def initialize(attributes = {})
    # ...
  end

  def save
    set_default_values

    # ...
  end

  def self.create(attributes = {})
    new(attributes).save
  end

  protected

  def set_default_values
    # ...
  end
end

First trouble is that ActiveModel does not provide any attribute access API. Or I did not google enough. So we need to create our own!

Let us have an instance variable @attributes where we will store our model’ data. We need to define getter and setter methods for all the attributes of our model. This may be done with the attr_accessor method. But when user sets the value for some attribute, we should store that in our @attributes variable. And here is the first step to our black magic: we will override the attr_accessor method.

module ActiveAttributes
  def attr_accessor(*args)
    args.each do |k|
      define_method("#{k}".to_sym) { @attributes[k.to_sym] }
      define_method("#{k}=".to_sym) { |value| @attributes[k.to_sym] = value }
    end
  end
end

I just wrapped the code into a single module. Remember: when you include the module in a class, all the module’ methods become class methods.

Now we will include this module before attr_accessor calls. But beware: you need to declare an @attributes instance variable in the constructor!

And let’s just agree with the following convention: all our attribute names should be symbols.

class Posting
  include ActiveModel::Validations
  include ActiveAttributes

  attr_accessor :id, :title, :body, :tags

  validates :title, :presence => true
  validates :body, :presence => true

  def initialize(attributes = {})
    @attributes = {}

    # ...
  end

  def save
    set_default_values

    # ...
  end

  def self.create(attributes = {})
    new(attributes).save
  end

  protected

  def set_default_values
    # ...
  end
end

Now, we can implement our constructor. We now have all the attributes’ getters and setter and thus we can simply call them in our constructor:

class Posting
  include ActiveModel::Validations
  include ActiveAttributes

  attr_accessor :id, :title, :body, :tags

  validates :title, :presence => true
  validates :body, :presence => true

  def initialize(attributes = {})
    @attributes = {}

    attributes.symbolize_keys.each do |k, v|
      v.symbolize_keys! if v.is_a? Hash

      send("#{k}=", v) if respond_to?("#{k}=".to_sym)
    end
  end

  def save
    set_default_values

    # ...
  end

  def self.create(attributes = {})
    new(attributes).save
  end

  protected

  def set_default_values
    # ...
  end
end

Now let’s implement some basic model persisting. First, we should not forget about our validations and add valid? test to the save method.

Let’s say our save method should return the model instance. Thus, we should put the model’ data into the database and get the id for that data (if we put the data with the INSERT statement).

So there is an important caveat: in order to get the correct model id, you need to get it from database in the same transaction as the update/insert statement. The mysql2 gem does support multiple query statements in a single transaction. But to perform such a query, you will need to set the MULTI_STATEMENTS flag when creating a Mysql2::Connection instance.

def save
  set_default_values

  return self unless valid?

  @connection = Mysql2::Client.new({ flags: Mysql2::Client::MULTI_STATEMENTS }.merge(...))

  # ...

  self
rescue
  self
ensure
  @connection.close
end

Here I used the instance variable @connection to make it available within the rescue and ensure statements.

Now we will use our instance variable, @attributes to create an SQL query:

def save
  set_default_values

  return self unless valid?

  @connection = Mysql2::Client.new({ flags: Mysql2::Client::MULTI_STATEMENTS }.merge(...))

    if @attributes[:id].blank?
    columns = @attributes.keys.map { |k| "`#{ k.to_s }`" }.join ','
    values = @attributes.values.map do |v|
      if v.nil?
        'NULL'
      else
        "'#{ ActionController::Base.helpers.sanitize(v.to_s) }'"
      end
    end.join ','

    query = "INSERT INTO postings#{ volume } (#{ columns }) VALUES (#{ values })"
  else
    mapping = @attributes.map { |k, v| "`#{ k.to_s }` = #{ v.nil? ? 'NULL' : "'#{ ActionController::Base.helpers.sanitize(v) }'" }" }.join ','

    query = "UPDATE postings#{ volume } SET #{ mapping } WHERE id = #{ @attributes[:id] }"
  end

  self
rescue
  self
ensure
  @connection.close
end

I used the ActionController::Base.helpers.sanitize helper method to escape the query parameters.

Now we should simply wrap our query into a transaction and get an id from the database.

def save
  set_default_values

  return self unless valid?

  @connection = Mysql2::Client.new({ flags: Mysql2::Client::MULTI_STATEMENTS }.merge(...))

  if @attributes[:id].blank?
    columns = @attributes.keys.map { |k| "`#{ k.to_s }`" }.join ','
    values = @attributes.values.map do |v|
      if v.nil?
        'NULL'
      else
        "'#{ ActionController::Base.helpers.sanitize(v.to_s) }'"
      end
    end.join ','

    query = "INSERT INTO postings#{ volume } (#{ columns }) VALUES (#{ values })"
  else
    mapping = @attributes.map { |k, v| "`#{ k.to_s }` = #{ v.nil? ? 'NULL' : "'#{ ActionController::Base.helpers.sanitize(v) }'" }" }.join ','

    query = "UPDATE postings#{ volume } SET #{ mapping } WHERE id = #{ @attributes[:id] }"
  end

  query = "START TRANSACTION; #{ query }; SELECT LAST_INSERT_ID() AS id; COMMIT;"

  @connection.query(query)

  while @connection.next_result
    result = @connection.store_result.to_a rescue nil

    @attributes[:id] = result.first['id'] if result.present? and result.first.present? and result.first.has_key? 'id'
  end

  self
rescue
  self
ensure
  @connection.close
end

Quite big method, sure. Yet, it performs all the UPDATEs and INSERTs for us.

Let’s add some attribute with the default value, created_at and check how the whole class works:

require 'date'

# ...

attr_accessor :created_at

# ...

protected

def set_default_values
  @attributes[:created_at] = DateTime.now
end

And the test:

p = Posting.new title: "Hello, ActiveModel!", body: "Hello, Database!"

p.save

puts p.created_at

What i always forget about when having an interview

The Ruby interview

I am always being asked at least two questions. Just to verify that I know Ruby basics.

  • What is the main difference between Module and Class?

That is so simple and obvious! Yet it’s too easy to forget… The answer is: you can not instantiate a Module. See, Modules in Ruby do not have constructors. Yeah, they may contain variables, but they do not have an initialize method.

You could define one this way:

module Moo
  def initialize(x)
    @x = x
  end
end

But when you try to call Moo.new you will get a method missing error. When you try to run Moo.initialize you will get a private method called error.

So yes, there is no way to instantiate Modules.

  • What’s the difference between Proc, lambda and block?

This is simple enough to remember as the answer contains only a few points:

  1. Proc is an object; block is not
  2. Proc does not check the number of arguments; lambda does
  3. lambda returns from itself; Proc returns from the outer (containing the Proc call) method
  • What is REST (application)?

The answer on that question hardly depends on what the asking person means.

So, I got two possible correct answers:

a. That is the principle of web application development, when the application responds to a request, depending on which HTTP method was provided (PUT, GET, POST, DELETE, OPTIONS).

b. This is a way of encapsulation Resource and its Handlers. That is a bit hard to explain. Something like “you have to split your application to Resources”.

  • Does Module is the ancestor of Class or does the Class is the child of Module?

This question, actually, may be asked on Class, Module or Object classes. This question is interesting when you do not know the answer.

The reality is plain however:

irb(main):005:0> Object.superclass
=> BasicObject
irb(main):006:0> Class.superclass
=> Module
irb(main):007:0> Module.superclass
=> Object
irb(main):008:0> BasicObject.superclass
=> nil

So, you can even draw a chain:

BasicObject => Object => Module => Class

Some hints

  • Think oral. Show an interviewing person how your thought flow. That is the good practice. It shows that you can think not just remember. And you could get to some friendly talk when you say some magic keyword or tell something the interviewer is interested in.

  • When I am asked of Rails best practices, or just creating my web application, I should never forget one core principle: web application controllers (looking at Rails’ MVC) should be thin. So, the most logic at Controller’s action should get or set some data on Model and provide a response. Nothing more.

Understanding the beauty of JSF

My web framework of choice is Ruby On Rails. It’s perfect for me. Not because of its scalability or performance, but because of its architecture. You do not need to do lot of work to create a website or a webservice. Ruby Gems have all the power you will ever need. Ruby itself allows you to do even a black magic nicely.

It’s true to say that i am a RoR fan.

So when i started learning JSF i thought Gosh! It’s ugly! It’s totally impossible to work with!. But in a while i realized that small yet mighty concept, i even did not imagine to be thinking of.

See, when you write a website, you need two things to be done:

  • give a user static content; user just should see something on a display!
  • take user data, process it and perform previous step; in order to make a dynamic website (which is 90% of all the websites you’ve seen, i guess) you should use web forms and process them

The last thing i did not mention here (because i did not dive in it yet) is: just use that javascript. You would never provide user-friendly interface on the web until you get full control of what’s going on client’ side. That’s the purpose of JavaScript. Different hints and tips, asynchronous operations, messages and other cool stuff making your UI looking great is the JS part.

But in other cases you use resources. That’s the bundle of data, stored (maybe) in database and being controlled by user via the forms.

Any form (yes, just any) could be described as relying on some resource. Login form? It uses User resource. Search form? It uses the SearchQuery resource. Post creation form? It serves Post!

So, that powerful concept i was talking above is the principle a Managed Java Bean describes the Resource wired to the User Interface.

See, when you show a form to a user - you get your database row maped onto a Java Bean. When user saves the form with the new data - that data gets stored in the same Bean and the method you’ve set to commandButton or whatever is invoked.

Just think of it! You are doing exactly the same with all those Rails, Play, Django or any other web framework, when you are creating a dynamic website!

JPA with SQLite autoincrement troubles

Today i’ve got an exception when JPA was trying to insert a new row to a table.

My model definition was like this:

@Entity
public class Merchant {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;

    // ...

    public Merchant(){   }

    // ...

And the table creation script was the following:

CREATE TABLE MERCHANT
(
    ID INTEGER PRIMARY KEY NOT NULL,
    NAME TEXT NOT NULL,
    BANKNAME TEXT NOT NULL,
    SWIFT TEXT NOT NULL,
    ACCOUNT TEXT NOT NULL,
    CHARGE REAL NOT NULL,
    PERIOD INTEGER NOT NULL,
    MINSUM REAL NOT NULL,
    TOTAL REAL NOT NULL
);

I faced some strange exceptions like id could not be null, SQL error or missing database (near “values”: syntax error), etc.

The solution to this consists of two steps:

  • in database: remove the AUTOINCREMENT and NOT NULL annotations from the ID column as SQLite will automatically increment its value (in DB)
  • in entity’ code: remove the GenerationStrategyType from the id member annotation