Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added rdoc for new alter_table functionality (#109).

  • Loading branch information...
commit 40d430c9cdbbd2dea6f9d12745f4d7a229edd417 1 parent ab89f60
@ciconia ciconia authored
View
2  CHANGELOG
@@ -1,5 +1,7 @@
=== SVN
+* Added rdoc for new alter_table functionality (#109).
+
* Fixed update_sql with array sub-item keys (#110).
* Refactored model specs.
View
58 lib/sequel/database.rb
@@ -188,43 +188,86 @@ def drop_table(*names)
names.each {|n| execute(drop_table_sql(n))}
end
+ # Renames a table:
+ #
+ # DB.tables #=> [:items]
+ # DB.rename_table :items, :old_items
+ # DB.tables #=> [:old_items]
def rename_table(*args)
execute(rename_table_sql(*args))
end
+ # Alters the given table with the specified block. Here are the currently
+ # available operations:
+ #
+ # DB.alter_table :items do
+ # add_column :category, :text, :default => 'ruby'
+ # drop_column :category
+ # rename_column :cntr, :counter
+ # set_column_type :value, :float
+ # set_column_default :value, :float
+ # add_index [:group, :category]
+ # drop_index [:group, :category]
+ # end
+ #
+ # Note that #add_column accepts all the options available for column
+ # definitions using create_table, and #add_index accepts all the options
+ # available for index definition.
def alter_table(name, &block)
g = Schema::AlterTableGenerator.new(self, &block)
alter_table_sql_list(name, g.operations).each {|sql| execute(sql)}
end
+ # Adds a column to the specified table. This method expects a column name,
+ # a datatype and optionally a hash with additional constraints and options:
+ #
+ # DB.add_column :items, :name, :text, :unique => true, :null => false
+ # DB.add_column :items, :category, :text, :default => 'ruby'
def add_column(table, *args)
alter_table(table) {add_column(*args)}
end
+ # Removes a column from the specified table:
+ #
+ # DB.drop_column :items, :category
def drop_column(table, *args)
alter_table(table) {drop_column(*args)}
end
+ # Renames a column in the specified table. This method expects the current
+ # column name and the new column name:
+ #
+ # DB.rename_column :items, :cntr, :counter
def rename_column(table, *args)
alter_table(table) {rename_column(*args)}
end
+ # Set the data type for the given column in the given table:
+ #
+ # DB.set_column_type :items, :price, :float
def set_column_type(table, *args)
alter_table(table) {set_column_type(*args)}
end
+ # Sets the default value for the given column in the given table:
+ #
+ # DB.set_column_default :items, :category, 'perl!'
def set_column_default(table, *args)
alter_table(table) {set_column_default(*args)}
end
# Adds an index to a table for the given columns:
#
- # DB.add_index(:posts, :title)
- # DB.add_index(:posts, [:author, :title], :unique => true)
+ # DB.add_index :posts, :title
+ # DB.add_index :posts, [:author, :title], :unique => true
def add_index(table, *args)
alter_table(table) {add_index(*args)}
end
+ # Removes an index for the given table and column/s:
+ #
+ # DB.drop_index :posts, :title
+ # DB.drop_index :posts, [:author, :title]
def drop_index(table, *args)
alter_table(table) {drop_index(*args)}
end
@@ -240,16 +283,27 @@ def table_exists?(name)
false
end
+ # Creates a view based on a dataset or an SQL string:
+ #
+ # DB.create_view(:cheap_items, "SELECT * FROM items WHERE price < 100")
+ # DB.create_view(:ruby_items, DB[:items].filter(:category => 'ruby'))
def create_view(name, source)
source = source.sql if source.is_a?(Dataset)
execute("CREATE VIEW #{name} AS #{source}")
end
+ # Creates a view, replacing it if it already exists:
+ #
+ # DB.create_or_replace_view(:cheap_items, "SELECT * FROM items WHERE price < 100")
+ # DB.create_or_replace_view(:ruby_items, DB[:items].filter(:category => 'ruby'))
def create_or_replace_view(name, source)
source = source.sql if source.is_a?(Dataset)
execute("CREATE OR REPLACE VIEW #{name} AS #{source}")
end
+ # Drops a view:
+ #
+ # DB.drop_view(:cheap_items)
def drop_view(name)
execute("DROP VIEW #{name}")
end
View
2  lib/sequel/model/validations.rb
@@ -71,7 +71,7 @@ class Model
#
# You may determine whether the model has validates via:
#
- # validations? # will return true / false based on existence of validations on the model.
+ # has_validations? # will return true / false based on existence of validations on the model.
#
# You may also retrieve the validations block if needed:
#
Please sign in to comment.
Something went wrong with that request. Please try again.