([\w]+))/i){
38
|
1
|
|
|
1
|
|
404
|
push(@table,$+{'table'}); |
|
1
|
|
|
|
|
297
|
|
|
1
|
|
|
|
|
971
|
|
|
0
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
} |
40
|
0
|
|
|
|
|
|
push(@version,$id); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
0
|
|
|
|
if(defined $action && $action eq 'delete'){ |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
44
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do("SET FOREIGN_KEY_CHECKS = 0;"); |
45
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do("DROP TABLE IF EXISTS `$_`;") for (@table); |
46
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do("DROP TABLE IF EXISTS `_version`;"); |
47
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do("SET FOREIGN_KEY_CHECKS = 1;"); |
48
|
0
|
|
|
|
|
|
$self->app->mysql->db->commit; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
elsif(defined $action && $action eq 'create'){ |
51
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do('CREATE TABLE IF NOT EXISTS `_version` (`version_id` int unsigned NOT NULL, `date` datetime NOT NULL, PRIMARY KEY (`version_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'); |
52
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do('INSERT INTO `_version` (`version_id`,`date`) VALUES(0,UTC_TIMESTAMP());'); |
53
|
0
|
|
|
|
|
|
$self->app->mysql->db->commit; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
elsif(defined $action && $action eq 'update'){ |
56
|
0
|
|
|
|
|
|
my $collection = $self->app->mysql->query('SELECT `version_id` FROM `_version` ORDER BY `version_id` DESC LIMIT 1;'); |
57
|
0
|
|
|
|
|
|
my $version_id = $collection->last->{'version_id'}; |
58
|
0
|
|
|
|
|
|
say colored ['bright_yellow'],'current sql version:'.$version_id; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
for my $file_version (sort {$a <=> $b} @version){ |
|
0
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
if($file_version > int $version_id){ |
62
|
0
|
|
|
|
|
|
my $sql = $all->{$file_version}; |
63
|
0
|
0
|
|
|
|
|
if(defined $sql){ |
64
|
0
|
|
|
|
|
|
$sql =~ s/\t//g; |
65
|
0
|
|
|
|
|
|
$sql =~ s/\n//g; |
66
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do($sql); |
67
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do("INSERT INTO `_version` (`version_id`,`date`) VALUES($file_version,UTC_TIMESTAMP());"); |
68
|
0
|
|
|
|
|
|
say colored ['bright_green'],'update sql version:'.$file_version; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
else { |
72
|
0
|
|
|
|
|
|
say colored ['bright_red'],'skip sql version:'.$file_version; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
else{ |
77
|
0
|
|
|
|
|
|
say $self->usage; |
78
|
|
|
|
|
|
|
} |
79
|
0
|
|
|
|
|
|
$self->app->mysql->db->commit; |
80
|
0
|
|
|
|
|
|
return $self; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub run1 { |
86
|
0
|
|
|
0
|
0
|
|
my ($self, $action, $id) = @_; |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
$id = '_default' if(!defined $id); |
89
|
0
|
|
|
|
|
|
my $migration_object = $self->app->mysql->{'migration'}->{$id}; |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my $e = load_class $migration_object; |
92
|
0
|
0
|
|
|
|
|
warn qq{Loading "$migration_object" failed: $e} if ref $e; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
my $all = data_section($migration_object); |
95
|
0
|
|
|
|
|
|
my @table = (); |
96
|
0
|
|
|
|
|
|
my @version = (); |
97
|
0
|
|
|
|
|
|
while(my ($id,$sql) = each(%{$all})){ |
|
0
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
$sql =~ s/\t//g; |
99
|
0
|
|
|
|
|
|
$sql =~ s/\n//g; |
100
|
0
|
|
|
|
|
|
$sql =~ s/`//g; |
101
|
0
|
0
|
0
|
|
|
|
if($sql =~ m/^CREATE\s+TABLE\sIF\sNOT\sEXISTS\s(?([\w]+))/i || $sql =~ m/^CREATE\s+TABLE\s(?([\w]+))/i){
102
|
0
|
|
|
|
|
|
push(@table,$+{'table'}); |
103
|
|
|
|
|
|
|
} |
104
|
0
|
|
|
|
|
|
push(@version,$id); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
0
|
|
|
|
if(defined $action && $action eq 'delete'){ |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
108
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do("SET FOREIGN_KEY_CHECKS = 0;"); |
109
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do("DROP TABLE IF EXISTS `$_`;") for (@table); |
110
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do("DROP TABLE IF EXISTS `_version`;"); |
111
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do("SET FOREIGN_KEY_CHECKS = 1;"); |
112
|
0
|
|
|
|
|
|
$self->app->mysql->db->commit; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
elsif(defined $action && $action eq 'create'){ |
115
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do('CREATE TABLE IF NOT EXISTS `_version` (`version_id` int unsigned NOT NULL, `date` datetime NOT NULL, PRIMARY KEY (`version_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'); |
116
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do('INSERT INTO `_version` (`version_id`,`date`) VALUES(0,UTC_TIMESTAMP());'); |
117
|
0
|
|
|
|
|
|
$self->app->mysql->db->commit; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
elsif(defined $action && $action eq 'update'){ |
120
|
0
|
|
|
|
|
|
my $collection = $self->app->mysql->query('SELECT `version_id` FROM `_version` ORDER BY `version_id` DESC LIMIT 1;'); |
121
|
0
|
|
|
|
|
|
my $version_id = $collection->last->{'version_id'}; |
122
|
0
|
|
|
|
|
|
say colored ['bright_yellow'],'current sql version:'.$version_id; |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
for my $file_version (sort {$a <=> $b} @version){ |
|
0
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
if($file_version > int $version_id){ |
126
|
0
|
|
|
|
|
|
my $sql = data_section($migration_object,$file_version); |
127
|
0
|
0
|
|
|
|
|
if(defined $sql){ |
128
|
0
|
|
|
|
|
|
$sql =~ s/\t//g; |
129
|
0
|
|
|
|
|
|
$sql =~ s/\n//g; |
130
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do($sql); |
131
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do("INSERT INTO `_version` (`version_id`,`date`) VALUES($file_version,UTC_TIMESTAMP());"); |
132
|
0
|
|
|
|
|
|
say colored ['bright_green'],'update sql version:'.$file_version; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
else { |
136
|
0
|
|
|
|
|
|
say colored ['bright_red'],'skip sql version:'.$file_version; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
elsif(defined $action && $action eq 'insert'){ |
141
|
0
|
|
|
|
|
|
my $insert_object = $self->app->mysql->{'insert'}->{$id}; |
142
|
0
|
|
|
|
|
|
my $e = load_class $insert_object; |
143
|
0
|
0
|
|
|
|
|
warn qq{Loading "$insert_object" failed: $e} if ref $e; |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
my @version = (); |
146
|
0
|
|
|
|
|
|
while(my ($id) = each(%{$all})){ |
|
0
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
push(@version,$id); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
for my $version (sort {$a <=> $b} @version){ |
|
0
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
my $sql = data_section($insert_object,$version); |
152
|
0
|
0
|
|
|
|
|
if(defined $sql){ |
153
|
0
|
|
|
|
|
|
$sql =~ s/\t//g; |
154
|
0
|
|
|
|
|
|
$sql =~ s/\n//g; |
155
|
0
|
|
|
|
|
|
$self->app->mysql->id($id)->do($sql); |
156
|
0
|
|
|
|
|
|
say colored ['bright_blue'],'insert sql version:'.$version; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
else{ |
162
|
0
|
|
|
|
|
|
say $self->usage; |
163
|
|
|
|
|
|
|
} |
164
|
0
|
|
|
|
|
|
$self->app->mysql->db->commit; |
165
|
0
|
|
|
|
|
|
return $self; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
1; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
__DATA__ |
|