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 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

#bigint(name, null: true, default: nil) ⇒ nil

A BIGINT column, an Integer in the model: an :integer column with limit 8.

Parameters:

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

    false adds NOT NULL

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

    SQL literal text: "0"

Returns:

  • (nil)


308
309
310
# File 'cybertrain/schema/table.rb', line 308

def bigint(name, null: true, default: nil)
  push_column(name, :integer, null, default, 8)
end

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

A BLOB column, a String of raw bytes in the model (NUL and bytes above 127 included; the model binds it as a DB::Blob).

Parameters:

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

    false adds NOT NULL

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

    SQL literal text: "x''"

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

    ignored

Returns:

  • (nil)


332
333
334
# File 'cybertrain/schema/table.rb', line 332

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

#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)


254
255
256
# File 'cybertrain/schema/table.rb', line 254

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)


320
321
322
# File 'cybertrain/schema/table.rb', line 320

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, precision: 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

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

    accepted for Rails’ precision: 6 and ignored (Rails’ datetime(6) reads back as a plain datetime)

Returns:

  • (nil)


269
270
271
# File 'cybertrain/schema/table.rb', line 269

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

#decimal(name, precision: 0, scale: -1,, null: true, default: nil) ⇒ nil

A DECIMAL(precision,scale) column (DECIMAL(precision) without a scale, plain DECIMAL without either), as Rails declares one.

Examples:

t.decimal "price", precision: 18, scale: 8

Parameters:

  • name (String, Symbol)
  • precision (Integer) (defaults to: 0) —

    total digits; 0 for none

  • scale (Integer) (defaults to: -1,) —

    digits after the point; -1 for none (a scale needs a precision: ArgumentError otherwise)

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

    false adds NOT NULL

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

    SQL literal text: "0"

Returns:

  • (nil)


285
286
287
288
# File 'cybertrain/schema/table.rb', line 285

def decimal(name, precision: 0, scale: -1, null: true, default: nil)
  Cybertrain::Schema::Column.check_decimal(precision, scale)
  @table.add_column(Column.new(name, :decimal, null, default, 0, precision, scale))
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)


242
243
244
# File 'cybertrain/schema/table.rb', line 242

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)


230
231
232
# File 'cybertrain/schema/table.rb', line 230

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

#json(name, null: true, default: nil) ⇒ nil

A JSON column, as Rails declares one on SQLite (the text of the document).

Parameters:

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

    false adds NOT NULL

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

    SQL literal text: "'{}'"

Returns:

  • (nil)


297
298
299
# File 'cybertrain/schema/table.rb', line 297

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

#references(name, null: false, foreign_key: true, on_delete: :no_action, index: 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)
  • on_delete (Symbol) (defaults to: :no_action) —

    what deleting the referenced row does: :no_action (refused while referenced), :cascade (deletes this row too), :nullify (sets the column NULL; pass null: true) or :restrict

  • index (Boolean) (defaults to: true) —

    false skips the index

Returns:

  • (nil)


354
355
356
357
358
359
360
361
362
363
364
365
# File 'cybertrain/schema/table.rb', line 354

def references(name, null: false, foreign_key: true, on_delete: :no_action, index: true)
  Cybertrain::Schema::ForeignKey.check_on_delete(on_delete)
  Cybertrain::Schema::ForeignKey.check_nullify(on_delete, null)
  column_name = "#{name}_id"
  push_column(column_name, :integer, null, nil, 0)
  @table.add_index(Index.new(@table.name, [column_name], false)) if index
  if foreign_key
    to_table = Cybertrain::Inflector.pluralize(name.to_s)
    @table.add_foreign_key(ForeignKey.new(@table.name, column_name, to_table, on_delete))
  end
  nil
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)


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

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)


218
219
220
# File 'cybertrain/schema/table.rb', line 218

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)


371
372
373
374
# File 'cybertrain/schema/table.rb', line 371

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