15
15
namespace PKP \migration \upgrade \v3_5_0 ;
16
16
17
17
use Illuminate \Database \Schema \Blueprint ;
18
- use Illuminate \Support \Facades \Schema as Schema ;
18
+ use Illuminate \Support \Facades \DB ;
19
+ use Illuminate \Support \Facades \Schema ;
19
20
use PKP \install \DowngradeNotSupportedException ;
20
21
use PKP \migration \Migration ;
21
22
@@ -26,15 +27,19 @@ class I10692_CitationMigration extends Migration
26
27
*/
27
28
public function up (): void
28
29
{
30
+ $ this ->migrateRawCitations ();
31
+
32
+ if (Schema::hasColumn ('citations ' , 'raw_citation ' )) {
33
+ Schema::table ('citations ' , function (Blueprint $ table ) {
34
+ $ table ->dropColumn ('raw_citation ' );
35
+ });
36
+ }
37
+
29
38
if (Schema::hasColumn ('citation_settings ' , 'setting_type ' )) {
30
39
Schema::table ('citation_settings ' , function (Blueprint $ table ) {
31
40
$ table ->dropColumn ('setting_type ' );
32
41
});
33
42
}
34
-
35
- Schema::table ('citations ' , function (Blueprint $ table ) {
36
- $ table ->text ('raw_citation ' )->nullable ()->default (null )->change ();
37
- });
38
43
}
39
44
40
45
/**
@@ -46,4 +51,31 @@ public function down(): void
46
51
{
47
52
throw new DowngradeNotSupportedException ();
48
53
}
54
+
55
+ /**
56
+ * Migrates raw_citations from citations to citation_settings table.
57
+ * This is needed because of multi-language support.
58
+ */
59
+ public function migrateRawCitations (): void
60
+ {
61
+ $ values = DB ::table ('citations as c ' )
62
+ ->select (
63
+ 'c.citation_id ' ,
64
+ 's.locale ' ,
65
+ DB ::raw ("'raw_citation' as setting_name " ),
66
+ DB ::raw ("c.raw_citation as setting_value " )
67
+ )
68
+ ->distinct ()
69
+ ->join ('publications as p ' , 'c.publication_id ' , '= ' , 'p.publication_id ' )
70
+ ->join ('submissions as s ' , 'p.submission_id ' , '= ' , 's.submission_id ' )
71
+ ->get ()
72
+ ->map (function ($ item ) {
73
+ return (array )$ item ;
74
+ })
75
+ ->all ();
76
+
77
+ DB ::table ('citation_settings ' )->upsert ($ values ,
78
+ ['citation_id ' , 'locale ' , 'setting_name ' ],
79
+ ['setting_value ' ]);
80
+ }
49
81
}
0 commit comments