Skip to content

Commit c6c1108

Browse files
committed
storing column params, so table representation can be recreated for migrations and playground
1 parent cfa67dc commit c6c1108

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

docs/src/piccolo/migrations/create.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,3 @@ Piccolo needs to be able to import these files using ``importlib``, so make
144144
sure the paths are correct.
145145

146146
Piccolo will then add the table definitions the migration.
147-
148-
To create all tables in a particular file, you can use a placeholder:
149-
150-
.. code-block:: bash
151-
152-
piccolo new -c ..tables.*

piccolo/columns/base.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class ColumnMeta:
3939
key: bool = False
4040
unique: bool = False
4141

42+
params: t.Dict[str, t.Any] = field(default_factory=dict)
43+
4244
# Set by the Table Metaclass:
4345
_name: t.Optional[str] = None
4446
_table: t.Optional[Table] = None
@@ -105,9 +107,15 @@ def __init__(
105107
primary: bool = False,
106108
key: bool = False,
107109
unique: bool = False,
110+
**kwargs,
108111
) -> None:
112+
# Used for migrations:
113+
kwargs.update(
114+
{"null": null, "primary": primary, "key": key, "unique": unique}
115+
)
116+
109117
self._meta = ColumnMeta(
110-
null=null, primary=primary, key=key, unique=unique
118+
null=null, primary=primary, key=key, unique=unique, params=kwargs
111119
)
112120

113121
def is_in(self, values: Iterable) -> Where:

piccolo/columns/column_types.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@ def __init__(
2121
) -> None:
2222
self.length = length
2323
self.default = default
24+
kwargs.update({"length": length, "default": default})
2425
super().__init__(**kwargs)
2526

27+
@property
28+
def column_type(self):
29+
if self.length:
30+
return f"VARCHAR({self.length})"
31+
else:
32+
return "VARCHAR"
33+
2634

2735
class Integer(Column):
2836
def __init__(self, default: int = None, **kwargs) -> None:
2937
self.default = default
38+
kwargs.update({"default": default})
3039
super().__init__(**kwargs)
3140

3241

@@ -68,6 +77,7 @@ class Timestamp(Column):
6877

6978
def __init__(self, default: "Datetime" = None, **kwargs) -> None:
7079
self.default = default
80+
kwargs.update({"default": default})
7181
super().__init__(**kwargs)
7282

7383

@@ -77,6 +87,7 @@ class Boolean(Column):
7787

7888
def __init__(self, default: bool = False, **kwargs) -> None:
7989
self.default = default
90+
kwargs.update({"default": default})
8091
super().__init__(**kwargs)
8192

8293

@@ -118,6 +129,7 @@ class ForeignKey(Integer):
118129
column_type = "INTEGER"
119130

120131
def __init__(self, references: t.Type[Table], **kwargs) -> None:
132+
kwargs.update({"references": references})
121133
super().__init__(**kwargs)
122134
self._foreign_key_meta = ForeignKeyMeta(references=references)
123135

piccolo/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __str__(cls):
4545
"""
4646
Returns a basic string representation of the table and its columns.
4747
48-
Used by the playground.
48+
Used by the playground, and migrations.
4949
"""
5050
spacer = "\n "
5151
columns = []

0 commit comments

Comments
 (0)