/
kmedia.go
263 lines (231 loc) · 10.2 KB
/
kmedia.go
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
package cmd
import (
"database/sql"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/Bnei-Baruch/mdb/importer/kmedia"
)
func init() {
command := &cobra.Command{
Use: "kmedia-lessons",
Short: "Import kmedia VirtualLessons to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportVirtualLessons()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-congresses",
Short: "Import KMedia Congresses to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportCongresses()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-programs",
Short: "Import KMedia Programs to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportProgramsChapters()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-lectures",
Short: "Import KMedia Lectures Series to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportLectures()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-holidays",
Short: "Import KMedia Holidays to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportHolidays()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-vls",
Short: "Import KMedia VLs to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportVLs()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-clips",
Short: "Import KMedia Clips to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportClips()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-flat",
Short: "Import KMedia flat catalogs to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportFlatCatalogs()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-articles",
Short: "Import KMedia articles to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportArticles()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-custom",
Short: "Import KMedia custom containers to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportCustom()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-mixed-lessons",
Short: "Import KMedia mixed lessons catalogs to MDB",
Run: func(cmd *cobra.Command, args []string) {
kmedia.ImportMixedLessons()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-map-units",
Short: "Do unit mappings analysis",
Run: func(cmd *cobra.Command, args []string) {
kmedia.MapUnits()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-update",
Short: "Update i18ns for collections and content units based on kmedia_id",
Run: func(cmd *cobra.Command, args []string) {
kmedia.UpdateI18ns()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-compare",
Short: "Compare collections and content units to their equivalent counterparts in kmedia",
Run: func(cmd *cobra.Command, args []string) {
kmedia.Compare()
},
}
RootCmd.AddCommand(command)
command = &cobra.Command{
Use: "kmedia-fkeys",
Short: "Add foreign keys to kmedia",
Long: "Add foreign keys to kmedia, then run:\n\tsqlboiler -o gmodels_old -p gmodels --no-hooks postgres",
Run: func(cmd *cobra.Command, args []string) {
createForeignKeys()
},
}
RootCmd.AddCommand(command)
}
// go run main.go kmedia-fkeys
// sqlboiler -o gmodels_old -p gmodels --no-hooks --no-tests postgres
func createForeignKeys() {
db, err := sql.Open("postgres", viper.GetString("kmedia.url"))
if err != nil {
panic(err)
}
defer db.Close()
_, err = db.Exec(`
ALTER TABLE languages DROP CONSTRAINT IF EXISTS code3_unique CASCADE;
ALTER TABLE languages ADD CONSTRAINT code3_unique UNIQUE (code3);
ALTER TABLE catalogs DROP CONSTRAINT IF EXISTS catalogs_fkey;
ALTER TABLE catalogs ADD CONSTRAINT catalogs_fkey FOREIGN KEY (parent_id) REFERENCES catalogs(id) NOT VALID;
ALTER TABLE catalogs DROP CONSTRAINT IF EXISTS users_fkey;
ALTER TABLE catalogs ADD CONSTRAINT users_fkey FOREIGN KEY (user_id) REFERENCES users(id) NOT VALID;
ALTER TABLE catalogs_containers DROP CONSTRAINT IF EXISTS catalogs_fkey;
ALTER TABLE catalogs_containers ADD CONSTRAINT catalogs_fkey FOREIGN KEY (catalog_id) REFERENCES catalogs(id) NOT VALID;
ALTER TABLE catalogs_containers DROP CONSTRAINT IF EXISTS containers_fkey;
ALTER TABLE catalogs_containers ADD CONSTRAINT containers_fkey FOREIGN KEY (container_id) REFERENCES containers(id) NOT VALID;
DO $$
BEGIN
ALTER TABLE catalog_descriptions RENAME COLUMN lang TO lang_id;
EXCEPTION WHEN OTHERS THEN RAISE NOTICE '%', 'Already Exists';
END
$$ LANGUAGE plpgsql;
ALTER TABLE catalog_descriptions DROP CONSTRAINT IF EXISTS catalogs_fkey;
ALTER TABLE catalog_descriptions ADD CONSTRAINT catalogs_fkey FOREIGN KEY (catalog_id) REFERENCES catalogs(id) NOT VALID;
ALTER TABLE catalog_descriptions DROP CONSTRAINT IF EXISTS languages_fkey;
ALTER TABLE catalog_descriptions ADD CONSTRAINT languages_fkey FOREIGN KEY (lang_id) REFERENCES languages(code3) NOT VALID;
SELECT DISTINCT * INTO tmp FROM catalogs_container_description_patterns; DROP TABLE catalogs_container_description_patterns; SELECT * INTO catalogs_container_description_patterns FROM tmp; DROP TABLE tmp;
ALTER TABLE catalogs_container_description_patterns DROP CONSTRAINT IF EXISTS catalog_container_description_pattern_pkey;
ALTER TABLE catalogs_container_description_patterns ADD CONSTRAINT catalog_container_description_pattern_pkey PRIMARY KEY (catalog_id, container_description_pattern_id);
ALTER TABLE catalogs_container_description_patterns DROP CONSTRAINT IF EXISTS catalogs_fkey;
ALTER TABLE catalogs_container_description_patterns ADD CONSTRAINT catalogs_fkey FOREIGN KEY (catalog_id) REFERENCES catalogs(id) NOT VALID;
ALTER TABLE catalogs_container_description_patterns DROP CONSTRAINT IF EXISTS container_description_patterns_fkey;
ALTER TABLE catalogs_container_description_patterns ADD CONSTRAINT container_description_patterns_fkey FOREIGN KEY (container_description_pattern_id) REFERENCES container_description_patterns(id) NOT VALID;
DO $$
BEGIN
ALTER TABLE containers RENAME COLUMN lang TO lang_id;
EXCEPTION WHEN OTHERS THEN RAISE NOTICE '%', 'Already Exists';
END
$$ LANGUAGE plpgsql;
ALTER TABLE containers DROP CONSTRAINT IF EXISTS languages_fkey;
ALTER TABLE containers ADD CONSTRAINT languages_fkey FOREIGN KEY (lang_id) REFERENCES languages(code3) NOT VALID;
ALTER TABLE containers DROP CONSTRAINT IF EXISTS content_types_fkey;
ALTER TABLE containers ADD CONSTRAINT content_types_fkey FOREIGN KEY (content_type_id) REFERENCES content_types(id) NOT VALID;
ALTER TABLE containers DROP CONSTRAINT IF EXISTS virtual_lessons_fkey;
ALTER TABLE containers ADD CONSTRAINT virtual_lessons_fkey FOREIGN KEY (virtual_lesson_id) REFERENCES virtual_lessons(id) NOT VALID;
DO $$
BEGIN
ALTER TABLE container_descriptions RENAME COLUMN lang TO lang_id;
EXCEPTION WHEN OTHERS THEN RAISE NOTICE '%', 'Already Exists';
END
$$ LANGUAGE plpgsql;
ALTER TABLE container_descriptions DROP CONSTRAINT IF EXISTS languages_fkey;
ALTER TABLE container_descriptions ADD CONSTRAINT languages_fkey FOREIGN KEY (lang_id) REFERENCES languages(code3) NOT VALID;
ALTER TABLE container_descriptions DROP CONSTRAINT IF EXISTS container_descriptions_fkey;
ALTER TABLE container_descriptions ADD CONSTRAINT container_descriptions_fkey FOREIGN KEY (container_id) REFERENCES containers(id) NOT VALID;
ALTER TABLE containers_file_assets DROP CONSTRAINT IF EXISTS lessonfiles_pkey;
ALTER TABLE containers_file_assets DROP CONSTRAINT IF EXISTS containers_file_assets_pkey;
ALTER TABLE containers_file_assets ADD CONSTRAINT containers_file_assets_pkey PRIMARY KEY (container_id, file_asset_id);
ALTER TABLE containers_file_assets DROP CONSTRAINT IF EXISTS containers_fkey;
ALTER TABLE containers_file_assets ADD CONSTRAINT containers_fkey FOREIGN KEY (container_id) REFERENCES containers(id) NOT VALID;
ALTER TABLE containers_file_assets DROP CONSTRAINT IF EXISTS file_assets_fkey;
ALTER TABLE containers_file_assets ADD CONSTRAINT file_assets_fkey FOREIGN KEY (file_asset_id) REFERENCES file_assets(id) NOT VALID;
ALTER TABLE containers_labels DROP CONSTRAINT IF EXISTS container_label_pkey;
ALTER TABLE containers_labels ADD CONSTRAINT container_label_pkey PRIMARY KEY (label_id, container_id);
ALTER TABLE containers_labels DROP CONSTRAINT IF EXISTS containers_fkey;
ALTER TABLE containers_labels ADD CONSTRAINT containers_fkey FOREIGN KEY (container_id) REFERENCES containers(id) NOT VALID;
ALTER TABLE containers_labels DROP CONSTRAINT IF EXISTS labels_fkey;
ALTER TABLE containers_labels ADD CONSTRAINT labels_fkey FOREIGN KEY (label_id) REFERENCES labels(id) NOT VALID;
ALTER TABLE file_asset_descriptions DROP CONSTRAINT IF EXISTS file_asset_descriptions_fkey;
ALTER TABLE file_asset_descriptions ADD CONSTRAINT file_asset_descriptions_fkey FOREIGN KEY (file_id) REFERENCES file_assets(id) NOT VALID;
DO $$
BEGIN
ALTER TABLE file_assets RENAME COLUMN lang TO lang_id;
EXCEPTION WHEN OTHERS THEN RAISE NOTICE '%', 'Already Exists';
END
$$ LANGUAGE plpgsql;
DO $$
BEGIN
ALTER TABLE file_assets RENAME COLUMN servername TO servername_id;
EXCEPTION WHEN OTHERS THEN RAISE NOTICE '%', 'Already Exists';
END
$$ LANGUAGE plpgsql;
ALTER TABLE file_assets DROP CONSTRAINT IF EXISTS languages_fkey;
ALTER TABLE file_assets ADD CONSTRAINT languages_fkey FOREIGN KEY (lang_id) REFERENCES languages(code3) NOT VALID;
ALTER TABLE file_assets DROP CONSTRAINT IF EXISTS users_fkey;
ALTER TABLE file_assets ADD CONSTRAINT users_fkey FOREIGN KEY (user_id) REFERENCES users(id) NOT VALID;
ALTER TABLE file_assets DROP CONSTRAINT IF EXISTS servers_fkey;
ALTER TABLE file_assets ADD CONSTRAINT servers_fkey FOREIGN KEY (servername_id) REFERENCES servers(servername) NOT VALID;
SELECT DISTINCT * INTO ru FROM roles_users; DROP TABLE roles_users; SELECT * INTO roles_users FROM ru; DROP TABLE ru;
ALTER TABLE roles_users DROP CONSTRAINT IF EXISTS role_user_pkey;
ALTER TABLE roles_users ADD CONSTRAINT role_user_pkey PRIMARY KEY (role_id, user_id);
ALTER TABLE roles_users DROP CONSTRAINT IF EXISTS roles_fkey;
ALTER TABLE roles_users ADD CONSTRAINT users_fkey FOREIGN KEY (user_id) REFERENCES users(id) NOT VALID;
`)
if err != nil {
panic(err)
}
}