| 1 | package CPAN::MetaPackager::Create; | ||
| 2 | |||
| 3 | use parent 'CPAN::MetaPackager::Database'; | ||
| 4 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | |||
| 8 | use Types::Standard qw/Object Str/; | ||
| 9 | |||
| 10 | our $VERSION = '1.05'; | ||
| 11 | |||
| 12 | # ----------------------------------------------- | ||
| 13 | |||
| 14 | sub create_all_tables | ||
| 15 | { | ||
| 16 | my($self) = @_; | ||
| 17 | |||
| 18 | $self -> init_config; | ||
| 19 | $self -> init_db; | ||
| 20 | $self -> logger -> info('Creating all tables'); | ||
| 21 | |||
| 22 | my($method); | ||
| 23 | my($result); | ||
| 24 | |||
| 25 | for my $table_name (@{$self -> table_names}) | ||
| 26 | { | ||
| 27 | $method = "create_${table_name}_table"; | ||
| 28 | |||
| 29 | $result = $self -> $method; | ||
| 30 | |||
| 31 | $self -> logger -> debug("Created table '$table_name'"); | ||
| 32 | } | ||
| 33 | |||
| 34 | $self -> logger -> info('Created all tables'); | ||
| 35 | $self -> logger -> info('-' x 50); | ||
| 36 | |||
| 37 | # Return 0 for OK and 1 for error. | ||
| 38 | |||
| 39 | return 0; | ||
| 40 | |||
| 41 | } # End of create_all_tables. | ||
| 42 | |||
| 43 | # -------------------------------------------------- | ||
| 44 | # In the log table: | ||
| 45 | # o action => 'add', 'delete', 'export', 'import', 'update'. | ||
| 46 | # o context => 'flower', etc. | ||
| 47 | # o key => Either 0 or a primary key associated with the context. | ||
| 48 | # o name => The name of the thing. | ||
| 49 | # o note => Any text. May contain other primary keys, e.g. when garden also has a property. | ||
| 50 | # o outcome => 'Success' or 'Error'. | ||
| 51 | |||
| 52 | sub create_log_table | ||
| 53 | { | ||
| 54 | my($self) = @_; | ||
| 55 | my($table_name) = 'log'; | ||
| 56 | my($engine) = $self -> engine; | ||
| 57 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 58 | my($time_option) = $self -> time_option; | ||
| 59 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 60 | create table $table_name | ||
| 61 | ( | ||
| 62 | id $primary_key, | ||
| 63 | action text not null, | ||
| 64 | context text not null, | ||
| 65 | file_name text not null, | ||
| 66 | key integer not null, | ||
| 67 | name text not null, | ||
| 68 | note text not null, | ||
| 69 | outcome text not null, | ||
| 70 | timestamp text $time_option not null default current_timestamp | ||
| 71 | ) strict $engine | ||
| 72 | SQL | ||
| 73 | |||
| 74 | return $result; | ||
| 75 | |||
| 76 | } # End of create_log_table. | ||
| 77 | |||
| 78 | # -------------------------------------------------- | ||
| 79 | |||
| 80 | sub create_packages_table | ||
| 81 | { | ||
| 82 | my($self) = @_; | ||
| 83 | my($table_name) = 'packages'; | ||
| 84 | my($engine) = $self -> engine; | ||
| 85 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 86 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 87 | create table $table_name | ||
| 88 | ( | ||
| 89 | id $primary_key, | ||
| 90 | author text not null, | ||
| 91 | name text not null, | ||
| 92 | version text not null | ||
| 93 | ) strict $engine | ||
| 94 | SQL | ||
| 95 | |||
| 96 | return $result; | ||
| 97 | |||
| 98 | } # End of create_packages_table. | ||
| 99 | |||
| 100 | # ----------------------------------------------- | ||
| 101 | |||
| 102 | sub drop_all_tables | ||
| 103 | { | ||
| 104 | my($self) = @_; | ||
| 105 | |||
| 106 | $self -> init_config; | ||
| 107 | $self -> init_db; | ||
| 108 | $self -> logger -> info('Dropping all tables'); | ||
| 109 | |||
| 110 | for my $table_name (reverse @{$self -> table_names}) | ||
| 111 | { | ||
| 112 | $self -> creator -> drop_table($table_name); | ||
| 113 | |||
| 114 | $self -> logger -> debug("Dropped table '$table_name'"); | ||
| 115 | } | ||
| 116 | |||
| 117 | $self -> logger -> info('Dropped all tables'); | ||
| 118 | $self -> logger -> info('-' x 50); | ||
| 119 | |||
| 120 | # Return 0 for OK and 1 for error. | ||
| 121 | |||
| 122 | return 0; | ||
| 123 | |||
| 124 | } # End of drop_all_tables. | ||
| 125 | |||
| 126 | # ----------------------------------------------- | ||
| 127 | |||
| 128 | 1; | ||
| 129 | |||
| 130 | =pod | ||
| 131 | |||
| 132 | =head1 NAME | ||
| 133 | |||
| 134 | CPAN::MetaPackager::Create - Manage the cpan.metapackager.sqlite database | ||
| 135 | |||
| 136 | =head1 Instructions | ||
| 137 | |||
| 138 | See POD in MetaPackager.pm. | ||
| 139 | |||
| 140 | =head1 Author | ||
| 141 | |||
| 142 | Ron Savage I<E<lt>ron@savage.net.auE<gt>>. | ||
| 143 | |||
| 144 | My homepage: L<https://savage.net.au/>. | ||
| 145 | |||
| 146 | =head1 License | ||
| 147 | |||
| 148 | Perl 5. | ||
| 149 | |||
| 150 | =head1 Copyright | ||
| 151 | |||
| 152 | Copyright (c) 2026, Ron Savage. | ||
| 153 | |||
| 154 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 155 | you can redistribute them and/or modify them under the terms of | ||
| 156 | The Perl License, a copy of which is available at: | ||
| 157 | http://dev.perl.org/licenses/ | ||
| 158 | |||
| 159 | =cut |