schemas.exs (10132B)
1 Code.require_file("types.exs", __DIR__) 2 3 defmodule Ecto.Integration.Schema do 4 defmacro __using__(_) do 5 quote do 6 use Ecto.Schema 7 8 type = 9 Application.compile_env(:ecto, :primary_key_type) || 10 raise ":primary_key_type not set in :ecto application" 11 12 @primary_key {:id, type, autogenerate: true} 13 @foreign_key_type type 14 end 15 end 16 end 17 18 defmodule Ecto.Integration.Post do 19 @moduledoc """ 20 This module is used to test: 21 22 * Overall functionality 23 * Overall types 24 * Non-null timestamps 25 * Relationships 26 * Dependent callbacks 27 28 """ 29 use Ecto.Integration.Schema 30 import Ecto.Changeset 31 32 schema "posts" do 33 field :counter, :id # Same as integer 34 field :title, :string 35 field :blob, :binary 36 field :temp, :string, default: "temp", virtual: true 37 field :public, :boolean, default: true 38 field :cost, :decimal 39 field :visits, :integer 40 field :wrapped_visits, WrappedInteger 41 field :intensity, :float 42 field :bid, :binary_id 43 field :uuid, Ecto.Integration.TestRepo.uuid(), autogenerate: true 44 field :meta, :map 45 field :links, {:map, :string} 46 field :intensities, {:map, :float} 47 field :posted, :date 48 has_many :comments, Ecto.Integration.Comment, on_delete: :delete_all, on_replace: :delete 49 has_many :force_comments, Ecto.Integration.Comment, on_replace: :delete_if_exists 50 has_many :ordered_comments, Ecto.Integration.Comment, preload_order: [:text] 51 # The post<->permalink relationship should be marked as uniq 52 has_one :permalink, Ecto.Integration.Permalink, on_delete: :delete_all, on_replace: :delete 53 has_one :force_permalink, Ecto.Integration.Permalink, on_replace: :delete_if_exists 54 has_one :update_permalink, Ecto.Integration.Permalink, foreign_key: :post_id, on_delete: :delete_all, on_replace: :update 55 has_many :comments_authors, through: [:comments, :author] 56 belongs_to :author, Ecto.Integration.User 57 many_to_many :users, Ecto.Integration.User, 58 join_through: "posts_users", on_delete: :delete_all, on_replace: :delete 59 many_to_many :ordered_users, Ecto.Integration.User, join_through: "posts_users", preload_order: [desc: :name] 60 many_to_many :unique_users, Ecto.Integration.User, 61 join_through: "posts_users", unique: true 62 many_to_many :constraint_users, Ecto.Integration.User, 63 join_through: Ecto.Integration.PostUserCompositePk 64 has_many :users_comments, through: [:users, :comments] 65 has_many :comments_authors_permalinks, through: [:comments_authors, :permalink] 66 has_one :post_user_composite_pk, Ecto.Integration.PostUserCompositePk 67 timestamps() 68 end 69 70 def changeset(schema, params) do 71 cast(schema, params, ~w(counter title blob temp public cost visits 72 intensity bid uuid meta posted)a) 73 end 74 end 75 76 defmodule Ecto.Integration.Comment do 77 @moduledoc """ 78 This module is used to test: 79 80 * Optimistic lock 81 * Relationships 82 * Dependent callbacks 83 84 """ 85 use Ecto.Integration.Schema 86 87 schema "comments" do 88 field :text, :string 89 field :lock_version, :integer, default: 1 90 belongs_to :post, Ecto.Integration.Post 91 belongs_to :author, Ecto.Integration.User 92 has_one :post_permalink, through: [:post, :permalink] 93 end 94 95 def changeset(schema, params) do 96 Ecto.Changeset.cast(schema, params, [:text]) 97 end 98 end 99 100 defmodule Ecto.Integration.Permalink do 101 @moduledoc """ 102 This module is used to test: 103 104 * Field sources 105 * Relationships 106 * Dependent callbacks 107 108 """ 109 use Ecto.Integration.Schema 110 111 schema "permalinks" do 112 field :url, :string, source: :uniform_resource_locator 113 field :title, :string 114 field :posted, :date, virtual: true 115 belongs_to :post, Ecto.Integration.Post, on_replace: :nilify 116 belongs_to :update_post, Ecto.Integration.Post, on_replace: :update, foreign_key: :post_id, define_field: false 117 belongs_to :user, Ecto.Integration.User 118 has_many :post_comments_authors, through: [:post, :comments_authors] 119 end 120 121 def changeset(schema, params) do 122 Ecto.Changeset.cast(schema, params, [:url, :title]) 123 end 124 end 125 126 defmodule Ecto.Integration.PostUser do 127 @moduledoc """ 128 This module is used to test: 129 130 * Many to many associations join_through with schema 131 132 """ 133 use Ecto.Integration.Schema 134 135 schema "posts_users_pk" do 136 belongs_to :user, Ecto.Integration.User 137 belongs_to :post, Ecto.Integration.Post 138 timestamps() 139 end 140 end 141 142 defmodule Ecto.Integration.User do 143 @moduledoc """ 144 This module is used to test: 145 146 * UTC Timestamps 147 * Relationships 148 * Dependent callbacks 149 150 """ 151 use Ecto.Integration.Schema 152 153 schema "users" do 154 field :name, :string 155 has_many :comments, Ecto.Integration.Comment, foreign_key: :author_id, on_delete: :nilify_all, on_replace: :nilify 156 has_one :permalink, Ecto.Integration.Permalink, on_replace: :nilify 157 has_many :posts, Ecto.Integration.Post, foreign_key: :author_id, on_delete: :nothing, on_replace: :delete 158 belongs_to :custom, Ecto.Integration.Custom, references: :bid, type: :binary_id 159 many_to_many :schema_posts, Ecto.Integration.Post, join_through: Ecto.Integration.PostUser 160 many_to_many :unique_posts, Ecto.Integration.Post, join_through: Ecto.Integration.PostUserCompositePk 161 162 has_many :related_2nd_order_posts, through: [:posts, :users, :posts] 163 has_many :users_through_schema_posts, through: [:schema_posts, :users] 164 165 has_many :v2_comments, Ecto.Integration.Comment, foreign_key: :author_id, where: [lock_version: 2] 166 has_many :v2_comments_posts, through: [:v2_comments, :post] 167 has_many :co_commenters, through: [:comments, :post, :comments_authors] 168 169 timestamps(type: :utc_datetime) 170 end 171 end 172 173 defmodule Ecto.Integration.Custom do 174 @moduledoc """ 175 This module is used to test: 176 177 * binary_id primary key 178 * Tying another schemas to an existing schema 179 180 Due to the second item, it must be a subset of posts. 181 """ 182 use Ecto.Integration.Schema 183 184 @primary_key {:bid, :binary_id, autogenerate: true} 185 schema "customs" do 186 field :uuid, Ecto.Integration.TestRepo.uuid() 187 many_to_many :customs, Ecto.Integration.Custom, 188 join_through: "customs_customs", join_keys: [custom_id1: :bid, custom_id2: :bid], 189 on_delete: :delete_all, on_replace: :delete 190 end 191 end 192 193 defmodule Ecto.Integration.Barebone do 194 @moduledoc """ 195 This module is used to test: 196 197 * A schema without primary keys 198 199 """ 200 use Ecto.Integration.Schema 201 202 @primary_key false 203 schema "barebones" do 204 field :num, :integer 205 end 206 end 207 208 defmodule Ecto.Integration.Tag do 209 @moduledoc """ 210 This module is used to test: 211 212 * The array type 213 * Embedding many schemas (uses array) 214 215 """ 216 use Ecto.Integration.Schema 217 218 schema "tags" do 219 field :ints, {:array, :integer} 220 field :uuids, {:array, Ecto.Integration.TestRepo.uuid()} 221 embeds_many :items, Ecto.Integration.Item 222 end 223 end 224 225 defmodule Ecto.Integration.Item do 226 @moduledoc """ 227 This module is used to test: 228 229 * Embedding 230 * Preloading associations in embedded schemas 231 232 """ 233 use Ecto.Schema 234 235 embedded_schema do 236 field :reference, PrefixedString 237 field :price, :integer 238 field :valid_at, :date 239 240 embeds_one :primary_color, Ecto.Integration.ItemColor 241 embeds_many :secondary_colors, Ecto.Integration.ItemColor 242 243 belongs_to :user, Ecto.Integration.User 244 end 245 end 246 247 defmodule Ecto.Integration.ItemColor do 248 @moduledoc """ 249 This module is used to test: 250 251 * Nested embeds 252 253 """ 254 use Ecto.Schema 255 256 embedded_schema do 257 field :name, :string 258 end 259 end 260 261 defmodule Ecto.Integration.Order do 262 @moduledoc """ 263 This module is used to test: 264 265 * Text columns 266 * Embedding one schema 267 * Preloading items inside embeds_many 268 * Preloading items inside embeds_one 269 * Field source with json_extract_path 270 271 """ 272 use Ecto.Integration.Schema 273 274 schema "orders" do 275 field :metadata, :map, source: :meta 276 embeds_one :item, Ecto.Integration.Item 277 embeds_many :items, Ecto.Integration.Item 278 belongs_to :permalink, Ecto.Integration.Permalink 279 end 280 end 281 282 defmodule Ecto.Integration.CompositePk do 283 @moduledoc """ 284 This module is used to test: 285 286 * Composite primary keys 287 288 """ 289 use Ecto.Integration.Schema 290 import Ecto.Changeset 291 292 @primary_key false 293 schema "composite_pk" do 294 field :a, :integer, primary_key: true 295 field :b, :integer, primary_key: true 296 field :name, :string 297 end 298 def changeset(schema, params) do 299 cast(schema, params, ~w(a b name)a) 300 end 301 end 302 303 defmodule Ecto.Integration.CorruptedPk do 304 @moduledoc """ 305 This module is used to test: 306 307 * Primary keys that is not unique on a DB side 308 309 """ 310 use Ecto.Integration.Schema 311 312 @primary_key false 313 schema "corrupted_pk" do 314 field :a, :string, primary_key: true 315 end 316 end 317 318 defmodule Ecto.Integration.PostUserCompositePk do 319 @moduledoc """ 320 This module is used to test: 321 322 * Composite primary keys for 2 belongs_to fields 323 324 """ 325 use Ecto.Integration.Schema 326 327 @primary_key false 328 schema "posts_users_composite_pk" do 329 belongs_to :user, Ecto.Integration.User, primary_key: true 330 belongs_to :post, Ecto.Integration.Post, primary_key: true 331 timestamps() 332 end 333 end 334 335 defmodule Ecto.Integration.Usec do 336 @moduledoc """ 337 This module is used to test: 338 339 * usec datetime types 340 341 """ 342 use Ecto.Integration.Schema 343 344 schema "usecs" do 345 field :naive_datetime_usec, :naive_datetime_usec 346 field :utc_datetime_usec, :utc_datetime_usec 347 end 348 end 349 350 defmodule Ecto.Integration.Logging do 351 @moduledoc """ 352 This module is used to test: 353 354 * Logging the casted version of parameters without array types 355 356 """ 357 use Ecto.Integration.Schema 358 359 @primary_key {:bid, :binary_id, autogenerate: true} 360 schema "loggings" do 361 field :int, :integer 362 field :uuid, Ecto.Integration.TestRepo.uuid() 363 timestamps() 364 end 365 end 366 367 defmodule Ecto.Integration.ArrayLogging do 368 @moduledoc """ 369 This module is used to test: 370 371 * Logging the casted version of parameters with array types 372 373 """ 374 use Ecto.Integration.Schema 375 376 schema "array_loggings" do 377 field :uuids, {:array, Ecto.Integration.TestRepo.uuid()} 378 timestamps() 379 end 380 end