Skip to content

Commit aeeacc6

Browse files
author
morten.lund@maskon.no
committed
Fix primary key generation for multitenancy
1 parent f9eaee9 commit aeeacc6

File tree

6 files changed

+145
-1
lines changed

6 files changed

+145
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"attributes": [
3+
{
4+
"allow_nil?": false,
5+
"default": "nil",
6+
"generated?": true,
7+
"primary_key?": true,
8+
"references": null,
9+
"size": null,
10+
"source": "id",
11+
"type": "bigint"
12+
},
13+
{
14+
"allow_nil?": false,
15+
"default": "nil",
16+
"generated?": false,
17+
"primary_key?": false,
18+
"references": null,
19+
"size": null,
20+
"source": "title",
21+
"type": "text"
22+
}
23+
],
24+
"base_filter": null,
25+
"check_constraints": [],
26+
"custom_indexes": [],
27+
"custom_statements": [],
28+
"has_create_action": true,
29+
"hash": "F547F05D353FC4B04CC604B8F2215A512BFB9FAD20B3C1DD2BCBF2455072D958",
30+
"identities": [],
31+
"multitenancy": {
32+
"attribute": null,
33+
"global": false,
34+
"strategy": "context"
35+
},
36+
"partitioning": null,
37+
"repo": "Elixir.AshPostgres.TestRepo",
38+
"schema": null,
39+
"table": "composite_key"
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"attributes": [
3+
{
4+
"allow_nil?": false,
5+
"default": "nil",
6+
"generated?": true,
7+
"primary_key?": true,
8+
"references": null,
9+
"size": null,
10+
"source": "id",
11+
"type": "bigint"
12+
},
13+
{
14+
"allow_nil?": false,
15+
"default": "nil",
16+
"generated?": false,
17+
"primary_key?": true,
18+
"references": null,
19+
"size": null,
20+
"source": "title",
21+
"type": "text"
22+
}
23+
],
24+
"base_filter": null,
25+
"check_constraints": [],
26+
"custom_indexes": [],
27+
"custom_statements": [],
28+
"has_create_action": true,
29+
"hash": "0EA09E46F197BAF8034CBFC7CCEFE46D2CCE9927ACD0991B5E90D5463B9B4AEC",
30+
"identities": [],
31+
"multitenancy": {
32+
"attribute": null,
33+
"global": false,
34+
"strategy": "context"
35+
},
36+
"partitioning": null,
37+
"repo": "Elixir.AshPostgres.TestRepo",
38+
"schema": null,
39+
"table": "composite_key"
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule AshPostgres.TestRepo.TenantMigrations.MigrateResources5 do
2+
@moduledoc """
3+
Updates resources based on their most recent snapshots.
4+
5+
This file was autogenerated with `mix ash_postgres.generate_migrations`
6+
"""
7+
8+
use Ecto.Migration
9+
10+
def up do
11+
create table(:composite_key, primary_key: false, prefix: prefix()) do
12+
add(:id, :bigserial, null: false, primary_key: true)
13+
add(:title, :text, null: false)
14+
end
15+
end
16+
17+
def down do
18+
drop(table(:composite_key, prefix: prefix()))
19+
end
20+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmodule AshPostgres.TestRepo.TenantMigrations.MigrateResources6 do
2+
@moduledoc """
3+
Updates resources based on their most recent snapshots.
4+
5+
This file was autogenerated with `mix ash_postgres.generate_migrations`
6+
"""
7+
8+
use Ecto.Migration
9+
10+
def up do
11+
drop(constraint("composite_key", "composite_key_pkey", prefix: prefix()))
12+
13+
alter table(:composite_key, prefix: prefix()) do
14+
modify(:title, :text)
15+
end
16+
17+
execute("ALTER TABLE \"#{prefix()}\".\"composite_key\" ADD PRIMARY KEY (id, title)")
18+
end
19+
20+
def down do
21+
drop(constraint("composite_key", "composite_key_pkey", prefix: prefix()))
22+
23+
alter table(:composite_key, prefix: prefix()) do
24+
modify(:title, :text)
25+
end
26+
27+
execute("ALTER TABLE \"#{prefix()}\".\"composite_key\" ADD PRIMARY KEY (id)")
28+
end
29+
end

test/migration_generator_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,12 @@ defmodule AshPostgres.MigrationGeneratorTest do
769769
migration_path: "test_migration_path",
770770
tenant_migration_path: "test_tenant_migration_path",
771771
quiet: false,
772+
<<<<<<< HEAD
772773
format: false,
773774
auto_name: true
775+
=======
776+
format: false
777+
>>>>>>> 37dd6436 (Fix primary key generation for multitenancy)
774778
)
775779

776780
assert [_file1, file2] =
@@ -1580,6 +1584,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
15801584
[domain: Domain]
15811585
end
15821586

1587+
<<<<<<< HEAD
15831588
test "raises an error on pending codegen", %{domain: domain} do
15841589
assert_raise Ash.Error.Framework.PendingCodegen, fn ->
15851590
AshPostgres.MigrationGenerator.generate(domain,
@@ -1589,6 +1594,16 @@ defmodule AshPostgres.MigrationGeneratorTest do
15891594
auto_name: true
15901595
)
15911596
end
1597+
=======
1598+
test "returns code(1) if snapshots and resources don't fit", %{domain: domain} do
1599+
assert catch_exit(
1600+
AshPostgres.MigrationGenerator.generate(domain,
1601+
snapshot_path: "test_snapshots_path",
1602+
migration_path: "test_migration_path",
1603+
check: true
1604+
)
1605+
) == {:shutdown, 1}
1606+
>>>>>>> 37dd6436 (Fix primary key generation for multitenancy)
15921607

15931608
refute File.exists?(Path.wildcard("test_migration_path2/**/*_migrate_resources*.exs"))
15941609
refute File.exists?(Path.wildcard("test_snapshots_path2/test_repo/posts/*.json"))

test/support/multitenancy/resources/composite_key_post.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule AshPostgres.MultitenancyTest.CompositeKeyPost do
2727
integer_primary_key(:id)
2828
attribute(:title, :string, public?: true, allow_nil?: false, primary_key?: true)
2929
end
30-
30+
3131
relationships do
3232
belongs_to(:org, AshPostgres.MultitenancyTest.Org) do
3333
public?(true)

0 commit comments

Comments
 (0)