Class: Cequel::Schema::Keyspace Deprecated

Inherits:
Object
  • Object
show all
Extended by:
Util::Forwardable
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

Methods included from Util::Forwardable

delegate

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



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

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



171
172
173
174
175
# File 'lib/cequel/schema/keyspace.rb', line 171

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

  • :replication (Hash) — default: { class: "SimpleStrategy", replication_factor: 1 }

    replication options for this keyspace

  • :durable_writes (Boolean) — default: true

    durable_writes option for the keyspace

See Also:

Since:

  • 1.0.0



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!(options = {})
  bare_connection =
    Metal::Keyspace.new(keyspace.configuration.except(:keyspace))

  default_options = {
    replication: {
      class: "SimpleStrategy",
      replication_factor: 1
    },
    durable_writes: true
  }

  options = options.symbolize_keys
  options.reverse_merge!(keyspace.configuration.symbolize_keys)
  options.reverse_merge!(default_options)

  if options.key?(:class)
    options[:replication][:class] = options[:class]
    if options[:class] != 'SimpleStrategy'
      raise 'For strategy other than SimpleStrategy, please ' \
        'use the :replication option.'
    end
  end

  if options.key?(:replication_factor)
    options[:replication][:replication_factor] =
      options[:replication_factor]
  end

  replication_options_strs = options[:replication].map do |name, value|
    "'#{name}': #{Cequel::Type.quote(value)}"
  end

  bare_connection.execute(<<-CQL.strip_heredoc)
    CREATE KEYSPACE #{keyspace.name}
    WITH REPLICATION = {#{replication_options_strs.join(', ')}}
    AND durable_writes = #{options[:durable_writes]}
  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:

  • block evaluated in the context of a CreateTableDSL

See Also:

  • CreateTableDSL

Since:

  • 1.0.0



150
151
152
153
# File 'lib/cequel/schema/keyspace.rb', line 150

def create_table(name, &block)
  table = TableDescDsl.new(name).eval(&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



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_materialized_view(name, exists: false) ⇒ void

This method returns an undefined value.

Drop this materialized view from the keyspace

Parameters:

  • name (Symbol)

    name of the materialized view to drop

  • exists (Boolean) (defaults to: false)

    if set to true, will drop only if exists (Cassandra 3.x)

Since:

  • 1.0.0



208
209
210
# File 'lib/cequel/schema/keyspace.rb', line 208

def drop_materialized_view(name, exists: false)
  keyspace.execute("DROP MATERIALIZED VIEW #{'IF EXISTS ' if exists}#{name}")
end

#drop_table(name, exists: false) ⇒ void

This method returns an undefined value.

Drop this table from the keyspace

Parameters:

  • name (Symbol)

    name of the table to drop

  • exists (Boolean) (defaults to: false)

    if set to true, will drop only if exists (Cassandra 3.x)

Since:

  • 1.0.0



197
198
199
# File 'lib/cequel/schema/keyspace.rb', line 197

def drop_table(name, exists: false)
  keyspace.execute("DROP TABLE #{'IF EXISTS ' if exists}#{name}")
end

#get_table_reader(name) ⇒ TableReader

Returns object.

Parameters:

  • name (Symbol)

    name of the table to read

Returns:

Since:

  • 1.0.0



125
126
127
# File 'lib/cequel/schema/keyspace.rb', line 125

def get_table_reader(name)
  TableReader.get(keyspace, name)
end

#has_table?(table_name) ⇒ Boolean

Returns true iff the specified table name exists in the keyspace.

Returns:

  • (Boolean)

Since:

  • 1.0.0



241
242
243
# File 'lib/cequel/schema/keyspace.rb', line 241

def has_table?(table_name)
  !!read_table(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



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.

Parameters:

  • name (Symbol)

    name of the table to synchronize

Yields:

  • block evaluated in the context of a CreateTableDSL

See Also:

Since:

  • 1.0.0



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/cequel/schema/keyspace.rb', line 225

def sync_table(name, &block)
  new_table_desc = TableDescDsl.new(name).eval(&block)
  patch = if has_table?(name)
            existing_table_desc = read_table(name)
            TableDiffer.new(existing_table_desc, new_table_desc).call
          else
            TableWriter.new(new_table_desc) # close enough to a patch
          end

  patch.statements.each{|stmt| keyspace.execute(stmt) }
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



186
187
188
# File 'lib/cequel/schema/keyspace.rb', line 186

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