Class: Cybertrain::Schema::TableDef
- Inherits:
-
Object
- Object
- Cybertrain::Schema::TableDef
- 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.
Instance Method Summary collapse
-
#bigint(name, null: true, default: nil) ⇒ nil
A
BIGINTcolumn, an Integer in the model: an:integercolumn withlimit8. -
#binary(name, null: true, default: nil, limit: 0) ⇒ nil
A
BLOBcolumn, a String of raw bytes in the model (NUL and bytes above 127 included; the model binds it as a DB::Blob). -
#boolean(name, null: true, default: nil, limit: 0) ⇒ nil
A
BOOLEANcolumn (0/1), true/false in the model. -
#date(name, null: true, default: nil, limit: 0) ⇒ nil
A
DATEcolumn, a String ("2026-10-05") in the model. -
#datetime(name, null: true, default: nil, limit: 0, precision: 0) ⇒ nil
A
DATETIMEcolumn, a UTC Time in the model (stored as"2026-10-05T09:00:00Z", to the second). -
#decimal(name, precision: 0, scale: -1,, null: true, default: nil) ⇒ nil
A
DECIMAL(precision,scale)column (DECIMAL(precision)without a scale, plainDECIMALwithout either), as Rails declares one. -
#float(name, null: true, default: nil, limit: 0) ⇒ nil
A
REALcolumn, a Float in the model. -
#integer(name, null: true, default: nil, limit: 0) ⇒ nil
An
INTEGERcolumn, an Integer in the model. -
#json(name, null: true, default: nil) ⇒ nil
A
JSONcolumn, as Rails declares one on SQLite (the text of the document). -
#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). -
#string(name, null: true, default: nil, limit: 0) ⇒ nil
A
VARCHARcolumn, a String in the model. -
#text(name, null: true, default: nil, limit: 0) ⇒ nil
A
TEXTcolumn, a String in the model. -
#timestamps ⇒ nil
created_atandupdated_at, NOT NULL datetimes that Model#save fills in.
Instance Method Details
#bigint(name, null: true, default: nil) ⇒ nil
A BIGINT column, an Integer in the model: an :integer column
with limit 8.
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).
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.
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.
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).
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.
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.
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.
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).
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 “
The foreign key is what gives the two models their association
readers (comment.article, article.comments).
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.
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.
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.
371 372 373 374 |
# File 'cybertrain/schema/table.rb', line 371 def push_column("created_at", :datetime, false, nil, 0) push_column("updated_at", :datetime, false, nil, 0) end |