feat: add organization membership data model
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
CREATE TABLE `organization_memberships` (
|
||||
`id` text PRIMARY KEY NOT NULL,
|
||||
`organization_id` text NOT NULL,
|
||||
`user_id` text NOT NULL,
|
||||
`role` text DEFAULT 'user' NOT NULL,
|
||||
`created_at` integer DEFAULT (cast(unixepoch('subsecond') * 1000 as integer)) NOT NULL,
|
||||
`updated_at` integer DEFAULT (cast(unixepoch('subsecond') * 1000 as integer)) NOT NULL,
|
||||
`archived_at` integer,
|
||||
FOREIGN KEY (`organization_id`) REFERENCES `organizations`(`id`) ON UPDATE no action ON DELETE cascade,
|
||||
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX `organization_memberships_organization_user_unique` ON `organization_memberships` (`organization_id`,`user_id`);--> statement-breakpoint
|
||||
CREATE INDEX `organization_memberships_organization_id_idx` ON `organization_memberships` (`organization_id`);--> statement-breakpoint
|
||||
CREATE INDEX `organization_memberships_user_id_idx` ON `organization_memberships` (`user_id`);--> statement-breakpoint
|
||||
INSERT INTO `organization_memberships` (`id`, `organization_id`, `user_id`, `role`)
|
||||
SELECT lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-' || lower(hex(randomblob(2))) || '-' || lower(hex(randomblob(2))) || '-' || lower(hex(randomblob(6))),
|
||||
organizations.`id`,
|
||||
user.`id`,
|
||||
CASE WHEN user.`role` = 'admin' THEN 'admin' ELSE 'user' END
|
||||
FROM `organizations`
|
||||
CROSS JOIN `user`
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM `organization_memberships`
|
||||
WHERE `organization_memberships`.`organization_id` = organizations.`id`
|
||||
AND `organization_memberships`.`user_id` = user.`id`
|
||||
);
|
||||
@@ -0,0 +1,65 @@
|
||||
PRAGMA foreign_keys=OFF;--> statement-breakpoint
|
||||
CREATE TABLE `__new_organizations` (
|
||||
`id` text PRIMARY KEY NOT NULL,
|
||||
`name` text NOT NULL,
|
||||
`address_line_1` text DEFAULT '' NOT NULL,
|
||||
`address_line_2` text,
|
||||
`address_line_3` text,
|
||||
`city` text,
|
||||
`region` text,
|
||||
`postcode` text,
|
||||
`country` text DEFAULT 'United Kingdom' NOT NULL,
|
||||
`bank_name` text,
|
||||
`bank_account_name` text,
|
||||
`bank_account_number` text,
|
||||
`bank_sort_code` text,
|
||||
`bank_iban` text,
|
||||
`bank_swift` text,
|
||||
`created_at` integer DEFAULT (cast(unixepoch('subsecond') * 1000 as integer)) NOT NULL,
|
||||
`updated_at` integer DEFAULT (cast(unixepoch('subsecond') * 1000 as integer)) NOT NULL,
|
||||
`archived_at` integer
|
||||
);
|
||||
--> statement-breakpoint
|
||||
INSERT INTO `__new_organizations` (
|
||||
"id",
|
||||
"name",
|
||||
"address_line_1",
|
||||
"address_line_2",
|
||||
"address_line_3",
|
||||
"city",
|
||||
"region",
|
||||
"postcode",
|
||||
"country",
|
||||
"bank_name",
|
||||
"bank_account_name",
|
||||
"bank_account_number",
|
||||
"bank_sort_code",
|
||||
"bank_iban",
|
||||
"bank_swift",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"archived_at"
|
||||
)
|
||||
SELECT
|
||||
"id",
|
||||
"name",
|
||||
COALESCE(NULLIF("address_line_1", ''), "workspace", ''),
|
||||
"address_line_2",
|
||||
NULL,
|
||||
"city",
|
||||
"region",
|
||||
"postcode",
|
||||
"country",
|
||||
"bank_name",
|
||||
"bank_account_name",
|
||||
"bank_account_number",
|
||||
"bank_sort_code",
|
||||
"bank_iban",
|
||||
"bank_swift",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"archived_at"
|
||||
FROM `organizations`;--> statement-breakpoint
|
||||
DROP TABLE `organizations`;--> statement-breakpoint
|
||||
ALTER TABLE `__new_organizations` RENAME TO `organizations`;--> statement-breakpoint
|
||||
PRAGMA foreign_keys=ON;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -85,6 +85,20 @@
|
||||
"when": 1782291600000,
|
||||
"tag": "0011_billing_flow",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 12,
|
||||
"version": "6",
|
||||
"when": 1782295200000,
|
||||
"tag": "0012_organization_memberships",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 13,
|
||||
"version": "6",
|
||||
"when": 1782313131621,
|
||||
"tag": "0013_flippant_jamie_braddock",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user