Class: Cequel::Schema::Keyspace Deprecated

Inherits:
Object
  • Object
show all
Defined in:
lib/cequel/schema/keyspace.rb

Overview

Deprecated.

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

Since:

  • 1.0.0

Instance Method Summary collapse

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.

Parameters:

  • keyspace (Keyspace)

    the keyspace whose schema this object manipulates

Since:

  • 1.0.0



17
18
19
# File 'lib/cequel/schema/keyspace.rb', line 17

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

Examples:

schema.alter_table :posts do
  add_set :categories, :text
  rename_column :author_id, :author_uuid
  create_index :title
end

Parameters:

  • name (Symbol)

    the name of the table to alter

Yields:

See Also:

Since:

  • 1.0.0



118
119
120
121
122
# File 'lib/cequel/schema/keyspace.rb', line 118

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

Parameters:

  • options (Options) (defaults to: {})

    persistence options for this keyspace.

Options Hash (options):

  • :class (String) — default: "SimpleStrategy"

    the replication strategy to use for this keyspace

  • :replication_factor (Integer) — default: 1

    the number of replicas that should exist for each piece of data

See Also:

Since:

  • 1.0.0



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cequel/schema/keyspace.rb', line 35

def create!(options = {})
  bare_connection =
    Metal::Keyspace.new(keyspace.configuration.except(:keyspace))

  options = options.symbolize_keys
  options[:class] ||= 'SimpleStrategy'
  if options[:class] == 'SimpleStrategy'
    options[:replication_factor] ||= 1
  end
  options_strs = options.map do |name, value|
    "'#{name}': #{CassandraCQL::Statement.quote(value)}"
  end

  bare_connection.execute(<<-CQL)
    CREATE KEYSPACE #{keyspace.name}
    WITH REPLICATION = {#{options_strs.join(', ')}}
  CQL
end

#create_table(name) { ... } ⇒ void

This method returns an undefined value.

Create a table in the keyspace

Examples:

schema.create_table :posts do
  partition_key :blog_subdomain, :text
  key :id, :timeuuid

  column :title, :text
  column :body, :text
  column :author_id, :uuid, :index => true

  with :caching, :all
end

Parameters:

  • name (Symbol)

    name of the new table to create

Yields:

See Also:

Since:

  • 1.0.0



96
97
98
99
100
# File 'lib/cequel/schema/keyspace.rb', line 96

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

See Also:

Since:

  • 1.0.0



62
63
64
# File 'lib/cequel/schema/keyspace.rb', line 62

def drop!
  keyspace.execute("DROP KEYSPACE #{keyspace.name}")
end

#drop_table(name) ⇒ void

This method returns an undefined value.

Drop this table from the keyspace

Parameters:

  • name (Symbol)

    name of the table to drop

Since:

  • 1.0.0



143
144
145
# File 'lib/cequel/schema/keyspace.rb', line 143

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.

Parameters:

  • name (Symbol)

    name of the table to read

Returns:

  • (Table)

    object representation of the table schema as it currently exists in the database

Since:

  • 1.0.0



71
72
73
# File 'lib/cequel/schema/keyspace.rb', line 71

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.

Parameters:

  • name (Symbol)

    name of the table to synchronize

Yields:

Raises:

See Also:

Since:

  • 1.0.0



160
161
162
163
164
165
# File 'lib/cequel/schema/keyspace.rb', line 160

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

Parameters:

  • name (Symbol)

    name of the table to truncate.

Since:

  • 1.0.0



133
134
135
# File 'lib/cequel/schema/keyspace.rb', line 133

def truncate_table(name)
  keyspace.execute("TRUNCATE #{name}")
end