Class: Cybertrain::Schema::TableDef

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/schema/table.rb

Overview

The t a create_table block yields.

Every table also gets id INTEGER PRIMARY KEY AUTOINCREMENT; do not declare it. The types are those below; there is no decimal, bigint, json, binary or time. default: is SQL literal text, and a new record starts with it when it is a plain literal.

Examples:

create_table "articles" do |t|
  t.string "title", null: false
  t.text "body"
  t.boolean "published", null: false, default: "false"
  t.references :author
  t.timestamps
end

Instance Method Summary collapse

Instance Method Details

#boolean(name, null: true, default: nil, limit: 0) ⇒ nil

A BOOLEAN column (0/1), true/false in the model.

Parameters:

  • name (String, Symbol)
  • null (Boolean) (defaults to: true) —

    false adds NOT NULL

  • default (String, nil) (defaults to: nil) —

    SQL literal text: "0", "'draft'", "false"

  • limit (Integer) (defaults to: 0) —

    ignored

Returns:

  • (nil)


181
182
183
# File 'cybertrain/schema/table.rb', line 181

def boolean(name, null: true, default: nil, limit: 0)
  push_column(name, :boolean, null, default, limit)
end

#date(name, null: true, default: nil, limit: 0) ⇒ nil

A DATE column, a String ("2026-10-05") in the model.

Parameters:

  • name (String, Symbol)
  • null (Boolean) (defaults to: true) —

    false adds NOT NULL

  • default (String, nil) (defaults to: nil) —

    SQL literal text: "0", "'draft'", "false"

  • limit (Integer) (defaults to: 0) —

    ignored

Returns:

  • (nil)


206
207
208
# File 'cybertrain/schema/table.rb', line 206

def date(name, null: true, default: nil, limit: 0)
  push_column(name, :date, null, default, limit)
end

#datetime(name, null: true, default: nil, limit: 0) ⇒ nil

A DATETIME column, a UTC Time in the model (stored as "2026-10-05T09:00:00Z", to the second).

Parameters:

  • name (String, Symbol)
  • null (Boolean) (defaults to: true) —

    false adds NOT NULL

  • default (String, nil) (defaults to: nil) —

    SQL literal text: "0", "'draft'", "false"

  • limit (Integer) (defaults to: 0) —

    ignored

Returns:

  • (nil)


194
195
196
# File 'cybertrain/schema/table.rb', line 194

def datetime(name, null: true, default: nil, limit: 0)
  push_column(name, :datetime, null, default, limit)
end

#float(name, null: true, default: nil, limit: 0) ⇒ nil

A REAL column, a Float in the model.

Parameters:

  • name (String, Symbol)
  • null (Boolean) (defaults to: true) —

    false adds NOT NULL

  • default (String, nil) (defaults to: nil) —

    SQL literal text: "0", "'draft'", "false"

  • limit (Integer) (defaults to: 0) —

    ignored

Returns:

  • (nil)


169
170
171
# File 'cybertrain/schema/table.rb', line 169

def float(name, null: true, default: nil, limit: 0)
  push_column(name, :float, null, default, limit)
end

#integer(name, null: true, default: nil, limit: 0) ⇒ nil

An INTEGER column, an Integer in the model.

Parameters:

  • name (String, Symbol)
  • null (Boolean) (defaults to: true) —

    false adds NOT NULL

  • default (String, nil) (defaults to: nil) —

    SQL literal text: "0", "'draft'", "false"

  • limit (Integer) (defaults to: 0) —

    ignored

Returns:

  • (nil)


157
158
159
# File 'cybertrain/schema/table.rb', line 157

def integer(name, null: true, default: nil, limit: 0)
  push_column(name, :integer, null, default, limit)
end

#references(name, null: false, foreign_key: true) ⇒ nil

Adds “_id" (Integer), an index on it, and -- unless `foreign_key: false` -- a foreign key to the pluralized table name ("post" -> "posts", "person" -> "people" via Cybertrain::Inflector).

The foreign key is what gives the two models their association readers (comment.article, article.comments).

Examples:

t.references :article   # article_id, NOT NULL, indexed, -> articles

Parameters:

  • name (Symbol, String) —

    the singular (:article)

  • null (Boolean) (defaults to: false) —

    the column is NOT NULL unless true

  • foreign_key (Boolean) (defaults to: true)

Returns:

  • (nil)


223
224
225
226
227
228
229
230
231
# File 'cybertrain/schema/table.rb', line 223

def references(name, null: false, foreign_key: true)
  column_name = "#{name}_id"
  push_column(column_name, :integer, null, nil, 0)
  @table.add_index(Index.new(@table.name, [column_name], false))
  if foreign_key
    to_table = Cybertrain::Inflector.pluralize(name.to_s)
    @table.add_foreign_key(ForeignKey.new(@table.name, column_name, to_table))
  end
end

#string(name, null: true, default: nil, limit: 0) ⇒ nil

A VARCHAR column, a String in the model.

Parameters:

  • name (String, Symbol)
  • null (Boolean) (defaults to: true) —

    false adds NOT NULL

  • default (String, nil) (defaults to: nil) —

    SQL literal text: "'draft'"

  • limit (Integer) (defaults to: 0) —

    VARCHAR(n) when positive

Returns:

  • (nil)


133
134
135
# File 'cybertrain/schema/table.rb', line 133

def string(name, null: true, default: nil, limit: 0)
  push_column(name, :string, null, default, limit)
end

#text(name, null: true, default: nil, limit: 0) ⇒ nil

A TEXT column, a String in the model.

Parameters:

  • name (String, Symbol)
  • null (Boolean) (defaults to: true) —

    false adds NOT NULL

  • default (String, nil) (defaults to: nil) —

    SQL literal text: "0", "'draft'", "false"

  • limit (Integer) (defaults to: 0) —

    ignored

Returns:

  • (nil)


145
146
147
# File 'cybertrain/schema/table.rb', line 145

def text(name, null: true, default: nil, limit: 0)
  push_column(name, :text, null, default, limit)
end

#timestamps ⇒ nil

created_at and updated_at, NOT NULL datetimes that Model#save fills in.

Returns:

  • (nil)


237
238
239
240
# File 'cybertrain/schema/table.rb', line 237

def timestamps
  push_column("created_at", :datetime, false, nil, 0)
  push_column("updated_at", :datetime, false, nil, 0)
end