You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+35-10Lines changed: 35 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,21 +10,46 @@ Flyway executes repeatable files every time their content changes: this is done
10
10
In other words, everytime a repeatable file changes, Flyway notices its checksum has changed and executes it again.<br><br>
11
11
12
12
13
-
### Folder structure
14
-
Repeatable files are executed in **alphabetical order** with respect to the name.<br>
15
-
Because of this, it's possible to apply an execution order by adding a cardinal number in the file name after the R__ prefix (e.g. R__00_mv.sql).<br>
13
+
### Naming convention and folder structure
14
+
If a materialized view B is based on a materialized view A, then A must be **created before** B.<br>
15
+
Moreover, A must be **refreshed before** B as well.<br>
16
16
17
-
However, multiple files in **different subfolders** having the **same name** will conflict when Flyway tries to execute them (e.g. /views/domains/R__mv.sql and /views/jwt/R__mv.sql).<br>
18
-
This problem can be solved by adding the name of the subfolder (in which the file is located) to the file name (e.g. /views/domains/R__domains_mv.sql and /views/jwt/R__jwt_mv.sql).<br>
17
+
To enforce such order, it's necessary to adopt a **naming convention** to be used in both the file's name and the view's name.<br>
19
18
20
-
Combining both solutions, the /views folder that stores the migration files has a structure like the following:<br>
19
+
####
20
+
The necessity of using a naming convention in the file's name is related to how Flyway applies repeatable migrations.<br>
21
+
In fact, repeatable files are executed in **alphabetical order** with respect to the name.<br>
22
+
By adding a cardinal number in the file's name after the R__ prefix (e.g. ```R__00_A_mv.sql```), it's possible to enforce an order in the Flyway's migration execution.<br>
23
+
24
+
Note: multiple files in **different subfolders** having the **same name** will conflict when Flyway tries to execute them (e.g. ```/views/domains/R__00_A_mv.sql``` and ```/views/jwt/R__00_A_mv.sql```).<br>
25
+
This problem can be solved by adding the name of the subfolder (in which the file is located) to the file's name (e.g. ```/views/domains/R__domains_00_A_mv.sql``` and ```/views/jwt/R__jwt_00_A_mv.sql```).<br>
26
+
27
+
####
28
+
The necessity of using a naming convention in the view's name is related to how the refreshing mechanism works.<br>
29
+
Redshift provides materialized views that can be refreshed automatically.<br>
30
+
However, there are some limitations: a materialized view that is based on another materialized view can't be refreshed automatically.<br>
31
+
To overcome this issue, it has been developed a mechanism to refresh materialized views (for which auto-refresh is disabled) in a **scheduled way**.<br>
32
+
This scheduled refresh mechanism relies on EventBridge to perform a scheduled query on the STV_MV_INFO system table to get a list of materialized view to be refreshed.<br>
33
+
Ideally, we can gather materialized views into logical layers:
34
+
- layer 0 gathers all the **root** materialized views based on data stored in tables;
35
+
- layer 1 gathers all the **intermediate** materialized views based on materialized views in layer 0;
36
+
- ...
37
+
- layer X gathers all the **intermediate** materialized views based on materialized views in layer X-1.
38
+
39
+
Note: materialized views belonging to the same layer are not based on each others, so they can be refreshed in a parallel way.<br>
40
+
41
+
Eventually, by adding a cardinal number in the views's name (e.g. ```$SCHEMA_NAME.mv_00_A```), it's possible to enforce a refreshing order for the materialized views.<br>
42
+
43
+
####
44
+
Combining what we've said, the /views folder that stores the migration files will have a structure like the following:
0 commit comments