Module: ApplicationSeeds
- Defined in:
- lib/application_seeds.rb,
lib/application_seeds/version.rb,
lib/application_seeds/database.rb,
lib/application_seeds/attributes.rb,
lib/application_seeds/capistrano2.rb
Overview
A library for managing a standardized set of seed data for applications in a non-production environment.
See README.md for API documentation.
Defined Under Namespace
Modules: Capistrano Classes: Attributes, Database
Constant Summary collapse
- VERSION =
"0.6.0"
Class Method Summary collapse
-
.config ⇒ Object
Fetch the configuration.
-
.config=(config) ⇒ Object
Specify any configuration, such as the type of ids to generate (:integer or :uuid).
-
.config_value(key) ⇒ Object
Fetch data from the _config.yml files.
-
.create_object!(clazz, id, attributes, options = {}) ⇒ Object
This call will create a new instance of the specified class, with the specified id and attributes.
-
.data_directory ⇒ Object
Fetch the name of the directory where the application seed data is loaded from, if it was set using
data_diretory=
. -
.data_directory=(directory) ⇒ Object
Specify the name of the directory that contains the application seed data.
-
.data_gem_name ⇒ Object
Fetch the name of the directory where the application seed data is loaded from.
-
.data_gem_name=(gem_name) ⇒ Object
Specify the name of the gem that contains the application seed data.
-
.dataset ⇒ Object
Returns the name of the dataset that has been loaded, or nil if not running an application_seeds dataset.
-
.dataset=(dataset) ⇒ Object
Specify the name of the dataset to use.
-
.defer_referential_integrity_checks ⇒ Object
Defer the enforcement of foreign key constraints while the block is being executed.
-
.reset_sequence_numbers ⇒ Object
This method will reset the sequence numbers on id columns for all tables in the database with an id column.
-
.seed_data_exists?(type) ⇒ Boolean
Returns
true
if the specified data file exists in this dataset,false
if it does not.
Class Method Details
.config ⇒ Object
Fetch the configuration.
32 33 34 |
# File 'lib/application_seeds.rb', line 32 def config @_config ||= { :id_type => :integer } end |
.config=(config) ⇒ Object
Specify any configuration, such as the type of ids to generate (:integer or :uuid).
24 25 26 27 |
# File 'lib/application_seeds.rb', line 24 def config=(config) warn "WARNING! Calling ApplicationSeeds.config= after dataset has been set (ApplicationSeeds.dataset=) may not produce expected results." unless @dataset.nil? @_config = config end |
.config_value(key) ⇒ Object
Fetch data from the _config.yml files.
39 40 41 |
# File 'lib/application_seeds.rb', line 39 def config_value(key) config_values[key.to_s] end |
.create_object!(clazz, id, attributes, options = {}) ⇒ Object
This call will create a new instance of the specified class, with the specified id and attributes.
117 118 119 120 121 122 123 124 125 |
# File 'lib/application_seeds.rb', line 117 def create_object!(clazz, id, attributes, ={}) validate = [:validate].nil? ? true : [:validate] x = clazz.new x.attributes = attributes.reject { |k,v| !x.respond_to?("#{k}=") } x.id = id x.save!(:validate => validate) x end |
.data_directory ⇒ Object
Fetch the name of the directory where the application seed data is loaded from, if it was set using data_diretory=
.
78 79 80 |
# File 'lib/application_seeds.rb', line 78 def data_directory @data_directory end |
.data_directory=(directory) ⇒ Object
Specify the name of the directory that contains the application seed data.
66 67 68 69 70 71 72 |
# File 'lib/application_seeds.rb', line 66 def data_directory=(directory) if Dir.exist?(directory) @data_directory = directory else raise "ERROR: The #{directory} directory does not appear to contain application seed data" end end |
.data_gem_name ⇒ Object
Fetch the name of the directory where the application seed data is loaded from. Defaults to "applicadtion_seed_data"
if it was not set using data_gem_name=
.
59 60 61 |
# File 'lib/application_seeds.rb', line 59 def data_gem_name @data_gem_name || "application_seed_data" end |
.data_gem_name=(gem_name) ⇒ Object
Specify the name of the gem that contains the application seed data.
46 47 48 49 50 51 52 53 |
# File 'lib/application_seeds.rb', line 46 def data_gem_name=(gem_name) spec = Gem::Specification.find_by_name(gem_name) if Dir.exist?(File.join(spec.gem_dir, "lib", "seeds")) @data_gem_name = gem_name else raise "ERROR: The #{gem_name} gem does not appear to contain application seed data" end end |
.dataset ⇒ Object
Returns the name of the dataset that has been loaded, or nil if not running an application_seeds dataset.
106 107 108 109 110 111 |
# File 'lib/application_seeds.rb', line 106 def dataset res = Database.connection.exec("SELECT dataset from application_seeds LIMIT 1;") res.getvalue(0, 0) rescue PG::Error => e e. =~ /relation "application_seeds" does not exist/ ? nil : raise end |
.dataset=(dataset) ⇒ Object
Specify the name of the dataset to use. An exception will be raised if the dataset could not be found.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/application_seeds.rb', line 86 def dataset=(dataset) clear_cached_data @dataset = dataset if dataset.nil? || dataset.strip.empty? || dataset_path(dataset).nil? datasets = Dir[File.join(seed_data_path, "**", "*")].select { |x| File.directory?(x) }.map { |x| File.basename(x) }.join(', ') = "\nERROR: A valid dataset is required!\n" << "Usage: bundle exec rake application_seeds:load[your_data_set]\n\n" << "Available datasets: #{datasets}\n\n" raise end store_dataset end |
.defer_referential_integrity_checks ⇒ Object
Defer the enforcement of foreign key constraints while the block is being executed.
164 165 166 167 168 |
# File 'lib/application_seeds.rb', line 164 def defer_referential_integrity_checks Database.without_foreign_keys do yield end end |
.reset_sequence_numbers ⇒ Object
This method will reset the sequence numbers on id columns for all tables in the database with an id column. If you are having issues where you are unable to insert new data into the databse after your dataset has been imported, then this should correct them.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/application_seeds.rb', line 144 def reset_sequence_numbers result = Database.connection.exec("SELECT table_name FROM information_schema.tables WHERE table_schema NOT IN ('pg_catalog', 'information_schema')") table_names = result.map { |row| row.values_at('table_name')[0] } table_names_with_id_column = table_names.select do |table_name| result = Database.connection.exec("SELECT column_name FROM information_schema.columns WHERE table_name = '#{table_name}';") column_names = result.map { |row| row.values_at('column_name')[0] } column_names.include?('id') end table_names_with_id_column.each do |table_name| result = Database.connection.exec("SELECT pg_get_serial_sequence('#{table_name}', 'id');") sequence_name = result.getvalue(0, 0) Database.connection.exec("SELECT setval('#{sequence_name}', (select MAX(id) from #{table_name}));") end end |
.seed_data_exists?(type) ⇒ Boolean
Returns true
if the specified data file exists in this dataset, false
if it does not.
Examples:
ApplicationSeeds.seed_data_exists?(:campaigns)
134 135 136 |
# File 'lib/application_seeds.rb', line 134 def seed_data_exists?(type) !processed_seed_data[type.to_s].nil? end |