sqlite3-foreigner 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9bb87d799d310c2a59a079c8e458a699d9a61c29
4
+ data.tar.gz: 46e9d673641560d1185a1d732426a0f52a801ab0
5
+ SHA512:
6
+ metadata.gz: 88af0fc4c71348fda654f25a7f89c1cf7bb7fc241952d44c58448e5dec709368e80c7d0ae4a9fcec3896799eb3d3fc98d956bb6eeee9eed4c93a67b4bd168198
7
+ data.tar.gz: dc05d66690e0bf8cc5a9a9d3d12eb0a460da1e09debe5a920bc49b6441399836785febcdde0841c424f43b86e34685ab3b5d22e51bf035f1275f1584376d6e3f
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2014 Kazuhiro Sera
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Foreigner extension for SQLite3 users
2
+
3
+ ## Foreginer
4
+
5
+ https://github.com/matthuhiggins/foreigner
6
+
7
+ ## Setup
8
+
9
+ ## Gemfile
10
+
11
+ ```
12
+ gem 'foreigner'
13
+ gem 'sqlite3-foreigner'
14
+ ```
15
+
16
+ ### config/initializers/sqlite3_foreigner.rb
17
+
18
+ ```
19
+ require 'foreigner/connection_adapters/sqlite3_adapter'
20
+ Foreigner::Adapter.register 'sqlite3', 'foreigner/connection_adapters/sqlite3_adapter'
21
+ ```
22
+
23
+ ## License
24
+
25
+ The MIT License
26
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,56 @@
1
+ module ActiveRecord
2
+ class Base
3
+ class << self
4
+ def sqlite3_connection_with_foreign_key(config)
5
+ sqlite3_connection_without_foreign_key(config).tap do |db|
6
+ db.execute("pragma foreign_keys = on")
7
+ end
8
+ end
9
+ alias_method_chain :sqlite3_connection, :foreign_key
10
+ end
11
+ end
12
+ end
13
+
14
+ module Foreigner
15
+ module ConnectionAdapters
16
+ module SQLite3Adapter
17
+ include Foreigner::ConnectionAdapters::Sql2003
18
+
19
+ def add_foreign_key(from_table, to_table, options = {})
20
+
21
+ fk_column_name = options[:column] || foreign_key_column(to_table)
22
+
23
+ meta_results = exec_query("select sql from sqlite_master where name = $1", nil, [[nil, from_table]])
24
+ if meta_results.nil? || meta_results.rows.size == 0
25
+ raise " [sqlite3-foreigner] error: '#{from_table}' is not found!\n\n"
26
+ end
27
+ create_table = meta_results[0]['sql']
28
+
29
+ count = exec_query("select count(1) c from #{from_table}")[0]['c'].to_i
30
+ if count > 0
31
+ puts "\n *** [sqlite3-foreigner]: Skipped non empty table (table: #{from_table}) ***\n\n"
32
+ return
33
+ end
34
+
35
+ execute("drop table #{from_table}")
36
+ re_create_table = create_table.gsub(/("#{fk_column_name}"\s+[^,]+),/, "\\1 references #{to_table}(id),")
37
+ execute(re_create_table)
38
+ end
39
+
40
+ def remove_foreign_key(table, options)
41
+ column = options[:name].gsub(/^#{table.to_s.singularize}_/, '').gsub(/_foreign_key$/, '')
42
+ ActiveRecord::Base.connection.remove_column(table, column)
43
+ end
44
+
45
+ private
46
+
47
+ def foreign_key_column(to_table)
48
+ "#{to_table.to_s.singularize}_id"
49
+ end
50
+
51
+ end
52
+ end
53
+ end
54
+
55
+ Foreigner::Adapter.safe_include :SQLite3Adapter, Foreigner::ConnectionAdapters::SQLite3Adapter
56
+
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sqlite3-foreigner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kazuhiro Sera
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: foreigner
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Foreigner like FK migration for SQLite3
28
+ email: seratch@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE.txt
34
+ - README.md
35
+ - Rakefile
36
+ - lib/foreigner/connection_adapters/sqlite3_adapter.rb
37
+ homepage: http://github.com/seratch/sqlite3-foreigner
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 1.9.2
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.5
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.2.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Foreign Keys for Rails on SQLite3
61
+ test_files: []
62
+ has_rdoc: