Class: Peatio::Sql::Schema
- Inherits:
-
Object
- Object
- Peatio::Sql::Schema
- Defined in:
- lib/peatio/sql/schema.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
- #create_database(name) ⇒ Object
- #create_tables(options = {}) ⇒ Object
-
#initialize(sql_client) ⇒ Schema
constructor
A new instance of Schema.
Constructor Details
#initialize(sql_client) ⇒ Schema
Returns a new instance of Schema.
5 6 7 |
# File 'lib/peatio/sql/schema.rb', line 5 def initialize(sql_client) @client = sql_client end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
3 4 5 |
# File 'lib/peatio/sql/schema.rb', line 3 def client @client end |
Instance Method Details
#create_database(name) ⇒ Object
9 10 11 |
# File 'lib/peatio/sql/schema.rb', line 9 def create_database(name) client.query("CREATE DATABASE IF NOT EXISTS `#{ name }`;") end |
#create_tables(options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/peatio/sql/schema.rb', line 13 def create_tables( = {}) statements = [] statements << "DROP TABLE IF EXISTS `operations`;" if [:drop_if_exists] statements << "CREATE TABLE IF NOT EXISTS `operations` (\nid INT UNSIGNED NOT NULL AUTO_INCREMENT,\ncode TINYINT UNSIGNED NOT NULL,\naccount_id INT UNSIGNED NOT NULL,\nreference INT UNSIGNED NOT NULL,\ndebit DECIMAL(32, 16) NOT NULL,\ncredit DECIMAL(32, 16) NOT NULL,\ncreated_at DATETIME NOT NULL,\nupdated_at DATETIME NOT NULL,\nPRIMARY KEY (id),\nINDEX `balance_key` (account_id, debit, credit)\n) ENGINE = InnoDB;\n" statements << "DROP TABLE IF EXISTS `orders`;" if [:drop_if_exists] statements << "CREATE TABLE IF NOT EXISTS`orders` (\n `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n `uid` INT(11) UNSIGNED NOT NULL,\n `bid` VARCHAR(5) NOT NULL,\n `ask` VARCHAR(5) NOT NULL,\n `market` VARCHAR(10) NOT NULL,\n `price` DECIMAL(32,16) DEFAULT NULL,\n `volume` DECIMAL(32,16) NOT NULL,\n `fee` DECIMAL(32,16) NOT NULL DEFAULT '0.0000000000000000',\n `type` TINYINT UNSIGNED NOT NULL,\n `state` TINYINT UNSIGNED NOT NULL,\n `created_at` DATETIME NOT NULL,\n `updated_at` DATETIME NOT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n" statements << "DROP TABLE IF EXISTS `trades`;" if [:drop_if_exists] statements << "CREATE TABLE IF NOT EXISTS `trades` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `market` varchar(10) NOT NULL,\n `volume` decimal(32,16) NOT NULL,\n `price` decimal(32,16) NOT NULL,\n `ask_id` int(11) NOT NULL,\n `bid_id` int(11) NOT NULL,\n `ask_uid` int(11) NOT NULL,\n `bid_uid` int(11) NOT NULL,\n `created_at` datetime NOT NULL,\n `updated_at` datetime NOT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n" statements.each do |statement| puts statement client.query(statement) end end |