This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 367
/
schemas.js
111 lines (103 loc) · 3.64 KB
/
schemas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
/**
* @license
* Copyright 2015 The Lovefield Project Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
goog.setTestOnly();
goog.provide('lf.testing.schemas');
goog.require('lf.ConstraintAction');
goog.require('lf.Type');
goog.require('lf.schema');
/**
* @param {!lf.ConstraintAction} constraintAction The type of foreign key
* constraints to be added to the schema.
* @return {!lf.schema.Database} A schema where TableC refers to TableB, and
* TableB refers to tableA.
*/
lf.testing.schemas.getTableChain = function(constraintAction) {
var schemaBuilder = lf.schema.create('contexttest', 1);
schemaBuilder.createTable('TableA').
addColumn('id', lf.Type.STRING).
addPrimaryKey(['id']);
schemaBuilder.createTable('TableB').
addColumn('id', lf.Type.STRING).
addColumn('foreignKey', lf.Type.STRING).
addPrimaryKey(['id']).
addForeignKey('fk_tableA', {
local: 'foreignKey',
ref: 'TableA.id',
action: constraintAction
});
schemaBuilder.createTable('TableC').
addColumn('id', lf.Type.STRING).
addColumn('foreignKey', lf.Type.STRING).
addForeignKey('fk_tableB', {
local: 'foreignKey',
ref: 'TableB.id',
action: constraintAction
});
return schemaBuilder.getSchema();
};
/**
* Generates a schema with two tables, Parent and Child, linked with a RESTRICT
* constraint of the given constraint timing.
* @param {!lf.ConstraintTiming} constraintTiming
* @return {!lf.schema.Database} A schema where table Child refers to Parent.
*/
lf.testing.schemas.getOneForeignKey = function(constraintTiming) {
var schemaBuilder = lf.schema.create('testschema', 1);
schemaBuilder.createTable('Child').
addColumn('id', lf.Type.STRING).
addForeignKey('fk_Id', {
local: 'id',
ref: 'Parent.id',
action: lf.ConstraintAction.RESTRICT,
timing: constraintTiming
});
schemaBuilder.createTable('Parent').
addColumn('id', lf.Type.STRING).
addPrimaryKey(['id']);
return schemaBuilder.getSchema();
};
/**
* @param {!lf.ConstraintAction} constraintAction The type of foreign key
* constraints to be added to the schema.
* @return {!lf.schema.Database} A schema where TableB1 and TableB2 both refer
* to TableA.
*/
lf.testing.schemas.getTwoForeignKeys = function(constraintAction) {
var schemaBuilder = lf.schema.create('contexttest', 1);
schemaBuilder.createTable('TableA').
addColumn('id1', lf.Type.STRING).
addColumn('id2', lf.Type.STRING).
addUnique('uq_id1', ['id1']).
addUnique('uq_id2', ['id2']);
schemaBuilder.createTable('TableB1').
addColumn('id', lf.Type.STRING).
addColumn('foreignKey', lf.Type.STRING).
addForeignKey('fk_tableA', {
local: 'foreignKey',
ref: 'TableA.id1',
action: constraintAction
});
schemaBuilder.createTable('TableB2').
addColumn('id', lf.Type.STRING).
addColumn('foreignKey', lf.Type.STRING).
addForeignKey('fk_tableA', {
local: 'foreignKey',
ref: 'TableA.id2',
action: constraintAction
});
return schemaBuilder.getSchema();
};