-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations.sql
405 lines (339 loc) · 13.8 KB
/
migrations.sql
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [Bank] (
[BankId] bigint NOT NULL IDENTITY,
[AccountName] nvarchar(60) NOT NULL,
[AccountNumber] nvarchar(18) NULL,
[Description] nvarchar(220) NULL,
[BankAccountType] int NOT NULL,
[InitialAmount] decimal(18,2) NOT NULL,
[Date] datetime2 NOT NULL,
CONSTRAINT [PK_Bank] PRIMARY KEY ([BankId])
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [Country] (
[CountryId] int NOT NULL IDENTITY,
[Name] nvarchar(120) NOT NULL,
[IconImage] nvarchar(800) NULL,
CONSTRAINT [PK_Country] PRIMARY KEY ([CountryId])
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [Currency] (
[CurrencyId] int NOT NULL IDENTITY,
[Name] nvarchar(120) NOT NULL,
CONSTRAINT [PK_Currency] PRIMARY KEY ([CurrencyId])
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [DocumentNumberSequence] (
[DocumentNumberSequenceId] bigint NOT NULL IDENTITY,
[DocumentType] int NOT NULL,
[Name] nvarchar(60) NOT NULL,
[Automatic] bit NOT NULL,
[Prefix] nvarchar(20) NOT NULL,
[Sequence] bigint NULL,
[FinalSequence] bigint NOT NULL,
[ValidFrom] datetime2 NOT NULL,
[ValidTo] datetime2 NOT NULL,
[IsDefault] bit NOT NULL DEFAULT CAST(0 AS bit),
CONSTRAINT [PK_DocumentNumberSequence] PRIMARY KEY ([DocumentNumberSequenceId])
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [PaymentDeadline] (
[PaymentDeadlineId] bigint NOT NULL IDENTITY,
[Description] nvarchar(120) NOT NULL,
[DayValue] int NOT NULL,
CONSTRAINT [PK_PaymentDeadline] PRIMARY KEY ([PaymentDeadlineId])
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [Tax] (
[TaxId] bigint NOT NULL IDENTITY,
[Name] nvarchar(60) NOT NULL,
[PercentValue] decimal(12,2) NOT NULL,
CONSTRAINT [PK_Tax] PRIMARY KEY ([TaxId])
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [TaxRegimeType] (
[TaxRegimeTypeId] bigint NOT NULL IDENTITY,
[Name] nvarchar(60) NOT NULL,
CONSTRAINT [PK_TaxRegimeType] PRIMARY KEY ([TaxRegimeTypeId])
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [Werehouse] (
[WerehouseId] bigint NOT NULL IDENTITY,
[Code] nvarchar(20) NOT NULL,
[Name] nvarchar(60) NOT NULL,
[Location] nvarchar(60) NOT NULL,
CONSTRAINT [PK_Werehouse] PRIMARY KEY ([WerehouseId])
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [Client] (
[ClientId] bigint NOT NULL IDENTITY,
[Code] nvarchar(20) NOT NULL,
[Name] nvarchar(60) NOT NULL,
[Email] nvarchar(60) NOT NULL,
[Phone] nvarchar(10) NOT NULL,
[Rnc] nvarchar(15) NULL,
[HomeOrApartment] nvarchar(200) NULL,
[City] nvarchar(60) NULL,
[Street] nvarchar(60) NULL,
[CountryId] int NULL,
CONSTRAINT [PK_Client] PRIMARY KEY ([ClientId]),
CONSTRAINT [FK_Client_Country_CountryId] FOREIGN KEY ([CountryId]) REFERENCES [Country] ([CountryId]) ON DELETE NO ACTION
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [Supplier] (
[SupplierId] bigint NOT NULL IDENTITY,
[Code] nvarchar(20) NOT NULL,
[Name] nvarchar(60) NOT NULL,
[Email] nvarchar(60) NOT NULL,
[Phone] nvarchar(10) NOT NULL,
[Rnc] nvarchar(15) NULL,
[HomeOrApartment] nvarchar(200) NULL,
[City] nvarchar(60) NULL,
[Street] nvarchar(60) NULL,
[CountryId] int NULL,
[IsIndependent] bit NOT NULL DEFAULT (default(0)),
CONSTRAINT [PK_Supplier] PRIMARY KEY ([SupplierId]),
CONSTRAINT [FK_Supplier_Country_CountryId] FOREIGN KEY ([CountryId]) REFERENCES [Country] ([CountryId]) ON DELETE NO ACTION
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [User] (
[UserId] bigint NOT NULL IDENTITY,
[Fullname] nvarchar(200) NOT NULL,
[Email] nvarchar(30) NULL,
[PasswordHash] varbinary(max) NULL,
[PasswordSalt] varbinary(max) NULL,
[CountryId] int NULL,
CONSTRAINT [PK_User] PRIMARY KEY ([UserId]),
CONSTRAINT [FK_User_Country_CountryId] FOREIGN KEY ([CountryId]) REFERENCES [Country] ([CountryId]) ON DELETE NO ACTION
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [Company] (
[CompanyId] bigint NOT NULL IDENTITY,
[Name] nvarchar(60) NOT NULL,
[Rnc] nvarchar(18) NULL,
[HomeOrApartment] nvarchar(50) NULL,
[City] nvarchar(30) NULL,
[Street] nvarchar(220) NULL,
[CountryId] int NULL,
[Phone] nvarchar(11) NULL,
[Website] nvarchar(max) NULL,
[RegimeTypeId] bigint NULL,
[CurrencyId] int NULL,
CONSTRAINT [PK_Company] PRIMARY KEY ([CompanyId]),
CONSTRAINT [FK_Company_Country_CountryId] FOREIGN KEY ([CountryId]) REFERENCES [Country] ([CountryId]) ON DELETE NO ACTION,
CONSTRAINT [FK_Company_Currency_CurrencyId] FOREIGN KEY ([CurrencyId]) REFERENCES [Currency] ([CurrencyId]) ON DELETE NO ACTION,
CONSTRAINT [FK_Company_TaxRegimeType_RegimeTypeId] FOREIGN KEY ([RegimeTypeId]) REFERENCES [TaxRegimeType] ([TaxRegimeTypeId]) ON DELETE NO ACTION
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [Product] (
[ProductId] bigint NOT NULL IDENTITY,
[Code] nvarchar(20) NOT NULL,
[Name] nvarchar(60) NOT NULL,
[Description] nvarchar(60) NULL,
[Reference] nvarchar(60) NULL,
[WerehouseId] bigint NULL,
[Cost] decimal(16,2) NOT NULL,
[Price] decimal(16,2) NOT NULL,
[Price2] decimal(16,2) NOT NULL,
[Price3] decimal(16,2) NOT NULL,
[Price4] decimal(16,2) NOT NULL,
CONSTRAINT [PK_Product] PRIMARY KEY ([ProductId]),
CONSTRAINT [FK_Product_Werehouse_WerehouseId] FOREIGN KEY ([WerehouseId]) REFERENCES [Werehouse] ([WerehouseId]) ON DELETE NO ACTION
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [InvoiceHeader] (
[InvoiceHeaderId] bigint NOT NULL IDENTITY,
[Code] nvarchar(20) NOT NULL,
[Ncf] bigint NOT NULL,
[Rnc] nvarchar(15) NULL,
[ClientId] bigint NULL,
[CompanyId] bigint NULL,
[Created] datetime2 NOT NULL,
[DueDate] datetime2 NOT NULL,
[PaymentTermId] bigint NULL,
[Notes] nvarchar(220) NULL,
[TermAndConditions] nvarchar(220) NULL,
[Footer] nvarchar(220) NULL,
[SystemCreated] datetime2 NOT NULL,
[SystemModified] datetime2 NULL,
CONSTRAINT [PK_InvoiceHeader] PRIMARY KEY ([InvoiceHeaderId]),
CONSTRAINT [FK_InvoiceHeader_Client_ClientId] FOREIGN KEY ([ClientId]) REFERENCES [Client] ([ClientId]) ON DELETE NO ACTION,
CONSTRAINT [FK_InvoiceHeader_Company_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Company] ([CompanyId]) ON DELETE NO ACTION,
CONSTRAINT [FK_InvoiceHeader_PaymentDeadline_PaymentTermId] FOREIGN KEY ([PaymentTermId]) REFERENCES [PaymentDeadline] ([PaymentDeadlineId]) ON DELETE NO ACTION
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [Category] (
[CategoryId] bigint NOT NULL IDENTITY,
[Code] nvarchar(20) NOT NULL,
[Name] nvarchar(60) NOT NULL,
[ShowOn] bit NOT NULL,
[ProductId] bigint NOT NULL,
CONSTRAINT [PK_Category] PRIMARY KEY ([CategoryId]),
CONSTRAINT [FK_Category_Product_ProductId] FOREIGN KEY ([ProductId]) REFERENCES [Product] ([ProductId]) ON DELETE CASCADE
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE TABLE [InvoiceDetail] (
[InvoiceDetailId] bigint NOT NULL IDENTITY,
[ProductId] bigint NULL,
[Discount] decimal(4,2) NOT NULL,
[TaxAmount] decimal(12,2) NOT NULL,
[Qty] (integer) NOT NULL,
[Subtotal] decimal(16,2) NOT NULL,
[Total] decimal(16,2) NOT NULL,
[InvoiceHeaderId] bigint NULL,
CONSTRAINT [PK_InvoiceDetail] PRIMARY KEY ([InvoiceDetailId]),
CONSTRAINT [FK_InvoiceDetail_InvoiceHeader_InvoiceHeaderId] FOREIGN KEY ([InvoiceHeaderId]) REFERENCES [InvoiceHeader] ([InvoiceHeaderId]) ON DELETE NO ACTION,
CONSTRAINT [FK_InvoiceDetail_Product_ProductId] FOREIGN KEY ([ProductId]) REFERENCES [Product] ([ProductId]) ON DELETE NO ACTION
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'CountryId', N'IconImage', N'Name') AND [object_id] = OBJECT_ID(N'[Country]'))
SET IDENTITY_INSERT [Country] ON;
INSERT INTO [Country] ([CountryId], [IconImage], [Name])
VALUES (1, N'', N'Dominican Republic'),
(2, N'', N'Puerto Rico'),
(3, N'', N'Panama'),
(4, N'', N'Colombia');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'CountryId', N'IconImage', N'Name') AND [object_id] = OBJECT_ID(N'[Country]'))
SET IDENTITY_INSERT [Country] OFF;
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'CurrencyId', N'Name') AND [object_id] = OBJECT_ID(N'[Currency]'))
SET IDENTITY_INSERT [Currency] ON;
INSERT INTO [Currency] ([CurrencyId], [Name])
VALUES (1, N'DOP Peso Dominicano'),
(2, N'US Dolar USA');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'CurrencyId', N'Name') AND [object_id] = OBJECT_ID(N'[Currency]'))
SET IDENTITY_INSERT [Currency] OFF;
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_Category_ProductId] ON [Category] ([ProductId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_Client_CountryId] ON [Client] ([CountryId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_Company_CountryId] ON [Company] ([CountryId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_Company_CurrencyId] ON [Company] ([CurrencyId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_Company_RegimeTypeId] ON [Company] ([RegimeTypeId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_InvoiceDetail_InvoiceHeaderId] ON [InvoiceDetail] ([InvoiceHeaderId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_InvoiceDetail_ProductId] ON [InvoiceDetail] ([ProductId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_InvoiceHeader_ClientId] ON [InvoiceHeader] ([ClientId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_InvoiceHeader_CompanyId] ON [InvoiceHeader] ([CompanyId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_InvoiceHeader_PaymentTermId] ON [InvoiceHeader] ([PaymentTermId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_Product_WerehouseId] ON [Product] ([WerehouseId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_Supplier_CountryId] ON [Supplier] ([CountryId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
CREATE INDEX [IX_User_CountryId] ON [User] ([CountryId]);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20200628232756_initialModel')
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20200628232756_initialModel', N'3.1.3');
END;
GO