Question

So, I am trying to get redmine_backlogs to work with SQL server. [We are using SQL Server rather than SQLite, to scale better] I am NOT a Ruby programmer, but I really want this plugin for my team, as we actively use Redmine for several projects.

After reading through some Ruby tutorials, I've managed to get make some modifications and get the plugin installed and migrated correctly [it appears].

On the plugin settings screen [in administration] in Redmine, it shows the migration wasn't successful. Even though all the list items are green, and the migrations appeared to work. Any ideas?

The changes I made were to bypass suspected issues with the way Active records handles direct SQL queries.

Here are the changes I've made -

ERROR #1 –

C:\Projects\Redmine\redmine-2.3.2>rake redmine:plugins:migrate
Migrating redmine_backlogs (Redmine Backlogs)...
==  AddStoryPositions: migrating ==============================================
-- execute("select max(position) from issues")
   -> 0.0020s
   -> -1 rows
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `each' for -1:FixnumC:/Projects/Redmine/redmine-2.3.2/plugins/redmine_backlogs/db/migrate/026_add_story_positions.rb:10:in `up'

FIX #1 –

Direct queries are not working correctly with the sqladapter [TinyTds + Active Record] 026_add_story_positions.rb

class AddStoryPositions < ActiveRecord::Migration
  def self.up
    # Rails doesn't support temp tables, mysql doesn't support update
    # from same-table subselect

    unless RbStory.trackers.size == 0
      max = 0
          dbconfig = YAML.load_file(File.join(File.dirname(__FILE__), '../../../../config/database.yml'))#[Rails.env]['username']

        if dbconfig[Rails.env]['adapter'] == 'sqlserver' then
            database = dbconfig[Rails.env]['database']
            dataserver = dbconfig[Rails.env]['dataserver']
            mode = dbconfig[Rails.env]['mode']
            port = dbconfig[Rails.env]['port']
            username = dbconfig[Rails.env]['username']
            password = dbconfig[Rails.env]['password']

            client = TinyTds::Client.new(
                :database => database,
                :dataserver => dataserver,
                :mode => mode,
                :port => port,
                :username => username,
                :password => password)

            client.execute("select max(position) from issues").each{|row| max = row[0]}

            client.execute "update issues
               set position = #{max} + id
               where position is null and tracker_id in (#{RbStory.trackers(:type=>:string)})"
        else
            execute("select max(position) from issues").each{|row| max = row[0]}

            execute "update issues
               set position = #{max} + id
               where position is null and tracker_id in (#{RbStory.trackers(:type=>:string)})"
        end

    end
  end

  def self.down
    puts "Reverting irreversible migration"
  end
end

ERROR #2 rake aborted! An error has occurred, this and all later migrations canceled:

TinyTds::Error: ALTER TABLE ALTER COLUMN position failed because one or more objects access this column.: ALTER TABLE [issues] ALTER COLUMN [position] integer NOT NULLC:/Projects/Redmine/redmine-2.3.2/plugins/redmine_backlogs/db/migrate/033_unique_positions.rb:30:in `up'

FIX #2

033_unique_positions.rb

    #SQLServer cannot change the type of an indexes column, so it must be dropped first
   remove_index :issues, :position
    change_column :issues, :position, :integer, :null => false
    add_index :issues, :position

ERROR #3 rake aborted! undefined method each' for -1:Fixnum C:/Projects/Redmine/redmine-2.3.2/plugins/redmine_backlogs/lib/backlogs_setup.rb:155:inmigrated?'

FIX #3

def migrated?
    available = Dir[File.join(File.dirname(__FILE__), '../db/migrate/*.rb')].collect{|m| Integer(File.basename(m).split('_')[0].gsub(/^0+/, ''))}.sort
    return true if available.size == 0
    available = available[-1]

    ran = []
    dbconfig = YAML.load_file(File.join(File.dirname(__FILE__), '../../../config/database.yml'))#[Rails.env]['username']

    if dbconfig[Rails.env]['adapter'] == 'sqlserver' then
      database = dbconfig[Rails.env]['database']
      dataserver = dbconfig[Rails.env]['dataserver']
      mode = dbconfig[Rails.env]['mode']
      port = dbconfig[Rails.env]['port']
      username = dbconfig[Rails.env]['username']
      password = dbconfig[Rails.env]['password']

      client = TinyTds::Client.new(
        :database => database,
        :dataserver => dataserver,
        :mode => mode,
        :port => port,
        :username => username,
        :password => password)

      client.execute("select version from schema_migrations where version like '%-redmine_backlogs'").each{|m|
        ran << Integer((m.is_a?(Hash) ? m.values : m)[0].split('-')[0])
      }
    else
      Setting.connection.execute("select version from schema_migrations where version like '%-redmine_backlogs'").each{|m|
        ran << Integer((m.is_a?(Hash) ? m.values : m)[0].split('-')[0])
      }
    end

    return false if ran.size == 0
    ran = ran.sort[-1]

    return ran >= available
  end
  module_function :migrated?    

I was using the wrong where clause - This is the correct one, I must have overwritten when debugging. '%-redmine_backlogs'

The above code works.

Was it helpful?

Solution

I could not answer my own question before, but now I can. The above code was tested and works. I have been running backlogs on Windows with MS SQL successfully since.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top