Skip to content

Commit

Permalink
Merge pull request #7 from hirocaster/refactor
Browse files Browse the repository at this point in the history
Refactor etc ...
  • Loading branch information
hirocaster committed Aug 27, 2015
2 parents c268a01 + 9b4d170 commit 60c70d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
32 changes: 15 additions & 17 deletions lib/active_record/sharding/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,45 +190,43 @@ 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

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
Expand Down
12 changes: 6 additions & 6 deletions lib/active_record/sharding/sequencer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 60c70d9

Please sign in to comment.