| 1 | package DBIx::OnlineDDL::Helper::SQLite; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | our $AUTHORITY = 'cpan:GSG'; | |||||||||
| 4 | # ABSTRACT: Private OnlineDDL helper for SQLite-specific code | ||||||||||
| 5 | 4 | ▶ | use version; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | our $VERSION = 'v1.1.2'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 4 | extends 'DBIx::OnlineDDL::Helper::Base'; | |||||||||
| 12 | |||||||||||
| 13 | 4 | ▶ | use Types::Standard qw( InstanceOf ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 4 | ▶ | use Sub::Util qw( set_subname ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 4 | ▶ | use namespace::clean; # don't export the above | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | #pod =encoding utf8 | ||||||||||
| 20 | #pod | ||||||||||
| 21 | #pod =head1 DESCRIPTION | ||||||||||
| 22 | #pod | ||||||||||
| 23 | #pod This is a private helper module for any SQLite-specific code. B<As a private module, any | ||||||||||
| 24 | #pod methods or attributes here are subject to change.> | ||||||||||
| 25 | #pod | ||||||||||
| 26 | #pod You should really be reading documentation for L<DBIx::OnlineDDL>. Or, if you want to | ||||||||||
| 27 | #pod create a helper module for a different RDBMS, read the docs for | ||||||||||
| 28 | #pod L<DBIx::OnlineDDL::Helper::Base>. | ||||||||||
| 29 | #pod | ||||||||||
| 30 | #pod =cut | ||||||||||
| 31 | |||||||||||
| 32 | 85 | ▶ | sub dbms_uses_global_fk_namespace { 0 } | ||||||||
|
Subroutine
dbms_uses_global_fk_namespace: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 33 | 84 | ▶ | sub child_fks_need_adjusting { 0 } | ||||||||
|
Subroutine
child_fks_need_adjusting: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 34 | 28 | ▶ | sub null_safe_equals_op { 'IS' } | ||||||||
|
Subroutine
null_safe_equals_op: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 35 | |||||||||||
| 36 | 29 | ▶ | sub current_catalog_schema { | ||||||||
|
Subroutine
current_catalog_schema: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | 29 | my $self = shift; | |||||||||
| 38 | 29 | my $dbh = $self->dbh; | |||||||||
| 39 | |||||||||||
| 40 | 29 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; | |||||||||
| 41 | 29 | my $databases = $dbh->selectall_hashref('PRAGMA database_list', 'seq'); | |||||||||
| 42 | 29 | my $schema = $databases->{0}{name}; # probably 'main' | |||||||||
| 43 | 29 | return (undef, $schema); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 28 | ▶ | sub insert_select_stmt { | ||||||||
|
Subroutine
insert_select_stmt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 47 | 28 | my ($self, $column_list_str) = @_; | |||||||||
| 48 | |||||||||||
| 49 | 28 | my $dbh = $self->dbh; | |||||||||
| 50 | |||||||||||
| 51 | 28 | my $id_name_quote = $dbh->quote_identifier( $self->copy_opts->{id_name} ); | |||||||||
| 52 | |||||||||||
| 53 | 28 | my $orig_table_name_quote = $dbh->quote_identifier($self->table_name); | |||||||||
| 54 | 28 | my $new_table_name_quote = $dbh->quote_identifier($self->new_table_name); | |||||||||
| 55 | |||||||||||
| 56 | # Use INSERT OR IGNORE to ignore dupe key errors | ||||||||||
| 57 | 28 | return join("\n", | |||||||||
| 58 | "INSERT OR IGNORE INTO $new_table_name_quote", | ||||||||||
| 59 | " ($column_list_str)", | ||||||||||
| 60 | "SELECT", | ||||||||||
| 61 | " $column_list_str", | ||||||||||
| 62 | "FROM $orig_table_name_quote", | ||||||||||
| 63 | "WHERE $id_name_quote BETWEEN ? AND ?", | ||||||||||
| 64 | ); | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 29 | ▶ | sub post_connection_stmts { | ||||||||
|
Subroutine
post_connection_stmts: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 68 | 29 | my $self = shift; | |||||||||
| 69 | |||||||||||
| 70 | 29 | my $db_timeouts = $self->db_timeouts; | |||||||||
| 71 | my @stmts = ( | ||||||||||
| 72 | # See FK comment in MySQL module. FKs in SQLite are a per-connection enabled | ||||||||||
| 73 | # feature, so this is always a "session" command. | ||||||||||
| 74 | 'PRAGMA foreign_keys = OFF', | ||||||||||
| 75 | |||||||||||
| 76 | # DB timeouts | ||||||||||
| 77 | 29 | 'PRAGMA busy_timeout = '.int($db_timeouts->{lock_file} * 1_000), # busy_timeout uses ms | |||||||||
| 78 | ); | ||||||||||
| 79 | |||||||||||
| 80 | # SQLite version 3.25.0 fixes table renames to also rename references to the table, | ||||||||||
| 81 | # ie: child FKs. Since SQLite doesn't yet have an DDL statement for renaming the FKs | ||||||||||
| 82 | # back to the old name, setting this PRAGMA variable is the only option. | ||||||||||
| 83 | # | ||||||||||
| 84 | # Also, while this change was introduced in 3.25.0, it seems to only manifest itself | ||||||||||
| 85 | # when the driver reports version 3.26.0, possibly due to how their production | ||||||||||
| 86 | # releases work. | ||||||||||
| 87 | 29 | ▶ | push @stmts, 'PRAGMA legacy_alter_table = ON' if $self->mmver >= 3.026; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | |||||||||||
| 89 | 29 | return @stmts; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 2 | ▶ | sub is_error_retryable { | ||||||||
|
Subroutine
is_error_retryable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 93 | 2 | my ($self, $error) = @_; | |||||||||
| 94 | |||||||||||
| 95 | # Disable /x flag to allow for whitespace within string, but turn it on for newlines | ||||||||||
| 96 | # and comments. | ||||||||||
| 97 | 2 | return $error =~ m< | |||||||||
| 98 | # Locks | ||||||||||
| 99 | (?-x:database( table)? is locked)| | ||||||||||
| 100 | |||||||||||
| 101 | # Connections | ||||||||||
| 102 | (?-x:attempt to [\w\s]+ on inactive database handle)| | ||||||||||
| 103 | |||||||||||
| 104 | # Queries | ||||||||||
| 105 | (?-x:query aborted)| | ||||||||||
| 106 | (?-x:interrupted) | ||||||||||
| 107 | >xi; | ||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 57 | ▶ | sub create_table_sql { | ||||||||
|
Subroutine
create_table_sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 111 | 57 | my ($self, $table_name) = @_; | |||||||||
| 112 | |||||||||||
| 113 | 57 | my $create_sql; | |||||||||
| 114 | 57 | ▶ | $self->dbh_runner(run => set_subname '_create_table_sql', sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 115 | 57 | ($create_sql) = $_->selectrow_array('SELECT sql FROM sqlite_master WHERE name = ?', undef, $table_name); | |||||||||
| 116 | 57 | }); | |||||||||
| 117 | |||||||||||
| 118 | 57 | return $create_sql; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | # Keep Base->rename_fks_in_table_sql (not used) | ||||||||||
| 122 | |||||||||||
| 123 | 29 | ▶ | sub has_conflicting_triggers_on_table { | ||||||||
|
Subroutine
has_conflicting_triggers_on_table: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 124 | 29 | my ($self, $table_name) = @_; | |||||||||
| 125 | |||||||||||
| 126 | 29 | ▶ | return $self->dbh_runner(run => set_subname '_has_triggers_on_table', sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 127 | 29 | $_->selectrow_array( | |||||||||
| 128 | 'SELECT name FROM sqlite_master WHERE type = ? AND tbl_name = ?', | ||||||||||
| 129 | undef, 'trigger', $table_name | ||||||||||
| 130 | ); | ||||||||||
| 131 | 29 | }); | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | # Always zero; multiple triggers per table/type aren't allowed | ||||||||||
| 135 | 28 | ▶ | sub has_triggers_on_table_to_be_copied { 0 } | ||||||||
|
Subroutine
has_triggers_on_table_to_be_copied: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 136 | |||||||||||
| 137 | 87 | ▶ | sub find_new_trigger_identifier { | ||||||||
|
Subroutine
find_new_trigger_identifier: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 138 | 87 | my ($self, $trigger_name) = @_; | |||||||||
| 139 | |||||||||||
| 140 | return $self->find_new_identifier( | ||||||||||
| 141 | 87 | ▶ | $trigger_name => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 142 | 87 | $_[0]->selectrow_array( | |||||||||
| 143 | 'SELECT name FROM sqlite_master WHERE type = ? AND name = ?', | ||||||||||
| 144 | undef, 'trigger', $_[1] | ||||||||||
| 145 | ); | ||||||||||
| 146 | }, | ||||||||||
| 147 | 87 | ); | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | # Keep Base->modify_trigger_dml_stmts (nothing changed) | ||||||||||
| 151 | |||||||||||
| 152 | 28 | ▶ | sub analyze_table { | ||||||||
|
Subroutine
analyze_table: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 153 | 28 | my ($self, $table_name) = @_; | |||||||||
| 154 | 28 | my $table_name_quote = $self->dbh->quote_identifier($table_name); | |||||||||
| 155 | 28 | $self->dbh_runner_do("ANALYZE $table_name_quote"); | |||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | # Keep Base->swap_tables (has transactional DDL) | ||||||||||
| 159 | |||||||||||
| 160 | # Keep the trigger methods (not used) | ||||||||||
| 161 | |||||||||||
| 162 | # Keep the other FK methods (not used) | ||||||||||
| 163 | |||||||||||
| 164 | 4 | 1; | |||||||||
| 165 | |||||||||||
| 166 | __END__ | ||||||||||