Module | Sequel::Plugins::Schema::ClassMethods |
In: |
lib/sequel/plugins/schema.rb
|
Creates table, using the column information from set_schema.
# File lib/sequel/plugins/schema.rb, line 24 24: def create_table(*args, &block) 25: set_schema(*args, &block) if block 26: db.create_table(table_name, :generator=>@schema) 27: @db_schema = get_db_schema(true) 28: columns 29: end
Drops the table if it exists and then runs create_table. Should probably not be used except in testing.
# File lib/sequel/plugins/schema.rb, line 33 33: def create_table!(*args, &block) 34: drop_table? 35: create_table(*args, &block) 36: end
Creates the table unless the table already exists
# File lib/sequel/plugins/schema.rb, line 39 39: def create_table?(*args, &block) 40: create_table(*args, &block) unless table_exists? 41: end
Drops table. If the table doesn‘t exist, this will probably raise an error.
# File lib/sequel/plugins/schema.rb, line 44 44: def drop_table 45: db.drop_table(table_name) 46: end
Drops table if it already exists, do nothing if it doesn‘t exist.
# File lib/sequel/plugins/schema.rb, line 49 49: def drop_table? 50: db.drop_table?(table_name) 51: end
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.
# File lib/sequel/plugins/schema.rb, line 56 56: def schema 57: @schema || (superclass.schema unless superclass == Model) 58: end
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.
# File lib/sequel/plugins/schema.rb, line 69 69: def set_schema(name = nil, &block) 70: set_dataset(db[name]) if name 71: @schema = db.create_table_generator(&block) 72: set_primary_key(@schema.primary_key_name) if @schema.primary_key_name 73: end