Auto-create check constraints for text fields with limit

Move column defaults from migration strategy to migrations.
Compact index creation inside migrations.
Disable database schema generation.
This commit is contained in:
2026-11-26 02:32:09 +01:00
parent 74f9a09098
commit b6d2c19b0b
9 changed files with 148 additions and 210 deletions
+1 -2
View File
@@ -1,11 +1,10 @@
class CreateUsers < ActiveRecord::Migration[8.1]
def change
create_table :users do |t|
t.string :email, null: false, limit: 64
t.string :email, null: false, limit: 2**6, index: {unique: true}
t.integer :status, null: false, default: 0
t.timestamps
end
add_index :users, :email
end
end
@@ -1,24 +1,24 @@
class AddDeviseToUsers < ActiveRecord::Migration[8.1]
def change
change_table :users do |t|
## NOTE: commented fields left for reference/inclusion in future migrations
## Commented fields left for reference/inclusion in future migrations.
## Database authenticatable
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.string :reset_password_token, index: {unique: true}
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Confirmable
t.string :confirmation_token
t.string :confirmation_token, index: {unique: true}
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
# Only if using reconfirmable
t.string :unconfirmed_email, limit: 64
# Only if using reconfirmable.
t.string :unconfirmed_email, limit: 2**6
## Trackable
# t.integer :sign_in_count, default: 0, null: false
@@ -28,14 +28,11 @@ class AddDeviseToUsers < ActiveRecord::Migration[8.1]
# t.string :last_sign_in_ip
## Lockable
# t.string :unlock_token # Only if unlock strategy is :email or :both
# Only if unlock strategy is :email or :both.
# t.string :unlock_token, index: {unique: true}
# t.datetime :locked_at
# Only if lock strategy is :failed_attempts
# Only if lock strategy is :failed_attempts.
# t.integer :failed_attempts, default: 0, null: false
end
add_index :users, :reset_password_token
add_index :users, :confirmation_token
# add_index :users, :unlock_token, unique: true
end
end
+8 -5
View File
@@ -2,14 +2,17 @@ class CreateUnits < ActiveRecord::Migration[8.1]
def change
create_table :units do |t|
t.references :user, foreign_key: {on_delete: :cascade}
t.string :symbol, null: false, limit: 15
t.text :description
t.string :symbol, null: false, limit: 2**4
t.text :description, limit: 2**16
t.float :multiplier, null: false, limit: Float::MANT_DIG, default: 1.0
t.references :base, foreign_key: {to_table: :units, on_delete: :cascade}
t.timestamps
# Constraints:
# * symbol has to be unique
t.index [:user_id, :symbol], unique: true
# * unit has to belong to the same user as quantity (used by :readouts)
t.index [:id, :user_id], unique: true
end
add_index :units, [:user_id, :symbol]
add_index :units, [:id, :user_id]
end
end
+11 -8
View File
@@ -2,17 +2,20 @@ class CreateQuantities < ActiveRecord::Migration[8.1]
def change
create_table :quantities do |t|
t.references :user, foreign_key: {on_delete: :cascade}
t.string :name, null: false, limit: 31
t.text :description
t.string :name, null: false, limit: 2**5
t.text :description, limit: 2**16
t.references :parent, foreign_key: {to_table: :quantities, on_delete: :cascade}
t.timestamps
# Caches; can be computed from other attributes
t.integer :depth, null: false, default: 0
# Cached, computed from other attributes. NULL default to ensure value
# has been properly initialized.
t.integer :depth, null: false
t.string :pathname, null: false, limit: 511
# Constraints:
# * name has to be unique
t.index [:user_id, :parent_id, :name], unique: true
# * quantity has to belong to the same user as unit (used by :readouts)
t.index [:id, :user_id], unique: true
end
add_index :quantities, [:user_id, :parent_id, :name]
add_index :quantities, [:id, :user_id]
end
end
+10 -11
View File
@@ -1,20 +1,17 @@
class CreateReadouts < ActiveRecord::Migration[8.1]
def change
create_table :notes do |t|
t.text :text, null: false
t.text :text, null: false, limit: 2**16
t.timestamps
end
create_table :measurements do |t|
t.datetime :taken_at, null: false
t.datetime :taken_at, null: false, index: {unique: true}
#t.references :collector, foreign_key: true
#t.references :device, foreign_key: true
t.references :note, foreign_key: {on_delete: :nullify}
t.timestamps
end
add_index :measurements, :taken_at
# Defining Readouts as a super-/subclass polymorphic relations for different
# subclass data types (numeric, string, file) is not possible with proper
@@ -40,14 +37,16 @@ class CreateReadouts < ActiveRecord::Migration[8.1]
# t.references :file
# Possibly mutually exclusive with :unit or check constraint for:
# :unit is not null <=> :value is not null
t.timestamps
# Constraints:
# * quantity + category has to be unique within measurement
t.index [:measurement_id, :quantity_id, :category], unique: true
# * quantity and unit has to belong to the same user as readout
t.foreign_key :quantities, column: [:quantity_id, :user_id],
primary_key: [:id, :user_id]
t.foreign_key :units, column: [:unit_id, :user_id], primary_key: [:id, :user_id]
end
add_index :readouts, [:measurement_id, :quantity_id, :category]
add_foreign_key :readouts, :quantities, column: [:quantity_id, :user_id],
primary_key: [:id, :user_id]
add_foreign_key :readouts, :units, column: [:unit_id, :user_id],
primary_key: [:id, :user_id]
# TODO: remove below tables after current setup verified
#create_table :numeric_values do |t|
+3 -3
View File
@@ -11,11 +11,11 @@ class CreateTags < ActiveRecord::Migration[8.1]
t.integer :depth, null: false
# Constraints:
# * label has to be unique
# * label has to be unique
t.index [:user_id, :label], unique: true
# * supertag cannot reference self
# * supertag cannot reference self
t.check_constraint 'id != supertag_id', name: :self_reference
# * supertag has to belong to the same user as tag
# * supertag has to belong to the same user as tag
t.index [:id, :user_id], unique: true
t.foreign_key :tags, primary_key: [:id, :user_id],
column: [:supertag_id, :user_id], name: :user_mismatch