-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·1336 lines (1108 loc) · 43.5 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# echo "Your script args ($#) are: $@"
function usage() {
echo -e "${BWHITE}.dbFlow/build.sh${NC} - build an installable artifact with eiter"
echo -e " all (${BWHITE}init${NC}) or only with changed (${BWHITE}patch${NC}) files"
echo ""
echo -e "${BWHITE}Usage:${NC}"
echo -e " ${0} --init --version <label>"
echo -e " ${0} --patch --version <label> [--start <hash|tag>] [--end <hash|tag>]"
echo ""
echo -e "${BWHITE}Options:${NC}"
echo -e " -h | --help - Show this screen"
echo -e ""
echo -e " -i | --init - Flag to build a full installable artifact "
echo -e " this will delete all objects in target schemas upon install"
echo -e " -p | --patch - Flag to build an update/patch as artifact "
echo -e " This will apply on top of the target schemas and consists"
echo -e " of the difference between the starthash/tag and endhash/tag"
echo -e " -v | --version <label> - Required label of version this artifact represents"
echo -e " -s | --start <hash|tag> - Optional hash or tag to determine the diff to the end, defaults to ORIG_HEAD"
echo -e " -e | --end <hash|tag> - Optional hash or tag to determine the diff to the start, defaults to HEAD"
echo -e " -c | --cached - Optional flag to determine the diff of Stage to HEAD, won't work with -s, -e"
echo ""
echo -e " -t | --transferall - Optional transfer (copy) all folders [mode=patch]"
echo -e " -k | --keepfolder - Optional keep buildfolder inside depot"
echo -e " -l | --listfiles - Optional flag to list files which will be a part of the patch"
echo -e " -a | --apply - Optional flag to call apply directly after patch is build."
echo -e " This will install the artifact in current environment."
echo -e " -f | --forceddl - Optional flag to switch off checking for new table-file through git itself."
echo -e " This will run table_ddl scripts when matching table is present in patch mode"
echo ""
echo -e "${BWHITE}Examples:${NC}"
echo -e " ${0} --init --version 1.0.0"
echo -e " ${0} --patch --version 1.1.0"
echo -e " ${0} --patch --version 1.2.0 --start 1.0.0"
echo -e " ${0} --patch --version 1.3.0 --start 71563f65 --end ba12010a"
echo -e " ${0} --patch --version 1.4.0 --start ORIG_HEAD --end HEAD"
exit $1
}
# get required functions and vars
source ./.dbFlow/lib.sh
# set project-settings from build.env if exists
if [[ -e ./build.env ]]; then
source ./build.env
fi
# set target-env settings from file if exists
# only relevant to get depot_path... this var should
# exist in both files
if [[ -e ./apply.env ]]; then
source ./apply.env
validate_passes
fi
function notify() {
[[ ${1} = 0 ]] || echo ❌ EXIT "${1}"
# you can notify some external services here,
# ie. Slack webhook, Github commit/PR etc.
}
trap '(exit 130)' INT
trap '(exit 143)' TERM
trap 'rc=$?; notify $rc; exit $rc' EXIT
function check_vars() {
# check require vars from build.env
do_exit="NO"
if [[ -z ${PROJECT:-} ]]; then
echo_error "undefined var: PROJECT"
do_exit="YES"
fi
# when MultisSchema or SingleSchema, this vars are required
if [[ ${PROJECT_MODE:-"MULTI"} != "FLEX" ]]; then
if [[ -z ${APP_SCHEMA:-} ]]; then
echo_error "undefined var: APP_SCHEMA"
do_exit="YES"
fi
if [[ ${PROJECT_MODE:-"MULTI"} != "SINGLE" ]]; then
if [[ -z ${DATA_SCHEMA:-} ]]; then
DATA_SCHEMA=${APP_SCHEMA}
fi
if [[ -z ${LOGIC_SCHEMA:-} ]]; then
LOGIC_SCHEMA=${APP_SCHEMA}
fi
fi
if [[ -z ${WORKSPACE:-} ]]; then
echo_error "undefined var: WORKSPACE"
do_exit="YES"
fi
fi
if [[ -z ${DEPOT_PATH+x} ]]; then
echo_error "undefined var: DEPOT_PATH"
do_exit="YES"
fi
####
if [[ ${do_exit} == "YES" ]]; then
echo_warning "aborting"
exit 1;
fi
}
function check_params() {
help_option="NO"
init_option="NO"
patch_option="NO"
version_option="NO"
version_argument="-"
start_option="NO"
start_argument=ORIG_HEAD
end_option="NO"
end_argument=HEAD
keep_option="NO"
all_option="NO"
list_option="NO"
cached_option="NO"
apply_option="NO"
forceddl_option="NO"
# echo "check_params: ${@}"
while getopts_long 'hipv:s:e:cktlaf help init patch version: start: end: cached keepfolder transferall listfiles apply forceddl' OPTKEY "${@}"; do
case ${OPTKEY} in
'h'|'help')
help_option="YES"
;;
'i'|'init')
init_option="YES"
;;
'p'|'patch')
patch_option="YES"
;;
'v'|'version')
version_option="YES"
version_argument="${OPTARG}"
;;
's'|'start')
start_option="YES"
start_argument="${OPTARG}"
;;
'e'|'end')
end_option="YES"
end_argument="${OPTARG}"
;;
'c'|'cached')
cached_option="YES"
;;
'k'|'keepfolder')
keep_option="YES"
;;
't'|'transferall')
all_option="YES"
;;
'l'|'listfiles')
list_option="YES"
;;
'a'|'apply')
apply_option="YES"
;;
'f'|'forceddl')
forceddl_option="YES"
;;
'?')
echo_error "INVALID OPTION -- ${OPTARG}" >&2
usage 10
;;
':')
echo_error "MISSING ARGUMENT for option -- ${OPTARG}" >&2
usage 11
;;
*)
echo_error "UNIMPLEMENTED OPTION -- ${OPTKEY}" >&2
usage 12
;;
esac
done
# help first
if [[ ${help_option} == "YES" ]]; then
usage 0
fi
if [[ $# -lt 1 ]]; then
echo_error "Missing arguments"
usage 1
fi
# Rule 1: init or patch
if [[ ${init_option} == "NO" ]] && [[ ${patch_option} == "NO" ]]; then
echo_error "Missing build mode, init or patch using flags -i or -p"
usage 2
fi
if [[ ${init_option} == "YES" ]] && [[ ${patch_option} == "YES" ]]; then
echo_error "Build mode can only be init or patch, not both"
usage 3
fi
# Rule 2: we always need a version
if [[ ${version_option} == "NO" ]] || [[ ${version_argument} == "-" ]]; then
echo_error "Missing version"
usage 4
else
version=${version_argument}
fi
if [[ ${init_option} == "YES" ]] && ([[ ${start_option} == "YES" ]] || [[ ${end_option} == "YES" ]] || [[ ${cached_option} == "YES" ]]); then
echo_error "Start, End or Cached are only valid in patch mode"
usage 5
fi
if [[ ${patch_option} == "YES" ]] && ( ( [[ ${start_option} == "YES" ]] || [[ ${end_option} == "YES" ]] ) && [[ ${cached_option} == "YES" ]] ); then
echo_error "You can use start and/or end OR cached! Not both"
usage 5
fi
# now check dependent params
if [[ ${init_option} == "YES" ]]; then
mode="init"
elif [[ ${patch_option} == "YES" ]]; then
mode="patch"
fi
# Rule 3: When patch, we need git tags or hashes to build the diff
if [[ $mode == "patch" ]]; then
if [[ $cached_option == "YES" ]]; then
diff_args="--cached"
log_args=""
else
if git cat-file -e "${start_argument}" 2> /dev/null; then
from_commit="${start_argument}"
else
echo_error "Start Commit or Tag ${start_argument} not found"
exit 6
fi
if git cat-file -e "${end_argument}" 2> /dev/null; then
until_commit="${end_argument}"
else
echo_error "End Commit or Tag ${end_argument} not found"
exit 7
fi
diff_args="${from_commit} ${until_commit}"
log_args="${from_commit}...${until_commit}"
fi
fi
# now check keep folder
if [[ ${keep_option} == "YES" ]]; then
KEEP_FOLDER="TRUE"
else
KEEP_FOLDER="FALSE"
fi
# now check ship all files
if [[ ${all_option} == "YES" ]]; then
SHIP_ALL="TRUE"
else
SHIP_ALL="FALSE"
fi
# should build.sh run apply.sh
if [[ ${apply_option} == "YES" ]]; then
APPLY_DIRECTLY="TRUE"
else
APPLY_DIRECTLY="FALSE"
fi
# if true, we won't strip table_ddls from patch when a new table file is present
if [[ ${forceddl_option} == "YES" ]]; then
FORCE_TABLE_DDL="TRUE"
else
FORCE_TABLE_DDL="FALSE"
fi
if [[ ${list_option} == "YES" ]] && [[ $mode == "patch" ]]; then
echo -e "${PURPLE}Listing changed files (build.env .gitignore apex db reports rest .hooks)${NC}"
git --no-pager diff -r --compact-summary --dirstat --stat-width=120 --no-commit-id ${diff_args} --diff-filter=ACMRTUXB -- build.env .gitignore apex db reports rest .hooks
exit 0
elif [[ ${list_option} == "YES" ]] && [[ $mode == "init" ]]; then
echo_error "Flag -l|--list is not valid on init mode, cause nothing to list as delta"
usage 8
fi
}
function setup_env() {
MAINFOLDERS=( apex db reports rest .hooks )
SCHEMAS=()
if [[ ${PROJECT_MODE} == "FLEX" ]]; then
SCHEMAS=(${DBFOLDERS[@]})
else
ALL_SCHEMAS=( ${DATA_SCHEMA} ${LOGIC_SCHEMA} ${APP_SCHEMA} )
SCHEMAS=($(printf "%s\n" "${ALL_SCHEMAS[@]}" | sort -u))
# if length is equal than ALL_SCHEMAS, otherwise distinct
if [[ ${#SCHEMAS[@]} == ${#ALL_SCHEMAS[@]} ]]; then
SCHEMAS=(${ALL_SCHEMAS[@]})
fi
fi
# folders for REST
rest_array=( access/roles access/privileges access/mapping modules )
# get branch name
{ #try
branch=$(git branch --show-current)
} || { # catch
branch="develop"
}
# set all folder names which has to be parsed for files to deploy in array SCAN_PATHES
define_folders ${mode} ${branch}
# if table changes are inside release, we have to call special-functionalities
table_changes="FALSE"
table_array=()
table_set=()
# folder outside git repo
depotpath="$(pwd)/$DEPOT_PATH/$branch"
targetpath="${depotpath}"/${mode}_${version}
sourcepath="."
rel_depotpath="$DEPOT_PATH/$branch"
rel_targetpath="${rel_depotpath}"/${mode}_${version}
# initialize logfile
MDATE=`date "+%Y%m%d%H%M%S"`
log_file="${MDATE}_bld_${mode}_${version}.log"
touch "${log_file}"
full_log_file="$( cd "$( dirname "${log_file}" )" >/dev/null 2>&1 && pwd )/${log_file}"
exec &> >(tee -a "${log_file}")
timelog "Building ${BWHITE}${mode}${NC} deployment version: ${BWHITE}${version}${NC}"
timelog "----------------------------------------------------------"
timelog "Mode: ${BWHITE}${mode}${NC}"
timelog "Version ${BWHITE}${version}${NC}"
timelog "Log File: ${BWHITE}${log_file}${NC}"
timelog "Branch: ${BWHITE}${branch}${NC}"
if [[ $mode == "patch" ]];then
if [[ ${diff_args} == "--cached" ]]; then
timelog "cached: ${BWHITE}yes${NC}"
else
display_from=`git rev-parse --short "${from_commit}"`
display_until=`git rev-parse --short "${until_commit}"`
timelog "from: ${BWHITE}${from_commit} (${display_from})${NC}"
timelog "until: ${BWHITE}${until_commit} (${display_until})${NC}"
timelog "transfer all: ${BWHITE}${SHIP_ALL}${NC}"
timelog "forceddl ${BWHITE}${FORCE_TABLE_DDL}${NC}"
fi
fi
timelog "Bash-Version: ${BWHITE}${BASH_VERSION}${NC}"
timelog "----------------------------------------------------------"
timelog "Project ${BWHITE}${PROJECT}${NC}"
if [[ ${PROJECT_MODE} != "FLEX" ]]; then
timelog "App Schema: ${BWHITE}${APP_SCHEMA}${NC}"
if [[ ${PROJECT_MODE} != "SINGLE" ]]; then
timelog "Data Schema: ${BWHITE}${DATA_SCHEMA}${NC}"
timelog "Logic Schema: ${BWHITE}${LOGIC_SCHEMA}${NC}"
fi
timelog "Workspace: ${BWHITE}${WORKSPACE}${NC}"
fi
timelog "Schemas: (${BWHITE}${SCHEMAS[*]}${NC})"
timelog "----------------------------------------------------------"
timelog "Depotpath: ${BWHITE}${rel_depotpath}${NC}"
timelog "Targetpath: ${BWHITE}${rel_targetpath}${NC}"
timelog "Sourcepath: ${BWHITE}${sourcepath}${NC}"
timelog "Keepfolder: ${BWHITE}${KEEP_FOLDER}${NC}"
timelog "----------------------------------------------------------"
timelog "----------------------------------------------------------"
timelog "----------------------------------------------------------"
}
function copy_all_files() {
timelog "Copy all files ..."
for folder in "${MAINFOLDERS[@]}"
do
if [[ -d ${folder} ]]; then
cp -R "${folder}" "${targetpath}"
fi
done
[ ! -f build.env ] || cp build.env "${targetpath}"
[ ! -f .gitignore ] || cp .gitignore "${targetpath}"
}
function copy_files {
timelog " ==== Checking Files and Folders ===="
[[ ! -d ${targetpath} ]] || rm -rf "${targetpath}"
timelog " "
# getting updated files, and
# copy (and overwrite forcefully) in exact directory structure as in git repo
if [[ "${mode}" == "init" ]]; then
timelog "Creating directory ${targetpath}"
mkdir -p "${targetpath}"
copy_all_files
else
# Changes on configs?
num_changes=`git diff -r --name-only --no-commit-id ${diff_args} --diff-filter=ACMRTUXB -- build.env .gitignore | wc -l | xargs`
if [[ $num_changes -gt 0 ]]; then
if [ ! -d "${targetpath}" ]; then
timelog "Creating directory '${targetpath}'"
mkdir -p "${targetpath}"
fi
if [[ $(uname) == "Darwin" ]]; then
rsync -Rr `git diff -r --name-only --no-commit-id ${diff_args} --diff-filter=ACMRTUXB -- build.env .gitignore` "${targetpath}"
else
cp --parents -Rf `git diff -r --name-only --no-commit-id ${diff_args} --diff-filter=ACMRTUXB -- build.env .gitignore` "${targetpath}"
fi
fi
# Patch
for folder in "${MAINFOLDERS[@]}"
do
num_changes=`git diff -r --name-only --no-commit-id ${diff_args} --diff-filter=ACMRTUXB -- "${folder}" | wc -l | xargs`
if [[ $num_changes -gt 0 ]]; then
if [ ! -d "${targetpath}" ]; then
timelog "Creating directory '${targetpath}'"
mkdir -p "${targetpath}"
fi
timelog "Copy files in folder: ${folder}"
if [[ $(uname) == "Darwin" ]]; then
rsync -Rr `git diff -r --name-only --no-commit-id ${diff_args} --diff-filter=ACMRTUXB -- "${folder}"` "${targetpath}"
else
cp --parents -Rf `git diff -r --name-only --no-commit-id ${diff_args} --diff-filter=ACMRTUXB -- "${folder}"` "${targetpath}"
fi
else
timelog "No changes in folder: ${folder}"
fi
done
# additionaly we need all triggers belonging to views
# loop through schemas
for schema in "${SCHEMAS[@]}"
do
# any views?
if [[ -d "${targetpath}"/db/${schema}/views ]]
then
# get view files
for file in $(ls "${targetpath}/db/${schema}/views" | sort )
do
# check if there is any file like viewfile_*.sql
myfile=${file//./"_*."}
for f in ${sourcepath}/db/${schema}/sources/triggers/${myfile}; do
if [[ -e "${f}" ]];
then
# yes, so copy it...
if [[ $(uname) == "Darwin" ]]; then
rsync -Rr "${f}" "${targetpath}"
else
cp --parents -Rf "${f}" "${targetpath}"
fi
timelog "Additionaly add ${f}"
fi
done
done
fi
done
# additionaly we need all conditions beloning to REST
if [[ -d "${targetpath}"/rest ]]; then
folders=()
if [[ ${PROJECT_MODE} == "FLEX" ]]; then
diritems=()
IFS=$'\n' read -r -d '' -a diritems < <( find "${targetpath}/rest" -maxdepth 1 -mindepth 1 -type d | sort -f && printf '\0' )
for dirname in "${diritems[@]}"
do
folders+=( $(basename "${dirname}") )
done
else
folders=( . )
fi
for fldr in "${folders[@]}"
do
path=modules
if [[ -d "${targetpath}"/rest/$fldr/$path ]]; then
depth=1
if [[ $path == "modules" ]]; then
depth=2
fi
items=()
IFS=$'\n' read -r -d '' -a items < <( find "${targetpath}/rest/${fldr}/${path}/" -maxdepth $depth -mindepth $depth -type f | sort && printf '\0' )
for file in "${items[@]}"
do
base=${targetpath}/rest/${fldr}/
part=${file#$base}
if [[ "${part}" == *".sql" ]] && [[ "${part}" != *".condition.sql" ]]; then
srcf=${sourcepath}/rest/$fldr/$part
if [[ -f ${srcf/.sql/.condition.sql} ]]; then
# yes, so copy it...
if [[ $(uname) == "Darwin" ]]; then
rsync -Rr "${srcf/.sql/.condition.sql}" "${targetpath}"
else
cp --parents -Rf "${srcf/.sql/.condition.sql}" "${targetpath}"
fi
fi
fi
done
fi
done
fi
if [ ! -d "${targetpath}" ]; then
timelog "Creating directory '${targetpath}'"
mkdir -p "${targetpath}"
fi
## and we need all hooks
for schema in "${SCHEMAS[@]}"
do
# yes, so copy it...
if [[ $(uname) == "Darwin" ]]; then
rsync -Rr "${sourcepath}/db/${schema}/.hooks" "${targetpath}"
else
if [[ -d "${sourcepath}/db/${schema}/.hooks" ]]; then
cp --parents -Rf "${sourcepath}/db/${schema}/.hooks" "${targetpath}"
fi
fi
done
if [[ $(uname) == "Darwin" ]]; then
rsync -Rr "${sourcepath}/.hooks" "${targetpath}"
else
if [[ -d "${sourcepath}/.hooks" ]]; then
cp --parents -Rf "${sourcepath}/.hooks" "${targetpath}"
fi
fi
# if there are table scripts that are new in this delta and there are also table_ddl scripts
# for these new table scripts, we leave out the table_ddl scripts
if [[ ${FORCE_TABLE_DDL} == "FALSE" ]]; then
timelog " "
for schema in "${SCHEMAS[@]}"
do
local table_folder="${sourcepath}/db/${schema}/tables"
local table_files=`git diff -r --name-only --no-commit-id ${diff_args} --diff-filter=A -- "${table_folder}" ":!${table_folder}/tables_ddl"`
# for each new (A) table file
for file in $table_files; do
local file_base=$(basename "${file}")
for f in "${targetpath}/db/${schema}/tables/tables_ddl"/${file_base%%.*}.*; do
timelog "New table detected: db/${schema}/tables/${file_base}"
timelog "└─> removing table_ddl db/${schema}/tables/tables_ddl/$(basename ${f})" warning
rm "$f"
done
done
done
fi
fi
timelog " "
}
function list_files_to_remove() {
# if patch mode we remove unnecessary files
if [[ "${mode}" == "patch" ]]; then
target_drop_file="${targetpath}"/remove_files_$version.lst
for folder in "${MAINFOLDERS[@]}"
do
# to avoid dead-files
num_changes=`git diff -r --name-only --no-commit-id ${diff_args} --diff-filter=D -- "${folder}" | wc -l | xargs`
if [[ $num_changes -gt 0 ]]; then
timelog "removing dead-files"
for line in `git diff -r --name-only --no-commit-id ${diff_args} --diff-filter=D -- "${folder}"`
do
echo "${line}" >> "${target_drop_file}"
done
else
timelog "No deleted files in folder: ${folder}"
fi
done
fi
}
function write_install_schemas(){
if [[ -d "${targetpath}"/db ]]; then
timelog " ==== Checking Schemas ${SCHEMAS[@]} ===="
timelog ""
# loop through schemas
for schema in "${SCHEMAS[@]}"
do
if [[ -d "${targetpath}"/db/${schema} ]]; then
# file to write to
target_install_base=${mode}_${schema}_${version}.sql
target_install_file="${targetpath}"/db/${schema}/$target_install_base
timelog ""
timelog " ==== Schema: ${schema} - /db/${schema}/$target_install_base ===="
timelog ""
# write some infos
{
echo "set define '^'"
echo "set concat on"
echo "set concat ."
echo "set verify off"
echo "WHENEVER SQLERROR EXIT SQL.SQLCODE"
echo ""
echo "define VERSION = '^1'"
echo "define MODE = '^2'"
echo "set timing on"
echo "set trim on"
echo "set linesize 2000"
echo "set sqlblanklines on"
echo "set tab off"
echo "set pagesize 9999"
echo "set trimspool on"
echo ""
echo "Prompt .............................................................................. "
echo "Prompt .............................................................................. "
echo "Prompt .. Start Installation for schema: ${schema} "
echo "Prompt .. Version: $mode $version "
echo "Prompt .............................................................................. "
echo "set serveroutput on"
echo "set scan off"
echo ""
if [[ "${mode}" == "patch" ]]; then
echo "Prompt .. Commit-History to install: "
if [[ ${diff_args} == "--cached" ]]; then
echo "Prompt .. no logs availabe installing patch from cache"
else
git log --pretty=format:'Prompt .. %h %s <%an>' ${log_args} -- "db/${schema}"
fi
echo " "
echo "Prompt .. "
# echo "Prompt "
echo "Prompt .............................................................................. "
echo "Prompt "
echo "Prompt "
fi
} > "${target_install_file}"
# check every path in given order
for path in "${SCAN_PATHES[@]}"
do
## if [[ -d "${targetpath}"/db/${schema}/${path} ]]; then
timelog "Writing calls for ${path}"
{
# set scan to on, to make use of vars inside main schema-hooks
if [[ "${path}" == ".hooks/pre" ]] || [[ "${path}" == ".hooks/post" ]]; then
echo "set scan on"
fi
# pre folder-hooks (something like db/schema/.hooks/pre/tables)
entries=("${targetpath}/db/${schema}/.hooks/pre/${path}"/*.*)
if [[ ${#entries[@]} -gt 0 ]]; then
for entry in "${entries[@]}"; do
file=$(basename "${entry}")
file_ext=${file#*.}
echo "set scan on"
if [[ "${file_ext}" == "tables.sql" ]]; then
if [ ${#table_set[@]} -gt 0 ]; then
echo "Prompt running .hooks/pre/${path}/${file} with table set"
for table_item in "${table_set[@]}"
do
echo "Prompt >>> db/${schema}/.hooks/pre/${path}/${file} ${version} ${mode} ${table_item}.sql"
echo "@@.hooks/pre/${path}/${file} ${version} ${mode} ${table_item}.sql"
echo "Prompt <<< db/${schema}/.hooks/pre/${path}/${file} ${version} ${mode} ${table_item}.sql"
done
echo "Prompt"
echo ""
fi
else
echo "Prompt >>> db/${schema}/.hooks/pre/${path}/${file}"
echo "@@.hooks/pre/${path}/${file}"
echo "Prompt <<< db/${schema}/.hooks/pre/${path}/${file}"
fi
done
echo "Prompt"
echo ""
fi
# read files from folder
# if packages then sort by extension descending
if [[ "${path}" == "sources/packages" ]] || [[ "${path}" == "sources/types" ]] || [[ "${path}" == "tests/packages" ]]; then
sorted=("${targetpath}/db/${schema}/${path}"/*.*ks) #pks|tks
sorted+=("${targetpath}/db/${schema}/${path}"/*.*kb) #pkb|tkb
sorted+=("${targetpath}/db/${schema}/${path}"/*.sql)
else
sorted=("${targetpath}/db/${schema}/${path}"/*.*)
fi
# nur wenn es files gibt
if [[ ${#sorted[@]} -gt 0 ]]; then
if [[ "${path}" == "ddl/patch/pre" ]] || [[ "${path}" == "ddl/patch/pre_*" ]] || [[ "${path}" == "views" ]]; then
echo ""
echo "WHENEVER SQLERROR CONTINUE"
echo ""
fi
echo "Prompt Installing ${path} ..."
echo "Prompt"
echo "set define off"
for entry in "${sorted[@]}"; do
file=$(basename "${entry}")
file_ext=${file#*.}
if [[ "${path}" == "tables" ]]; then
skipfile="FALSE"
table_changes="TRUE"
# store tablename in array
table_name="${file%%.*}"
table_array+=( ${table_name} )
if [[ "${mode}" == "patch" ]]; then
# is there any matching file in tables_ddl
if [[ -d "${targetpath}/db/${schema}/tables/tables_ddl" ]]; then
for f in "${targetpath}/db/${schema}/tables/tables_ddl"/${file%%.*}.*; do
if [[ -e "$f" ]]; then
skipfile="TRUE"
fi
done
fi
# is there any matching file in tables_ddl defined just for the target branch?
if [[ -d "${targetpath}/db/${schema}/tables/tables_ddl/${branch}" ]]; then
for f in "${targetpath}/db/${schema}/tables/tables_ddl/${branch}"/${file%%.*}.*; do
if [[ -e "$f" ]]; then
skipfile="TRUE"
fi
done
fi
fi
if [[ "$skipfile" == "TRUE" ]]; then
echo "Prompt ... skipped ${file}"
else
echo "Prompt >>> db/${schema}/${path}/${file}"
echo "@@${path}/${file}"
echo "Prompt <<< db/${schema}/${path}/${file}"
fi
else
if ([[ "${path}" == ".hooks/pre" ]] || [[ "${path}" == ".hooks/post" ]]) && [[ "${file_ext}" == "tables.sql" ]]; then
if [ ${#table_set[@]} -gt 0 ]; then
echo "Prompt running ${path}/${file} with table set"
for table_item in "${table_set[@]}"
do
echo "Prompt >>> db/${schema}/${path}/${file} ${version} ${mode} ${table_item}.sql"
echo "@@${path}/${file} ${version} ${mode} ${table_item}.sql"
echo "Prompt <<< db/${schema}/${path}/${file} ${version} ${mode} ${table_item}.sql"
done
echo "Prompt"
echo ""
fi
else
echo "Prompt >>> db/${schema}/${path}/${file}"
if [[ "${path}" == "ddl/pre_*" ]] && [[ "${mode}" == "patch" ]]; then
target_stage="${path/'ddl/pre_'/}"
echo "--${target_stage}@@${path}/${file}"
else
echo "@@${path}/${file}"
echo "Prompt <<< db/${schema}/${path}/${file}"
fi
fi
fi
done #files in folder (sorted)
echo "set define '^'"
echo "Prompt"
echo "Prompt"
echo ""
if [[ "${path}" == "ddl/patch/pre" ]] || [[ "${path}" == "ddl/patch/pre_*" ]] || [[ "${path}" == "views" ]]
then
echo "WHENEVER SQLERROR EXIT SQL.SQLCODE"
echo ""
fi
fi
# union table names
if [[ "${path}" == "tables" ]]; then
# get distinct values of array
table_set=($(printf "%s\n" "${table_array[@]}" | sort -u))
fi
# post folder hooks
entries=("${targetpath}/db/${schema}/.hooks/post/${path}"/*.*)
if [[ ${#entries[@]} -gt 0 ]]; then
for entry in "${entries[@]}"; do
file=$(basename "${entry}")
file_ext=${file#*.}
if [[ "${file_ext}" == "tables.sql" ]]; then
if [ ${#table_set[@]} -gt 0 ]; then
echo "Prompt running .hooks/post/${path}/${file} with table set"
for table_item in "${table_set[@]}"
do
echo "Prompt >>> db/${schema}/.hooks/post/${path}/${file} ${version} ${mode} ${table_item}.sql"
echo "@@.hooks/post/${path}/${file} ${version} ${mode} ${table_item}.sql"
echo "Prompt <<< db/${schema}/.hooks/post/${path}/${file} ${version} ${mode} ${table_item}.sql"
done
echo "Prompt"
echo ""
fi
else
echo "Prompt >>> db/${schema}/.hooks/post/${path}/${file}"
echo "@@.hooks/post/${path}/${file}"
echo "Prompt <<< db/${schema}/.hooks/post/${path}/${file}"
fi
done
echo "Prompt"
echo ""
fi
# set scan to off, to make use of vars inside main schema-hooks
if [[ "${path}" == ".hooks/pre" ]] || [[ "${path}" == ".hooks/post" ]]; then
echo "set scan off"
fi
} >> "${target_install_file}"
## fi #path exists
done #paths
{
echo "prompt compiling schema"
echo "exec dbms_utility.compile_schema(schema => user, compile_all => false);"
echo "exec dbms_session.reset_package"
echo "Prompt"
echo "Prompt"
echo "exit"
} >> "${target_install_file}"
else
echo " .. db/${schema} does not exist in ${targetpath}"
fi
done
else
timelog " ... no db folder"
fi
}
function write_install_apps() {
# loop through applications
if [[ -d "${targetpath}/apex" ]]; then
timelog ""
timelog " ==== Checking APEX Applications ===="
timelog ""
# file to write to
target_apex_file="${targetpath}/apex_files_$version.lst"
[ -f "${target_apex_file}" ] && rm "${target_apex_file}"
depth=1
if [[ ${PROJECT_MODE} == "FLEX" ]]; then
depth=3
fi
items=()
IFS=$'\n' read -r -d '' -a items < <( find "${targetpath}/apex" -maxdepth "${depth}" -mindepth "${depth}" -type d && printf '\0' )
for dirname in "${items[@]}"
do
echo "${dirname/${targetpath}\//}" >> "${target_apex_file}"
timelog "Writing call to install APP: ${dirname/${targetpath}\//} "
done
fi
}
function write_install_rest() {
# check rest
if [[ -d "${targetpath}"/rest ]]; then
timelog ""
timelog " ==== Checking REST Modules ===="
timelog ""
folders=()
if [[ ${PROJECT_MODE} == "FLEX" ]]; then
items=()
IFS=$'\n' read -r -d '' -a items < <( find rest -maxdepth 1 -mindepth 1 -type d | sort -f && printf '\0' )
for dirname in "${items[@]}"
do
folders+=( $(basename "${dirname}") )
done
else
folders=( . )
fi
for fldr in "${folders[@]}"
do
if [[ ${fldr} != "." ]]; then
timelog " == Schema: $fldr"
fi
rest_to_install="FALSE"
# file to write to
target_install_base=rest_${mode}_${version}.sql
target_install_file="${targetpath}/rest/$fldr/$target_install_base"
[ -f "${target_install_file}" ] && rm "${target_install_file}"
# write some infos
echo "Prompt .............................................................................. " >> "${target_install_file}"
echo "Prompt .............................................................................. " >> "${target_install_file}"
echo "Prompt .. Start REST installation " >> "${target_install_file}"
echo "Prompt .. Version: $mode $version " >> "${target_install_file}"
echo "Prompt .. Folder: $fldr " >> "${target_install_file}"
echo "Prompt .............................................................................. " >> "${target_install_file}"
echo "set serveroutput on" >> "${target_install_file}"
echo "" >> "${target_install_file}"
# check every path in given order
for path in "${rest_array[@]}"
do
if [[ -d "${targetpath}"/rest/$fldr/$path ]]; then
depth=1
if [[ $path == "modules" ]]; then
depth=2
fi
items=()
IFS=$'\n' read -r -d '' -a items < <( find "${targetpath}/rest/${fldr}/${path}/" -maxdepth $depth -mindepth $depth -type f | sort && printf '\0' )
for file in "${items[@]}"
do
rest_to_install="TRUE"
base="${targetpath}/rest/$fldr/"
part=${file#$base}
timelog "Writing call to install RESTModul: ${part} "
if [[ "${part}" == *".sql" ]] && [[ "${part}" != *".condition.sql" ]]; then
echo "Prompt ... $part" >> "${target_install_file}"
if [[ -f ${file/.sql/.condition.sql} ]]; then
echo "begin" >> "${target_install_file}"
echo " if" >> "${target_install_file}"
echo " @@${part/.sql/.condition.sql}" >> "${target_install_file}"
echo " then" >> "${target_install_file}"
echo " @@${part}" >> "${target_install_file}"
echo " else" >> "${target_install_file}"
echo " dbms_output.put_line('!!! ${part} not installed cause condition did not match');" >> "${target_install_file}"
echo " end if;" >> "${target_install_file}"
echo "end;" >> "${target_install_file}"
else