-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallable.sh
More file actions
235 lines (206 loc) · 6.1 KB
/
callable.sh
File metadata and controls
235 lines (206 loc) · 6.1 KB
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
#!/bin/bash
Ash__import "github.com/ash-shell/sql"
# Set defaults if none set
if [[ "$MIGRATE_MIGRATIONS_TABLE" = "" ]]; then
MIGRATE_MIGRATIONS_TABLE="ash_migrations"
fi
if [[ "$MIGRATE_MIGRATIONS_DIRECTORY" = "" ]]; then
MIGRATE_MIGRATIONS_DIRECTORY="ash_migrations"
fi
if [[ "$MIGRATE_DATABASE_DRIVER" = "" ]]; then
MIGRATE_DATABASE_DRIVER="$Sql__DRIVER_POSTGRES"
fi
# Global Const
Migrate_PACKAGE_LOCATION="$(Ash__find_module_directory "github.com/ash-shell/migrate")"
Migrate_MIGRATIONS_CURRENT_DIRECTORY="$Ash__CALL_DIRECTORY/$MIGRATE_MIGRATIONS_DIRECTORY"
Migrate_MIGRATION_TEMPLATE="$Migrate_PACKAGE_LOCATION/extra/migration_template.sql"
#################################################
# Runs more on the contents of the HELP.txt file
# which provides usage information for this module.
#################################################
Migrate__callable_help(){
more "$Ash__ACTIVE_MODULE_DIRECTORY/HELP.txt"
}
#################################################
# Runs all outstanding migrations.
#################################################
Migrate__callable_main(){
# Setup
Migrate_setup
if [[ $? -ne $Ash__TRUE ]]; then
return $Ash__FALSE
fi
# Migrate
Migrate_migrate
if [[ $? -ne $Ash__TRUE ]]; then
Migrate_shutdown
return $Ash__FALSE
fi
# Shutdown
Migrate_shutdown
}
#################################################
# Creates a new migration, which creates a new
# file in the "$Migrate_MIGRATIONS_CURRENT_DIRECTORY"
# directory for the user to fill out with their
# migration.
#################################################
Migrate__callable_make(){
# Setup
Migrate_setup
if [[ $? -ne $Ash__TRUE ]]; then
return $Ash__FALSE
fi
# Get name + timestamp
local name="$1"
local timestamp="$(date +%s)"
# Add migration to DB
local result=""
result=$(Migrate_create_migration "$name" "$timestamp")
if [[ $? -eq $Ash__TRUE ]]; then
# Create migration file
local migration_file="$Migrate_MIGRATIONS_CURRENT_DIRECTORY"/"$timestamp"_"$name".sql
cp "$Migrate_MIGRATION_TEMPLATE" "$migration_file"
Logger__success "Created migration $name in ./$MIGRATE_MIGRATIONS_DIRECTORY"
else
Logger__error "Failed to create migration."
Logger__error "$result"
Migrate_shutdown
return $Ash__FALSE
fi
# Shutdown
Migrate_shutdown
}
#################################################
# Rolls back all of the migrations that have been
# run in the database.
#################################################
Migrate__callable_rollback(){
# Setup
Migrate_setup
if [[ $? -ne $Ash__TRUE ]]; then
return $Ash__FALSE
fi
# Rollback
Migrate_rollback
if [[ $? -ne $Ash__TRUE ]]; then
Migrate_shutdown
return $Ash__FALSE
fi
# Shutdown
Migrate_shutdown
}
#################################################
# Rolls back all of the migrations that have been
# run in the database, then runs all of the
# migrations in the database.
#
# The same end result would occur if you run this
# command vs running `rollback` followed by a
# `migrate`.
#################################################
Migrate__callable_refresh(){
# Setup
Migrate_setup
if [[ $? -ne $Ash__TRUE ]]; then
return $Ash__FALSE
fi
# Rollback
Migrate_rollback
if [[ $? -ne $Ash__TRUE ]]; then
return $Ash__FALSE
Migrate_shutdown
fi
Logger__warning "------------------------"
# Migrate
Migrate_migrate
if [[ $? -ne $Ash__TRUE ]]; then
return $Ash__FALSE
Migrate_shutdown
fi
# Shutdown
Migrate_shutdown
}
#################################################
# Displays the current state of the migrations.
#################################################
Migrate__callable_map(){
# Setup
Migrate_setup
if [[ $? -ne $Ash__TRUE ]]; then
return $Ash__FALSE
fi
# Loading all migrations
local result=""
result="$(Sql__execute "$(Migrate_select_all_migrations_asc_query)")"
if [[ $? -eq $Ash__FALSE ]]; then
Logger__error "Failed to load migrations"
Logger__error "$result"
Migrate_shutdown
return $Ash__FALSE
fi
# Go through all migrations and log them
Logger__disable_prefix
while read -r record; do
while IFS=$'\t' read id name active created_at; do
if [[ "$active" = $Sql__TRUE ]]; then
Logger__success " > $name"
else
Logger__log " $name"
fi done <<< "$record"
done <<< "$result"
Logger__enable_prefix
# Shutdown
Migrate_shutdown
}
#################################################
# Allows the user to step through their migrations.
#
# @param $1: This defines the number and direction
# of steps.
#
# To migrate, this parameter should be +X, where
# X is the number of migrations to run.
#
# To revert, this parameter should be -Y, where
# Y is the number of migrations to revert.
#################################################
Migrate__callable_step(){
# Setup
Migrate_setup
if [[ $? -ne $Ash__TRUE ]]; then
return $Ash__FALSE
fi
# Check if we have a step param
if [[ -z "$1" ]]; then
Logger__error "Step requires a parameter"
Migrate_shutdown
return $Ash__FALSE
fi
# Verify proper format
if [[ ! "$1" =~ [+-][[:digit:]]+$ ]]; then
Logger__error "Invalid parameter. Step parameter must be in this format: [+-][[:digit:]]+$"
Migrate_shutdown
return $Ash__FALSE
fi
# Split Action + Number
local action=${1:0:1}
local number=${1:1:${#1}-1}
# Handle migrate
if [[ "$action" = "+" ]]; then
Migrate_migrate "$number"
if [[ $? -ne $Ash__TRUE ]]; then
Migrate_shutdown
return $Ash__FALSE
fi
# Handle revert
elif [[ "$action" = "-" ]]; then
Migrate_rollback "$number"
if [[ $? -ne $Ash__TRUE ]]; then
Migrate_shutdown
return $Ash__FALSE
fi
fi
# Shutdown
Migrate_shutdown
}