lundi 11 janvier 2016

Getting started with Active Record

I have a script that currently utilizes the sqlite3 gem however this has gotten messy so I want to migrate over to Active Record.

I am having trouble getting the basics set up.

#!/usr/bin/ruby

require 'active_record'

ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'w'))

ActiveRecord::Base.establish_connection(
  :adapter  => 'sqlite3',
  :database => 'example.db'
)

ActiveRecord::Schema.define do
  unless ActiveRecord::Base.connection.tables.include? 'admins'
    create_table :admins do |table|
      table.column :network_id,     :integer
      table.column :text,           :string
    end
  end

  unless ActiveRecord::Base.connection.tables.include? 'channels'
    create_table :channels do |table|
      table.column :network_id,     :integer
      table.column :channel_name,   :string
      table.column :channel_users,  :integer
      table.column :channel_topic,  :string
    end
  end

  unless ActiveRecord::Base.connection.tables.include? 'motds'
    create_table :motds do |table|
      table.column :network_id,     :integer
      table.column :text,           :string
    end
  end

  unless ActiveRecord::Base.connection.tables.include? 'networks'
    create_table :networks do |table|
      table.column :network_id,     :integer
      table.column :network_name,   :string
    end
  end

  unless ActiveRecord::Base.connection.tables.include? 'servers'
    create_table :servers do |table|
      table.column :network_id,     :integer
      table.column :server_ip,      :string
      table.column :server_port,    :integer
    end
  end

  unless ActiveRecord::Base.connection.tables.include? 'users'
    create_table :users do |table|
      table.column :network_id,     :integer
      table.column :global,         :integer
      table.column :global_max,     :integer
      table.column :local,          :string
      table.column :local_max,      :integer
    end
  end
end

For some reason Active Record is creating more columns that I have set up above and I do not completely understand how to sort it out.

Basically all tables should be linked to the networks table, hence why I have set up network_id in the other tables, but for some reason Active Record is adding other columns/primary keys like id.

Here is database.log:

D, [2016-01-11T09:36:33.223197 #17400] DEBUG -- :    (3.1ms)  CREATE TABLE "admins" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "text" varchar)
D, [2016-01-11T09:36:33.226336 #17400] DEBUG -- :    (1.7ms)  CREATE TABLE "channels" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "channel_name" varchar, "channel_users" integer, "channel_topic" varchar)
D, [2016-01-11T09:36:33.229804 #17400] DEBUG -- :    (2.3ms)  CREATE TABLE "motds" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "text" varchar)
D, [2016-01-11T09:36:33.232999 #17400] DEBUG -- :    (1.7ms)  CREATE TABLE "networks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "network_name" varchar)
D, [2016-01-11T09:36:33.235667 #17400] DEBUG -- :    (1.4ms)  CREATE TABLE "servers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "server_ip" varchar, "server_port" integer)
D, [2016-01-11T09:36:33.238509 #17400] DEBUG -- :    (1.8ms)  CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "global" integer, "global_max" integer, "local" varchar, "local_max" integer)

What is the best way to design this in Active Record? The other tables should rely on the networks table, for example if something tries to insert in to channels with a network_id that doesn't exist in networks it should error.

Appreciate any assistance with this design to get me on the right track.

Aucun commentaire:

Enregistrer un commentaire