Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 94632453

Von G. Richardson vor fast 15 Jahren hinzugefügt

  • ID 946324538fb1706eb9f3ff7f98ecad8e27710c95
  • Vorgänger 1c603341
  • Nachfolger 5b53af15

migration from cp_greeting to cp_gender

this commit replaces commit e09347c89ca119213c4d8ba43083653cda793399 which introduced gender.sql

In database table contacts cp_greeting is removed and instead cp_gender is introduced
For print variables cp_greeting is generated from cp_gender and generic_translations

During migration user gets a chance to
  • set gender of contacts where gender couldn't be easily inferred from cp_greeting
  • set default greeting (Herr/Frau) for male and female
  • transfer any data in cp_greeting that is not gender-related to cp_title

gender.sql is deleted, as there was a risk of losing data in cp_title

Migration code was originally copied from warehouse code

Unterschiede anzeigen:

sql/Pg-upgrade2/cp_greeting_migration.pl
1
# @tag: cp_greeting_migration
2
# @description: Migration of cp_greeting to cp_gender
3
# @depends: generic_translations
4

  
5
die("This script cannot be run from the command line.") unless ($main::form);
6

  
7

  
8
sub mydberror {
9
  my ($msg) = @_;
10
  die($dbup_locale->text("Database update error:") .
11
      "<br>$msg<br>" . $DBI::errstr);
12
}
13

  
14
sub do_query {
15
  my ($query, $may_fail) = @_;
16

  
17
  if (!$dbh->do($query)) {
18
    mydberror($query) unless ($may_fail);
19
    $dbh->rollback();
20
    $dbh->begin_work();
21
  }
22
}
23

  
24

  
25
sub query_result {
26

  
27
  # list of all entries where cp_greeting is empty, meaning can't determine gender from parsing Herr/Frau/...
28
  # this assumes cp_greeting still exists, i.e. gender.sql was not run yet
29
  my $gender_table;
30

  
31
  my $sql2 = "select cp_id,cp_givenname,cp_name,cp_title,cp_greeting from contacts where not (cp_greeting ILIKE '%frau%' OR cp_greeting ILIKE '%herr%' or cp_greeting ILIKE '%mrs.%' or cp_greeting ILIKE '%miss%') ";
32
  my $sth2 = $dbh->prepare($sql2) or die $dbh->errstr();
33
  $sth2->execute() or die $dbh->errstr(); 
34

  
35
  my $i = 1;   
36
  $gender_table .= '<table border="1"><tr><th>cp_givenname</th><th>cp_name</th><th>cp_title</th><th>cp_greeting</th><th><translate>male/female</th></tr>';
37
  $gender_table .= "\n";
38

  
39
  while (my $row = $sth2->fetchrow_hashref()) {
40
    if ( main::form->{"gender_$i"} eq "f" ) {
41
			$mchecked = "";
42
			$fchecked = "checked";
43
	} else {
44
			$mchecked = "checked";
45
			$fchecked = "";
46
	};
47
    
48
   $gender_table .= "<tr><input type=hidden name=\"cp_id_$i\" value=\"$row->{cp_id}\"> <td>$row->{cp_givenname}</td> <td>$row->{cp_name}</td> <td>$row->{cp_title} </td> <td>$row->{cp_greeting} </td><td> <input type=\"radio\" name=\"gender_$i\" value=\"m\" $mchecked> <input type=\"radio\" name=\"gender_$i\" value=\"f\" $fchecked></td></tr>\n";
49
   $i++;
50
  };
51

  
52
  $gender_table .= "<input type=hidden name=\"number_of_gender_entries\" value=\"$i\">"; 
53
  $gender_table .= "</table>";
54

  
55
  $main::form->{gender_table} = $gender_table;
56

  
57
  my $title_table;
58
  
59
  my $sql3 = "select cp_id,cp_givenname,cp_name,cp_title,cp_greeting from contacts where not ( (cp_greeting ILIKE '%frau%' OR cp_greeting ILIKE '%herr%' or cp_greeting ILIKE '%mrs.%' or cp_greeting ILIKE '%miss%')) and not (cp_greeting like ''); ";
60

  
61
  my $sth3 = $dbh->prepare($sql3) or die $dbh->errstr();
62
  $sth3->execute() or die $dbh->errstr(); 
63

  
64
  $title_table = '<table border="1"><tr><th>cp_givenname</th><th>cp_name</th><th>cp_title</th><th>cp_greeting</th><th>cp_title new</th></tr>';
65

  
66
  my $j = 1;
67
  while (my $row = $sth3->fetchrow_hashref()) {
68
# Vorschlagsfeld fuer neuen Titel mit Werten von cp_greeting und cp_title vorbelegen
69
		  my $value = "$row->{cp_greeting}";
70
		  $value .= " " if $row->{cp_greeting};
71
		  $value .= "$row->{cp_title}";
72

  
73
		  $title_table .= "<tr> <td><input type=hidden name=\"cp_id_title_$j\" value=$row->{cp_id}> $row->{cp_givenname}</td> <td>$row->{cp_name}</td><td>$row->{cp_title}</td> <td>$row->{cp_greeting}</td><td><input type=\"text\" id=\"cp_title_$j\" name=\"cp_name_$j\" value=\"$value\"></td> </tr>\n";
74
		  $j++;
75
  };
76

  
77
  $title_table .= "<input type=hidden name=\"number_of_title_entries\" value=\"$j\">"; 
78
  $title_table .= "</table>";
79
  $main::form->{title_table} = $title_table;
80

  
81
};
82

  
83

  
84

  
85
sub print_question {
86
  query_result();
87
  # parse html form in /templates/webpages/dbupgrade/cp_greeting_update_form
88
  print $main::form->parse_html_template("dbupgrade/cp_greeting_update_form");
89
}
90

  
91
sub do_update {
92
  # main function
93

  
94
  # first of all check if gender.sql was already run and thus cp_gender exists
95
  # if it exists there is no need for this update anymore, so return
96
  # without doing anything
97

  
98
  my $column_exists = 1;
99
  if (!$dbh->do("SELECT cp_gender FROM contacts LIMIT 1")) {
100
    $dbh->rollback();
101
    $dbh->begin_work();
102
    $column_exists = 0;
103
  }
104
  return 1 if $column_exists;
105

  
106

  
107
  if (!$main::form->{do_migrate}) {
108
    # case 1: first call of page
109
    set_default_greetings();
110
    print_question();
111
    return 2;
112
  } else {
113
    # case 2: submit button was pressed, hidden field do_migrate was set
114
    migrate_data();
115
  };
116

  
117
return 1;
118

  
119
}
120

  
121
sub migrate_data {
122

  
123
  my $sqlcode = <<EOF
124
ALTER TABLE contacts ADD COLUMN cp_gender char(1);
125
UPDATE contacts SET cp_gender = 'm';
126
UPDATE contacts SET cp_gender = 'f'
127
  WHERE (cp_greeting ILIKE '%frau%')
128
     OR (cp_greeting ILIKE '%mrs.%')
129
     OR (cp_greeting ILIKE '%miss%');
130
EOF
131
;
132

  
133
  for (my $i = 1; $i <= $main::form->{number_of_gender_entries}; $i++ ) {
134
    next unless $main::form->{"cp_id_$i"};
135
	if ( $main::form->{"gender_$i"} eq "f" ) {
136
	  $sqlcode .= "UPDATE contacts SET cp_gender = \'f\' WHERE cp_id = $main::form->{\"cp_id_$i\"};\n";
137
    };
138
  };
139

  
140
  for (my $i = 1; $i <= $main::form->{number_of_title_entries}; $i++ ) {
141
    next unless $main::form->{"cp_id_title_$i"} and $main::form->{"cp_id_$i"};
142
	$sqlcode .= "UPDATE contacts SET cp_title = \'$main::form->{\"cp_name_$i\"}\' WHERE cp_id = $main::form->{\"cp_id_$i\"};\n";
143
  };
144
  $sqlcode .= "ALTER TABLE contacts DROP COLUMN cp_greeting;";
145

  
146
  # insert chosen default values
147
  $sqlcode .= "INSERT INTO generic_translations (translation_type, translation) VALUES ('greetings::male','$main::form->{default_male}');";
148
  $sqlcode .= "INSERT INTO generic_translations (translation_type, translation) VALUES ('greetings::female','$main::form->{default_female}');";
149

  
150
  my $query  = $sqlcode;
151
  do_query($query);
152
};
153

  
154

  
155

  
156
sub set_default_greetings {
157
  # add html input boxes to template so user can specify default greetings
158

  
159
   my $default_male = "Herr";
160
   my $default_female = "Frau";
161

  
162
   my $default_greeting_text_male = "<input type=\"text\" id=\"default_male\" name=\"default_male\" value=\"$default_male\"><br>";
163
   my $default_greeting_text_female = "<input type=\"text\" id=\"default_female\" name=\"default_female\" value=\"$default_female\"><br>";
164
   $main::form->{default_greeting_text_male} = $default_greeting_text_male;
165
   $main::form->{default_greeting_text_female} = $default_greeting_text_female;
166
};
167

  
168

  
169
return do_update();
170

  
sql/Pg-upgrade2/gender.sql
1
-- @tag: gender
2
-- @description: Feld "Geschlecht" zu Kontaktdaten hinzufuegen, cp_greeting entferne
3
-- @depends: release_2_6_0
4

  
5
ALTER TABLE contacts ADD COLUMN cp_gender char(1);
6
UPDATE contacts SET cp_gender = 'm';
7
UPDATE contacts SET cp_gender = 'f'
8
  WHERE (cp_greeting ILIKE '%frau%')
9
     OR (cp_greeting ILIKE '%mrs.%')
10
     OR (cp_greeting ILIKE '%miss%');
11

  
12
UPDATE contacts SET cp_title = cp_greeting WHERE NOT (cp_greeting ILIKE '%frau%' OR cp_greeting ILIKE '%herr%' or cp_greeting ILIKE '%mrs.%' or cp_greeting ILIKE '%miss%');
13

  
14
ALtER TABLE contacts DROP COLUMN cp_greeting;
templates/webpages/dbupgrade/cp_greeting_update_form_de.html
1
[% USE HTML %]<div class="listtop">cp_greeting to cp_gender migration</div>
2

  
3

  
4
<form action="[% script %]" method="POST">
5
<br>
6
<p> The method for generating greetings ("Dear Mr. Smith") is being changed so the greeting is controlled by the gender. For this a new database field cp_gender is introduced, while the old database field cp_greeting will be removed. The various translations (Mr./Herr/Monsieur) will be stored in the database, under generic_translations. The print variable cp_greeting still exists, however, so print templates don't have to be changed, and the variable is assigned according to the gender of the contact person and the desired language.</p>
7

  
8
<p>During the conversion process all contact persons need to be assigned a gender. The gender is detected automatically where possible, but in some cases will have to be set manually.</p>
9

  
10
During the conversion process:<br>
11
1. All contact persons will have their gender set to "m" (male) as a default<br>
12
2. All contact persons where the greeting contains "frau,mrs,miss" are set to "f" (female)<br>
13

  
14
<p>The gender of the following contact persons couldn't be determined automatically from the greeting, please set the gender manually:</p>
15
[% gender_table %]
16

  
17

  
18
<p>Please enter your desired default greeting for men and women. To change the
19
default greeting or add a greeting in more languages, go to: System -> Languages -> Greetings
20
</p>
21

  
22
<p>
23
Greeting for men: [% default_greeting_text_male %] <br>
24
Greeting for women: [% default_greeting_text_female %] <br>
25
</p>
26

  
27
<br><br>
28
The data from the cp_greeting field will now be dropped. To prevent any data loss if you used the cp_greeting field for anything else, we suggest you transfer this information to the cp_title field:
29
<br>
30
[% title_table %]
31

  
32
<input type=hidden name="do_migrate" value="1"> 
33
<input type=submit VALUE="submit">
34

  
35
</form>
36

  
templates/webpages/dbupgrade/cp_greeting_update_form_master.html
1
[% USE HTML %]<div class="listtop"><translate>cp_greeting to cp_gender migration</translate></div>
2

  
3

  
4
<form action="[% script %]" method="POST">
5
<br>
6
<p> The method for generating greetings ("Dear Mr. Smith") is being changed so the greeting is controlled by the gender. For this a new database field cp_gender is introduced, while the old database field cp_greeting will be removed. The various translations (Mr./Herr/Monsieur) will be stored in the database, under generic_translations. The print variable cp_greeting still exists, however, so print templates don't have to be changed, and the variable is assigned according to the gender of the contact person and the desired language.</p>
7

  
8
<p>During the conversion process all contact persons need to be assigned a gender. The gender is detected automatically where possible, but in some cases will have to be set manually.</p>
9

  
10
During the conversion process:<br>
11
1. All contact persons will have their gender set to "m" (male) as a default<br>
12
2. All contact persons where the greeting contains "frau,mrs,miss" are set to "f" (female)<br>
13

  
14
<p>The gender of the following contact persons couldn't be determined automatically from the greeting, please set the gender manually:</p>
15
[% gender_table %]
16

  
17

  
18
<p>Please enter your desired default greeting for men and women. To change the
19
default greeting or add a greeting in more languages, go to: System -> Languages -> Greetings
20
</p>
21

  
22
<p>
23
Greeting for men: [% default_greeting_text_male %] <br>
24
Greeting for women: [% default_greeting_text_female %] <br>
25
</p>
26

  
27
<br><br>
28
The data from the cp_greeting field will now be dropped. To prevent any data loss if you used the cp_greeting field for anything else, we suggest you transfer this information to the cp_title field:
29
<br>
30
[% title_table %]
31

  
32
<input type=hidden name="do_migrate" value="1"> 
33
<input type=submit VALUE="submit">
34

  
35
</form>
36

  

Auch abrufbar als: Unified diff