postgres.sql 7.22 KB
Newer Older
w  
Qiang Xue committed
1 2
/**
 * This is the database schema for testing PostgreSQL support of yii Active Record.
3 4
 * To test this feature, you need to create a database named 'yiitest' on 'localhost'
 * and create an account 'postgres/postgres' which owns this test database.
w  
Qiang Xue committed
5 6
 */

Carsten Brandt committed
7
DROP TABLE IF EXISTS "composite_fk" CASCADE;
8 9
DROP TABLE IF EXISTS "order_item" CASCADE;
DROP TABLE IF EXISTS "item" CASCADE;
10
DROP TABLE IF EXISTS "order_item_with_null_fk" CASCADE;
11
DROP TABLE IF EXISTS "order" CASCADE;
12
DROP TABLE IF EXISTS "order_with_null_fk" CASCADE;
13 14 15 16 17 18
DROP TABLE IF EXISTS "category" CASCADE;
DROP TABLE IF EXISTS "customer" CASCADE;
DROP TABLE IF EXISTS "profile" CASCADE;
DROP TABLE IF EXISTS "type" CASCADE;
DROP TABLE IF EXISTS "null_values" CASCADE;
DROP TABLE IF EXISTS "constraints" CASCADE;
19
DROP TABLE IF EXISTS "bool_values" CASCADE;
20 21

CREATE TABLE "constraints"
22 23 24 25
(
  id integer not null,
  field1 varchar(255)
);
26

27
CREATE TABLE "profile" (
28 29 30 31
  id serial not null primary key,
  description varchar(128) NOT NULL
);

32
CREATE TABLE "customer" (
33 34
  id serial not null primary key,
  email varchar(128) NOT NULL,
35
  name varchar(128),
36
  address text,
37
  status integer DEFAULT 0,
38
  bool_status boolean DEFAULT FALSE,
39
  profile_id integer
w  
Qiang Xue committed
40 41
);

42
comment on column public.customer.email is 'someone@example.com';
43

44
CREATE TABLE "category" (
45 46
  id serial not null primary key,
  name varchar(128) NOT NULL
w  
Qiang Xue committed
47 48
);

49
CREATE TABLE "item" (
50 51
  id serial not null primary key,
  name varchar(128) NOT NULL,
52
  category_id integer NOT NULL references "category"(id) on UPDATE CASCADE on DELETE CASCADE
w  
Qiang Xue committed
53 54
);

55
CREATE TABLE "order" (
56
  id serial not null primary key,
57
  customer_id integer NOT NULL references "customer"(id) on UPDATE CASCADE on DELETE CASCADE,
Alexander Kochetov committed
58
  created_at integer NOT NULL,
59
  total decimal(10,0) NOT NULL
w  
Qiang Xue committed
60 61
);

62 63 64 65 66 67 68
CREATE TABLE "order_with_null_fk" (
  id serial not null primary key,
  customer_id integer,
  created_at integer NOT NULL,
  total decimal(10,0) NOT NULL
);

69
CREATE TABLE "order_item" (
70 71
  order_id integer NOT NULL references "order"(id) on UPDATE CASCADE on DELETE CASCADE,
  item_id integer NOT NULL references "item"(id) on UPDATE CASCADE on DELETE CASCADE,
72 73 74
  quantity integer NOT NULL,
  subtotal decimal(10,0) NOT NULL,
  PRIMARY KEY (order_id,item_id)
w  
Qiang Xue committed
75 76
);

77 78 79 80 81 82 83
CREATE TABLE "order_item_with_null_fk" (
  order_id integer,
  item_id integer,
  quantity integer NOT NULL,
  subtotal decimal(10,0) NOT NULL
);

Carsten Brandt committed
84 85 86 87 88 89 90 91
CREATE TABLE "composite_fk" (
  id integer NOT NULL,
  order_id integer NOT NULL,
  item_id integer NOT NULL,
  PRIMARY KEY (id),
  CONSTRAINT FK_composite_fk_order_item FOREIGN KEY (order_id, item_id) REFERENCES "order_item" (order_id, item_id) ON DELETE CASCADE
);

92
CREATE TABLE "null_values" (
93 94
  id INT NOT NULL,
  var1 INT NULL,
95 96 97 98 99 100
  var2 INT NULL,
  var3 INT DEFAULT NULL,
  stringcol VARCHAR(32) DEFAULT NULL,
  PRIMARY KEY (id)
);

101
CREATE TABLE "type" (
102 103
  int_col integer NOT NULL,
  int_col2 integer DEFAULT '1',
104
  smallint_col smallint DEFAULT '1',
105 106 107 108 109 110 111 112
  char_col char(100) NOT NULL,
  char_col2 varchar(100) DEFAULT 'something',
  char_col3 text,
  float_col double precision NOT NULL,
  float_col2 double precision DEFAULT '1.23',
  blob_col bytea,
  numeric_col decimal(5,2) DEFAULT '33.22',
  time timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
113 114
  bool_col boolean NOT NULL,
  bool_col2 boolean DEFAULT TRUE,
115
  ts_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
Carsten Brandt committed
116
  bit_col BIT(8) NOT NULL DEFAULT B'10000010'
w  
Qiang Xue committed
117 118
);

119 120
CREATE TABLE "bool_values" (
  id serial not null primary key,
121 122 123
  bool_col bool,
  default_true bool not null default true,
  default_false boolean not null default false
124 125
);

126 127
INSERT INTO "profile" (description) VALUES ('profile customer 1');
INSERT INTO "profile" (description) VALUES ('profile customer 3');
128

129 130 131
INSERT INTO "customer" (email, name, address, status, bool_status, profile_id) VALUES ('user1@example.com', 'user1', 'address1', 1, true, 1);
INSERT INTO "customer" (email, name, address, status, bool_status) VALUES ('user2@example.com', 'user2', 'address2', 1, true);
INSERT INTO "customer" (email, name, address, status, bool_status, profile_id) VALUES ('user3@example.com', 'user3', 'address3', 2, false, 2);
132

133 134
INSERT INTO "category" (name) VALUES ('Books');
INSERT INTO "category" (name) VALUES ('Movies');
135

136 137 138 139 140
INSERT INTO "item" (name, category_id) VALUES ('Agile Web Application Development with Yii1.1 and PHP5', 1);
INSERT INTO "item" (name, category_id) VALUES ('Yii 1.1 Application Development Cookbook', 1);
INSERT INTO "item" (name, category_id) VALUES ('Ice Age', 2);
INSERT INTO "item" (name, category_id) VALUES ('Toy Story', 2);
INSERT INTO "item" (name, category_id) VALUES ('Cars', 2);
141

142 143 144
INSERT INTO "order" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);
145

146 147 148 149
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);

150 151 152 153 154 155
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
156

157 158 159 160 161 162 163
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);

164
/**
165
 * (Postgres-)Database Schema for validator tests
166 167
 */

168 169
DROP TABLE IF EXISTS "validator_main" CASCADE;
DROP TABLE IF EXISTS "validator_ref" CASCADE;
170

171
CREATE TABLE "validator_main" (
172 173 174 175
  id integer not null primary key,
  field1 VARCHAR(255)
);

176
CREATE TABLE "validator_ref" (
177 178 179 180 181
  id integer not null primary key,
  a_field VARCHAR(255),
  ref     integer
);

182 183 184 185 186 187 188 189 190 191
INSERT INTO "validator_main" (id, field1) VALUES (1, 'just a string1');
INSERT INTO "validator_main" (id, field1) VALUES (2, 'just a string2');
INSERT INTO "validator_main" (id, field1) VALUES (3, 'just a string3');
INSERT INTO "validator_main" (id, field1) VALUES (4, 'just a string4');
INSERT INTO "validator_ref" (id, a_field, ref) VALUES (1, 'ref_to_2', 2);
INSERT INTO "validator_ref" (id, a_field, ref) VALUES (2, 'ref_to_2', 2);
INSERT INTO "validator_ref" (id, a_field, ref) VALUES (3, 'ref_to_3', 3);
INSERT INTO "validator_ref" (id, a_field, ref) VALUES (4, 'ref_to_4', 4);
INSERT INTO "validator_ref" (id, a_field, ref) VALUES (5, 'ref_to_4', 4);
INSERT INTO "validator_ref" (id, a_field, ref) VALUES (6, 'ref_to_5', 5);