Module: Sequel::Plugins::Schema::ClassMethods
- Defined in:
- lib/sequel/plugins/schema.rb
Instance Method Summary collapse
-
#create_table(*args, &block) ⇒ Object
Creates table, using the column information from set_schema.
-
#create_table!(*args, &block) ⇒ Object
Drops the table if it exists and then runs create_table.
-
#create_table?(*args, &block) ⇒ Boolean
Creates the table unless the table already exists.
-
#drop_table ⇒ Object
Drops table.
-
#drop_table? ⇒ Boolean
Drops table if it already exists, do nothing if it doesn’t exist.
-
#schema ⇒ Object
Returns table schema created with set_schema for direct descendant of Model.
-
#set_schema(name = nil, &block) ⇒ Object
Defines a table schema (see Schema::Generator for more information).
-
#table_exists? ⇒ Boolean
Returns true if table exists, false otherwise.
Instance Method Details
#create_table(*args, &block) ⇒ Object
Creates table, using the column information from set_schema.
24 25 26 27 28 29 |
# File 'lib/sequel/plugins/schema.rb', line 24 def create_table(*args, &block) set_schema(*args, &block) if block db.create_table(table_name, :generator=>@schema) @db_schema = get_db_schema(true) columns end |
#create_table!(*args, &block) ⇒ Object
Drops the table if it exists and then runs create_table. Should probably not be used except in testing.
33 34 35 36 |
# File 'lib/sequel/plugins/schema.rb', line 33 def create_table!(*args, &block) drop_table? create_table(*args, &block) end |
#create_table?(*args, &block) ⇒ Boolean
Creates the table unless the table already exists
39 40 41 |
# File 'lib/sequel/plugins/schema.rb', line 39 def create_table?(*args, &block) create_table(*args, &block) unless table_exists? end |
#drop_table ⇒ Object
Drops table. If the table doesn’t exist, this will probably raise an error.
44 45 46 |
# File 'lib/sequel/plugins/schema.rb', line 44 def drop_table db.drop_table(table_name) end |
#drop_table? ⇒ Boolean
Drops table if it already exists, do nothing if it doesn’t exist.
49 50 51 |
# File 'lib/sequel/plugins/schema.rb', line 49 def drop_table? db.drop_table?(table_name) end |
#schema ⇒ Object
Returns table schema created with set_schema for direct descendant of Model. Does not retreive schema information from the database, see db_schema if you want that.
56 57 58 |
# File 'lib/sequel/plugins/schema.rb', line 56 def schema @schema || (superclass.schema unless superclass == Model) end |
#set_schema(name = nil, &block) ⇒ Object
Defines a table schema (see Schema::Generator for more information).
This is only needed if you want to use the create_table/create_table! methods. Will also set the dataset if you provide a name, as well as setting the primary key if you defined one in the passed block.
In general, it is a better idea to use migrations for production code, as migrations allow changes to existing schema. set_schema is mostly useful for test code or simple examples.
69 70 71 72 73 |
# File 'lib/sequel/plugins/schema.rb', line 69 def set_schema(name = nil, &block) set_dataset(db[name]) if name @schema = db.create_table_generator(&block) set_primary_key(@schema.primary_key_name) if @schema.primary_key_name end |
#table_exists? ⇒ Boolean
Returns true if table exists, false otherwise.
76 77 78 |
# File 'lib/sequel/plugins/schema.rb', line 76 def table_exists? db.table_exists?(table_name) end |