Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add created_at, created_at to workspace_schedule and workspaces… #709

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions apps/workspaces/migrations/0049_auto_20241223_1039.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.2.14 on 2024-12-23 10:39

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('workspaces', '0048_alter_workspace_onboarding_state'),
]

operations = [
migrations.AddField(
model_name='workspaceschedule',
name='created_at',
field=models.DateTimeField(auto_now_add=True, help_text='Created at datetime', null=True),
),
migrations.AddField(
model_name='workspaceschedule',
name='updated_at',
field=models.DateTimeField(auto_now=True, help_text='Updated at datetime', null=True),
),
migrations.RunSQL(
sql="""
ALTER TABLE workspaces_user
ADD COLUMN created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW();
""",
reverse_sql="""
ALTER TABLE workspaces_user
DROP COLUMN created_at;
""",
),
]
2 changes: 2 additions & 0 deletions apps/workspaces/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class WorkspaceSchedule(models.Model):
additional_email_options = JSONField(default=list, help_text='Email and Name of person to send email', null=True)
emails_selected = ArrayField(base_field=models.CharField(max_length=255), null=True, help_text='Emails that has to be send mail')
schedule = models.OneToOneField(Schedule, on_delete=models.PROTECT, null=True)
created_at = models.DateTimeField(auto_now_add=True, null=True, help_text='Created at datetime')
updated_at = models.DateTimeField(auto_now=True, null=True, help_text='Updated at datetime')

class Meta:
db_table = 'workspace_schedules'
Expand Down
49 changes: 28 additions & 21 deletions tests/sql_fixtures/reset_db_fixtures/reset_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,9 @@ CREATE TABLE public.workspace_schedules (
workspace_id integer NOT NULL,
additional_email_options jsonb,
emails_selected character varying(255)[],
error_count integer
error_count integer,
created_at timestamp with time zone,
updated_at timestamp with time zone
Comment on lines +1909 to +1911
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add default values and NOT NULL constraints for timestamp columns

The timestamp columns should have appropriate defaults and constraints to ensure data integrity:

  • created_at should default to now() and be NOT NULL
  • updated_at should default to now() and have a trigger to update it automatically
-    created_at timestamp with time zone,
-    updated_at timestamp with time zone
+    created_at timestamp with time zone DEFAULT now() NOT NULL,
+    updated_at timestamp with time zone DEFAULT now() NOT NULL

Also, consider adding an update trigger:

CREATE TRIGGER update_workspace_schedules_updated_at
    BEFORE UPDATE ON public.workspace_schedules
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();

);


Expand Down Expand Up @@ -1987,7 +1989,8 @@ ALTER SEQUENCE public.workspaces_qbocredential_id_seq OWNED BY public.qbo_creden
CREATE TABLE public.workspaces_user (
id integer NOT NULL,
workspace_id integer NOT NULL,
user_id integer NOT NULL
user_id integer NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);


Expand Down Expand Up @@ -4082,6 +4085,9 @@ COPY public.django_migrations (id, app, name, applied) FROM stdin;
196 tasks 0012_auto_20241127_0730 2024-11-27 07:49:43.762663+00
197 workspaces 0047_alter_workspacegeneralsettings_change_accounting_period 2024-11-27 07:49:43.779315+00
198 quickbooks_online 0018_creditcardpurchase_exchange_rate 2024-12-10 06:00:05.224041+00
199 fyle_accounting_mappings 0027_alter_employeemapping_source_employee 2024-12-23 10:37:03.397276+00
200 workspaces 0048_alter_workspace_onboarding_state 2024-12-23 10:37:03.418502+00
201 workspaces 0049_auto_20241223_1039 2024-12-23 10:41:16.03056+00
\.


Expand Down Expand Up @@ -33916,11 +33922,11 @@ COPY public.workspace_general_settings (id, reimbursable_expenses_object, corpor
-- Data for Name: workspace_schedules; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.workspace_schedules (id, enabled, start_datetime, interval_hours, schedule_id, workspace_id, additional_email_options, emails_selected, error_count) FROM stdin;
1 f \N \N \N 2 [] \N \N
2 f \N \N \N 3 [] \N \N
3 f \N \N \N 4 [] \N \N
4 f \N \N \N 5 [] \N \N
COPY public.workspace_schedules (id, enabled, start_datetime, interval_hours, schedule_id, workspace_id, additional_email_options, emails_selected, error_count, created_at, updated_at) FROM stdin;
1 f \N \N \N 2 [] \N \N 2024-12-23 10:41:16.017908+00 2024-12-23 10:41:16.028838+00
2 f \N \N \N 3 [] \N \N 2024-12-23 10:41:16.017908+00 2024-12-23 10:41:16.028838+00
3 f \N \N \N 4 [] \N \N 2024-12-23 10:41:16.017908+00 2024-12-23 10:41:16.028838+00
4 f \N \N \N 5 [] \N \N 2024-12-23 10:41:16.017908+00 2024-12-23 10:41:16.028838+00
\.


Expand All @@ -33941,12 +33947,12 @@ COPY public.workspaces (id, name, fyle_org_id, qbo_realm_id, last_synced_at, cre
-- Data for Name: workspaces_user; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.workspaces_user (id, workspace_id, user_id) FROM stdin;
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
COPY public.workspaces_user (id, workspace_id, user_id, created_at) FROM stdin;
1 1 1 2024-12-23 10:41:16.018067+00
2 2 1 2024-12-23 10:41:16.018067+00
3 3 1 2024-12-23 10:41:16.018067+00
4 4 1 2024-12-23 10:41:16.018067+00
5 5 1 2024-12-23 10:41:16.018067+00
\.


Expand Down Expand Up @@ -34010,7 +34016,7 @@ SELECT pg_catalog.setval('public.django_content_type_id_seq', 47, true);
-- Name: django_migrations_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.django_migrations_id_seq', 198, true);
SELECT pg_catalog.setval('public.django_migrations_id_seq', 201, true);


--
Expand Down Expand Up @@ -34447,6 +34453,14 @@ ALTER TABLE ONLY public.employee_mappings
ADD CONSTRAINT employee_mappings_pkey PRIMARY KEY (id);


--
-- Name: employee_mappings employee_mappings_source_employee_id_dd9948ba_uniq; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.employee_mappings
ADD CONSTRAINT employee_mappings_source_employee_id_dd9948ba_uniq UNIQUE (source_employee_id);


--
-- Name: errors errors_expense_attribute_id_key; Type: CONSTRAINT; Schema: public; Owner: postgres
--
Expand Down Expand Up @@ -35101,13 +35115,6 @@ CREATE INDEX employee_mappings_destination_employee_id_b6764819 ON public.employ
CREATE INDEX employee_mappings_destination_vendor_id_c4bd73df ON public.employee_mappings USING btree (destination_vendor_id);


--
-- Name: employee_mappings_source_employee_id_dd9948ba; Type: INDEX; Schema: public; Owner: postgres
--

CREATE INDEX employee_mappings_source_employee_id_dd9948ba ON public.employee_mappings USING btree (source_employee_id);


--
-- Name: employee_mappings_workspace_id_4a25f8c9; Type: INDEX; Schema: public; Owner: postgres
--
Expand Down
Loading