Skip to content

Commit 199bf7b

Browse files
authored
Merge pull request #340 from tinybirdco/alter-default-column
add default values
2 parents 7457be4 + 045f5ff commit 199bf7b

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Alter DEFAULT value in a column of the landing Data Source
2+
3+
[Pull Request](https://github.com/tinybirdco/use-case-examples/pull/340/files)
4+
5+
- Just a add the default values to the desired columns. If the event is not sending the value or it is null, the default value will be applied.
6+
7+
8+
```diff
9+
SCHEMA >
10+
+ `timestamp` DateTime `json:$.timestamp` DEFAULT now(),
11+
+ `session_id` String `json:$.session_id` DEFAULT '',
12+
+ `action` LowCardinality(String) `json:$.action` DEFAULT 'None',
13+
+ `version` LowCardinality(String) `json:$.version` DEFAULT '1.0',
14+
+ `payload` String `json:$.payload` DEFAULT '{}',
15+
- `timestamp` DateTime `json:$.timestamp`,
16+
- `session_id` String `json:$.session_id`,
17+
- `action` LowCardinality(String) `json:$.action`,
18+
- `version` LowCardinality(String) `json:$.version`,
19+
- `payload` String `json:$.payload`
20+
```
21+
22+
- Create a PR with the change above, a new branch will be created as part of the CI process. You can double check the default value is what you expect by ingesting some null value in the CI branch created.
23+
24+
For instance, in this case ingest an empty event using the Tinybird branch token:
25+
26+
```sh
27+
curl \
28+
-X POST 'https://api.tinybird.co/v0/events?name=analytics_events' \
29+
-H "Authorization: Bearer $BRANCH_TOKEN" \
30+
-d $'{}'
31+
{"successful_rows":1,"quarantined_rows":0}%
32+
```
33+
34+
Check the content of the Data Source:
35+
36+
```
37+
curl https://api.tinybird.co/v0/sql?q=SELECT%20*%20FROM%20analytics_events%20LIMIT%20100%20%0A%20FORMAT%20JSON&token=$BRANCH_TOKEN
38+
39+
{
40+
"meta":
41+
[
42+
{
43+
"name": "timestamp",
44+
"type": "DateTime"
45+
},
46+
{
47+
"name": "session_id",
48+
"type": "String"
49+
},
50+
{
51+
"name": "action",
52+
"type": "LowCardinality(String)"
53+
},
54+
{
55+
"name": "version",
56+
"type": "LowCardinality(String)"
57+
},
58+
{
59+
"name": "payload",
60+
"type": "String"
61+
}
62+
],
63+
64+
"data":
65+
[
66+
{
67+
"timestamp": "2024-07-15 18:04:45",
68+
"session_id": "",
69+
"action": "None",
70+
"version": "1.0",
71+
"payload": "{}"
72+
}
73+
],
74+
75+
"rows": 1,
76+
77+
"rows_before_limit_at_least": 1,
78+
79+
"statistics":
80+
{
81+
"elapsed": 0.00113733,
82+
"rows_read": 1,
83+
"bytes_read": 26
84+
}
85+
}
86+
```
87+
88+
You can see all columns have their default value.
89+
90+
91+
- To deploy the change, merge to PR to you main branch, the CD job will run and will deploy the changes.
92+
93+
```
94+
...
95+
** Diffs from current commit '7457be4ad47c137aff1537ae91626b9722c4a89d' and new 'e0c6d30e685bb996c01b2dd37ec66690a2fc0ce8':
96+
modified: alter_default_value_in_column/datasources/analytics_events.datasource
97+
** Preparing commit ...
98+
** Processing ./datasources/analytics_events.datasource
99+
** Building dependencies
100+
** [DRY RUN] Deploying commit ...
101+
** [DRY RUN] Running 'analytics_events'
102+
** Deploying commit ...
103+
** Running 'analytics_events'
104+
** The description or schema of 'analytics_events' has changed.
105+
** - MODIFY COLUMN `timestamp` DEFAULT now()
106+
** - MODIFY COLUMN `session_id` DEFAULT ''
107+
** - MODIFY COLUMN `action` DEFAULT 'None'
108+
** - MODIFY COLUMN `version` DEFAULT '1.0'
109+
** - MODIFY COLUMN `payload` DEFAULT '{}'
110+
** The Data Source has been correctly updated.
111+
** 'analytics_events' created
112+
...
113+
```

alter_default_value_in_column/datasources/analytics_events.datasource

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ DESCRIPTION >
44
Analytics events landing data source
55

66
SCHEMA >
7-
`timestamp` DateTime `json:$.timestamp`,
8-
`session_id` String `json:$.session_id`,
9-
`action` LowCardinality(String) `json:$.action`,
10-
`version` LowCardinality(String) `json:$.version`,
11-
`payload` String `json:$.payload`
7+
`timestamp` DateTime `json:$.timestamp` DEFAULT now(),
8+
`session_id` String `json:$.session_id` DEFAULT '',
9+
`action` LowCardinality(String) `json:$.action` DEFAULT 'None',
10+
`version` LowCardinality(String) `json:$.version` DEFAULT '1.0',
11+
`payload` String `json:$.payload` DEFAULT '{}'
1212

1313
ENGINE "MergeTree"
1414
ENGINE_PARTITION_KEY "toYYYYMM(timestamp)"

0 commit comments

Comments
 (0)