Sha256: 847775553a79e95c0c14a6c482df233d9fce55542b864d4ecc83f8444fe5fea0

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

module Sequel
  class Model
    # Creates table, using the column information from set_schema.
    def self.create_table
      db.create_table(table_name, :generator=>@schema)
      @db_schema = get_db_schema(true)
      columns
    end
    
    # Drops the table if it exists and then runs create_table.  Should probably
    # not be used except in testing.
    def self.create_table!
      drop_table rescue nil
      create_table
    end
    
    # Drops table.
    def self.drop_table
      db.drop_table(table_name)
    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.
    def self.schema
      @schema || (superclass.schema unless superclass == Model)
    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.
    def self.set_schema(name = nil, &block)
      set_dataset(db[name]) if name
      @schema = Schema::Generator.new(db, &block)
      set_primary_key(@schema.primary_key_name) if @schema.primary_key_name
    end
    
    # Returns true if table exists, false otherwise.
    def self.table_exists?
      db.table_exists?(table_name)
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
colincasey-sequel-2.10.1 lib/sequel_model/schema.rb
colincasey-sequel-2.10.2 lib/sequel_model/schema.rb
colincasey-sequel-2.10.4 lib/sequel_model/schema.rb
sequel-2.10.0 lib/sequel_model/schema.rb
sequel-2.11.0 lib/sequel_model/schema.rb