зеркало из https://github.com/github/github-ds.git
Add github sql examples
Also wrapped up attempting to create the database.
This commit is contained in:
Родитель
f0149a1d1e
Коммит
c6ff728a30
|
@ -7,22 +7,33 @@ $:.unshift(lib_path)
|
|||
|
||||
require "active_record"
|
||||
|
||||
ActiveRecord::Base.establish_connection({
|
||||
adapter: "mysql2",
|
||||
database: "github_kv_test",
|
||||
})
|
||||
attempts = 0
|
||||
begin
|
||||
ActiveRecord::Base.establish_connection({
|
||||
adapter: "mysql2",
|
||||
database: "github_store_test",
|
||||
})
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `key_values`")
|
||||
require "generators/github/store/templates/migration"
|
||||
ActiveRecord::Migration.verbose = false
|
||||
CreateKeyValuesTable.up
|
||||
rescue ActiveRecord::NoDatabaseError
|
||||
raise if attempts >= 1
|
||||
ActiveRecord::Base.establish_connection({
|
||||
adapter: "mysql2",
|
||||
})
|
||||
ActiveRecord::Base.connection.execute("CREATE DATABASE `github_store_test`")
|
||||
attempts += 1
|
||||
retry
|
||||
end
|
||||
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `key_values`")
|
||||
ActiveRecord::Base.connection.execute(<<-EOS)
|
||||
CREATE TABLE `key_values` (
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `example_key_values`")
|
||||
ActiveRecord::Base.connection.execute(<<-SQL)
|
||||
CREATE TABLE `example_key_values` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`key` varchar(255) NOT NULL,
|
||||
`value` blob NOT NULL,
|
||||
`created_at` datetime NOT NULL,
|
||||
`updated_at` datetime NOT NULL,
|
||||
`expires_at` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `index_key_values_on_key` (`key`),
|
||||
KEY `index_key_values_on_expires_at` (`expires_at`)
|
||||
UNIQUE KEY `index_key_values_on_key` (`key`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8
|
||||
EOS
|
||||
SQL
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
require File.expand_path("../example_setup", __FILE__)
|
||||
require "github/sql"
|
||||
|
||||
insert_statement = "INSERT INTO example_key_values (`key`, `value`) VALUES (:key, :value)"
|
||||
select_statement = "SELECT value FROM example_key_values WHERE `key` = :key"
|
||||
update_statement = "UPDATE example_key_values SET value = :value WHERE `key` = :key"
|
||||
delete_statement = "DELETE FROM example_key_values WHERE `key` = :key"
|
||||
|
||||
################################# Class Style ##################################
|
||||
sql = GitHub::SQL.run insert_statement, key: "foo", value: "bar"
|
||||
p sql.last_insert_id
|
||||
# 1
|
||||
|
||||
p GitHub::SQL.value select_statement, key: "foo"
|
||||
# "bar"
|
||||
|
||||
sql = GitHub::SQL.run update_statement, key: "foo", value: "new value"
|
||||
p sql.affected_rows
|
||||
# 1
|
||||
|
||||
sql = GitHub::SQL.run delete_statement, key: "foo"
|
||||
p sql.affected_rows
|
||||
# 1
|
||||
|
||||
|
||||
################################ Instance Style ################################
|
||||
sql = GitHub::SQL.new insert_statement, key: "foo", value: "bar"
|
||||
sql.run
|
||||
p sql.last_insert_id
|
||||
# 2
|
||||
|
||||
sql = GitHub::SQL.new select_statement, key: "foo"
|
||||
p sql.value
|
||||
# "bar"
|
||||
|
||||
sql = GitHub::SQL.new update_statement, key: "foo", value: "new value"
|
||||
sql.run
|
||||
p sql.affected_rows
|
||||
# 1
|
||||
|
||||
sql = GitHub::SQL.new delete_statement, key: "foo"
|
||||
sql.run
|
||||
p sql.affected_rows
|
||||
# 1
|
|
@ -138,6 +138,7 @@ module GitHub
|
|||
query = nil
|
||||
end
|
||||
|
||||
@last_insert_id = nil
|
||||
@affected_rows = nil
|
||||
@binds = binds ? binds.dup : {}
|
||||
@query = ""
|
||||
|
@ -241,7 +242,7 @@ module GitHub
|
|||
|
||||
# Public: The last inserted ID for this connection.
|
||||
def last_insert_id
|
||||
connection.raw_connection.last_insert_id
|
||||
@last_insert_id || connection.raw_connection.last_insert_id
|
||||
end
|
||||
|
||||
# Public: Map each row to an instance of an ActiveRecord::Base subclass.
|
||||
|
|
|
@ -6,12 +6,22 @@ require "timecop"
|
|||
require "minitest/autorun"
|
||||
require "mocha/mini_test"
|
||||
|
||||
ActiveRecord::Base.establish_connection({
|
||||
adapter: "mysql2",
|
||||
database: "github_data_test",
|
||||
})
|
||||
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `key_values`")
|
||||
require "generators/github/store/templates/migration"
|
||||
ActiveRecord::Migration.verbose = false
|
||||
CreateKeyValuesTable.up
|
||||
attempts = 0
|
||||
begin
|
||||
ActiveRecord::Base.establish_connection({
|
||||
adapter: "mysql2",
|
||||
database: "github_store_test",
|
||||
})
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `key_values`")
|
||||
require "generators/github/store/templates/migration"
|
||||
ActiveRecord::Migration.verbose = false
|
||||
CreateKeyValuesTable.up
|
||||
rescue ActiveRecord::NoDatabaseError
|
||||
raise if attempts >= 1
|
||||
ActiveRecord::Base.establish_connection({
|
||||
adapter: "mysql2",
|
||||
})
|
||||
ActiveRecord::Base.connection.execute("CREATE DATABASE `github_store_test`")
|
||||
attempts += 1
|
||||
retry
|
||||
end
|
||||
|
|
Загрузка…
Ссылка в новой задаче