Class: Cequel::Schema::Keyspace Deprecated
- Inherits:
-
Object
- Object
- Cequel::Schema::Keyspace
- Extended by:
- Util::Forwardable
- Defined in:
- lib/cequel/schema/keyspace.rb
Overview
These methods will be exposed directly on Metal::Keyspace in a future version of Cequel
Provides read/write access to the schema for a keyspace and the tables it contains
Instance Method Summary collapse
-
#alter_table(name) { ... } ⇒ void
Make changes to an existing table in the keyspace.
-
#create!(options = {}) ⇒ void
Create this keyspace in the database.
-
#create_table(name) { ... } ⇒ void
Create a table in the keyspace.
-
#drop! ⇒ void
Drop this keyspace from the database.
-
#drop_table(name) ⇒ void
Drop this table from the keyspace.
-
#initialize(keyspace) ⇒ Keyspace
constructor
private
A new instance of Keyspace.
-
#read_table(name) ⇒ Table
Object representation of the table schema as it currently exists in the database.
-
#sync_table(name) { ... } ⇒ void
(also: #synchronize_table)
Create or update a table to match a given schema structure.
-
#truncate_table(name) ⇒ void
Remove all data from this table.
Methods included from Util::Forwardable
Constructor Details
#initialize(keyspace) ⇒ Keyspace
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Keyspace.
20 21 22 |
# File 'lib/cequel/schema/keyspace.rb', line 20 def initialize(keyspace) @keyspace = keyspace end |
Instance Method Details
#alter_table(name) { ... } ⇒ void
This method returns an undefined value.
Make changes to an existing table in the keyspace
164 165 166 167 168 |
# File 'lib/cequel/schema/keyspace.rb', line 164 def alter_table(name, &block) updater = TableUpdater.apply(keyspace, name) do |updater| UpdateTableDSL.apply(updater, &block) end end |
#create!(options = {}) ⇒ void
This method returns an undefined value.
Create this keyspace in the database
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 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cequel/schema/keyspace.rb', line 42 def create!( = {}) = Metal::Keyspace.new(keyspace.configuration.except(:keyspace)) = { replication: { class: "SimpleStrategy", replication_factor: 1 }, durable_writes: true } = .symbolize_keys .reverse_merge!(keyspace.configuration) .reverse_merge!() if .key?(:class) [:replication][:class] = [:class] if [:class] != 'SimpleStrategy' raise 'For strategy other than SimpleStrategy, please ' \ 'use the :replication option.' end end if .key?(:replication_factor) [:replication][:replication_factor] = [:replication_factor] end = [:replication].map do |name, value| "'#{name}': #{Cequel::Type.quote(value)}" end .execute(<<-CQL.strip_heredoc) CREATE KEYSPACE #{keyspace.name} WITH REPLICATION = {#{.join(', ')}} AND durable_writes = #{[:durable_writes]} CQL end |
#create_table(name) { ... } ⇒ void
This method returns an undefined value.
Create a table in the keyspace
142 143 144 145 146 |
# File 'lib/cequel/schema/keyspace.rb', line 142 def create_table(name, &block) table = Table.new(name) CreateTableDSL.apply(table, &block) TableWriter.apply(keyspace, table) end |
#drop! ⇒ void
This method returns an undefined value.
Drop this keyspace from the database
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cequel/schema/keyspace.rb', line 90 def drop! keyspace.execute("DROP KEYSPACE #{keyspace.name}").tap do # If you execute a DROP KEYSPACE statement on a Cassandra::Session # with keyspace set to the one being dropped, then it will set # its keyspace to nil after the statement finishes. E.g. # session.keyspace # => "cequel_test" # session.execute("DROP KEYSPACE cequel_test") # session.keyspace # => nil # This causes problems in the specs where we drop the test keyspace # and recreate it. Cequel::Record.connection.client's keyspace will # be set to nil after dropping the keyspace, but after creating it # again, it will still be set to nil. Easy fix is to just call # clear_active_connections! after dropping any keyspace. keyspace.clear_active_connections! end end |
#drop_table(name) ⇒ void
This method returns an undefined value.
Drop this table from the keyspace
189 190 191 |
# File 'lib/cequel/schema/keyspace.rb', line 189 def drop_table(name) keyspace.execute("DROP TABLE #{name}") end |
#read_table(name) ⇒ Table
Returns object representation of the table schema as it currently exists in the database.
117 118 119 |
# File 'lib/cequel/schema/keyspace.rb', line 117 def read_table(name) TableReader.read(keyspace, name) end |
#sync_table(name) { ... } ⇒ void Also known as: synchronize_table
This method returns an undefined value.
Create or update a table to match a given schema structure. The desired schema structure is defined by the directives given in the block; this is then compared to the existing table in the database (if it is defined at all), and then the table is created or altered accordingly.
206 207 208 209 210 211 |
# File 'lib/cequel/schema/keyspace.rb', line 206 def sync_table(name, &block) existing = read_table(name) updated = Table.new(name) CreateTableDSL.apply(updated, &block) TableSynchronizer.apply(keyspace, existing, updated) end |
#truncate_table(name) ⇒ void
This method returns an undefined value.
Remove all data from this table. Truncating a table can be much slower than simply iterating over its keys and issuing ‘DELETE` statements, particularly if the table does not have many rows. Truncating is equivalent to dropping a table and then recreating it
179 180 181 |
# File 'lib/cequel/schema/keyspace.rb', line 179 def truncate_table(name) keyspace.execute("TRUNCATE #{name}") end |