Skip to content

Commit 9d2875e

Browse files
edit readme
1 parent 06ef06f commit 9d2875e

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,46 @@ Flyway executes repeatable files every time their content changes: this is done
1010
In other words, everytime a repeatable file changes, Flyway notices its checksum has changed and executes it again.<br><br>
1111

1212

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>
1616

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>
1918

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:
2145
- /views
2246
- /domains
23-
- R__domains_00_mv.sql
24-
- R__domains_01_mv.sql
47+
- R__domains_00_A_mv.sql
48+
- R__domains_00_B_mv.sql
49+
- R__domains_01_A_mv.sql
2550
- /jwt
26-
- R__jwt_00_mv.sql
27-
- R__jwt_01_mv.sql
51+
- R__jwt_00_A_mv.sql
52+
- R__jwt_00_B_mv.sql
2853
- ...<br><br>
2954

3055

0 commit comments

Comments
 (0)