communard 0.0.1

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: 93fe86a1450877912e94bf0d8d6c61d6994d0731
4
+ data.tar.gz: 8b41d0725025b0901e1c574f3be1196c7441219c
5
+ SHA512:
6
+ metadata.gz: 3725a5b59c63946c9532e4f189719590404faad6e2df3584f66c6d5673ae9d8e1f75fa895dc4c39f5c970f9628f3cb72af0fecae98284ade648f6ba965b1dd87
7
+ data.tar.gz: 300f1d6b8033c35ab87101581d499ba93102e090fcda8774c7817e01b4e3908b9d186332a054009bd0b9c49a0a71f9a2aa20b170392d1fb34592d27da447d6d7
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in communard.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # Communard
2
+
3
+ Communard adds some conventions from [ActiveRecord][ar] to [Sequel][sq].
4
+
5
+ This means you can use `config/database.yml` and `db/migrate` again, so you
6
+ don't have to change deployment scripts that are made for ActiveRecord.
7
+
8
+ Sequel doesn't provide the exact same functionality as ActiveRecord. Communard
9
+ doesn't try to make Sequel quack like ActiveRecord, it just tries to help with
10
+ some (not all) setup.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```
17
+ gem 'communard'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ ```
23
+ $ bundle
24
+ ```
25
+
26
+ Or install it yourself as:
27
+
28
+ ```
29
+ $ gem install communard
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ### Connecting to the database
35
+
36
+ To get a database connection:
37
+
38
+ ``` ruby
39
+ DB = Communard.connect
40
+ ```
41
+
42
+ The `DB` object will be familiar to you if you've ever read the Sequel documentation.
43
+
44
+ Note: Communard doesn't remember your connection.
45
+
46
+ ### Rake integration
47
+
48
+ To add most Rake tasks, add this to your `Rakefile` or to `lib/tasks/communard.rake`:
49
+
50
+ ``` ruby
51
+ require "communard/rake"
52
+ Communard::Rake.add_tasks
53
+ ```
54
+
55
+ This will add the most used rake tasks, like `db:create`, `db:migrate`, and `db:setup`.
56
+
57
+ To see them all:
58
+
59
+ ```
60
+ $ rake -T db
61
+ ```
62
+
63
+ ### Migrations
64
+
65
+ To generate a migration:
66
+
67
+ ```
68
+ $ bundle exec communard --generate-migration create_posts
69
+ ```
70
+
71
+ Communard doesn't support more arguments, like the Rails generator does. You'll
72
+ have to edit the generated migration file yourself.
73
+
74
+ ### Configuration
75
+
76
+ There are a couple of configuration options available. They can be set by giving
77
+ a block to `connect` or `add_tasks`. Under normal circumstances you don't need
78
+ to set them.
79
+
80
+ ``` ruby
81
+ DB = Communard.connect { |config|
82
+ config.root = Rails.root
83
+ config.logger = Rails.logger
84
+ config.environment = Rails.env.to_s
85
+ }
86
+ ```
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it ( https://github.com/yourkarma/communard/fork )
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create a new Pull Request
95
+
96
+ [ar]: http://rubyonrails.org
97
+ [sq]: http://sequel.jeremyevans.net
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/communard ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "communard"
4
+ require "optparse"
5
+
6
+ parser = OptionParser.new do |opts|
7
+
8
+ opts.on "-m", "--generate-migration NAME", "Generates a migration" do |name|
9
+ Communard.context.generate_migration(name: name)
10
+ exit
11
+ end
12
+
13
+ opts.on_tail "-v", "--version" do
14
+ puts "Communard version #{Communard::VERSION}"
15
+ exit
16
+ end
17
+
18
+ opts.on_tail "-h", "--help" do
19
+ puts parser
20
+ exit
21
+ end
22
+
23
+ end
24
+ parser.parse!
25
+
26
+ puts parser
data/communard.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'communard/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "communard"
8
+ spec.version = Communard::VERSION
9
+ spec.authors = ["iain"]
10
+ spec.email = ["iain@iain.nl"]
11
+ spec.summary = %q{Adds some conventions from ActiveRecord to Sequel.}
12
+ spec.description = %q{Adds some conventions from ActiveRecord to Sequel.}
13
+ spec.homepage = "https://github.com/yourkarma/communard"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sequel", "~> 4.13"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,29 @@
1
+ require "logger"
2
+ require "pathname"
3
+
4
+ module Communard
5
+ class Configuration
6
+
7
+ attr_accessor :environment, :root, :logger
8
+
9
+ def initialize
10
+ self.environment = ENV["RACK_ENV"] || ENV["RUBY_ENV"] || ENV["RACK_ENV"] || "development"
11
+ self.root = Pathname(Dir.pwd)
12
+ self.logger = default_logger
13
+ yield self if block_given?
14
+ end
15
+
16
+ private
17
+
18
+ def default_logger
19
+ ::Logger.new($stdout).tap { |l|
20
+ alternate = 0
21
+ l.formatter = proc { |sev, _, _, msg|
22
+ color = sev == "INFO" ? 35 + ((alternate += 1) % 2) : 31
23
+ "\e[#{color}m[#{sev}]\e[0m #{msg}\n"
24
+ }
25
+ }
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,95 @@
1
+ require "sequel"
2
+
3
+ module Communard
4
+ class Context
5
+
6
+ attr_reader :configuration
7
+
8
+ def initialize(configuration)
9
+ @configuration = configuration
10
+ end
11
+
12
+ def connect(opts = options)
13
+ connection = ::Sequel.connect(opts)
14
+ connection.loggers = [logger]
15
+ connection
16
+ end
17
+
18
+ def generate_migration(name: nil)
19
+ fail ArgumentError, "Name is required" if name.to_s == ""
20
+ require "fileutils"
21
+ underscore = name.
22
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
23
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
24
+ tr("-", "_").
25
+ downcase
26
+ filename = root.join("db/migrate/#{Time.now.strftime("%Y%m%d%H%M%S")}_#{underscore}.rb")
27
+ FileUtils.mkdir_p(File.dirname(filename))
28
+ File.open(filename, "w") { |f| f << "Sequel.migration do\n change do\n end\nend" }
29
+ puts "#{filename} created"
30
+ end
31
+
32
+ def create_database(env: environment)
33
+ run_without_database("CREATE DATABASE IF NOT EXISTS %{database_name}", env: env)
34
+ end
35
+
36
+ def drop_database(env: environment)
37
+ fail ArgumentError, "Don't drop the production database, you monkey!" if env.to_s == "production"
38
+ run_without_database("DROP DATABASE IF EXISTS %{database_name}", env: env)
39
+ end
40
+
41
+ def migrate(target: nil, env: environment)
42
+ maintenance(env: env).migrate(target: target)
43
+ end
44
+
45
+ def seed(env: environment)
46
+ maintenance(env: env).seed
47
+ end
48
+
49
+ def rollback(step: 1, env: environment)
50
+ maintenance(env: env).rollback(step: step)
51
+ end
52
+
53
+ def load_schema(env: environment)
54
+ maintenance(env: env).load_schema
55
+ end
56
+
57
+ def dump_schema(env: environment)
58
+ maintenance(env: env).dump_schema
59
+ end
60
+
61
+ def status(env: environment)
62
+ maintenance(env: env).status
63
+ end
64
+
65
+ def run_without_database(cmd, env: environment)
66
+ opts = options(env: env).dup
67
+ database_name = opts.delete("database")
68
+ connection = connect(opts)
69
+ connection.run(cmd % { database_name: database_name })
70
+ end
71
+
72
+ def options(env: environment)
73
+ YAML.load_file(root.join("config/database.yml")).fetch(env.to_s)
74
+ end
75
+
76
+ private
77
+
78
+ def maintenance(env: env)
79
+ Maintenance.new(connection: connect(options(env: env)), root: root)
80
+ end
81
+
82
+ def environment
83
+ configuration.environment
84
+ end
85
+
86
+ def root
87
+ configuration.root
88
+ end
89
+
90
+ def logger
91
+ configuration.logger
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,102 @@
1
+ module Communard
2
+ class Maintenance
3
+
4
+ attr_reader :connection, :root
5
+
6
+ def initialize(connection: nil, root: nil)
7
+ ::Sequel.extension :migration, :core_extensions
8
+ @connection = connection
9
+ @root = root
10
+ end
11
+
12
+ def migrate(target: nil)
13
+ target = Integer(target) if target
14
+ ::Sequel::Migrator.run(connection, migrations, target: target, allow_missing_migration_files: true)
15
+ dump_schema
16
+ end
17
+
18
+ def seed
19
+ load seeds_file if seeds_file.exist?
20
+ end
21
+
22
+ def rollback(step: 1)
23
+ target = applied_migrations[-step - 1]
24
+ if target
25
+ migrate(target: target.split(/_/, 2).first)
26
+ else
27
+ fail ArgumentError, "Cannot roll back that far"
28
+ end
29
+ end
30
+
31
+ def load_schema
32
+ migration = instance_eval(schema_file.read, schema_file.expand_path.to_s, 1)
33
+ migration.apply(connection, :up)
34
+ end
35
+
36
+ def dump_schema
37
+ connection.extension :schema_dumper
38
+ schema = connection.dump_schema_migration(same_db: false)
39
+ schema_file.open("w") { |f| f << schema.gsub(/\s+$/m, "") }
40
+ end
41
+
42
+ def status
43
+ results = Hash.new { |h,k| h[k] = Status.new(k, false, false) }
44
+ available = Pathname.glob(migrations.join("*.rb")).map(&:basename).map(&:to_s).reverse
45
+ available.each { |migration| results[migration].available = true }
46
+ applied_migrations.each { |migration| results[migration].applied = true }
47
+
48
+ puts
49
+ puts "database: #{connection.opts.fetch(:database)}"
50
+ puts
51
+ puts " Status Migration ID Migration Name"
52
+ puts "--------------------------------------------------"
53
+ results.values.sort.each do |result|
54
+ puts " #{result.status} #{result.id} #{result.name}"
55
+ end
56
+ puts
57
+ end
58
+
59
+ private
60
+
61
+ def applied_migrations
62
+ ::Sequel::Migrator.migrator_class(migrations).new(connection, migrations, allow_missing_migration_files: true).applied_migrations
63
+ end
64
+
65
+ def schema_file
66
+ root.join("db/schema.rb")
67
+ end
68
+
69
+ def seeds_file
70
+ root.join("db/seeds.rb")
71
+ end
72
+
73
+ def migrations
74
+ root.join("db/migrate")
75
+ end
76
+
77
+ Status = Struct.new(:file, :applied, :available) do
78
+
79
+ def <=>(other)
80
+ id <=> other.id
81
+ end
82
+
83
+ def status
84
+ available ? applied ? " up " : "down" : "????"
85
+ end
86
+
87
+ def id
88
+ splitted.first
89
+ end
90
+
91
+ def name
92
+ splitted.last.capitalize.gsub("_", " ").sub(/\.rb$/, "")
93
+ end
94
+
95
+ def splitted
96
+ file.split(/_/, 2)
97
+ end
98
+
99
+ end
100
+
101
+ end
102
+ end
@@ -0,0 +1,87 @@
1
+ require "communard"
2
+
3
+ module Communard
4
+ class Rake
5
+
6
+ extend ::Rake::DSL if defined?(::Rake::DSL)
7
+
8
+ def self.add_tasks(ns = :db, &block)
9
+
10
+ namespace ns do
11
+ desc "Creates the database, migrate the schema, and loads seed data"
12
+ task :setup => [:create, :migrate, :seed, "db:test:prepare"]
13
+
14
+ desc "Creates the database"
15
+ task :create do
16
+ Communard.context(&block).create_database
17
+ end
18
+
19
+ desc "Drops the database"
20
+ task :drop do
21
+ Communard.context(&block).drop_database
22
+ end
23
+
24
+ desc "Drops and creates the database"
25
+ task :reset => [:drop, :setup]
26
+
27
+ desc "Migrate the database"
28
+ task :migrate do
29
+ target = ENV["VERSION"] || ENV["TARGET"]
30
+ Communard.context(&block).migrate(target: target)
31
+ end
32
+
33
+ desc "Load the seed data from db/seeds.rb"
34
+ task :seed do
35
+ Communard.context(&block).seed
36
+ end
37
+
38
+ desc "Rolls the schema back to the previous version"
39
+ task :rollback do
40
+ step = Integer(ENV["STEP"] || 1)
41
+ Communard.context(&block).rollback(step: step)
42
+ end
43
+
44
+ namespace :test do
45
+ desc "Cleans the test database"
46
+ task :prepare do
47
+ context = Communard.context(&block)
48
+ context.drop_database(env: "test")
49
+ context.create_database(env: "test")
50
+ context.migrate(env: "test")
51
+ end
52
+ end
53
+
54
+ namespace :migrate do
55
+
56
+ desc "Redo the last migration"
57
+ task :redo do
58
+ context = Communard.context(&block)
59
+ context.rollback
60
+ context.migrate
61
+ end
62
+
63
+ desc "Display status of migrations"
64
+ task :status do
65
+ Communard.context(&block).status
66
+ end
67
+ end
68
+
69
+ namespace :schema do
70
+
71
+ desc "Load the schema from db/schema.rb"
72
+ task :load do
73
+ Communard.context(&block).load_schema
74
+ end
75
+
76
+ desc "Dumps the schema to db/schema.rb"
77
+ task :dump do
78
+ Communard.context(&block).dump_schema
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module Communard
2
+ VERSION = "0.0.1"
3
+ end
data/lib/communard.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "communard/version"
2
+ require "communard/maintenance"
3
+ require "communard/configuration"
4
+ require "communard/context"
5
+
6
+ module Communard
7
+
8
+ def self.connect(&block)
9
+ context(&block).connect
10
+ end
11
+
12
+ def self.context(&block)
13
+ Context.new(configuration(&block))
14
+ end
15
+
16
+ def self.configuration(&block)
17
+ Configuration.new(&block)
18
+ end
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: communard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - iain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Adds some conventions from ActiveRecord to Sequel.
56
+ email:
57
+ - iain@iain.nl
58
+ executables:
59
+ - communard
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/communard
69
+ - communard.gemspec
70
+ - lib/communard.rb
71
+ - lib/communard/configuration.rb
72
+ - lib/communard/context.rb
73
+ - lib/communard/maintenance.rb
74
+ - lib/communard/rake.rb
75
+ - lib/communard/version.rb
76
+ homepage: https://github.com/yourkarma/communard
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Adds some conventions from ActiveRecord to Sequel.
100
+ test_files: []
101
+ has_rdoc: