diff --git a/lib/active_record/sharding/database_tasks.rb b/lib/active_record/sharding/database_tasks.rb index 52930a0..c44cf08 100644 --- a/lib/active_record/sharding/database_tasks.rb +++ b/lib/active_record/sharding/database_tasks.rb @@ -190,17 +190,13 @@ def drop_sequencer_database(args) end def create_table_sequencer_database(args) - sequencer_name = sequencer_name_or_error "create_table", args - sequencer = sequencer_or_error sequencer_name - + sequencer = sequencer_or_error "create_table", args create_table_sql = "CREATE TABLE #{sequencer.table_name} (id BIGINT unsigned NOT NULL DEFAULT 0) ENGINE=MyISAM" execute sequencer.connection_name.to_s, create_table_sql end def insert_initial_record_sequencer_database(args) - sequencer_name = sequencer_name_or_error "insert_initial_record", args - sequencer = sequencer_or_error sequencer_name - + sequencer = sequencer_or_error "insert_initial_record", args insert_initial_record_sql = "INSERT INTO #{sequencer.table_name} VALUES (0)" execute sequencer.connection_name.to_s, insert_initial_record_sql end @@ -208,27 +204,29 @@ def insert_initial_record_sequencer_database(args) private def exec_task_for_sequencer_database(task_name, args) - sequencer_name = sequencer_name_or_error task_name, args - sequencer = sequencer_or_error sequencer_name + sequencer = sequencer_or_error task_name, args __send__ task_name, sequencer.connection_name.to_s end - def sequencer_name_or_error(name, args) + def sequencer_or_error(task_name, args) + sequencer_name = sequencer_name_or_error task_name, args + fetch_sequencer_config sequencer_name.to_sym + rescue KeyError + $stderr.puts %(sequencer name "#{sequencer_name}" not found.) + exit + end + + def sequencer_name_or_error(task_name, args) unless sequencer_name = args[:sequencer_name] + # rubocop:disable Metrics/LineLength $stderr.puts <<-MSG -Missing sequencer_name. Find sequencer_name via `rake active_record:sharding:info` then call `rake "active_record:sharding:sequencer#{name}[$sequencer_name]"`. +Missing sequencer_name. Find sequencer_name via `rake active_record:sharding:info` then call `rake "active_record:sharding:sequencer#{task_name}[$sequencer_name]"`. MSG exit + # rubocop:enable Metrics/LineLength end sequencer_name end - - def sequencer_or_error(sequencer_name) - fetch_sequencer_config sequencer_name.to_sym - rescue KeyError - $stderr.puts %(sequencer name "#{sequencer_name}" not found.) - exit - end end extend TasksForSingleSequencerTask end # module DatabaseTasks diff --git a/lib/active_record/sharding/sequencer.rb b/lib/active_record/sharding/sequencer.rb index 050ee0c..57e8fb9 100644 --- a/lib/active_record/sharding/sequencer.rb +++ b/lib/active_record/sharding/sequencer.rb @@ -20,16 +20,16 @@ def use_sequencer(name) end def current_sequence_id - connection = sequencer_repository.fetch(sequencer_name).connection - connection.execute "UPDATE `#{sequencer_config.table_name}` SET id = LAST_INSERT_ID(id)" - res = connection.execute "SELECT LAST_INSERT_ID()" - new_id = res.first.first.to_i - new_id + execute_sql "id" end def next_sequence_id + execute_sql "id +1" + end + + def execute_sql(last_insert_id_args) connection = sequencer_repository.fetch(sequencer_name).connection - connection.execute "UPDATE `#{sequencer_config.table_name}` SET id = LAST_INSERT_ID(id +1)" + connection.execute "UPDATE `#{sequencer_config.table_name}` SET id = LAST_INSERT_ID(#{last_insert_id_args})" res = connection.execute "SELECT LAST_INSERT_ID()" new_id = res.first.first.to_i new_id