pg_schema 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/Rakefile +3 -0
- data/lib/generators/pg_schema/install_generator.rb +16 -0
- data/lib/generators/pg_schema/templates/lib/pg_schema.rake +51 -0
- data/lib/pg_schema/postgresql_adapter.rb +20 -0
- data/lib/pg_schema/version.rb +3 -0
- data/lib/pg_schema.rb +37 -0
- data/pg_schema.gemspec +24 -0
- metadata +56 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3@postgres_multi_tenancy --create
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012
|
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.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module PgSchema
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
namespace "pg_schema:install"
|
7
|
+
desc "Copy schema support files"
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
def copy_rake_tasks
|
11
|
+
template('lib/pg_schema.rake', "lib/tasks/pg_schema.rake")
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# https://github.com/rails/rails/blob/6c2a0675f11a9b5b8e88ed7dbccd65cb51be8029/activerecord/lib/active_record/railties/databases.rake
|
2
|
+
#
|
3
|
+
namespace :db do
|
4
|
+
|
5
|
+
desc 'Create the database defined in config/database.yml for the current Rails.env - also makes test database if in development mode'
|
6
|
+
task :create_with_schema => :load_config do
|
7
|
+
# Make the test database at the same time as the development one, if it exists
|
8
|
+
if Rails.env.development? && ActiveRecord::Base.configurations['test']
|
9
|
+
create_database(ActiveRecord::Base.configurations['test'])
|
10
|
+
end
|
11
|
+
create_database(ActiveRecord::Base.configurations[Rails.env])
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_database(config)
|
15
|
+
if valid_connection?(config)
|
16
|
+
$stderr.puts "#{config['database']} already exists"
|
17
|
+
else
|
18
|
+
case config['adapter']
|
19
|
+
when 'postgresql'
|
20
|
+
encoding = config[:encoding] || ENV['CHARSET'] || 'utf8'
|
21
|
+
schema_search_path = config.delete('schema_search_path') || 'public'
|
22
|
+
default_schema = schema_search_path.split(',').first.strip
|
23
|
+
|
24
|
+
begin
|
25
|
+
unless valid_connection?(config)
|
26
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
27
|
+
ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => encoding))
|
28
|
+
ActiveRecord::Base.establish_connection(config)
|
29
|
+
$stderr.puts "Database #{config['database']} has been created."
|
30
|
+
end
|
31
|
+
ActiveRecord::Base.connection.create_schema(default_schema, config['username'])
|
32
|
+
$stderr.puts "Schema #{default_schema} has been created in #{config['database']}."
|
33
|
+
rescue
|
34
|
+
$stderr.puts $!, *($!.backtrace)
|
35
|
+
$stderr.puts "Couldn't create database #{config.inspect}."
|
36
|
+
end
|
37
|
+
else
|
38
|
+
$stderr.puts "Only PostgreSQL is supported."
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def valid_connection?(config)
|
45
|
+
ActiveRecord::Base.establish_connection(config)
|
46
|
+
ActiveRecord::Base.connection && true
|
47
|
+
rescue
|
48
|
+
false
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
|
2
|
+
# Example:
|
3
|
+
# create_schema('products', 'postgres')
|
4
|
+
def create_schema(schema_name, pg_username)
|
5
|
+
execute("CREATE SCHEMA \"#{schema_name}\" AUTHORIZATION \"#{pg_username}\"")
|
6
|
+
end
|
7
|
+
|
8
|
+
# Drops a schema
|
9
|
+
#
|
10
|
+
# Example:
|
11
|
+
# drop_schema('products', 'postgres')
|
12
|
+
def drop_schema(schema_name)
|
13
|
+
execute("DROP SCHEMA \"#{schema_name}\"")
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns an array of all schemas in the database
|
17
|
+
def all_schemas
|
18
|
+
query('SELECT schema_name FROM information_schema.schemata').flatten
|
19
|
+
end
|
20
|
+
end
|
data/lib/pg_schema.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'pg_schema/postgresql_adapter'
|
2
|
+
|
3
|
+
module PgSchema
|
4
|
+
module Loader
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# Let ActiveRecord do its magic and then include our overrides once a
|
10
|
+
# model inherits from ActiveRecord::Base
|
11
|
+
#
|
12
|
+
def inherited(base)
|
13
|
+
base.send(:include, PgSchema::Base)
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Base
|
20
|
+
extend ActiveSupport::Concern
|
21
|
+
|
22
|
+
included do
|
23
|
+
self.table_name = "#{schema_name}.#{table_name}"
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
def schema_name
|
28
|
+
@schema_name ||= ActiveRecord::Base.connection.schema_search_path.split.first
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class ActiveRecord::Base
|
36
|
+
include PgSchema::Loader
|
37
|
+
end
|
data/pg_schema.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "pg_schema/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "pg_schema"
|
7
|
+
s.version = PgSchema::VERSION
|
8
|
+
s.authors = ["Alex Skryl"]
|
9
|
+
s.email = ["rut216@gmail.com"]
|
10
|
+
s.homepage = "http://skryl.org"
|
11
|
+
s.summary = %q{Postgres schema support}
|
12
|
+
s.description = %q{Support for postgres schema}
|
13
|
+
|
14
|
+
s.rubyforge_project = "pg_schema"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_schema
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alex Skryl
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Support for postgres schema
|
15
|
+
email:
|
16
|
+
- rut216@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rvmrc
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- Rakefile
|
26
|
+
- lib/generators/pg_schema/install_generator.rb
|
27
|
+
- lib/generators/pg_schema/templates/lib/pg_schema.rake
|
28
|
+
- lib/pg_schema.rb
|
29
|
+
- lib/pg_schema/postgresql_adapter.rb
|
30
|
+
- lib/pg_schema/version.rb
|
31
|
+
- pg_schema.gemspec
|
32
|
+
homepage: http://skryl.org
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project: pg_schema
|
52
|
+
rubygems_version: 1.8.15
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Postgres schema support
|
56
|
+
test_files: []
|