From 724462edc63d8ca71894902c774ee38b0cbc0648 Mon Sep 17 00:00:00 2001 From: Chris Salzberg Date: Wed, 11 Apr 2018 09:19:19 +0900 Subject: [PATCH] Enforce null: false on columns consistently --- .../templates/create_string_translations.rb | 10 +-- .../templates/create_text_translations.rb | 10 +-- spec/active_record/schema.rb | 28 +++--- .../mobility/translations_generator_spec.rb | 4 +- .../backends/active_record/container_spec.rb | 6 +- spec/sequel/schema.rb | 88 +++++++++---------- 6 files changed, 73 insertions(+), 73 deletions(-) diff --git a/lib/rails/generators/mobility/templates/create_string_translations.rb b/lib/rails/generators/mobility/templates/create_string_translations.rb index 75533adf7..1cf1fc68a 100644 --- a/lib/rails/generators/mobility/templates/create_string_translations.rb +++ b/lib/rails/generators/mobility/templates/create_string_translations.rb @@ -2,12 +2,12 @@ class CreateStringTranslations < <%= activerecord_migration_class %> def change create_table :mobility_string_translations do |t| - t.string :locale - t.string :key + t.string :locale, null: false + t.string :key, null: false t.string :value - t.integer :translatable_id - t.string :translatable_type - t.timestamps + t.integer :translatable_id, null: false + t.string :translatable_type, null: false + t.timestamps null: false end add_index :mobility_string_translations, [:translatable_id, :translatable_type, :locale, :key], unique: true, name: :index_mobility_string_translations_on_keys add_index :mobility_string_translations, [:translatable_id, :translatable_type, :key], name: :index_mobility_string_translations_on_translatable_attribute diff --git a/lib/rails/generators/mobility/templates/create_text_translations.rb b/lib/rails/generators/mobility/templates/create_text_translations.rb index fc2be53d8..e7c914ec1 100644 --- a/lib/rails/generators/mobility/templates/create_text_translations.rb +++ b/lib/rails/generators/mobility/templates/create_text_translations.rb @@ -2,12 +2,12 @@ class CreateTextTranslations < <%= activerecord_migration_class %> def change create_table :mobility_text_translations do |t| - t.string :locale - t.string :key + t.string :locale, null: false + t.string :key, null: false t.text :value - t.integer :translatable_id - t.string :translatable_type - t.timestamps + t.integer :translatable_id, null: false + t.string :translatable_type, null: false + t.timestamps null: false end add_index :mobility_text_translations, [:translatable_id, :translatable_type, :locale, :key], unique: true, name: :index_mobility_text_translations_on_keys add_index :mobility_text_translations, [:translatable_id, :translatable_type, :key], name: :index_mobility_text_translations_on_translatable_attribute diff --git a/spec/active_record/schema.rb b/spec/active_record/schema.rb index 168beefcb..571411713 100644 --- a/spec/active_record/schema.rb +++ b/spec/active_record/schema.rb @@ -10,13 +10,13 @@ class << self def up create_table "posts" do |t| t.boolean :published - t.timestamps + t.timestamps null: false end create_table "articles" do |t| t.string :slug t.boolean :published - t.timestamps + t.timestamps null: false end create_table "article_translations" do |t| @@ -24,27 +24,27 @@ def up t.integer :article_id t.string :title t.text :content - t.timestamps + t.timestamps null: false end create_table "multitable_posts" do |t| t.string :slug t.boolean :published - t.timestamps + t.timestamps null: false end create_table "multitable_post_translations" do |t| t.string :locale t.integer :multitable_post_id t.string :title - t.timestamps + t.timestamps null: false end create_table "multitable_post_foo_translations" do |t| t.string :locale t.integer :multitable_post_id t.string :foo - t.timestamps + t.timestamps null: false end create_table "mobility_string_translations" do |t| @@ -53,7 +53,7 @@ def up t.string :value, null: false t.integer :translatable_id, null: false t.string :translatable_type, null: false - t.timestamps + t.timestamps null: false end add_index :mobility_string_translations, [:translatable_id, :translatable_type, :locale, :key], unique: true, name: :index_mobility_string_translations_on_keys add_index :mobility_string_translations, [:translatable_id, :translatable_type, :key], name: :index_mobility_string_translations_on_translatable_attribute @@ -65,7 +65,7 @@ def up t.text :value, null: false t.integer :translatable_id, null: false t.string :translatable_type, null: false - t.timestamps + t.timestamps null: false end add_index :mobility_text_translations, [:translatable_id, :translatable_type, :locale, :key], unique: true, name: :index_mobility_text_translations_on_keys add_index :mobility_text_translations, [:translatable_id, :translatable_type, :key], name: :index_mobility_text_translations_on_translatable_attribute @@ -80,14 +80,14 @@ def up t.text :author_pt_br t.text :author_ru t.boolean :published - t.timestamps + t.timestamps null: false end create_table "serialized_posts" do |t| t.text :my_title_i18n t.text :my_content_i18n t.boolean :published - t.timestamps + t.timestamps null: false end if ENV['DB'] == 'postgres' @@ -95,20 +95,20 @@ def up t.jsonb :my_title_i18n, default: {} t.jsonb :my_content_i18n, default: {} t.boolean :published - t.timestamps + t.timestamps null: false end create_table "json_posts" do |t| t.json :my_title_i18n, default: {} t.json :my_content_i18n, default: {} t.boolean :published - t.timestamps + t.timestamps null: false end create_table "container_posts" do |t| t.jsonb :translations, default: {} t.boolean :published - t.timestamps + t.timestamps null: false end execute "CREATE EXTENSION IF NOT EXISTS hstore" @@ -117,7 +117,7 @@ def up t.hstore :my_title_i18n, default: '' t.hstore :my_content_i18n, default: '' t.boolean :published - t.timestamps + t.timestamps null: false end end end diff --git a/spec/generators/rails/mobility/translations_generator_spec.rb b/spec/generators/rails/mobility/translations_generator_spec.rb index 516c88210..1c9d2c406 100644 --- a/spec/generators/rails/mobility/translations_generator_spec.rb +++ b/spec/generators/rails/mobility/translations_generator_spec.rb @@ -88,8 +88,8 @@ before do connection.create_table :post_translations do |t| t.string :locale - t.integer :post_id - t.timestamps + t.integer :post_id, null: false + t.timestamps null: false end end diff --git a/spec/mobility/backends/active_record/container_spec.rb b/spec/mobility/backends/active_record/container_spec.rb index 84fd70346..2906ae355 100644 --- a/spec/mobility/backends/active_record/container_spec.rb +++ b/spec/mobility/backends/active_record/container_spec.rb @@ -68,9 +68,9 @@ m = ActiveRecord::Migration.new m.verbose = false m.create_table :json_container_posts do |t| - t.json :json_translations, default: {} + t.json :json_translations, default: {}, null: false t.boolean :published - t.timestamps + t.timestamps null: false end end before(:each) do @@ -94,7 +94,7 @@ m.create_table :string_column_posts do |t| t.string :foo t.boolean :published - t.timestamps + t.timestamps null: false end end after(:all) do diff --git a/spec/sequel/schema.rb b/spec/sequel/schema.rb index 7eefec79e..ff29c26b7 100644 --- a/spec/sequel/schema.rb +++ b/spec/sequel/schema.rb @@ -16,7 +16,7 @@ def migrate(*) DB.create_table? :post_metadatas do primary_key :id String :metadata - Integer :post_id + Integer :post_id, allow_null: false DateTime :created_at, allow_null: false DateTime :updated_at, allow_null: false end @@ -31,8 +31,8 @@ def migrate(*) DB.create_table? :article_translations do primary_key :id - Integer :article_id - String :locale + Integer :article_id, allow_null: false + String :locale, allow_null: false String :title String :content, size: 65535 DateTime :created_at, allow_null: false @@ -48,45 +48,45 @@ def migrate(*) DB.create_table? :multitable_post_translations do primary_key :id - Integer :multitable_post_id - String :locale + Integer :multitable_post_id, allow_null: false + String :locale, allow_null: false String :title - DateTime :created_at, allow_null: false - DateTime :updated_at, allow_null: false + DateTime :created_at, allow_null: false + DateTime :updated_at, allow_null: false end DB.create_table? :multitable_post_foo_translations do primary_key :id - Integer :multitable_post_id - String :locale + Integer :multitable_post_id, allow_null: false + String :locale, allow_null: false String :foo - DateTime :created_at, allow_null: false - DateTime :updated_at, allow_null: false + DateTime :created_at, allow_null: false + DateTime :updated_at, allow_null: false end DB.create_table? :mobility_text_translations do primary_key :id - String :locale, null: false - String :key, null: false - String :value, null: false, size: 65535 - Integer :translatable_id, null: false - String :translatable_type, null: false - DateTime :created_at, allow_null: false - DateTime :updated_at, allow_null: false + String :locale, allow_null: false + String :key + String :value + Integer :translatable_id, allow_null: false + String :translatable_type, allow_null: false + DateTime :created_at, allow_null: false + DateTime :updated_at, allow_null: false index [:translatable_id, :translatable_type, :locale, :key], unique: true, name: :index_mobility_text_translations_on_keys index [:translatable_id, :translatable_type, :key], name: :index_mobility_text_translations_on_translatable_attribute end DB.create_table? :mobility_string_translations do primary_key :id - String :locale, null: false - String :key, null: false - String :value, null: false - Integer :translatable_id, null: false - String :translatable_type, null: false - DateTime :created_at, allow_null: false - DateTime :updated_at, allow_null: false + String :locale, allow_null: false + String :key, allow_null: false + String :value + Integer :translatable_id, allow_null: false + String :translatable_type, allow_null: false + DateTime :created_at, allow_null: false + DateTime :updated_at, allow_null: false index [:translatable_id, :translatable_type, :locale, :key], unique: true, name: :index_mobility_string_translations_on_keys index [:translatable_id, :translatable_type, :key], name: :index_mobility_string_translations_on_translatable_attribute index [:translatable_type, :key, :value, :locale], name: :index_mobility_string_translations_on_query_keys @@ -109,48 +109,48 @@ def migrate(*) DB.create_table? :serialized_posts do primary_key :id - String :my_title_i18n, size: 65535 - String :my_content_i18n, size: 65535 + String :my_title_i18n, size: 65535 + String :my_content_i18n, size: 65535 TrueClass :published - DateTime :created_at, allow_null: false - DateTime :updated_at, allow_null: false + DateTime :created_at, allow_null: false + DateTime :updated_at, allow_null: false end if ENV['DB'] == 'postgres' DB.create_table? :jsonb_posts do primary_key :id - jsonb :my_title_i18n, default: '{}' - jsonb :my_content_i18n, default: '{}' + jsonb :my_title_i18n, default: '{}', allow_null: false + jsonb :my_content_i18n, default: '{}', allow_null: false TrueClass :published - DateTime :created_at, allow_null: false - DateTime :updated_at, allow_null: false + DateTime :created_at, allow_null: false + DateTime :updated_at, allow_null: false end DB.create_table? :json_posts do primary_key :id - json :my_title_i18n, default: '{}' - json :my_content_i18n, default: '{}' + json :my_title_i18n, default: '{}', allow_null: false + json :my_content_i18n, default: '{}', allow_null: false TrueClass :published - DateTime :created_at, allow_null: false - DateTime :updated_at, allow_null: false + DateTime :created_at, allow_null: false + DateTime :updated_at, allow_null: false end DB.create_table? :container_posts do primary_key :id - jsonb :translations, default: '{}' + jsonb :translations, default: '{}', allow_null: false TrueClass :published - DateTime :created_at, allow_null: false - DateTime :updated_at, allow_null: false + DateTime :created_at, allow_null: false + DateTime :updated_at, allow_null: false end DB.run "CREATE EXTENSION IF NOT EXISTS hstore" DB.create_table? :hstore_posts do primary_key :id - hstore :my_title_i18n, default: '' - hstore :my_content_i18n, default: '' + hstore :my_title_i18n, default: '', allow_null: false + hstore :my_content_i18n, default: '', allow_null: false TrueClass :published - DateTime :created_at, allow_null: false - DateTime :updated_at, allow_null: false + DateTime :created_at, allow_null: false + DateTime :updated_at, allow_null: false end end end