From ef75901e4fa7f537890c9fff5a70781f59315929 Mon Sep 17 00:00:00 2001 From: Chris Patuzzo Date: Thu, 1 Feb 2024 16:12:36 +0000 Subject: [PATCH 1/2] Create a schools table with an organisation_id index --- db/migrate/20240201160923_create_schools.rb | 11 +++++++++++ db/schema.rb | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20240201160923_create_schools.rb diff --git a/db/migrate/20240201160923_create_schools.rb b/db/migrate/20240201160923_create_schools.rb new file mode 100644 index 000000000..337348668 --- /dev/null +++ b/db/migrate/20240201160923_create_schools.rb @@ -0,0 +1,11 @@ +class CreateSchools < ActiveRecord::Migration[7.0] + def change + create_table :schools, id: :uuid do |t| + t.uuid :organisation_id, null: false + t.string :name, null: false + t.timestamps + end + + add_index :schools, :organisation_id, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 98c4280ab..fe193049a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_11_06_151705) do +ActiveRecord::Schema[7.0].define(version: 2024_02_01_160923) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "pgcrypto" @@ -139,6 +139,14 @@ t.index ["remixed_from_id"], name: "index_projects_on_remixed_from_id" end + create_table "schools", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.uuid "organisation_id", null: false + t.string "name", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["organisation_id"], name: "index_schools_on_organisation_id", unique: true + end + create_table "words", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "word" t.index ["word"], name: "index_words_on_word" From 35616fb07949c0f575dd2a453657b395c1a60c21 Mon Sep 17 00:00:00 2001 From: Chris Patuzzo Date: Thu, 1 Feb 2024 16:23:07 +0000 Subject: [PATCH 2/2] Add a School model --- Gemfile.lock | 3 +++ app/models/school.rb | 6 ++++++ spec/factories/school.rb | 8 ++++++++ spec/models/school_spec.rb | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 app/models/school.rb create mode 100644 spec/factories/school.rb create mode 100644 spec/models/school_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index c6c5ec1ea..c5b30421b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -192,6 +192,8 @@ GEM nio4r (2.5.8) nokogiri (1.14.2-aarch64-linux) racc (~> 1.4) + nokogiri (1.14.2-arm64-darwin) + racc (~> 1.4) nokogiri (1.14.2-x86_64-linux) racc (~> 1.4) open-uri (0.3.0) @@ -340,6 +342,7 @@ GEM PLATFORMS aarch64-linux + arm64-darwin-22 x86_64-linux DEPENDENCIES diff --git a/app/models/school.rb b/app/models/school.rb new file mode 100644 index 000000000..f3817e23e --- /dev/null +++ b/app/models/school.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class School < ApplicationRecord + validates :organisation_id, presence: true + validates :name, presence: true +end diff --git a/spec/factories/school.rb b/spec/factories/school.rb new file mode 100644 index 000000000..ac793140c --- /dev/null +++ b/spec/factories/school.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :school do + organisation_id { SecureRandom.uuid } + sequence(:name) { |n| "School #{n}" } + end +end diff --git a/spec/models/school_spec.rb b/spec/models/school_spec.rb new file mode 100644 index 000000000..4d1fdb90c --- /dev/null +++ b/spec/models/school_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe School do + describe 'validations' do + subject(:school) { build(:school) } + + it 'has a valid default factory' do + expect(school).to be_valid + end + + it 'can save the default factory' do + expect { school.save! }.not_to raise_error + end + + it 'is invalid if no organisation_id' do + school.organisation_id = ' ' + expect(school).to be_invalid + end + + it 'is invalid if organisation_id is not a UUID' do + school.organisation_id = 'invalid' + expect(school).to be_invalid + end + + it 'is invalid if no name' do + school.name = ' ' + expect(school).to be_invalid + end + end +end