Skip to content

Commit 7f44148

Browse files
authored
improvement: do not create snapshots for resources that have no attributes #571 (#599)
1 parent 5bbc43f commit 7f44148

File tree

2 files changed

+116
-25
lines changed

2 files changed

+116
-25
lines changed

lib/migration_generator/migration_generator.ex

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,17 @@ defmodule AshPostgres.MigrationGenerator do
19831983
end)
19841984
end
19851985

1986+
defp resource_has_meaningful_content?(snapshot) do
1987+
[
1988+
snapshot.attributes,
1989+
snapshot.identities,
1990+
snapshot.custom_indexes,
1991+
snapshot.custom_statements,
1992+
snapshot.check_constraints
1993+
]
1994+
|> Enum.any?(&Enum.any?/1)
1995+
end
1996+
19861997
defp do_fetch_operations(snapshot, existing_snapshot, opts, acc \\ [])
19871998

19881999
defp do_fetch_operations(
@@ -1996,34 +2007,41 @@ defmodule AshPostgres.MigrationGenerator do
19962007
end
19972008

19982009
defp do_fetch_operations(snapshot, nil, opts, acc) do
1999-
empty_snapshot = %{
2000-
attributes: [],
2001-
identities: [],
2002-
schema: nil,
2003-
custom_indexes: [],
2004-
custom_statements: [],
2005-
check_constraints: [],
2006-
table: snapshot.table,
2007-
repo: snapshot.repo,
2008-
base_filter: nil,
2009-
empty?: true,
2010-
multitenancy: %{
2011-
attribute: nil,
2012-
strategy: nil,
2013-
global: nil
2014-
}
2015-
}
2016-
2017-
do_fetch_operations(snapshot, empty_snapshot, opts, [
2018-
%Operation.CreateTable{
2010+
if resource_has_meaningful_content?(snapshot) do
2011+
empty_snapshot = %{
2012+
attributes: [],
2013+
identities: [],
2014+
schema: nil,
2015+
custom_indexes: [],
2016+
custom_statements: [],
2017+
check_constraints: [],
20192018
table: snapshot.table,
2020-
schema: snapshot.schema,
20212019
repo: snapshot.repo,
2022-
multitenancy: snapshot.multitenancy,
2023-
old_multitenancy: empty_snapshot.multitenancy
2020+
base_filter: nil,
2021+
empty?: true,
2022+
multitenancy: %{
2023+
attribute: nil,
2024+
strategy: nil,
2025+
global: nil
2026+
}
20242027
}
2025-
| acc
2026-
])
2028+
2029+
do_fetch_operations(snapshot, empty_snapshot, opts, [
2030+
%Operation.CreateTable{
2031+
table: snapshot.table,
2032+
schema: snapshot.schema,
2033+
repo: snapshot.repo,
2034+
multitenancy: snapshot.multitenancy,
2035+
old_multitenancy: empty_snapshot.multitenancy
2036+
}
2037+
| acc
2038+
])
2039+
else
2040+
unless opts.quiet do
2041+
Logger.info("Skipping migration for empty resource: #{snapshot.table} (no attributes, identities, indexes, statements, or constraints)")
2042+
end
2043+
acc
2044+
end
20272045
end
20282046

20292047
defp do_fetch_operations(snapshot, old_snapshot, opts, acc) do

test/migration_generator_test.exs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,79 @@ defmodule AshPostgres.MigrationGeneratorTest do
111111
end
112112
end
113113

114+
describe "empty resources" do
115+
setup do
116+
on_exit(fn ->
117+
File.rm_rf!("test_snapshots_path")
118+
File.rm_rf!("test_migration_path")
119+
end)
120+
end
121+
122+
test "empty resource does not generate migration files" do
123+
defresource EmptyPost, "empty_posts" do
124+
resource do
125+
require_primary_key?(false)
126+
end
127+
128+
actions do
129+
defaults([:read, :create])
130+
end
131+
end
132+
133+
defdomain([EmptyPost])
134+
135+
AshPostgres.MigrationGenerator.generate(Domain,
136+
snapshot_path: "test_snapshots_path",
137+
migration_path: "test_migration_path",
138+
quiet: false,
139+
format: false,
140+
auto_name: true
141+
)
142+
143+
migration_files =
144+
Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
145+
|> Enum.reject(&String.contains?(&1, "extensions"))
146+
147+
assert migration_files == []
148+
149+
snapshot_files =
150+
Path.wildcard("test_snapshots_path/**/*.json")
151+
|> Enum.reject(&String.contains?(&1, "extensions"))
152+
153+
assert snapshot_files == []
154+
end
155+
156+
test "resource with only primary key generates migration" do
157+
defresource PostWithId, "posts_with_id" do
158+
attributes do
159+
uuid_primary_key(:id)
160+
end
161+
end
162+
163+
defdomain([PostWithId])
164+
165+
AshPostgres.MigrationGenerator.generate(Domain,
166+
snapshot_path: "test_snapshots_path",
167+
migration_path: "test_migration_path",
168+
quiet: false,
169+
format: false,
170+
auto_name: true
171+
)
172+
173+
migration_files =
174+
Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
175+
|> Enum.reject(&String.contains?(&1, "extensions"))
176+
177+
assert length(migration_files) == 1
178+
179+
snapshot_files =
180+
Path.wildcard("test_snapshots_path/**/*.json")
181+
|> Enum.reject(&String.contains?(&1, "extensions"))
182+
183+
assert length(snapshot_files) == 1
184+
end
185+
end
186+
114187
describe "creating initial snapshots" do
115188
setup do
116189
on_exit(fn ->

0 commit comments

Comments
 (0)