Repository: fossasia/open-event-server

Issue:
Sponsors: datatype of level should be int instead of string
**I'm submitting a ...**  (check one with "x")
- [x] bug report
- [ ] feature request
- [ ] support request => Please do not submit support requests here, instead ask your query in out Gitter channel at https://gitter.im/fossasia/open-event-orga-server

Sponsors: datatype of level should be int instead of string.





Discussion:

Resolving patch:
diff --git a/app/api/sponsors.py b/app/api/sponsors.py
--- a/app/api/sponsors.py
+++ b/app/api/sponsors.py
@@ -33,7 +33,7 @@ class Meta:
     name = fields.Str(required=True)
     description = fields.Str(allow_none=True)
     url = fields.Url(allow_none=True)
-    level = fields.Str(allow_none=True)
+    level = fields.Integer(allow_none=True)
     logo_url = fields.Url(allow_none=True)
     type = fields.Str(allow_none=True)
     event = Relationship(attribute='event',
diff --git a/app/models/sponsor.py b/app/models/sponsor.py
--- a/app/models/sponsor.py
+++ b/app/models/sponsor.py
@@ -10,7 +10,7 @@ class Sponsor(db.Model):
     name = db.Column(db.String, nullable=False)
     description = db.Column(db.String)
     url = db.Column(db.String)
-    level = db.Column(db.String)
+    level = db.Column(db.Integer)
     logo_url = db.Column(db.String)
     event_id = db.Column(db.Integer, db.ForeignKey('events.id', ondelete='CASCADE'))
     type = db.Column(db.String)
diff --git a/migrations/versions/dbb9670cd902_.py b/migrations/versions/dbb9670cd902_.py
new file mode 100644
--- /dev/null
+++ b/migrations/versions/dbb9670cd902_.py
@@ -0,0 +1,58 @@
+"""empty message
+
+Revision ID: dbb9670cd902
+Revises: f63b9b9b645b
+Create Date: 2017-07-31 02:20:34.268384
+
+"""
+
+from alembic import op
+import sqlalchemy as sa
+import sqlalchemy_utils
+from sqlalchemy.dialects import postgresql
+
+# revision identifiers, used by Alembic.
+revision = 'dbb9670cd902'
+down_revision = 'f63b9b9b645b'
+
+
+class ReplaceableObject(object):
+    def __init__(self, name, sqltext):
+        self.name = name
+        self.sqltext = sqltext
+
+is_numeric = ReplaceableObject(
+    "isnumeric(text)",
+    """
+    RETURNS BOOLEAN AS $$
+    DECLARE
+        x NUMERIC;
+    BEGIN
+        x = $1::integer;
+        RETURN TRUE;
+    EXCEPTION WHEN others THEN
+        RETURN FALSE;
+    END;
+    $$
+    STRICT
+    LANGUAGE plpgsql IMMUTABLE;
+    """)
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.create_or_replace_sp(is_numeric)
+    op.execute('UPDATE sponsors SET level=NULL FROM (SELECT isnumeric(level) as is_numeric_level FROM sponsors) AS subquery WHERE subquery.is_numeric_level is FALSE ')
+    op.execute('ALTER TABLE sponsors ALTER COLUMN level TYPE integer USING (trim(level)::integer)')
+
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.drop_sp(is_numeric)
+    op.alter_column('sponsors', 'level',
+                    existing_type=sa.Integer(),
+                    type_=sa.VARCHAR(),
+                    existing_nullable=True)
+    # ### end Alembic commands ###


Test patch: