You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: 20151105145127_state_order_function.rb
    path of file: ./repos/catarse/db/migrate
    
    the code of the file until where you have to start completion: class StateOrderFunction < ActiveRecord::Migration
  def up
    execute <<-SQL
CREATE FUNCTION state_order(project projects)
RETURNS project_state_order
STABLE
LANGUAGE SQL
AS $$
SELECT
    CASE project.mode
    WHEN 'flexible' THEN
        (
        SELECT state_order
        FROM
        flexible_project_states ps
        WHERE
        ps.state = project.state
        )
    ELSE
        (
        SELECT 
    
  def up
    execute <<-SQL
CREATE FUNCTION state_order(project projects)
RETURNS project_state_order
STABLE
LANGUAGE SQL
AS $$
SELECT
    CASE project.mode
    WHEN 'flexible' THEN
        (
        SELECT state_order
        FROM
        flexible_project_states ps
        WHERE
        ps.state = project.state
        )
    ELSE
        (
        SELECT state_order
        FROM
        project_states ps
        WHERE
        ps.state = project.state
        )
    END;
$$;
    SQL
  end