summaryrefslogtreecommitdiff
path: root/util/sqleditfunc.pl
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2019-07-15 09:20:14 +0200
committerYorhel <git@yorhel.nl>2019-07-21 14:55:51 +0200
commit8aacb0a6fc3de26d8718759152c80f00f2ef972e (patch)
tree8325ab98429451182baff8e8a087bc3df57dcef8 /util/sqleditfunc.pl
parent30c77700970890ea61053d67743f0b10fcf6150f (diff)
Add (fullish) database dumps
Diffstat (limited to 'util/sqleditfunc.pl')
-rwxr-xr-xutil/sqleditfunc.pl45
1 files changed, 16 insertions, 29 deletions
diff --git a/util/sqleditfunc.pl b/util/sqleditfunc.pl
index 773214f9..0d1749a2 100755
--- a/util/sqleditfunc.pl
+++ b/util/sqleditfunc.pl
@@ -8,66 +8,53 @@ use Cwd 'abs_path';
our $ROOT;
BEGIN { ($ROOT = abs_path $0) =~ s{/util/sqleditfunc\.pl$}{}; }
-my %tabletypes; # table_name => dbentry_type
-my %tables; # table_name => [ column_names ]
-my %items; # item_name => { tables_without_hist => [ data_column_names ] }
-
-
-# Fills %tables
-sub readschema {
- open my $F, '<', "$ROOT/util/sql/schema.sql" or die $!;
- my $table = '';
- while(<$F>) {
- chomp;
- if(/^\s*CREATE\s+TABLE\s+([^ ]+)/) {
- $table = $1;
- $tables{$table} = [];
- $tabletypes{$table} = $1 if /--.*\s+dbentry_type=(.)/;
- } elsif($table && /^\s+("?[^\( ]+"?)\s/ && !/^\s+PRIMARY\s+KEY/) {
- push @{$tables{$table}}, $1;
- }
- }
-}
+use lib "$ROOT/lib";
+use VNDBSchema;
+my $schema = VNDBSchema::schema("$ROOT/util/sql/schema.sql");
+my $template = join '', <DATA>;
sub gensql {
- my($template, $item) = @_;
+ my $item = shift;
# table_name_without_hist => [ column_names_without_chid ]
- my %ts = map +($_, [ grep !/^chid$/, @{$tables{"${_}_hist"}} ]), map /^${item}_/ && /^(.+)_hist$/ ? $1 : (), keys %tables;
+ my %ts = map
+ +($_, [ map "\"$_->{name}\"", grep $_->{name} !~ /^chid$/, @{$schema->{"${_}_hist"}{cols}} ]),
+ map /^${item}_/ && /^(.+)_hist$/ ? $1 : (), keys %$schema;
+
+ my %replace = ( item => $item, itemtype => $schema->{$item}{dbentry_type} );
- my %replace = ( item => $item, itemtype => $tabletypes{$item} );
$replace{createtemptables} = join "\n", map sprintf(
" CREATE TEMPORARY TABLE edit_%s (LIKE %s INCLUDING DEFAULTS INCLUDING CONSTRAINTS);\n".
" ALTER TABLE edit_%1\$s DROP COLUMN %s;",
$_, $_ eq 'staff_alias' ? ($_, 'id') : ("${_}_hist", 'chid') # staff_alias copies from the non-_hist table, because it needs the sequence
), sort keys %ts;
+
$replace{temptablenames} = join ', ', map "edit_$_", sort keys %ts;
+
$replace{loadtemptables} = join "\n", map sprintf(
" INSERT INTO edit_%s (%s) SELECT %2\$s FROM %1\$s_hist WHERE chid = xchid;",
$_, join ', ', @{$ts{$_}}), sort keys %ts;
+
$replace{copyfromtemp} = join "\n", map sprintf(
" DELETE FROM %1\$s WHERE id = r.itemid;\n".
" INSERT INTO %1\$s (id, %2\$s) SELECT r.itemid, %2\$s FROM edit_%1\$s;\n".
" INSERT INTO %1\$s_hist (chid, %2\$s) SELECT r.chid, %2\$s FROM edit_%1\$s;",
$_, join ', ', @{$ts{$_}}), grep $_ ne $item, sort keys %ts;
+
$replace{copymainfromtemp} = sprintf
" INSERT INTO %1\$s_hist (chid, %2\$s) SELECT r.chid, %2\$s FROM edit_%1\$s;\n".
" UPDATE %1\$s SET locked = (SELECT ilock FROM edit_revision), hidden = (SELECT ihid FROM edit_revision),\n".
" %3\$s FROM edit_%1\$s x WHERE id = r.itemid;",
$item, join(', ', @{$ts{$item}}), join(', ', map "$_ = x.$_", @{$ts{$item}});
- $template =~ s/{([a-z]+)}/$replace{$1}/g;
- $template;
+ $template =~ s/{([a-z]+)}/$replace{$1}/gr;
}
-readschema;
-my $template = join '', <DATA>;
-
open my $F, '>', "$ROOT/util/sql/editfunc.sql" or die $!;
print $F "-- Automatically generated by util/sqleditfunc.pl. DO NOT EDIT.\n";
-print $F gensql $template, $_ for sort keys %tabletypes;
+print $F gensql $_ for sort grep $schema->{$_}{dbentry_type}, keys %$schema;
__DATA__