Module: Fixie

Extended by:
Fixie
Included in:
Fixie
Defined in:
lib/fixie.rb,
lib/fixie/version.rb

Defined Under Namespace

Modules: Fixtures, Model

Constant Summary collapse

MAX_ID =
2 ** 30 - 1
VERSION =
"0.5.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dbsObject

Returns the value of attribute dbs.



22
23
24
# File 'lib/fixie.rb', line 22

def dbs
  @dbs
end

.dirObject

Returns the value of attribute dir.



22
23
24
# File 'lib/fixie.rb', line 22

def dir
  @dir
end

.fixturesObject

Returns the value of attribute fixtures.



22
23
24
# File 'lib/fixie.rb', line 22

def fixtures
  @fixtures
end

Class Method Details

.all_fixturesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fixie.rb', line 38

def self.all_fixtures
  @all_fixtures ||= begin
    unless Fixie.dir.present? && Dir.exists?(Fixie.dir)
      raise "There is no directory in the load path with a 'fixtures' directory in it"
    end

    all_fixtures = {}

    now = Time.now.utc

    dbs.each do |db_name, db|
      all_fixtures[db_name] = {}

      # First pass, load all the fixtures
      Dir[File.join(Fixie.dir, "#{db_name}/*.yml")].each do |file|
        table_name = File.basename(file, '.yml')

        fixtures = YAML.load(ERB.new(IO.read(file)).result(binding)).symbolize_keys

        fixtures.each do |name, data|
          data[:id] ||= identify(name)
          data.symbolize_keys!
        end

        Fixie::Fixtures.define_fixture_accessor(db_name, table_name.to_sym)

        all_fixtures[db_name][table_name.to_sym] = fixtures
      end

      # Do a second pass to resolve associations and load data in DB
      db.transaction(deferrable: true) do
        all_fixtures[db_name].each do |table_name, fixtures|
          table = db[table_name]
          table.truncate(cascade: true, restart: true)
          table_has_created_at = table.columns.include?(:created_at)
          table_has_updated_at = table.columns.include?(:updated_at)

          json_columns = Set.new
          db.schema(table_name).each do |column,|
            json_columns << column if [:db_type] == 'json'
          end

          fixtures.each do |name, data|

            # Change attributes like city: baltimore to city_id: baltimore.id
            data.keys.each do |attr|
              associated_fixtures = all_fixtures[db_name][attr.to_s.pluralize.to_sym]
              if associated_fixtures && table.columns.include?("#{attr}_id".to_sym)
                associated_fixture = associated_fixtures[data[attr].to_sym]
                if associated_fixture
                  data["#{attr}_id".to_sym] = associated_fixture[:id]
                  data.delete(attr)
                end
              end

              if db.database_type == :postgres && json_columns.include?(attr)
                data[attr] = Sequel.pg_json(data[attr])
              end
            end

            # Set created_at/updated_at if they exist
            data[:created_at] = now if table_has_created_at && !data.key?(:created_at)
            data[:updated_at] = now if table_has_updated_at && !data.key?(:updated_at)

            # Finally, replace the data in the DB
            table.insert(data)
          end
        end
      end
    end

    all_fixtures
  end
end

.fixture(db_name, table_name, fixture_name) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fixie.rb', line 113

def self.fixture(db_name, table_name, fixture_name)
  db = all_fixtures[db_name]
  if db
    fixtures = db[table_name]
    if fixtures
      fixture = fixtures[fixture_name]
      if fixture
        fixture
      else
        raise "No fixture #{fixture_name.inspect} in #{db_name}.#{table_name}"
      end
    else
      raise "No fixtures for #{table_name.inspect} in #{db_name.inspect}"
    end
  else
    raise "Unknown fixture database #{db_name.inspect}"
  end
end

.identify(label) ⇒ Object



13
14
15
# File 'lib/fixie.rb', line 13

def self.identify(label)
  Zlib.crc32(label.to_s) % MAX_ID
end

.load_fixturesObject



132
133
134
# File 'lib/fixie.rb', line 132

def self.load_fixtures
  Fixie.all_fixtures
end

.versionObject



17
18
19
# File 'lib/fixie.rb', line 17

def self.version
  VERSION
end