· 7 years ago · Feb 09, 2019, 07:54 AM
1#!/usr/bin/env ruby
2#
3# A single file example of renaming
4require 'rubygems'
5require 'dm-core'
6require 'dm-migrations'
7
8
9# setup the logger
10DataMapper::Logger.new($stdout, :debug)
11
12# connect to the DB
13DataMapper.setup(:default, 'sqlite3::memory:')
14
15class Project
16 include DataMapper::Resource
17
18 # properties
19 property :id, Serial
20
21 has n, :ndas, 'NDA'
22end
23
24class NDA
25 include DataMapper::Resource
26
27 # properties
28 property :id, Serial
29 belongs_to :project
30 has n, :developers, :through => Resource
31end
32
33class Developer
34 include DataMapper::Resource
35
36 property :id, Serial
37
38 has n, :ndas, 'NDA', :through => Resource
39end
40
41DataMapper.finalize.auto_migrate!
42
43@p = Project.create
44@d = Developer.create
45@n = @p.ndas.create
46@n.developers = [@d]
47@n.save
48
49__END__
50 ~ (0.000099) SELECT sqlite_version(*)
51 ~ (0.000209) DROP TABLE IF EXISTS "projects"
52 ~ (0.000022) PRAGMA table_info("projects")
53 ~ (0.000481) CREATE TABLE "projects" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
54 ~ (0.000035) DROP TABLE IF EXISTS "ndas"
55 ~ (0.000016) PRAGMA table_info("ndas")
56 ~ (0.000226) CREATE TABLE "ndas" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "project_id" INTEGER NOT NULL)
57 ~ (0.000154) CREATE INDEX "index_ndas_project" ON "ndas" ("project_id")
58 ~ (0.000032) DROP TABLE IF EXISTS "developers"
59 ~ (0.000017) PRAGMA table_info("developers")
60 ~ (0.000212) CREATE TABLE "developers" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
61 ~ (0.000029) DROP TABLE IF EXISTS "developer_ndas"
62 ~ (0.000016) PRAGMA table_info("developer_ndas")
63 ~ (0.000277) CREATE TABLE "developer_ndas" ("nda_id" INTEGER NOT NULL, "developer_id" INTEGER NOT NULL, PRIMARY KEY("nda_id", "developer_id"))
64 ~ (0.000092) INSERT INTO "projects" DEFAULT VALUES
65 ~ (0.000099) INSERT INTO "developers" DEFAULT VALUES
66 ~ (0.000142) INSERT INTO "ndas" ("project_id") VALUES (1)
67 ~ (0.000141) SELECT "developers"."id" FROM "developers" INNER JOIN "developer_ndas" ON "developers"."id" = "developer_ndas"."developer_id" INNER JOIN "ndas" ON "developer_ndas"."nda_id" = "ndas"."id" WHERE "developer_ndas"."nda_id" = 1 GROUP BY "developers"."id" ORDER BY "developers"."id"
68 ~ (0.000106) SELECT "nda_id", "developer_id" FROM "developer_ndas" WHERE ("nda_id" = 1 AND "developer_id" = 1) ORDER BY "nda_id", "developer_id" LIMIT 1
69 ~ (0.000120) INSERT INTO "developer_ndas" ("nda_id", "developer_id") VALUES (1, 1)