| 1 | package CPAN::MetaCurator::Create; | ||
| 2 | |||
| 3 | use boolean; | ||
| 4 | use feature 'say'; | ||
| 5 | use parent 'CPAN::MetaCurator::Database'; | ||
| 6 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 7 | |||
| 8 | our $VERSION = '1.33'; | ||
| 9 | |||
| 10 | # ----------------------------------------------- | ||
| 11 | |||
| 12 | sub create_all_tables | ||
| 13 | { | ||
| 14 | my($self) = @_; | ||
| 15 | |||
| 16 | $self -> init_config; | ||
| 17 | $self -> init_db; | ||
| 18 | $self -> logger -> info('Creating all tables'); | ||
| 19 | |||
| 20 | my($method); | ||
| 21 | my($result); | ||
| 22 | |||
| 23 | for my $table_name (@{$self -> table_names}) | ||
| 24 | { | ||
| 25 | $method = "create_${table_name}_table"; | ||
| 26 | |||
| 27 | $result = $self -> $method; | ||
| 28 | |||
| 29 | $self -> logger -> debug("Created table '$table_name'"); | ||
| 30 | } | ||
| 31 | |||
| 32 | $self -> logger -> info('Created all tables'); | ||
| 33 | $self -> logger -> info('-' x 50); | ||
| 34 | |||
| 35 | # Return 0 for OK and 1 for error. | ||
| 36 | |||
| 37 | return 0; | ||
| 38 | |||
| 39 | } # End of create_all_tables. | ||
| 40 | |||
| 41 | # -------------------------------------------------- | ||
| 42 | |||
| 43 | sub create_constants_table | ||
| 44 | { | ||
| 45 | my($self) = @_; | ||
| 46 | my($table_name) = 'constants'; | ||
| 47 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 48 | my($engine) = $self -> engine; | ||
| 49 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 50 | create table $table_name | ||
| 51 | ( | ||
| 52 | id $primary_key, | ||
| 53 | name text not null, | ||
| 54 | value text not null | ||
| 55 | ) strict $engine | ||
| 56 | SQL | ||
| 57 | |||
| 58 | return $result; | ||
| 59 | |||
| 60 | } # End of create_constants_table. | ||
| 61 | |||
| 62 | # -------------------------------------------------- | ||
| 63 | # In the log table: | ||
| 64 | # o action => 'add', 'delete', 'export', 'import', 'update'. | ||
| 65 | # o context => 'flower', etc. | ||
| 66 | # o key => Either 0 or a primary key associated with the context. | ||
| 67 | # o name => The name of the thing. | ||
| 68 | # o note => Any text. May contain other primary keys, e.g. when garden also has a property. | ||
| 69 | # o outcome => 'Success' or 'Error'. | ||
| 70 | |||
| 71 | sub create_log_table | ||
| 72 | { | ||
| 73 | my($self) = @_; | ||
| 74 | my($table_name) = 'log'; | ||
| 75 | my($engine) = $self -> engine; | ||
| 76 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 77 | my($time_option) = $self -> time_option; | ||
| 78 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 79 | create table $table_name | ||
| 80 | ( | ||
| 81 | id $primary_key, | ||
| 82 | action text not null, | ||
| 83 | context text not null, | ||
| 84 | file_name text not null, | ||
| 85 | key integer not null, | ||
| 86 | name text not null, | ||
| 87 | note text not null, | ||
| 88 | outcome text not null, | ||
| 89 | timestamp text $time_option not null default current_timestamp | ||
| 90 | ) strict $engine | ||
| 91 | SQL | ||
| 92 | |||
| 93 | return $result; | ||
| 94 | |||
| 95 | } # End of create_log_table. | ||
| 96 | |||
| 97 | # -------------------------------------------------- | ||
| 98 | |||
| 99 | sub create_modules_table | ||
| 100 | { | ||
| 101 | my($self) = @_; | ||
| 102 | my($table_name) = 'modules'; | ||
| 103 | my($engine) = $self -> engine; | ||
| 104 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 105 | my($time_option) = $self -> time_option; | ||
| 106 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 107 | create table $table_name | ||
| 108 | ( | ||
| 109 | id $primary_key, | ||
| 110 | name text not null, | ||
| 111 | timestamp text $time_option not null default current_timestamp | ||
| 112 | ) strict $engine | ||
| 113 | SQL | ||
| 114 | |||
| 115 | return $result; | ||
| 116 | |||
| 117 | } # End of create_modules_table. | ||
| 118 | |||
| 119 | # -------------------------------------------------- | ||
| 120 | # Note: The columns are not in alphabetical order | ||
| 121 | # so that they display prettily in sqlite3. | ||
| 122 | # create table topics(id integer primary key autoincrement, parent_id integer references topics(id), | ||
| 123 | # title text not null, text text not null, timestamp text not null default current_timestamp); | ||
| 124 | |||
| 125 | sub create_topics_table | ||
| 126 | { | ||
| 127 | my($self) = @_; | ||
| 128 | my($table_name) = 'topics'; | ||
| 129 | my($engine) = $self -> engine; | ||
| 130 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 131 | my($time_option) = $self -> time_option; | ||
| 132 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 133 | create table $table_name | ||
| 134 | ( | ||
| 135 | id $primary_key, | ||
| 136 | parent_id integer references topics(id), | ||
| 137 | title text not null, | ||
| 138 | text text not null, | ||
| 139 | timestamp text $time_option not null default current_timestamp | ||
| 140 | ) strict $engine | ||
| 141 | SQL | ||
| 142 | |||
| 143 | return $result; | ||
| 144 | |||
| 145 | } # End of create_modules_table. | ||
| 146 | |||
| 147 | # ----------------------------------------------- | ||
| 148 | |||
| 149 | sub drop_all_tables | ||
| 150 | { | ||
| 151 | my($self) = @_; | ||
| 152 | |||
| 153 | $self -> init_config; | ||
| 154 | $self -> init_db; | ||
| 155 | $self -> logger -> info('Dropping all tables'); | ||
| 156 | |||
| 157 | for my $table_name (reverse @{$self -> table_names}) | ||
| 158 | { | ||
| 159 | $self -> creator -> drop_table($table_name); | ||
| 160 | |||
| 161 | $self -> logger -> debug("Dropped table '$table_name'"); | ||
| 162 | } | ||
| 163 | |||
| 164 | $self -> logger -> info('Dropped all tables'); | ||
| 165 | $self -> logger -> info('-' x 50); | ||
| 166 | |||
| 167 | # Return 0 for OK and 1 for error. | ||
| 168 | |||
| 169 | return 0; | ||
| 170 | |||
| 171 | } # End of drop_all_tables. | ||
| 172 | |||
| 173 | # ----------------------------------------------- | ||
| 174 | |||
| 175 | 1; | ||
| 176 | |||
| 177 | =pod | ||
| 178 | |||
| 179 | =head1 NAME | ||
| 180 | |||
| 181 | CPAN::MetaCurator::Create - Manage the cpan.metacurator.sqlite database | ||
| 182 | |||
| 183 | =head1 Machine-Readable Change Log | ||
| 184 | |||
| 185 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 186 | |||
| 187 | =head1 Version Numbers | ||
| 188 | |||
| 189 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 190 | |||
| 191 | =head1 Support | ||
| 192 | |||
| 193 | Email the author. | ||
| 194 | |||
| 195 | =head1 Author | ||
| 196 | |||
| 197 | L<CPAN::MetaCurator> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2025. | ||
| 198 | |||
| 199 | My homepage: L<https://savage.net.au/>. | ||
| 200 | |||
| 201 | =head1 Copyright | ||
| 202 | |||
| 203 | Australian copyright (c) 2025, Ron Savage. | ||
| 204 | |||
| 205 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 206 | you can redistribute them and/or modify them under the terms of | ||
| 207 | The Perl License, a copy of which is available at: | ||
| 208 | http://dev.perl.org/licenses/ | ||
| 209 | |||
| 210 | =cut |